package com.cottondb.messages.warm import android.app.Application import androidx.compose.runtime.getValue import androidx.compose.runtime.setValue import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.assertTextEquals import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onAllNodesWithText import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick import androidx.compose.ui.test.performScrollTo import com.cottondb.messages.chat.Avatar import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.jsonArray import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config /** * Comms' You, grouped (assets/design/chat/you-grouped.html, the owner's review of 22 Nov 2026): a profile, then seven * rows that each say how they stand and each open their own page. Every answer here is invented, shaped as the core * answers it (`chat.you`, `assistant.screen`, `shell.settings`). */ @RunWith(RobolectricTestRunner::class) @Config(sdk = [35], qualifiers = "w360dp-h1600dp-mdpi", application = Application::class) class YouGroupedTest { @get:Rule val compose = createComposeRule() private fun j(text: String): JsonObject = Json.parseToJsonElement(text).jsonObject private val face = Avatar("KR", 1, null) private val you = j( """{"rows":[{"id":"export","label":"Export","line":"Your chats as text, with their pictures and files."},{"id":"sync","label":"Sync","line":"Synced at 9:53"},{"id":"claude","label":"Your Claude","line":"Can read any time"}, {"id":"privacy","label":"Privacy","line":"2 blocked · read receipts off"},{"id":"notifications","label":"Notifications","line":"On · 3 chats muted"}, {"id":"storage","label":"Storage","line":"1.4 GB on this phone"},{"id":"help","label":"Help and feedback","line":"Share a report"}, {"id":"about","label":"About","line":"Version 1.4"}], "sync":{"title":"3 changes waiting to sync","last":"Last synced at 9:53","trouble":true}, "muted":[{"thread":"t-family","name":"Family","picture":{"initials":"F","tint":2},"until":"Until you turn it back on"}]}""", ) @Test fun you_is_the_profile_then_seven_rows_each_saying_how_it_stands() { val opened = mutableListOf() var profile = 0 compose.setContent { WarmTheme(night = false) { YouTop("You", "Kavi Rao", "+1 555 0100 100", face, "Edit", YouRow.of(you), { profile++ }, { opened += it }, starred = "Starred messages") } } compose.onNodeWithTag("you-card").assertIsDisplayed().performClick() assertEquals(1, profile) val ids = listOf("export", "sync", "claude", "privacy", "notifications", "storage", "help", "about") ids.forEach { compose.onNodeWithTag("you-row-$it").performScrollTo().performClick() } assertEquals(ids, opened) assertEquals(ids.map(::pageOf), listOf(YouPage.Export, YouPage.Sync, YouPage.Claude, YouPage.Privacy, YouPage.Notifications, YouPage.Storage, YouPage.Help, YouPage.About)) compose.onNodeWithTag("you-row-claude-line", useUnmergedTree = true).assertTextEquals("Can read any time") compose.onNodeWithTag("you-row-privacy-line", useUnmergedTree = true).assertTextEquals("2 blocked · read receipts off") compose.onNodeWithTag("you-starred").assertExists() } /** * Owner, build 24: You drew the card, and the rows a second or two later. From only the quick answer * (`chat.you.now` on a phone that never had a whole one: Storage and Privacy still to come), every row is composed * on the first frame; the whole answer then fills Storage and Privacy in place, and no row moves or changes height. */ @Test fun every_row_stands_on_the_first_frame_and_fills_in_place() { val quick = j( """{"rows":[{"id":"export","label":"Export","line":"Your chats as text, with their pictures and files."},{"id":"sync","label":"Sync","line":"Synced at 9:53"},{"id":"claude","label":"Your Claude","line":"Can read any time"}, {"id":"privacy","label":"Privacy","line":"Read receipts off","later":true},{"id":"notifications","label":"Notifications","line":"On · 3 chats muted"}, {"id":"storage","label":"Storage","later":true},{"id":"help","label":"Help and feedback","line":"Share a report"}, {"id":"about","label":"About","line":"Version 1.4"}], "sync":{"title":"3 changes waiting to sync","trouble":false},"muted":[],"profile":{"name":"Kavi Rao","number":"+1 555 0100 100"}}""", ) var answer by androidx.compose.runtime.mutableStateOf(quick) compose.mainClock.autoAdvance = false compose.setContent { WarmTheme(night = false) { YouTop("You", "Kavi Rao", "+1 555 0100 100", face, "Edit", YouRow.of(answer), {}, {}, starred = "Starred messages") } } compose.mainClock.advanceTimeByFrame() val tags = listOf("you-card", "you-starred") + listOf("export", "sync", "claude", "privacy", "notifications", "storage", "help", "about").map { "you-row-$it" } fun bounds() = tags.associateWith { compose.onNodeWithTag(it).fetchSemanticsNode().boundsInRoot } val first = bounds() compose.mainClock.autoAdvance = true answer = you compose.waitForIdle() compose.onNodeWithTag("you-row-storage-line", useUnmergedTree = true).assertTextEquals("1.4 GB on this phone") val filled = bounds() tags.forEach { assertEquals("$it stands where it stood on the first frame", first[it], filled[it]) } } private val profileWords = ProfilePageWords("Back", "Change picture", "Add a picture", "Your name", "This is the name people see in their chats with you.", "Phone number", "You sign in with this number.", "Getting back in", "To come back you'll need your number and a code.") /** Sign out stands at the foot of the profile, with what coming back takes under it (the review, item 3). */ @Test fun the_profile_has_sign_out_at_its_foot_with_what_coming_back_takes() { var out = 0 var back = 0 compose.setContent { WarmTheme(night = false) { ProfilePage("Kavi Rao", "+1 555 0100 100", face, false, profileWords, "Sign out", {}, { out++ }, { back++ }) } } compose.onNodeWithTag("you-sign-out").performScrollTo().performClick() assertEquals(1, out) compose.onNodeWithTag("you-sign-out-line", useUnmergedTree = true).assertTextEquals("To come back you'll need your number and a code.") compose.onNodeWithText("You sign in with this number.").assertExists() // DISCOVERY OFF 1: no "people can find me" switch. compose.onNodeWithTag("you-findable", useUnmergedTree = true).assertDoesNotExist() compose.onNodeWithTag("you-profile-page-back").performClick() assertEquals(1, back) } /** Sync is how it stands and Sync now: no "full copy" (the review, item 2). */ @Test fun sync_is_the_state_the_waiting_the_out_of_reach_line_and_sync_now() { var now = 0 val sync = you["sync"] as JsonObject compose.setContent { WarmTheme(night = false) { SyncPage("Sync", sync["title"].toString().trim('"'), "Last synced at 9:53", "Can't reach Cotton right now. Your changes are safe on this phone and will sync when it's back.", "Sync now", "Back", { now++ }, {}) } } compose.onNodeWithTag("you-sync-state", useUnmergedTree = true).assertTextEquals("3 changes waiting to sync") compose.onNodeWithTag("you-sync-last", useUnmergedTree = true).assertTextEquals("Last synced at 9:53") compose.onNodeWithTag("you-sync-unreachable").assertExists() compose.onNodeWithTag("you-sync-now").performClick() assertEquals(1, now) assertTrue(compose.onAllNodesWithText("full copy", substring = true, ignoreCase = true).fetchSemanticsNodes().isEmpty()) } // UNSENT HEAL, as `chat.you`'s `sync.stuck` answers it (crates/cotton-ffi/src/methods/sync.rs `stuck_part`). private val stuck = j( """{"title":"1 change can't be sent","detail":"It's saved on this phone, but Cotton can't take it. Send it again, or let it go.","things":1,"kept":0, "acts":[{"id":"again","label":"Send again","method":"sync.stuck.again","request":{"ids":["op-invented-stuck-1"]}}, {"id":"let_go","label":"Let go","method":"sync.stuck.letGo","request":{"ids":["op-invented-stuck-1"]},"tone":"danger", "confirm":{"title":"Let these changes go?","body":"They stay on this phone, but your other devices won't get them.","yes":"Let go","no":"Keep them"}}]}""", ) /** You's Sync row says what is stuck while anything is, over the live line (the unsent walk: the live line hid it). */ @Test fun the_sync_row_says_what_cannot_be_sent_over_the_live_line() { val row = YouRow("sync", "Sync", "Synced at 9:53") assertEquals("1 change can't be sent", syncRow(row, "Synced just now", stuck).line) assertEquals("Synced just now", syncRow(row, "Synced just now", null).line) assertEquals("Synced at 9:53", syncRow(row, null, null).line) val other = YouRow("claude", "Your Claude", "Can read any time") assertEquals(other, syncRow(other, "Synced just now", stuck)) } /** * The Sync page says what is stuck once; Send again is the one filled button (Sync now steps down) and sends its own * request; Let go asks first, Keep them sends nothing, and its Let go sends the act's own request. */ @Test fun stuck_changes_are_said_once_with_send_again_and_let_go_behind_keep_them() { val sent = mutableListOf() compose.setContent { WarmTheme(night = false) { SyncPage("Sync", "Synced at 9:53", null, null, "Sync now", "Back", {}, {}, stuck = stuck, onStuck = { sent += it }) } } assertEquals(1, compose.onAllNodesWithText("1 change can't be sent", substring = true).fetchSemanticsNodes().size) // The line is the stuck title, not the sync line; the block says only why and what to do. compose.onNodeWithTag("you-sync-state", useUnmergedTree = true).assertTextEquals("1 change can't be sent") assertTrue(compose.onAllNodesWithText("Synced at 9:53").fetchSemanticsNodes().isEmpty()) compose.onNodeWithText("It's saved on this phone, but Cotton can't take it. Send it again, or let it go.").assertExists() assertTrue(!syncNowFilled(stuck) && syncNowFilled(null)) compose.onNodeWithTag("you-sync-stuck-again").performScrollTo().performClick() assertEquals(listOf("again"), sent.map { it["id"].toString().trim('"') }) assertEquals(stuck["acts"]!!.jsonArray[0].jsonObject["request"], sent[0]["request"]) compose.onNodeWithTag("you-sync-stuck-let_go").performScrollTo().performClick() compose.onNodeWithText("Let these changes go?").assertIsDisplayed() compose.onNodeWithText("They stay on this phone, but your other devices won't get them.").assertIsDisplayed() compose.onNodeWithTag("you-sync-stuck-keep").performClick() assertEquals(1, sent.size) compose.onNodeWithTag("you-sync-stuck-let_go").performScrollTo().performClick() compose.onNodeWithTag("you-sync-stuck-yes").performClick() assertEquals(listOf("again", "let_go"), sent.map { it["id"].toString().trim('"') }) assertEquals(stuck["acts"]!!.jsonArray[1].jsonObject["request"], sent[1]["request"]) } /** Changes kept only on this phone (no acts): the sync line stays, the block says them, nothing to press. */ @Test fun changes_kept_only_here_keep_the_sync_line_and_say_themselves_in_the_block() { val kept = j("""{"title":"1 change stays only on this phone, because this app doesn't send it.","detail":null,"things":0,"kept":1,"acts":[]}""") assertEquals(null, stuckLine(kept)) assertEquals("1 change stays only on this phone, because this app doesn't send it.", stuckBlock(kept)) assertEquals("It's saved on this phone, but Cotton can't take it. Send it again, or let it go.", stuckBlock(stuck)) compose.setContent { WarmTheme(night = false) { SyncPage("Sync", "Synced at 9:53", null, null, "Sync now", "Back", {}, {}, stuck = kept) } } compose.onNodeWithTag("you-sync-state", useUnmergedTree = true).assertTextEquals("Synced at 9:53") compose.onNodeWithText("1 change stays only on this phone, because this app doesn't send it.").assertExists() assertTrue(syncNowFilled(kept)) } private fun claude(reading: String) = ClaudeView.of(j( """{"heading":"Your Claude","promise":"Cotton cannot read your data on its own.","connections":[{"said":"Claude · connected 3 days ago"}],"mayRead":"What it may read","tables":[], "reading":$reading, "groups":[{"id":"chats","label":"Chats","line":"Messages and voice notes","access":"on","needsLock":false,"tables":["message"], "choices":[{"access":"on","label":"On","on":true},{"access":"ask","label":"Ask each time","on":false},{"access":"off","label":"Off","on":false}]}, {"id":"mail","label":"Mail","line":"Messages and attachments","access":"off","needsLock":true,"tables":["email"], "choices":[{"access":"on","label":"On","on":false},{"access":"ask","label":"Ask each time","on":false},{"access":"off","label":"Off","on":true}]}]}""", ))!! private val standing = """{"state":"standing","row":"Can read any time","connected":"Connected","title":"Claude can read any time", "line":"Only what you've allowed below, whenever you ask it something.","act":{"id":"ask_all","label":"Ask each time instead"}, "note":"\"Ask each time\" means Claude asks you on this phone first.","disconnect":"Disconnect Claude"}""" /** * Your Claude is groups a person knows, standing or for a while, never Connect while connected (the review, item * 1). Turning a group on widens what Claude reads, so it asks the phone's lock; stepping down does not. */ @Test fun your_claude_is_groups_standing_or_for_a_while_and_never_offers_connect_while_connected() { val groups = mutableListOf>() val acts = mutableListOf() var view by androidx.compose.runtime.mutableStateOf(claude(standing)) compose.setContent { WarmTheme(night = false) { ClaudePage(view, "Back", false, null, onGroup = { id, access, widens, _, _ -> groups += Triple(id, access, widens) }, onAct = { acts += it }, onConnect = {}, onDisconnect = { acts += "disconnect" }, onBack = {}) } } compose.onNodeWithTag("you-claude-connected", useUnmergedTree = true).assertTextEquals("Connected") compose.onNodeWithTag("you-claude-state-title", useUnmergedTree = true).assertTextEquals("Claude can read any time") compose.onNodeWithTag("you-claude-connect").assertDoesNotExist() compose.onNodeWithTag("you-claude-act-ask_all").performClick() compose.onNodeWithTag("you-claude-mail-on").performScrollTo().performClick() compose.onNodeWithTag("you-claude-chats-ask").performScrollTo().performClick() compose.onNodeWithTag("you-claude-chats-on").performClick() // already on: nothing assertEquals(listOf(Triple("mail", "on", true), Triple("chats", "ask", false)), groups) compose.onNodeWithTag("you-claude-disconnect").performScrollTo().performClick() assertEquals(listOf("ask_all", "disconnect"), acts) view = claude("""{"state":"timed","row":"Can read until 10:30","connected":"Connected","title":"Claude can read until 10:30","line":"line","act":{"id":"stop","label":"Stop now","grantIds":["w1"]},"disconnect":"Disconnect Claude"}""") compose.onNodeWithTag("you-claude-state-title", useUnmergedTree = true).assertTextEquals("Claude can read until 10:30") compose.onNodeWithTag("you-claude-act-stop").performClick() assertEquals("stop", acts.last()) } /** * What Connect Claude said (the address to add in Claude) stands under the acts, above the groups: on the emulator * (22 Nov 2026) it stood under the six groups, a screen below the button that made it. */ @Test fun what_an_act_said_stands_above_the_groups() { compose.setContent { WarmTheme(night = false) { ClaudePage(claude(standing), "Back", false, "In Claude, add a connector with the address example.test/mcp.", { _, _, _, _, _ -> }, {}, {}, {}, {}) } } val said = compose.onNodeWithTag("you-claude-said").fetchSemanticsNode().boundsInRoot.top val firstGroup = compose.onNodeWithTag("you-claude-group-chats").fetchSemanticsNode().boundsInRoot.top assertTrue("said at $said, the groups from $firstGroup", said < firstGroup) } @Test fun not_connected_your_claude_offers_connect() { var connect: String? = null val view = ClaudeView.of(j("""{"heading":"Your Claude","promise":"p","connections":[],"none":"Claude is not connected.","connect":"Connect Claude","mayRead":"m","tables":[],"groups":[],"reading":{"state":"none","row":"Not connected"}}"""))!! compose.setContent { WarmTheme(night = false) { ClaudePage(view, "Back", false, null, { _, _, _, _, _ -> }, {}, { connect = it }, {}, {}) } } compose.onNodeWithTag("you-claude-connect").performClick() assertEquals("Connect Claude", connect) compose.onNodeWithTag("you-claude-disconnect").assertDoesNotExist() } /** Privacy keeps what was there: the blocked people, read receipts and typing, last seen, and the way to Your Claude. */ @Test fun privacy_holds_blocked_people_what_others_see_safety_numbers_and_your_claude() { var blocked = 0 var claudeOpened = 0 val receipts = mutableListOf() compose.setContent { WarmTheme(night = false) { PrivacyPage( PrivacyWords("Back", "Privacy", "Blocked people", "Nobody", "What others see", "Read receipts", "rl", "Typing", "tl", "Comms never shows when you were last here.", "Safety numbers", "Compare your safety number.", "Your Claude", "What Claude may read is set there."), 2, false, false, true, { blocked++ }, { receipts += it }, {}, { claudeOpened++ }, {}, ) } } compose.onNodeWithTag("you-blocked").performClick() compose.onNodeWithTag("you-read-receipts").performClick() compose.onNodeWithTag("you-typing").assertExists() compose.onNodeWithTag("you-safety-line", useUnmergedTree = true).assertExists() compose.onNodeWithTag("you-privacy-claude").performScrollTo().performClick() assertEquals(listOf(1, 1, listOf(true)), listOf(blocked, claudeOpened, receipts)) } @Test fun notifications_list_each_muted_chat_and_until_when() { var opened: String? = null compose.setContent { WarmTheme(night = false) { NotificationsPage(NotificationWords("Back", "Notifications", "Messages and calls, even while Comms is closed", "Your phone's settings", "Sounds", "Muted", "No chat is muted.", "To mute a chat, open it and tap its name."), "On", MutedChat.of(you), {}, { opened = it }, {}) } } compose.onNodeWithText("Until you turn it back on").assertExists() compose.onNodeWithTag("you-muted-t-family").performClick() assertEquals("t-family", opened) compose.onNodeWithTag("you-muted-none").assertDoesNotExist() } /** Storage is the shared measured four and Free up space, which gives the figure it was shown to be asked about. */ @Test fun storage_is_the_four_measured_parts_and_free_up_space() { var asked: Long? = null val storage = j("""{"title":"Storage","said":"1.4 GB","parts":[{"id":"data","label":"Your data","bytes":142000000,"said":"142 MB"}, {"id":"files","label":"Photos and files","bytes":1174000000,"said":"1,174 MB"},{"id":"downloads","label":"Downloads","bytes":96000000,"said":"96 MB","line":"Films, books and songs kept for offline"}, {"id":"other","label":"Other","bytes":21000000,"said":"21 MB"}],"free_up":{"label":"Free up space","bytes":830000000,"line":"Frees 830 MB."}}""") compose.setContent { WarmTheme(night = false) { StoragePage(storage, "Storage", "Back", "Comms keeps this much on this phone", "Each number is measured on this phone.", { asked = it }, {}) } } compose.onNodeWithTag("you-storage-total", useUnmergedTree = true).assertTextEquals("1.4 GB") listOf("data", "files", "downloads", "other").forEach { compose.onNodeWithTag("you-storage-$it").assertExists() } compose.onNodeWithTag("you-free-up").performScrollTo().performClick() assertEquals(830000000L, asked) } /** About says Account and Items on this phone, as the core labels them; the app's own fact stands as the version. */ @Test fun about_says_the_account_and_the_items_on_this_phone() { var copied: String? = null val about = j("""{"title":"About","version":{"label":"Version","said":"1.4"},"facts":[{"id":"app","label":"App","said":"Cotton Comms 1.4 · abc1234"}, {"id":"database","label":"Account","said":"f4a36b4d · invented.example"},{"id":"synced","label":"Synced","said":"22 Nov 09:53"}, {"id":"here","label":"Items on this phone","said":"48210"},{"id":"error","label":"Last error","said":"None"}]}""") compose.setContent { WarmTheme(night = false) { AboutPage(about, "Comms", "For the curious", "Copy these details", "Back", { copied = it }, {}) } } compose.onNodeWithText("Account").assertExists() compose.onNodeWithText("Items on this phone").assertExists() compose.onNodeWithTag("you-about-app").assertDoesNotExist() assertTrue(compose.onAllNodesWithText("Database").fetchSemanticsNodes().isEmpty()) assertTrue(compose.onAllNodesWithText("Rows", substring = true).fetchSemanticsNodes().isEmpty()) compose.onNodeWithTag("you-about-copy").performScrollTo().performClick() assertTrue(copied!!.contains("Account: f4a36b4d")) } /** Delete account (DELETE_ACCOUNT.md; App Store 5.1.1(v)): Comms' profile carries the shell's row last, below Sign out, and it opens the page naming Comms. */ @Test fun the_profile_carries_delete_account_last() { val invented = object : com.cottondb.shell.Deleting { fun obj(text: String) = kotlinx.serialization.json.Json.parseToJsonElement(text) as kotlinx.serialization.json.JsonObject override suspend fun page(app: String) = obj("""{"row":"Delete account","heading":"Delete your Cotton account","whatGoes":"Everything goes.","whatStays":"Messages you sent stay.","when":"Your account is deleted 30 days after you ask.","otherApps":"Every Cotton app, not only in $app.","send":"Send code by text","codeLabel":"Code from text","confirm":"Delete my account in 30 days","cancel":"Go back","working":"Deleting"}""") override suspend fun code() = obj("""{"challengeId":"c","sent":"Sent."}""") override suspend fun delete(challengeId: String, code: String, app: String) = obj("""{"deleted":true}""") } compose.setContent { // Every app runs under the shell's root, which gives the parts their materials (ShellRoot). androidx.compose.runtime.CompositionLocalProvider(com.cottondb.shell.LocalMaterials provides com.cottondb.shell.rememberMaterials(androidx.compose.ui.platform.LocalContext.current)) { WarmTheme(night = false) { ProfilePage("Kavi Rao", "+1 555 0100 100", face, false, profileWords, "Sign out", {}, {}, {}, deleteAccount = { com.cottondb.shell.DeleteAccount(invented, "Comms") {} }) } } } val out = compose.onNodeWithTag("you-sign-out").fetchSemanticsNode().boundsInRoot.top val delete = compose.onNodeWithTag("account-delete").performScrollTo().assertExists().fetchSemanticsNode().boundsInRoot.top assertTrue("Delete account stands below Sign out", delete > out) compose.onNodeWithTag("account-delete").performClick() compose.onNodeWithText("Every Cotton app, not only in Comms.").assertExists() } }