214 lines · 13.8 KB · KotlinRaw
package com.cottondb.mailimport com.cottondb.shell.arrimport com.cottondb.shell.objimport com.cottondb.shell.strimport kotlinx.coroutines.runBlockingimport kotlinx.serialization.json.Jsonimport kotlinx.serialization.json.JsonObjectimport kotlinx.serialization.json.buildJsonObjectimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertFalseimport org.junit.Assert.assertNullimport org.junit.Assert.assertTrueimport org.junit.Beforeimport org.junit.Testimport java.io.File/** * The owner's asks of 26 Nov 2026 on Android, on the core's POST CHIPS (73ce5697b6): Mail's chips from `mail.home` * `chips` (each a `mail.list` place newest first, its unread count, the chosen one kept per inbox; the grouped Inbox * gone), `mail.list` `unread`, a sender's mail from `mail.sender` `mails` (`from:<sender>`, paged), Senders' search by * the core (`mail.senders {text}`, `found_none`), and You as the core answers it, Cotton not answering said once. */class PostTabsTest { private val repo = generateSequence(File("").absoluteFile) { it.parentFile }.first { File(it, "apps/registry.json").isFile } private fun src(name: String) = File(repo, "apps/android/app/src/mail/kotlin/com/cottondb/mail/$name").readText() private val words by lazy { flatten(Json.parseToJsonElement(File(repo, "crates/cotton-core/kits/shell/lines/post.json").readText()) as JsonObject) } private fun json(s: String) = Json.parseToJsonElement(s) as JsonObject private fun flatten(o: JsonObject, prefix: String = ""): Map<String, String> = o.entries.flatMap { (k, v) -> if (v is JsonObject) flatten(v, "$prefix$k.").entries.map { it.key to it.value } else listOf("$prefix$k" to ((v as? kotlinx.serialization.json.JsonPrimitive)?.content ?: v.toString())) }.toMap() private fun row(thread: String, unread: Boolean, from: String = "a@shop.invented") = json("""{"thread":"$thread","email":"e$thread","from":{"name":"N","address":"$from"},"subject":"S","unread":$unread}""") /** A stand-in core: answers by method and place, and keeps every request it was asked. */ private class Core(val answer: (String, JsonObject) -> Asked) { val asked = mutableListOf<Pair<String, JsonObject>>() val fetch: Fetch = { method, build -> val req = buildJsonObject(build); asked += method to req; answer(method, req) } } private val rust by lazy { listOf("chat_mail_post.rs", "chat_mail_post_senders.rs", "chat_mail_post_you.rs").joinToString("\n") { File(repo, "crates/cotton-ffi/src/phone/$it").readText() } } private val all by lazy { File(repo, "apps/android/app/src/mail/kotlin/com/cottondb/mail").listFiles()!!.joinToString("\n") { it.readText() } } // ── the chips ────────────────────────────────────────────────────── @Test fun the_chips_are_the_cores_with_its_counts() { assertTrue("the core says them", rust.contains("\"chips\": chips(&unread)") && rust.contains("\"place\": id, \"count\": count(id)")) val home = json("""{"chips":[{"id":"all","label":"All","place":"inbox"},{"id":"people","label":"People","place":"people","count":"3"},{"id":"unread","label":"Unread","place":"unread","count":"1,204"},{"id":"receipts","label":"Receipts","place":"group:receipts","count":null},{"id":"x","label":"No place"}]}""") val chips = MailChips.of(home) assertEquals(listOf("all", "people", "unread", "receipts"), chips.map { it.id }) assertEquals(listOf("inbox", "people", "unread", "group:receipts"), chips.map { it.place }) assertEquals(listOf(null, "3", "1,204", null), chips.map { it.count }) // All never carries one, nor does a zero. assertNull(MailChips.of(json("""{"chips":[{"id":"all","label":"All","place":"inbox","count":"9"}]}"""))[0].count) assertNull(MailChips.of(json("""{"chips":[{"id":"people","label":"People","place":"people","count":"0"}]}"""))[0].count) assertTrue("no chips said, none drawn", MailChips.of(json("""{"title":"Inbox"}""")).isEmpty()) } @Test fun the_chosen_chip_falls_back_to_all_then_the_inbox() { val chips = MailChips.of(json("""{"chips":[{"id":"all","label":"All","place":"inbox"},{"id":"unread","label":"Unread","place":"unread"}]}""")) assertEquals("unread", MailChips.chosen(chips, "unread").place) assertEquals("a chip no longer offered", "inbox", MailChips.chosen(chips, "gone").place) assertEquals("no chips at all", "inbox", MailChips.chosen(emptyList(), "unread").place) } @Test fun a_chip_says_its_words_and_count_to_talkback() { assertEquals("People, 3", MailChip("people", "People", "people", "3").spoken) assertEquals("All", MailChip("all", "All", "inbox", null).spoken) } @Test fun the_chosen_chip_is_kept_per_inbox_and_senders_order_beside_it() { val kept = mutableMapOf<String, String>() val memory = ChipMemory({ kept[it] }, { k, v -> kept[k] = v }) assertEquals("all", memory.of(null)) assertEquals("never_read", memory.sort()) memory.keep(null, "receipts") memory.keep("me@invented.example", "unread") memory.keepSort("az") val again = ChipMemory({ kept[it] }, { _, _ -> }) assertEquals("receipts", again.of(null)) assertEquals("unread", again.of("me@invented.example")) assertEquals("az", again.sort()) } @Test fun mail_draws_the_cores_chips_and_none_of_its_own() { val home = src("Home.kt") listOf("home.obj(\"people\")", "home.obj(\"waiting\")", "home.obj(\"machines\")", "post-group-", "mail.side", "sideUnread", "labelKey").forEach { assertFalse("$it is gone", home.contains(it)) } assertTrue(home.contains("val chips = MailChips.of(answer)")) assertTrue(home.contains("CountChip(\n c.label, c.count, c.id == chosen, c.spoken")) assertTrue(home.contains("state.chips.keep(state.account, id)")) assertTrue(home.contains("MailPages.first(chosen.place, state.account, state.fetch)")) assertTrue(home.contains("pagedRows(state, pages,")) assertTrue(src("Parts.kt").contains("control(spoken, selected = on)")) assertTrue("the count in quiet type", src("Parts.kt").contains("T(it, 13.5.sp, c.quiet, lines = 1")) } @Test fun every_stand_in_the_core_replaced_is_gone() { listOf("object Wanted", "Wanted.", "unreadOnly", "MailPages.sender", "fromThem", "keysFrom", "unkept", "YouHeld", "youStands", "mailsHeading", "fun wanted").forEach { assertFalse("$it is gone", all.contains(it)) } listOf("home.chips.all", "home.chips.unread", "senders.search", "senders.found_none", "senders.mails.other", "empty.unread", "a11y.clear").forEach { assertTrue("post.$it is written", it in words) } } // ── a list's pages ───────────────────────────────────────────────── @Test fun unread_is_the_cores_own_place() = runBlocking { assertTrue(rust.contains("\"unread\" => Scope::Unread")) val core = Core { _, req -> Asked(json("""{"rows":[${row("t1", true)}],"after":"c1"}""").takeIf { req.str("place") == "unread" }) } val page = MailPages.first("unread", "me@invented.example", core.fetch) assertEquals(1, page.rows.size) assertEquals(Cursor("unread", "c1"), page.next) assertEquals("me@invented.example", core.asked.single().second.str("account")) assertEquals("Nothing unread.", words["empty.unread"]) } @Test fun a_page_after_asks_the_same_place_from_its_cursor_and_a_refusal_says_why() = runBlocking { val core = Core { _, req -> if (req.str("after") == "c1") Asked(json("""{"rows":[${row("t2", false)}]}""")) else Asked(null, "bad_request", "no such place") } val after = MailPages.next(Cursor("group:shops", "c1"), null, core.fetch) assertEquals(listOf("t2"), after.rows.map { it.str("thread") }) assertNull(after.next) assertEquals("group:shops", core.asked.single().second.str("place")) assertEquals("no such place", MailPages.first("inbox", null, core.fetch).refused) } @Test fun a_chip_with_nothing_says_its_lists_own_empty_line() { val home = src("Home.kt") assertTrue("All: the home's own line, else the Inbox list's", home.contains("val empty = if (chip.place == \"inbox\") home?.str(\"empty\") ?: pages.answer?.str(\"empty\") else pages.answer?.str(\"empty\")")) assertTrue("the core's empty line for Unread", rust.contains("p(&format!(\"empty.{}\", if group.is_some() || place == \"people\" { \"inbox\" } else { place.as_str() })")) } // ── a sender's mail ──────────────────────────────────────────────── @Test fun a_senders_mail_is_the_place_the_core_names_paged() = runBlocking { assertTrue(rust.contains("\"mails\": { \"title\": super::counted(\"senders.mails\", sender.all() as usize), \"place\": format!(\"from:{}\", sender.key) }")) val core = Core { m, req -> Asked(json("""{"title":"557 mails","rows":[${row("t1", false)}],"after":"c"}""").takeIf { m == "mail.list" && req.str("place") == "from:@shop.invented" }) } val page = MailPages.first("from:@shop.invented", null, core.fetch) assertEquals(1, page.rows.size) assertEquals(Cursor("from:@shop.invented", "c"), page.next) assertNull("no account on a sender's page", core.asked.single().second.str("account")) } @Test fun a_senders_page_lists_its_mail_under_the_acts_headed_by_the_cores_count() { val senders = src("Senders.kt") val acts = senders.indexOf("item(key = \"acts\")") val mails = senders.indexOf("item(key = \"mails\")") assertTrue("the mail stands under the acts", acts in 1 until mails) assertTrue(senders.contains("val place = shown?.obj(\"mails\")?.str(\"place\")") && senders.contains("MailPages.first(place, null, state.fetch)")) assertTrue(senders.contains("Section(band.str(\"title\").orEmpty())")) assertTrue(senders.contains("pagedRows(state, mails, \"m\", { after = it }) { cursor -> MailPages.next(cursor, null, state.fetch) }")) assertEquals("{n} mails", words["senders.mails.other"]) assertEquals("1 mail", words["senders.mails.one"]) } // ── Senders' search ──────────────────────────────────────────────── @Test fun senders_search_is_the_cores_and_pages_by_its_cursor() = runBlocking { assertTrue(rust.contains("request.get(\"text\")") && rust.contains("\"found_none\"")) val core = Core { _, req -> assertEquals("kite", req.str("text")) Asked(json("""{"search":{"hint":"Search senders"},"rows":[{"key":"@kitebox.invented","title":"Kitebox"}],"after":"${if (req.str("after") == null) "60" else ""}"}""")) } val page = SenderSearch.find(" kite ", "az", core.fetch) assertEquals(1, page.rows.size) assertEquals(Cursor("az", "60"), page.next) SenderSearch.next("kite", page.next!!, core.fetch) assertEquals(listOf("az", "az"), core.asked.map { it.second.str("sort") }) assertEquals("60", core.asked[1].second.str("after")) assertTrue("one call each: never every page read", core.asked.all { it.first == "mail.senders" } && core.asked.size == 2) } @Test fun senders_search_draws_the_cores_hint_and_found_none() { val senders = src("Senders.kt") assertTrue(senders.contains("shown?.obj(\"search\")?.str(\"hint\") ?: state.w(\"senders.search\")")) assertTrue(senders.contains("foundAnswer?.str(\"found_none\")")) assertTrue("the cross says Clear the search", senders.contains("state.w(\"a11y.clear\")")) assertTrue("the field above the chips", senders.indexOf("SenderSearchField(state,") < senders.indexOf("Chip(s.str(\"label\")")) assertTrue("opening a sender puts the keyboard away", senders.contains("keyboard?.hide(); focus.clearFocus()")) assertTrue(senders.contains("testTag(\"post-senders-search\")")) assertEquals("No sender by that name or address.", words["senders.found_none"]) } // ── You ──────────────────────────────────────────────────────────── @Test fun you_says_cotton_not_answering_once_and_only_when_the_card_does_not() { val reach = "Cotton could not be reached just now. Your things come to this phone once it answers." assertEquals(reach, YouSaid.unreached(json("""{"firstSync":{"state":"unreached","line":"$reach"},"card":{"count":"3,005 mails"}}"""))) assertNull("the card already says it", YouSaid.unreached(json("""{"firstSync":{"state":"unreached","line":"$reach"},"card":{"count":"$reach"}}"""))) assertNull("bringing is the card's", YouSaid.unreached(json("""{"firstSync":{"state":"bringing","line":"Loading your data…"},"card":{"count":"Loading your data…"}}"""))) assertNull(YouSaid.unreached(json("""{"card":{"count":"3,005 mails"}}"""))) } @Test fun you_draws_the_core_and_sync_does_not_repeat_it() { val you = src("You.kt") assertTrue(you.contains("val unreached = YouSaid.unreached(answer)")) assertTrue(you.contains("unreached?.let { line -> Note(line, Modifier.testTag(\"post-you-standing\")")) assertTrue(you.contains("seen.line.takeUnless { standing != null && seen.state == \"unreached\" }")) assertTrue("the rows are the core's as answered", you.contains("val shown = answer\n")) assertTrue("the core fills them from what the phone holds", rust.contains("([], [one]) => one.clone()") && rust.contains("([], []) if coming.is_some() => dash()")) }}