package com.cottondb.settings import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.semantics.SemanticsNode import androidx.compose.ui.semantics.SemanticsProperties import androidx.compose.ui.semantics.getOrNull import androidx.compose.ui.test.junit4.ComposeContentTestRule import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.onRoot import androidx.compose.ui.test.performClick import androidx.compose.ui.test.performScrollTo import androidx.compose.ui.text.AnnotatedString import com.cottondb.shell.OwnRow import com.cottondb.shell.SettingsSkin import com.cottondb.shell.SettingsSlots import com.cottondb.shell.SettingsView import com.cottondb.shell.SettingsWords import com.cottondb.shell.core.Lines import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue import java.io.File /** * SETTINGS ORGANISED AS COMMS' YOU, CHECKED IN AN APP'S OWN LOOK (owner, 23 Nov 2026; shared/DECISIONS.md "Settings"): * the profile card first, the app's own rows next, then the shared rows in Comms' order; every row opens its page or * acts, every act a person had stays reachable, and no screen says a key, a fill or an internal word. The answers are * invented, shaped as `shell.settings` answers them; the words are the kit's own lines. */ object SettingsCheck { private val repo = generateSequence(File("").absoluteFile) { it.parentFile }.first { File(it, "crates/cotton-core/kits/shell/lines").isDirectory } /** The kit's lines as `shell.lines` answers them, for [app]. */ fun lines(app: String): Lines { val sets = listOf("chat", "words", "settings", "notes", "days", "story", "boot").associateWith { Json.parseToJsonElement(File(repo, "crates/cotton-core/kits/shell/lines/$it.json").readText()) } return Lines.of(buildJsonObject { put("sets", JsonObject(sets)) }, app) } private fun j(text: String): JsonObject = Json.parseToJsonElement(text).jsonObject /** Signed in, as `shell.settings` answers an app of no notifications ([notifies]: Days). */ fun signedIn(notifies: Boolean): JsonObject = j( """{"rows":[{"id":"sync","label":"Sync","line":"Synced at 9:53","group":"keep"},{"id":"claude","label":"Your Claude","line":"Asks each time","group":"keep"}, {"id":"privacy","label":"Privacy","line":"2 people","group":"behave"}${if (notifies) """,{"id":"notifications","label":"Notifications","line":"On","group":"behave"}""" else ""}, {"id":"storage","label":"Storage","line":"12 MB on this phone","group":"behave"},{"id":"help","label":"Help and feedback","line":"Share a report","group":"help"}, {"id":"about","label":"About","line":"Version 1.0.0 (11021)","group":"help"}], "profile":{"signed_in":true,"name":"Kavi Rao"}, "sync":{"title":"3 changes waiting to sync","last":"Last synced at 9:53","trouble":true}, "pages":{"storage_kept":"Words keeps this much on this phone","notifications_phone_line":"Sounds, banners and the lock screen for Days."}, "account":{"key":"account","line":"Synced at 9:53","stand":"connected","card":{"title":"Account","subtitle":"Signed in as Kavi Rao"}, "actions":[{"kind":"tool","tool":"account","id":"account_back_up","label":"Sync now"},{"kind":"tool","tool":"account","id":"account_sign_out","label":"Sign out"}], "people":{"title":"Your people","sections":[]}}, "storage":{"title":"Storage","line":"Words keeps 12 MB on this phone.","said":"12 MB","total_bytes":12000000, "parts":[{"id":"data","label":"Your data","bytes":8000000,"said":"8 MB"},{"id":"files","label":"Photos and files","bytes":3000000,"said":"3 MB"},{"id":"other","label":"Other","bytes":1000000,"said":"1 MB"}], "free_up":{"label":"Free up space","bytes":3000000,"line":"Frees 3 MB. Only what is safely in your account goes, and it comes back when you open it.","ask":"Free up 3 MB?","ask_body":"Nothing is deleted from your account.","confirm":"Free up"}}, "help":{"title":"Help and feedback","share":{"label":"Share a report","line":"A small text file for support or your assistant."}, "links":[{"id":"privacy","label":"Privacy policy","url":"https://cottondb.com/privacy"},{"id":"support","label":"Help and contact","url":"https://cottondb.com/support"}]}, "about":{"title":"About","version":{"label":"Version","said":"1.0.0 (11021)"},"facts":[{"id":"app","label":"App","said":"Cotton Words 1.0.0"},{"id":"database","label":"Account","said":"27ec53b6"},{"id":"here","label":"Items on this phone","said":"6141"}]}}""", ) /** Signed out: Sync, Storage, Help and feedback and About; the card offers Sign in. */ val signedOut: JsonObject = j( """{"rows":[{"id":"sync","label":"Sync","line":"Signed out","group":"keep"},{"id":"storage","label":"Storage","line":"0.4 MB on this phone","group":"behave"}, {"id":"help","label":"Help and feedback","line":"Share a report","group":"help"},{"id":"about","label":"About","line":"Version 1.0.0","group":"help"}], "profile":{"signed_in":false,"line":"Everything stays on this phone until you sign in. Then it syncs."}, "sync":{"title":"Signed out","trouble":false}, "account":{"key":"account","line":"Signed out","stand":"local","card":{"title":"Account","subtitle":"Signed out"}, "actions":[{"kind":"tool","tool":"account","id":"account_sign_in","label":"Sign in"}]}, "storage":{"title":"Storage","said":"0.4 MB","parts":[],"free_up":{"label":"Free up space","bytes":0,"line":"Nothing to free up."}}, "help":{"title":"Help and feedback","share":{"label":"Share a report","line":"A small text file."}}, "about":{"title":"About","version":{"label":"Version","said":"1.0.0"},"facts":[]}}""", ) /** What a run of Settings asked for: account acts by id, and the phone's own acts by name. */ class Done { val acts = mutableListOf() val did = mutableListOf() } private fun draw(compose: ComposeContentTestRule, settings: JsonObject, app: String, skin: SettingsSkin, wrap: @Composable (@Composable () -> Unit) -> Unit, own: (Done) -> List, done: Done, notifying: Boolean?, backup: JsonObject? = null) { val words = SettingsWords(lines(app)) compose.setContent { var page by remember { mutableStateOf(null) } wrap { SettingsView( settings, skin, words, "Settings", "Back", "+1 555 0190 1234", page, { page = it }, { done.did += "close" }, own = own(done), slots = SettingsSlots( own = { id, back, onBack -> skin.Page(id, back, "own-page-$id", onBack) { skin.Quiet("own $id") } }, gettingBack = { skin.Quiet("recovery and passkeys", "getting-back") }, delete = { skin.Row("Delete account", null, "account-delete") { done.did += "delete" } }, claude = { skin.Quiet("your claude", "claude-part") }, people = { skin.Quiet("your people", "people-part") }, backup = { back, onBack -> skin.Page("Backup", back, "settings-page-backup", onBack) { skin.Toggle("Back up to your Google account", false, "backup-toggle") { done.did += "backup $it" } } }, ), notifying = notifying, onAct = { done.acts += it["id"].toString().trim('"') }, onPhoneNotifications = { done.did += "phone" }, onShareReport = { done.did += "report" }, onCopy = { done.did += "copy" }, onFreeUp = { done.did += "free $it" }, app = app, onOpen = { done.did += "open $it" }, backup = backup, ) } } } /** Every word on screen, the merged tree's text and descriptions. */ fun said(node: SemanticsNode): List = (node.config.getOrNull(SemanticsProperties.Text).orEmpty().map(AnnotatedString::text) + node.config.getOrNull(SemanticsProperties.ContentDescription).orEmpty()) + node.children.flatMap(::said) /** No key, fill or internal word stands anywhere on the screen now. */ fun nothingRaw(compose: ComposeContentTestRule, where: String) { val said = said(compose.onRoot(useUnmergedTree = true).fetchSemanticsNode()) val raw = said.filter { w -> w.contains('{') || Regex("""\b(app\.you|settings|chat|words|notes|days|story|boot)\.[a-z_]+""").containsMatchIn(w) || listOf("rows", "object", "business", "seal").any { Regex("\\b$it\\b").containsMatchIn(w) } } assertEquals("said raw on $where: $raw", emptyList(), raw) } private fun top(compose: ComposeContentTestRule, tag: String) = compose.onNodeWithTag(tag).fetchSemanticsNode().positionInRoot.y /** * The whole check, signed in: the card, [ownIds] in order, then the shared rows in Comms' order; each page opens * and goes Back; the acts land (Sync now, Sign out, Share a report, Copy, Free up space after its ask, the phone's * notification settings, Your Claude from Privacy), Delete account and Getting back in stand on the profile. */ fun signedInCheck(compose: ComposeContentTestRule, app: String, skin: SettingsSkin, wrap: @Composable (@Composable () -> Unit) -> Unit, ownIds: List, own: (Done) -> List, notifies: Boolean, acting: List = emptyList()): Done { val done = Done() draw(compose, signedIn(notifies), app, skin, wrap, own, done, notifying = if (notifies) true else null) val shared = listOfNotNull("sync", "claude", "privacy", if (notifies) "notifications" else null, "storage", "help", "about") val order = listOf("settings-card") + (ownIds + shared).map { "settings-row-$it" } order.forEach { compose.onNodeWithTag(it).performScrollTo() } compose.onNodeWithTag("settings-card").performScrollTo() val tops = order.map { tag -> compose.onNodeWithTag(tag).performScrollTo(); top(compose, tag) to tag } // Laid out in this order down the page (each scrolled into sight in turn, so compare neighbours in one frame). order.zipWithNext().forEach { (a, b) -> compose.onNodeWithTag(a).performScrollTo() assertTrue("$a stands above $b", top(compose, a) < top(compose, b)) } assertEquals(order.size, tops.size) nothingRaw(compose, "the top level") // The profile: the name, the number, Getting back in, Sign out at the foot with what coming back takes, Delete account. compose.onNodeWithTag("settings-card").performScrollTo().performClick() compose.onNodeWithTag("settings-page-profile").assertExistsHere() compose.onNodeWithTag("getting-back").performScrollTo() compose.onNodeWithTag("settings-sign-out").performScrollTo().performClick() compose.onNodeWithTag("settings-sign-out-line").performScrollTo() compose.onNodeWithTag("account-delete").performScrollTo().performClick() nothingRaw(compose, "the profile") compose.onNodeWithTag("settings-page-profile-back").performClick() // The app's own rows: a page each, or the act itself. ownIds.forEach { id -> compose.onNodeWithTag("settings-row-$id").performScrollTo().performClick() if (id !in acting) { compose.onNodeWithTag("own-page-$id").assertExistsHere() compose.onNodeWithTag("own-page-$id-back").performClick() } } fun open(id: String) = compose.onNodeWithTag("settings-row-$id").performScrollTo().performClick() fun back(id: String) { nothingRaw(compose, id); compose.onNodeWithTag("settings-page-$id-back").performClick() } open("sync") compose.onNodeWithTag("settings-sync-unreachable").performScrollTo() compose.onNodeWithTag("settings-sync-last").performScrollTo() compose.onNodeWithTag("settings-sync-now").performScrollTo().performClick() back("sync") open("claude"); compose.onNodeWithTag("claude-part").assertExistsHere(); back("claude") open("privacy"); compose.onNodeWithTag("people-part").assertExistsHere() compose.onNodeWithTag("settings-privacy-claude").performScrollTo().performClick() compose.onNodeWithTag("claude-part").assertExistsHere() compose.onNodeWithTag("settings-page-claude-back").performClick() compose.onNodeWithTag("settings-page-privacy").assertExistsHere() back("privacy") if (notifies) { open("notifications") compose.onNodeWithTag("settings-notifications-phone").performScrollTo().performClick() back("notifications") } open("storage") compose.onNodeWithTag("settings-storage-total").performScrollTo() compose.onNodeWithTag("settings-storage-data").performScrollTo() compose.onNodeWithTag("settings-free-up").performScrollTo().performClick() compose.onNodeWithTag("settings-free-up-confirm").performClick() back("storage") open("help"); compose.onNodeWithTag("settings-share-report").performScrollTo().performClick() // The privacy policy and the way to reach us, inside the app (both stores ask; store pass, 24 Nov 2026). compose.onNodeWithTag("settings-link-privacy").performScrollTo().performClick() compose.onNodeWithTag("settings-link-support").performScrollTo().performClick() back("help") open("about") compose.onNodeWithTag("settings-about-version").performScrollTo() compose.onNodeWithTag("settings-about-database").performScrollTo() compose.onNodeWithTag("settings-about-copy").performScrollTo().performClick() back("about") // Back at the top level closes Settings. compose.onNodeWithTag("settings-top-back").performClick() compose.runOnIdle { assertEquals(listOf("account_sign_out", "account_back_up"), done.acts) assertTrue("did ${done.did}", done.did.containsAll(listOf("delete", "report", "copy", "free 3000000", "close", "open https://cottondb.com/privacy", "open https://cottondb.com/support") + if (notifies) listOf("phone") else emptyList())) } return done } /** Signed out: the card says Sign in and signs in; no Your Claude or Privacy; Sync's page offers Sign in too. */ fun signedOutCheck(compose: ComposeContentTestRule, app: String, skin: SettingsSkin, wrap: @Composable (@Composable () -> Unit) -> Unit, own: (Done) -> List) { val done = Done() // Backup's row (`backup.screen`, signed out): right after Sync, in the keep group; its page's switch is the whole row. val backup = j("""{"row": {"id": "backup", "label": "Backup", "line": "Off", "group": "keep", "trouble": false}}""") draw(compose, signedOut, app, skin, wrap, own, done, notifying = null, backup = backup) compose.onNodeWithTag("settings-row-backup").performScrollTo().assertTextContainsHere("Off") compose.runOnIdle { assertTrue(top(compose, "settings-row-backup") > top(compose, "settings-row-sync")) } compose.onNodeWithTag("settings-row-backup").performClick() compose.onNodeWithTag("backup-toggle").performClick() compose.onNodeWithTag("settings-page-backup-back").performClick() compose.runOnIdle { assertEquals(listOf("backup true"), done.did) } compose.onNodeWithTag("settings-card").performScrollTo() compose.onNodeWithTag("settings-card-line", useUnmergedTree = true).assertExistsHere() listOf("claude", "privacy").forEach { compose.onNodeWithTag("settings-row-$it").assertDoesNotExistHere() } nothingRaw(compose, "the top level, signed out") compose.onNodeWithTag("settings-card").performClick() compose.onNodeWithTag("settings-row-sync").performScrollTo().performClick() compose.onNodeWithTag("settings-sync-sign-in").performScrollTo().performClick() compose.runOnIdle { assertEquals(listOf("account_sign_in", "account_sign_in"), done.acts) } } /** * The quick answer (`shell.settings.now`) of a phone that never had a whole one: Privacy and Storage stand with * `later` and no line, there is no Storage page yet and no Your people; every other row says how it stands. */ fun quick(notifies: Boolean): JsonObject { val whole = signedIn(notifies) val rows = kotlinx.serialization.json.JsonArray(whole["rows"]!!.let { it as kotlinx.serialization.json.JsonArray }.map { row -> val r = row.jsonObject if (r["id"].toString().trim('"') in listOf("privacy", "storage")) JsonObject(r - "line" + ("later" to kotlinx.serialization.json.JsonPrimitive(true))) else r }) val account = whole["account"]!!.jsonObject return JsonObject(whole - "storage" + ("rows" to rows) + ("account" to JsonObject(account - "people"))) } /** * THE LIST ON ITS FIRST FRAME (owner, build 24: the card drew, and the rows one to two seconds later, in all four * apps): drawn from only the quick answer, the card, [ownIds] and every shared row are composed at once, each * where it will stay; when the whole answer comes, the slow lines fill in place and no row moves or changes height. */ fun firstFrameCheck(compose: ComposeContentTestRule, app: String, skin: SettingsSkin, wrap: @Composable (@Composable () -> Unit) -> Unit, ownIds: List, own: (Done) -> List, notifies: Boolean) { val words = SettingsWords(lines(app)) var answer by mutableStateOf(quick(notifies)) compose.mainClock.autoAdvance = false compose.setContent { wrap { SettingsView(answer, skin, words, "Settings", "Back", "+1 555 0190 1234", null, {}, {}, own = own(Done()), notifying = if (notifies) true else null) } } compose.mainClock.advanceTimeByFrame() val shared = listOfNotNull("sync", "claude", "privacy", if (notifies) "notifications" else null, "storage", "help", "about") val tags = listOf("settings-card") + (ownIds + shared).map { "settings-row-$it" } fun bounds() = tags.associateWith { compose.onNodeWithTag(it).fetchSemanticsNode().boundsInRoot } val first = bounds() compose.onNodeWithTag("settings-card").assertTextContainsHere("Kavi Rao") compose.onNodeWithTag("settings-row-sync").assertTextContainsHere("Synced at 9:53") compose.mainClock.autoAdvance = true answer = signedIn(notifies) compose.waitForIdle() compose.onNodeWithTag("settings-row-storage").assertTextContainsHere("12 MB on this phone") compose.onNodeWithTag("settings-row-privacy").assertTextContainsHere("2 people") val filled = bounds() tags.forEach { assertEquals("$it stands where it stood on the first frame", first[it], filled[it]) } nothingRaw(compose, "the quick top level") } private fun androidx.compose.ui.test.SemanticsNodeInteraction.assertTextContainsHere(text: String) = assertTrue("said ${said(fetchSemanticsNode())}, not $text", said(fetchSemanticsNode()).any { it.contains(text) }) private fun androidx.compose.ui.test.SemanticsNodeInteraction.assertExistsHere() = assertExists() private fun androidx.compose.ui.test.SemanticsNodeInteraction.assertDoesNotExistHere() = assertDoesNotExist() }