package com.cottondb.sites import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.jsonObject import kotlinx.serialization.json.jsonPrimitive import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue import org.junit.Test import java.io.File /** * SITES ROWS on Android (assets/design/sites/SYNC-DESIGN.md, design 2; the core's d91b912731): the eight calls the rows * add, each drawn from the core's answer and words. */ class SitesRowsTest { 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/sites/kotlin/com/cottondb/sites/$name").readText() private val words by lazy { Json.parseToJsonElement(File(repo, "crates/cotton-core/kits/shell/lines/sites.json").readText()).jsonObject } private val rust by lazy { File(repo, "crates/cotton-ffi/src/phone/sites.rs").readText() + File(repo, "crates/cotton-ffi/src/phone/sites_app.rs").readText() } private val state by lazy { src("SitesState.kt") } /** A body of [name] in [text]: from `fun name` to the next `fun ` at the same depth (good enough for these files). */ private fun body(text: String, name: String): String { val at = text.indexOf("fun $name") assertTrue("fun $name is there", at >= 0) val next = Regex("""\n (suspend |private |internal )*fun """).find(text, at + 10)?.range?.first ?: text.length return text.substring(at, next) } @Test fun every_rows_call_is_the_cores_and_is_made() { listOf("sites.undo", "sites.keep_one", "sites.stores.gone", "sites.stores.keep", "sites.stores.removed").forEach { m -> assertTrue("$m is a core call", "\"$m\"" in rust) assertTrue("$m is asked", state.contains("ask(\"$m\")")) } } @Test fun the_words_used_are_in_the_line_set() { listOf("said" to "undo", "said" to "removed", "twins" to "keep", "add" to "cancel").forEach { (set, key) -> assertTrue("$set.$key", words[set]!!.jsonObject.containsKey(key)) } } /** 1. A removal offers Undo for the core's five seconds, its store kept until the core names it; said once. */ @Test fun a_removal_offers_undo_and_keeps_its_store_until_the_core_says() { val remove = body(state, "remove(") assertTrue(remove.contains("ask(\"sites.delete\")")) assertFalse("the store is not deleted at once: Undo brings it back signed in", remove.contains("Pages.delete")) assertTrue(remove.contains("offerUndo(row.id, a.str(\"said\")") && remove.contains("a.obj(\"undo\")")) val offer = body(state, "offerUndo") assertTrue("the core's seconds", offer.contains("\"seconds\"") && offer.contains("undo?.str(\"label\") ?: w(\"said.undo\")")) assertTrue("the list asked again when the time is up, which is when the core erases", offer.contains("if (undoing == now) { undoing = null; load() }")) assertTrue(body(state, "undo").contains("ask(\"sites.undo\") { put(\"id\", u.id) }")) val app = src("SitesApp.kt") val bar = app.substring(app.indexOf("private fun UndoBar")) assertTrue("TalkBack hears it as it appears", bar.contains("liveRegion = LiveRegionMode.Polite")) assertTrue("Undo is a named control", bar.contains("control(u.label) { state.undo() }")) assertFalse("never counted down aloud", bar.contains("seconds")) } /** * SITES NO SWITCH (owner, 27 Nov 2026: "We do not need that switch at all in sites"): no call, sheet or row keeps a * site only on this phone or asks about it at sign-in; Sign in is the shell's own. */ @Test fun the_switch_is_gone() { val gone = listOf("sites.only_here", "sites.signin.plan", "sites.signin.leave", "sites.signin.abandon") gone.forEach { m -> assertFalse("$m is no core call", "\"$m\"" in rust) } val all = File(repo, "apps/android/app/src/sites/kotlin/com/cottondb/sites").listFiles()!!.joinToString("\n") { it.readText() } (gone + listOf("not_elsewhere", "notElsewhere", "list_here", "Sheet.Join", "JoinSheet", "onlyHere", "addElsewhere", "\"only.label\"")).forEach { assertFalse("$it is gone", all.contains(it)) } listOf("only", "signin", "not_elsewhere").forEach { assertFalse("its words left the line set: $it", words.containsKey(it)) } assertTrue(src("You.kt").contains("else state.signIn()")) assertTrue(body(state, "signIn").contains("shell.flow = \"signIn\"")) } /** SITES ROWS 2 retired `sites.before` (an old book is taken in only by the copy it was marked with): nothing asks it or draws it. */ @Test fun sites_before_is_gone() { assertFalse("the core no longer answers it", "\"sites.before\"" in File(repo, "crates/cotton-ffi/src/methods.rs").readText()) listOf("ListScreen.kt", "SitesState.kt", "SitesApp.kt", "Sheets.kt").forEach { f -> val text = src(f) listOf("sites.before", "obj(\"before\")", "Before(", "fun before(").forEach { assertFalse("$f: $it", text.contains(it)) } } assertFalse("its words left the line set", words.containsKey("before")) // Every Sites call the app makes is one the core answers. val methods = File(repo, "crates/cotton-ffi/src/methods.rs").readText() val asked = File(repo, "apps/android/app/src/sites/kotlin/com/cottondb/sites").listFiles()!!.flatMap { Regex("\"(sites\\.[a-z_.]+)\"").findAll(it.readText()).map { m -> m.groupValues[1] }.toList() }.toSet() asked.forEach { assertTrue("$it is a core call", "\"$it\"" in methods) } } /** 4. Keep one: the list's twins in the list, hidden twins only inside the hidden part (shown only behind the lock). */ @Test fun keep_one_never_names_a_hidden_site_outside_the_lock() { val list = src("ListScreen.kt") val showing = list.indexOf("if (showing) {") assertTrue(list.indexOf("home.arr(\"twins\")") in 0 until showing) assertTrue("hidden twins only while the hidden sites show", list.indexOf("hidden.arr(\"twins\")") > showing) assertTrue("only rows that part of the list shows", list.contains("among.firstOrNull { it.id == id }") && list.contains("Twin(state, t, hrows)") && list.contains("Twin(state, t, rows)")) val keep = body(state, "keepOne") assertTrue(keep.contains("ask(\"sites.keep_one\") { put(\"keep\", keep.id); put(\"drop\", d.id) }") && keep.contains("offerUndo(")) assertTrue("the one signed in here is offered first", src("Sheets.kt").contains("s.rows.firstOrNull { !it.signInOnce }")) } /** 5. The website stores the core names go from this phone, and only those truly gone are confirmed. */ @Test fun website_stores_go_when_the_core_says() { val clean = body(state, "cleanStores") assertTrue(clean.contains("ask(\"sites.stores.gone\")") && clean.contains("StoresGone.plan(StoresGone.of(a, unlocked || noLock))")) assertTrue("a set he kept is not asked again (the core keeps it across launches)", clean.contains("if (a?.bool(\"kept\") == true) return") && clean.indexOf("kept") < clean.indexOf("StoresGone.plan")) assertTrue("never a throwaway place's or another app's", state.contains("o.str(\"store\")?.takeIf { it.startsWith(\"site-\") }")) val remove = body(state, "removeStores") assertTrue("its live page ends first", remove.contains("Pages.drop(p.key)")) assertTrue(remove.contains("Pages.delete(store)") && remove.contains("ask(\"sites.stores.removed\") { putJsonArray(\"stores\")")) val pages = src("Pages.kt") assertTrue("true only once the store is off this phone", pages.contains("store !in all.allProfileNames || all.deleteProfile(store)") && pages.contains("fun delete(store: String): Boolean")) assertTrue("at a cold start, before any page", body(state, "sweep").let { it.indexOf("Pages.sweep(") < it.indexOf("cleanStores()") }) assertTrue("and each time the list is drawn", body(state, "load").contains("cleanStores()")) } /** 6. The build 1 guard is dropped (coordinator, 26 Nov: phones do not hold other devices' rows): no update-first line. */ @Test fun there_is_no_update_first_line() { listOf("ListScreen.kt", "SitesState.kt", "SitesApp.kt", "Sheets.kt").forEach { assertFalse(it, src(it).contains("update_first")) } } /** Nothing behind an open sheet is reachable by TalkBack: the screen, and the page's web view. */ @Test fun nothing_behind_a_sheet_is_reachable() { assertTrue(src("SitesApp.kt").contains("semantics { if (state.sheet != null) hideFromAccessibility() }")) assertTrue(src("SiteScreen.kt").contains("if (covered) android.view.View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS")) } @Test fun twin_ids_read_the_cores_ids() { val t = Json.parseToJsonElement("""{"line":"Club appears twice","keep":"Keep one","ids":["a","b"]}""") as JsonObject assertEquals("a,b", twinIds(t)) assertEquals("", twinIds(Json.parseToJsonElement("""{"line":"x"}""") as JsonObject)) } private fun json(text: String) = Json.parseToJsonElement(text) as JsonObject private fun gone(n: Int, label: (Int) -> String = { "" }) = json("""{"stores":[${(1..n).joinToString(",") { """{"store":"site-$it","store_uuid":"u$it"${label(it)}}""" }}]}""") /** 7. More than three stores named gone in one answer are asked first; three or fewer go as before. */ @Test fun more_than_three_stores_at_once_are_asked_first() { assertEquals(3, StoresGone.ASK_ABOVE) assertEquals(StoresGone.Plan.Nothing, StoresGone.plan(StoresGone.of(gone(0), false))) assertEquals(StoresGone.Plan.Nothing, StoresGone.plan(StoresGone.of(null, false))) val three = StoresGone.plan(StoresGone.of(gone(3), false)) assertTrue("three go at once", three is StoresGone.Plan.Remove && three.stores.size == 3) val four = StoresGone.plan(StoresGone.of(gone(4), false)) assertTrue("four are asked", four is StoresGone.Plan.Ask && four.stores.size == 4) // Never another app's or a throwaway place's; one store named twice counts once. assertEquals(listOf("site-a"), StoresGone.of(json("""{"stores":[{"store":"quick-1"},{"store":"site-a"},{"store":"site-a"}]}"""), false).map { it.store }) } /** The ask names a site only where the core gives its name, and a hidden one only behind the lock. */ @Test fun the_ask_never_names_a_hidden_site_outside_the_lock() { val answer = gone(4) { i -> if (i == 2) ""","label":"Bank","hidden":true""" else if (i == 1) ""","label":"Club"""" else "" } assertEquals(listOf("Club", null, null, null), StoresGone.of(answer, unlocked = false).map { it.label }) assertEquals(listOf("Club", "Bank", null, null), StoresGone.of(answer, unlocked = true).map { it.label }) val sheets = src("Sheets.kt") val sheet = sheets.substring(sheets.indexOf("private fun StoresSheet")) assertTrue("Keep them deletes and confirms nothing", sheet.contains("state.storesAnswer(s.stores, remove = false)") && sheet.contains("state.storesAnswer(s.stores, remove = true)")) val answerFun = body(state, "storesAnswer") assertTrue("Keep for now is told to the core", answerFun.contains("if (remove) removeStores(stores.map { it.store }) else ask(\"sites.stores.keep\") { putJsonArray(\"stores\")")) assertFalse("keeping never confirms", answerFun.contains("stores.removed")) assertTrue("the words are the core's, else the line set's stores words (as the iPhone)", sheet.contains("s.ask?.str(\"line\") ?: state.w(\"stores.ask\"") && sheet.contains("state.w(\"stores.delete\")") && sheet.contains("state.w(\"stores.keep\")") && sheet.contains("state.w(\"stores.line\")")) listOf("ask", "line", "delete", "keep").forEach { assertTrue("stores.$it", words["stores"]!!.jsonObject.containsKey(it)) } } private fun row(id: String, label: String, hidden: Boolean = false) = SiteRow(id, label.take(1), label, "", "", "", true, false, hidden, true, "site-$id", false) /** SITES ROWS 3 (6205368238): every field and call it adds is the core's, and every guess before it is gone. */ @Test fun rows_three_reads_the_cores_fields_and_no_guess() { listOf("\"orphans\"", "\"book_stuck\"", "\"loading\"", "\"waiting_rows\"", "\"sites.book.leave\"", "\"sites.orphans.remove\"").forEach { assertTrue("$it is the core's", it in rust) } assertTrue(state.contains("ask(\"sites.book.leave\")") && state.contains("ask(\"sites.orphans.remove\")")) val all = File(repo, "apps/android/app/src/sites/kotlin/com/cottondb/sites").listFiles()!!.joinToString("\n") { it.readText() } listOf("RowsNext", "SitesNext", "ORPHANS_UNNAMED", "not_on_server", "NotOnServer", "addToServer", "\"caught_up\"", "CAUGHT_UP", "rememberFirstSync", "FirstSyncNote").forEach { assertFalse("the guess $it is gone", all.contains(it)) } } /** 9. The earlier Sites' list that does not open here: one plain line in the core's words, and its Leave them. */ @Test fun a_book_that_does_not_open_is_one_line_and_leave_them() { val list = src("ListScreen.kt") val stuck = list.substring(list.indexOf("private fun BookStuck(")) assertTrue(list.contains("home.obj(\"book_stuck\")?.let { b -> item(\"book-stuck\") { BookStuck(state, b) } }")) assertTrue(stuck.contains("b.str(\"line\")") && stuck.contains("b.str(\"leave\")") && stuck.contains("state.leaveBook()")) assertTrue("said as it appears", stuck.substring(0, 900).contains("liveRegion")) assertTrue(body(state, "leaveBook").contains("ask(\"sites.book.leave\")")) assertEquals("Leave them", words["book_stuck"]!!.jsonObject["leave"]!!.jsonPrimitive.content) } /** 10. Orphans (SITES ROWS 4): the core's `sites` {id, label}, in You only; one Remove per site behind its own confirm; never all. */ @Test fun orphans_are_offered_one_at_a_time_by_id_and_name() { assertTrue("the core sends id and name", rust.contains("json!({ \"id\": id, \"label\": label })") && rust.contains("\"sites\": sites")) assertEquals(null, Orphans.of(json("""{"rows":[]}"""))) assertEquals("nothing offered, nothing drawn", null, Orphans.of(json("""{"orphans":{"line":"x","remove":"Remove","sites":[]}}"""))) assertEquals("the old shape (a count) draws nothing", null, Orphans.of(json("""{"orphans":{"line":"x","remove":"Remove","count":2}}"""))) val o = Orphans.of(json("""{"orphans":{"line":"Sign-ins for sites no longer in your list","remove":"Remove","sites":[{"id":"a1","label":"Club"},{"id":"b2","label":"Bank"}]}}"""))!! assertEquals(listOf(Orphan("a1", "Club"), Orphan("b2", "Bank")), o.sites) assertEquals("Remove", o.remove) assertEquals("C", o.sites[0].letter) assertEquals("ยท", Orphan("c3", "").letter) val you = src("You.kt") val section = you.substring(you.indexOf("private fun OrphansSection("), you.indexOf("private fun hideMinute")) assertTrue("drawn only when the core sends it", you.contains("Orphans.of(state.home)?.let { OrphansSection(state, it) }")) assertTrue("one Remove per site, named for TalkBack", section.contains("o.sites.forEachIndexed") && section.contains("state.askRemoveOrphan(o, r)") && section.contains("spoken = \"\$remove, \${r.label}\"")) assertFalse("never all at once, never a count", section.contains("removeAll") || section.contains("count") || state.contains("fun removeOrphans")) assertFalse("never in the list", src("ListScreen.kt").contains("orphans")) val ask = body(state, "askRemoveOrphan") // ANDROID READY 2 C: the core's words, as the iPhone asks (SitesOrphanSheet): orphans.ask, Remove, Cancel. assertTrue("its confirm names that one site in the core's words: Remove or Cancel", ask.contains("w(\"orphans.ask\", mapOf(\"label\" to row.label))") && ask.contains("w(\"add.cancel\")") && ask.contains("removeOrphan(row.id)")) assertFalse("no shell English left for it", state.contains("ORPHAN_ASK") || state.contains("ORPHAN_KEEP")) val lines = java.io.File(repo, "crates/cotton-core/kits/shell/lines/sites.json").readText() assertTrue(lines.contains("\"ask\": \"Remove the sign-in for {label}? This phone forgets everything the site kept.\"")) val remove = body(state, "removeOrphan(") assertTrue("ONE id, the core's field", remove.contains("ask(\"sites.orphans.remove\") { put(\"id\", id) }") && rust.contains("request.get(\"id\")")) assertTrue("its store then goes as the core names it (stores.gone, then stores.removed)", remove.contains("load()") && body(state, "load").contains("cleanStores()")) } }