package com.cottondb.mailimport androidx.compose.ui.graphics.Colorimport kotlinx.serialization.json.Jsonimport kotlinx.serialization.json.JsonObjectimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertTrueimport org.junit.Testimport androidx.compose.ui.test.assertCountEqualsimport androidx.compose.foundation.layout.widthimport androidx.compose.ui.unit.dpimport androidx.compose.runtime.setValueimport androidx.compose.runtime.getValueimport androidx.compose.ui.test.performClickimport androidx.compose.ui.test.performTouchInputimport androidx.compose.ui.test.assertimport androidx.compose.ui.test.longClickimport java.io.File/** Cotton Post on Android (assets/design/mail/CONTRACT.md row 16). */classPostAppTest {privateval repo = generateSequence(File("").absoluteFile) { it.parentFile }.first { File(it, "apps/registry.json").isFile }privateval sources by lazy { File(repo, "apps/android/app/src/mail/kotlin/com/cottondb/mail").listFiles()!!.filter { it.extension == "kt" }.joinToString("\n") { it.readText() } }/** Until the core answers a look for Post, Sage stands by day and Night by dark, in franking green. */ @Testfun sage_and_franking_green_stand_until_the_core_answers() { assertEquals(Color(0xFFF1F3EE), Palette.of(null, dark = false).ground) assertEquals(Color(0xFF1E6B4A), Palette.of(null, dark = false).accent) assertEquals(Color(0xFF131915), Palette.of(null, dark = true).ground) assertEquals(Color(0xFF7CC9A0), Palette.of(null, dark = true).accent) }/** The core's colours win where it names them; what it leaves out stays the Stamp's. */ @Testfun the_cores_look_is_laid_over_sage() {val answer = Json.parseToJsonElement("""{"app":"mail","colours":{"light":{"ground":"#FBFAF6","accent":"#0E6372"},"dark":{"ground":"#000000"}}}""") asJsonObjectval day = Palette.of(answer, dark = false) assertEquals(Color(0xFFFBFAF6), day.ground) assertEquals(Color(0xFF0E6372), day.accent) assertEquals(Palette.sage.ink, day.ink) assertTrue(Palette.of(answer, dark = true).dark) }/** Every word the screens ask of the `post` line set is written there. */ @Testfun every_word_asked_is_in_the_post_line_set() {val flat = flatten(Json.parseToJsonElement(File(repo, "crates/cotton-core/kits/shell/lines/post.json").readText()) asJsonObject)val asked = Regex("""(?<!Log\.)\bw\("([a-z_.]+)"""").findAll(sources).map { it.groupValues[1] }.toSet() assertTrue("the scan reads the screens", asked.size >= 6) asked.forEach { key -> assertTrue("post.$key is written", key in flat) } }/** Every core call the screens make is the core's own: a `mail.*` method (or `look`) in the core's list. */ @Testfun every_call_is_the_cores() {val methods = File(repo, "crates/cotton-ffi/src/methods.rs").readText()val asked = (Regex("""ask\("([a-z_.]+)"""").findAll(sources) + Regex("""act\("(mail\.[a-z_.]+)"""").findAll(sources) + Regex("""read\(state, "([a-z_.]+)"""").findAll(sources) + Regex("""core\.obj\("(mail\.[a-z_.]+)"""").findAll(sources)).map { it.groupValues[1] }.toSet() assertTrue(asked.containsAll(setOf("mail.home", "mail.list", "mail.side", "mail.thread"))) // Slices 2 and 3: writing and sending, the acts, links, attachments, Senders, Search, Files and You. assertTrue( "every slice 2 and 3 call is made: ${asked.sorted()}", asked.containsAll( setOf( "mail.compose", "mail.save", "mail.send", "mail.unsend", "mail.sending", "mail.attach", "mail.unattach", "mail.discard", "mail.hold", "mail.act", "mail.archive", "mail.undo", "mail.after", "mail.ask", "mail.do", "mail.bin.empty", "mail.link", "mail.file", "mail.page", "mail.pictures.always", "mail.senders", "mail.sender", "mail.sender.merge", "mail.sender.part", "mail.sender.name", "mail.blocked", "mail.unblock", "mail.search", "mail.files", "mail.you", "mail.set", ), ), ) // Slice 4: You's pages, accounts and how mail arrives, Domains, the rules, and this phone's fetcher. assertTrue( "every slice 4 call is made: ${asked.sorted()}", asked.containsAll( setOf( "mail.accounts", "mail.account.plan", "mail.account.add", "mail.account", "mail.account.way", "mail.account.set", "mail.account.forget", "mail.account.remove", "mail.fetch", "mail.rules", "mail.verdict", "mail.domains", "mail.domain", "mail.domain.add", "mail.domain.check", "mail.domain.catch_all", "mail.address.add", "mail.address.remove", ), ), ) asked.forEach { m -> assertTrue("$m is Post's", m.startsWith("mail.") || m == "look") assertTrue("$m is a core call", "\"$m\"" in methods) } } /** The screens write no sentence of their own: nothing drawn is a literal. */ @Test fun no_sentence_is_written_on_the_phone() { val drawn = Regex("""\bT\("([^"]*)"""").findAll(sources).map { it.groupValues[1] }.toList() assertTrue("no literal is drawn: $drawn", drawn.isEmpty()) } /** Every tap goes through the shell's named control, never a bare tap detector. */ @Test fun every_tap_is_a_named_control() { assertTrue("detectTapGestures" !in sources) assertTrue(".clickable" !in sources) } /** Every screen carries its tag for walks and tests. */ @Test fun every_screen_is_tagged() { listOf( "\"post-home\"", "\"post-thread\"", "\"post-side\"", "\"post-list-\$place\"", "\"post-row-\${row.key}\"", "\"post-write-page\"", "\"post-sending\"", "\"post-search\"", "\"post-senders\"", "\"post-sender\"", "\"post-blocked\"", "\"post-files\"", "\"post-file-page\"", "\"post-you\"", "\"post-after\"", "\"post-tabs\"", "\"post-accounts\"", "\"post-account-new\"", "\"post-account\"", "\"post-signature\"", "\"post-rules\"", "\"post-domains\"", "\"post-domain\"", "\"post-privacy\"", "\"post-look\"", "\"post-sync\"", "\"post-shared-\$page\"", "\"post-password\"", ).forEach { tag -> assertTrue("$tag is a test tag", "testTag($tag)" in sources || "YouPage(state, $tag" in sources || "Panel(state, $tag" in sources) } } /** The four tabs and the writer's words, asked by key rather than as a literal, are written in the `post` set. */ @Test fun the_tabs_and_the_writers_words_are_written() { val flat = flatten(Json.parseToJsonElement(File(repo, "crates/cotton-core/kits/shell/lines/post.json").readText()) as JsonObject) Tab.entries.forEach { assertTrue("post.tab.${it.id}", "tab.${it.id}" in flat) } listOf("from", "to", "cc", "bcc", "cc_bcc", "subject", "body", "attach", "send", "discard", "cancel").forEach { assertTrue("post.write.$it", "write.$it" in flat) } listOf("read", "unread", "archive", "delete").forEach { assertTrue("post.act.$it", "act.$it" in flat) } } /** An act's answer stands as the after-state: its line, its Undo, and his mailbox's line (`mail.archive`, `mail.act`). */ @Test fun an_act_answers_the_after_state() { val after = After.of(json("""{"after":"a1","act":"archive","count":9,"line":"Archived 9 from shop3.invented.","mailbox":{"state":"waiting","line":"Your mailbox will follow in a moment."},"undo":"Undo"}"""))!! assertEquals("a1", after.after) assertEquals("Undo", after.undo) assertEquals("waiting", after.mailbox?.state) val undone = After.of(json("""{"after":"a1","act":"archive","count":9,"line":"Undone.","mailbox":{"state":"here_only","line":"Back in your Inbox here."},"undo":null}"""))!! assertEquals(null, undone.undo) assertEquals("Back in your Inbox here.", undone.mailbox?.line) } /** `mail.do`'s Block with "also delete" carries the delete's Undo; an Unsubscribe without one has nothing to undo. */ @Test fun a_block_carries_the_undo_of_what_it_deleted() { val blocked = After.of(json("""{"act":"block","line":"Blocked Invented Air 3.","deleted":{"after":"d1","act":"delete","count":4,"line":"Moved 4 to the Bin.","mailbox":null,"undo":"Undo"}}"""))!! assertEquals("d1", blocked.after) assertEquals("Undo", blocked.undo) assertEquals("Blocked Invented Air 3. Moved 4 to the Bin.", blocked.line) val asked = After.of(json("""{"act":"unsubscribe","line":"Asked Invented Paper to stop at 1:20 am."}"""))!! assertEquals(null, asked.after) assertEquals(null, asked.undo) assertEquals("That mail is no longer here.", After.refused("That mail is no longer here.").line) } /** A row as the core words it: a conversation, its newest mail, unread, its count, its files and its `a11y` words. */ @Test fun a_row_reads_as_the_core_words_it() { val row = MailRow.of(json("""{"thread":"t-flat","email":"m1","from":{"name":"Priya Nair","address":"priya@people.invented.test","face":"person","initials":"PN"},"subject":"Flat papers","line":"Here is the lease draft.","when":"Today","unread":true,"count":2,"attachments":1,"account":{"tag":"studio"},"a11y":["Unread","From a person"]}""")) assertEquals("m1", row.key) assertTrue(row.unread && row.from.person) assertEquals("studio", row.tag) assertTrue(row.spoken.startsWith("Priya Nair, Flat papers, Here is the lease draft., Today, studio, Unread")) val draft = MailRow.of(json("""{"thread":"","draft":"d9","from":{"name":"Sara","face":"machine"},"subject":"Invoice","mark":"Draft"}""")) assertEquals(null, draft.thread) assertEquals("d9", draft.key) assertEquals("S", draft.from.initials) assertEquals("I", From.initialsOf("@invented.example").take(1)) } /** A link goes to the Sites of this build's kind, as `mail.link` names it, with Sites' own extra saying it came from Post. */ @Test fun a_link_goes_to_the_sites_of_this_builds_kind() { val link = json("""{"url":"https://plans.invented.test/flat-4b","read":"Read here","sites":{"label":"Open in Sites…","android":{"package":"com.cottondb.sites","dev_package":"com.cottondb.sites.dev","extra":{"from":"post"}}},"missing":"Get Sites to keep this signed in."}""") assertEquals("com.cottondb.sites.dev", PostOpening.sitesPackage(link, dev = true)) assertEquals("com.cottondb.sites", PostOpening.sitesPackage(link, dev = false)) assertEquals("com.cottondb.sites.dev", PostOpening.sitesPackage(json("{}"), dev = true)) assertEquals("com.cottondb.sites.FROM", PostOpening.SITES_FROM) val sites = File(repo, "apps/android/app/src/sites/kotlin/com/cottondb/sites/SitesApplication.kt").readText() assertTrue("Sites reads the same extra", "const val FROM = \"${PostOpening.SITES_FROM}\"" in sites) val manifest = File(repo, "apps/android/app/src/mail/AndroidManifest.xml").readText() assertTrue("Post may see Sites", "com.cottondb.sites\${serverSuffix}" in manifest) } /** Attachments land where the FileProvider shares them, and leave Post only through it. */ @Test fun attachments_land_where_the_file_provider_shares() { val paths = File(repo, "apps/android/app/src/main/res/xml/shared_files.xml").readText() assertTrue("""<cache-path name="${PostOpening.FOLDER}" path="${PostOpening.FOLDER}/" />""" in paths) assertTrue("FileProvider.getUriForFile" in sources) assertTrue("file://" !in sources.replace("loadUrl(\"file://\$path\")", "")) } /** The designed page runs no script and loads nothing from the web until he asks for its pictures. */ @Test fun the_designed_page_holds_pictures_back() { assertTrue("javaScriptEnabled = false" in sources) assertTrue("blockNetworkLoads = !pictures" in sources) } /** Archive is one tap (owner, 25 Nov 2026): no sheet before it; Delete, Unsubscribe and Block ask once. */ @Test fun archive_is_one_tap_and_the_rest_ask_once() { val senders = File(repo, "apps/android/app/src/mail/kotlin/com/cottondb/mail/Senders.kt").readText() assertTrue(Regex("""archive"\) onAfter\(state\.archive\(""").containsMatchIn(senders)) assertTrue("askSheet(state, scope, target"in senders) }/** Every page You's rows name (`mail.you`'s `open.page`) opens a page Post draws, and the groups' rules link opens the rules on You. */ @Testfun every_page_you_names_is_drawn() { listOf("accounts", "signature", "rules", "domains", "look", "sync", "privacy", "storage", "help", "about").forEach { page -> assertTrue("$page opens", youPage(page, "Title") != null) } assertEquals(Screen.Privacy("Privacy"), youPage("privacy", "Privacy")) assertEquals(Screen.Shared("sync", "Not signed in"), youPage("sync", "Not signed in")) assertEquals(null, youPage("nowhere", "x")) assertTrue("the groups' see the rules opens Sorting rules on You", "state.openYou(Screen.Rules)"in sources) }/** The ways are asked for this phone's words, and the shell's own parts stand for Sync, Storage, Help, About and Delete account. */ @Testfun the_shared_parts_are_the_shells() { assertEquals("android", PLATFORM) listOf("DeleteAccountPart(", "BackupPage(", "StoragePart(", "HelpPart(", "AboutPart(", "LookScreen(", "AskingNotifications(shell)", "shell.flow = \"signIn\"").forEach { assertTrue("$it is used", it in sources) } }privatefun json(text: String) = Json.parseToJsonElement(text) asJsonObject/** Every drawn mark parses. */ @Testfun every_mark_parses() { assertTrue(everyMarkParses()) }privatefun flatten(o: JsonObject, prefix: String = ""): Set<String> = o.flatMap { (k, v) ->if (v isJsonObject) {val inner = flatten(v, "$prefix$k.")if (v.keys.any { it in setOf("one", "other", "zero") }) inner + "$prefix$k"else inner } else listOf("$prefix$k") }.toSet()}/** A mail row in the hand: a tap opens it, a hold offers the rest, and a swipe's acts are TalkBack's too (Robolectric). */@org.junit.runner.RunWith(org.robolectric.RobolectricTestRunner::class)@org.robolectric.annotation.Config(sdk = [35], qualifiers = "w400dp-h800dp-mdpi", application = android.app.Application::class)classPostRowTest { @get:org.junit.Ruleval compose = androidx.compose.ui.test.junit4.createComposeRule()privateval row = MailRow( thread = "t-garden", email = "m2", draft = null, from = From("Tomas Reyes", "tomas@invented.example", true, "TR"), subject = "The plan for the garden", line = "I sketched where the beds could go.", whenSaid = "Yesterday", unread = false, count = 1, attachments = 1, tag = null, act = null, mark = null, a11y = listOf("From a person", "Has attachments"), ) @Testfun a_tap_opens_and_a_hold_offers_the_rest() {var opened = 0var held = 0var archived = 0 compose.setContent {MailLine(row, onHold = { held++ }, actions = listOf(com.cottondb.shell.action("Archive") { archived++ })) { opened++ } }val node = compose.onNode(androidx.compose.ui.test.hasTestTag("post-row-m2")) node.assert(androidx.compose.ui.test.hasContentDescription("Tomas Reyes, The plan for the garden", substring = true)) node.performClick() node.performTouchInput { longClick() } compose.runOnIdle { node.fetchSemanticsNode().config[androidx.compose.ui.semantics.SemanticsActions.CustomActions].first().action() } compose.waitForIdle() assertEquals(1, opened) assertEquals(1, held) assertEquals(1, archived) } @Testfun a_file_post_wont_open_says_so_and_stays() {var opened = 0 compose.setContent { FileLine("plan.exe", "Post won't open this kind of file. You can save it.", "1 KB", tag = "post-attachment-x", enabled = false) { opened++ } } compose.onNode(androidx.compose.ui.test.hasTestTag("post-attachment-x")).performClick() compose.waitForIdle() assertEquals(0, opened) } @Testfun a_button_says_its_count_and_a_spent_one_does_nothing() {var taps = 0 compose.setContent { androidx.compose.foundation.layout.Column {PostButton("Archive 9", line = "from your Inbox", weight = Weight.Go, tag = "go") { taps++ }PostButton("Mark 0 read", enabled = false, tag = "spent") { taps += 10 } } } compose.onNode(androidx.compose.ui.test.hasTestTag("go")).assert(androidx.compose.ui.test.hasContentDescription("Archive 9, from your Inbox")).performClick() compose.onNode(androidx.compose.ui.test.hasTestTag("spent")).performClick() compose.waitForIdle() assertEquals(1, taps) }/** * Retry never stands where Undo stood (the Undo re-walk, 27 Nov 2026: a tap meant for a late Undo hit Retry and * sent again): once the send ends, nothing tappable sits in Undo's place, Retry comes only 2 seconds later, and * never over Undo's spot. */ @Testfun retry_comes_late_and_never_where_undo_stood() {val holding = Json.parseToJsonElement("""{"draft":"d1","state":"holding","line":"Sending in 1 second","undo":"Undo","seconds":1}""") asJsonObjectval unknown = Json.parseToJsonElement("""{"draft":"d1","state":"failed","line":"Post couldn't tell whether this went. Check Sent, then Retry if it didn't.","retry":"Retry"}""") asJsonObjectvar now by androidx.compose.runtime.mutableStateOf(holding)var retried = 0 compose.mainClock.autoAdvance = false compose.setContent { androidx.compose.foundation.layout.Box(androidx.compose.ui.Modifier.width(400.dp)) { Sending("Franked", now, onUndo = {}, onRetry = { retried++ }) } } compose.mainClock.advanceTimeByFrame()val undo = compose.onNode(androidx.compose.ui.test.hasTestTag("post-send-undo")).fetchSemanticsNode().boundsInRoot compose.runOnIdle { now = unknown; androidx.compose.runtime.snapshots.Snapshot.sendApplyNotifications() } compose.mainClock.advanceTimeByFrame() compose.mainClock.advanceTimeBy(1_900) compose.onAllNodes(androidx.compose.ui.test.hasTestTag("post-send-retry")).assertCountEquals(0) compose.onAllNodes(androidx.compose.ui.test.hasTestTag("post-send-undo")).assertCountEquals(0)val tappable = compose.onAllNodes(androidx.compose.ui.test.hasClickAction()).fetchSemanticsNodes() assertTrue("nothing tappable in Undo's place", tappable.none { it.boundsInRoot.overlaps(undo) }) compose.mainClock.advanceTimeBy(300)val retry = compose.onNode(androidx.compose.ui.test.hasTestTag("post-send-retry")).fetchSemanticsNode().boundsInRoot assertTrue("Retry $retry is not over Undo $undo", !retry.overlaps(undo)) compose.onNode(androidx.compose.ui.test.hasTestTag("post-send-retry")).performClick() compose.mainClock.advanceTimeByFrame() assertEquals(1, retried) }}