Cotton code
193 lines · 13.0 KB · KotlinRaw
package com.cottondb.cinemasimport com.cottondb.cinemas.stream.BinPageimport com.cottondb.cinemas.stream.HiddenPageimport com.cottondb.cinemas.stream.forOneimport kotlinx.serialization.json.Jsonimport kotlinx.serialization.json.JsonPrimitiveimport kotlinx.serialization.json.jsonObjectimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertFalseimport org.junit.Assert.assertNullimport org.junit.Assert.assertTrueimport org.junit.Testimport java.io.File/** * HIDDEN (CONTRACT-2.md; owner, 26 Nov 2026): the filter is the core's, so what the phone must hold is the trust: it * says `hidden: true` only inside Hidden after the lock has passed, forgets what it read there when the app leaves, * keeps the window secure while it shows, and reuses the shell's one lock (Sites' scheme), not a second one. */class HiddenTest {    private val root = generateSequence(File("").absoluteFile) { it.parentFile }.first { File(it, "apps/android").isDirectory }    private val stream = File(root, "apps/android/app/src/cinemas/kotlin/com/cottondb/cinemas/stream")    private val state = File(stream, "CinemasState.kt").readText()    private val hidden = File(stream, "Hidden.kt").readText()    private val app = File(stream, "CinemasApp.kt").readText()    @Test    fun hidden_is_asked_only_inside_hidden_once_unlocked() {        assertTrue(state.contains("val inHidden: Boolean get() = unlocked && Screen.Hidden in stack"))        assertTrue("ask adds hidden only when inHidden", state.contains("val hiddenAsk = inHidden && method in KNOW_HIDDEN"))        assertTrue("a call refused as locked locks Hidden again", state.contains("if (refused.code == \"locked\" && unlocked) relock()"))        assertTrue("the place itself is asked unlocked only", state.contains("if (!unlocked) { hidden = Loaded.Waiting; return }"))        // HIDDEN 2: the core holds the unlock, said once the lock has passed, and shut on leaving.        val unlock = state.substring(state.indexOf("suspend fun unlock(")).substringBefore("\n    }\n")        assertTrue(unlock.indexOf("cinemas.unlock") in 0 until unlock.indexOf("unlocked = true"))        assertTrue(state.substring(state.indexOf("fun lock()")).substringBefore("\n    }\n").contains("cinemas.lock"))        assertTrue("leaving Hidden locks it", app.contains("Screen.Hidden in state.stack }.collect { inside -> if (!inside) state.lock() }"))    }    @Test    fun a_binned_hidden_category_shows_in_hidden_with_restore() {        // Core 3, M1: inside Hidden a hidden category the Bin holds is said so ("In the Bin") with Restore, the core's call.        val model = File(stream, "Model.kt").readText()        val home = File(stream, "Home.kt").readText()        assertTrue(model.contains("o.str(\"binned\"), CoreCall.of(o.obj(\"restore\"))"))        assertTrue(home.contains("if (hiddenPlace && row.binned != null)") && home.contains("state.call(restore)"))        assertTrue(state.contains("val answer = ask(made.method, made.args)"))    }    @Test    fun leaving_the_app_locks_it_and_forgets_what_was_read() {        assertTrue(app.contains("Lifecycle.Event.ON_STOP) { state.lock(); state.playback?.release() }"))        // A discreet film's picture never stands past Hidden's lock: coming back shuts it (not one on the TV).        assertTrue(app.contains("state.playback?.takeIf { it.veiled && it.castingOn == null }?.close()"))        val lock = state.substring(state.indexOf("fun lock()")).substringBefore("\n    }\n")        listOf("unlocked = false", "hiddenFound = null", "hiddenKeys.forEach", "stack.removeAt").forEach { assertTrue("lock does $it", lock.contains(it)) }    }    @Test    fun the_shells_one_lock_and_a_secure_window() {        assertTrue(hidden.contains("import com.cottondb.shell.deviceLock"))        // Core 3, H1: the window holds FLAG_SECURE while Hidden or anything over it, or a veiled film, is on screen,        // and lets it go only back in front with neither (not in Hidden's own screen, which a title page covers).        assertTrue(app.contains("val secure = state.inHidden || state.playback?.veiled == true"))        assertTrue(app.contains("if (secure) window.addFlags(flag)"))        assertTrue(!hidden.contains("clearFlags"))        // Core 3, M3: a phone with no screen lock never opens Hidden; it says to set one.        assertTrue("no screen lock is said, not hidden", hidden.contains("hidden-no-lock"))        assertTrue(hidden.contains("if (lockless) return"))        // Walked 27 Nov (API 29): a lock set in Settings from Hidden is seen on return, read again as the app resumes.        assertTrue(hidden.contains("Lifecycle.Event.ON_RESUME) lockless = !context.hasDeviceLock()"))    }    @Test    fun every_way_into_hidden_goes_through_one_switch() {        // NEXT-HIDDEN2.md item 8: Hide this category, Stop hiding and You › Hidden come and go together (walked 26 Nov).        assertTrue(state.contains("const val HIDDEN_READY = "))        val home = File(stream, "Home.kt").readText()        val sheets = File(stream, "Sheets.kt").readText()        val you = File(stream, "You.kt").readText()        assertTrue(home.contains("row.group?.takeIf { CinemasState.HIDDEN_READY }"))        assertTrue(sheets.contains("if (CinemasState.HIDDEN_READY) Row("))        assertTrue(you.contains("if (CinemasState.HIDDEN_READY) {"))    }    @Test    fun a_film_from_hidden_is_never_named_outside_the_app() {        // The coordinator, 26 Nov 2026: the notification, the lock screen and the TV never name a hidden title.        val player = File(stream.parentFile, "player/FilmPlayback.kt").readText()        val cast = File(stream.parentFile, "player/FilmCast.kt").readText()        assertTrue(state.contains("playback?.fromHidden = inHidden") && state.contains("film.fromHidden = inHidden"))        // Core 3, M2: the veil follows the core's `now.discreet` at every answer, the playing item's words laid again.        assertTrue(player.contains("discreet(film[\"discreet\"]?.jsonPrimitive?.booleanOrNull == true, film)"))        assertTrue(player.contains("var veiled by mutableStateOf(false); private set"))        // Walked 27 Nov: the 15-second report's answer is taken, so a category hidden mid-film veils it then, not at the next play.        val report = player.substring(player.indexOf("private fun report(")).substringBefore("\n    }\n")        assertTrue(report.contains("answer = answered"))        // A change in the data asks again at once, reporting nothing (so no write, and no loop of its own events).        assertTrue(player.contains("else if (shell.revision != seen) recheck()"))        val recheck = player.substring(player.indexOf("private fun recheck(")).substringBefore("\n    }\n")        assertTrue(recheck.contains("answer = answered") && !recheck.contains("listened"))        // ...and a film veiled while it floats over Home leaves its window.        assertTrue(File(stream.parentFile, "CinemasApplication.kt").readText().contains("if (film.floating && film.veiled) {"))        assertTrue(player.contains("if (veiled) MediaMetadata.Builder().setTitle(veil).build()"))        assertTrue("no poster laid on a veiled film", player.contains("if (veiled) return"))        assertTrue("a film from Hidden never floats over Home", File(stream.parentFile, "CinemasApplication.kt").readText().contains("film.playing && !film.veiled"))        assertTrue("played and prepared from among the hidden titles", player.contains("if (fromHidden) put(\"hidden\", true)") && player.contains("if (hidden) put(\"hidden\", true)"))        assertTrue(player.contains("veiled = if (veiled) veil else null") && cast.contains("setTitle(veiled ?: cast.str(\"title\"))"))    }    @Test    fun locked_under_the_person_it_waits_for_show_hidden() {        // Only opening Hidden from You asks the phone's lock by itself; locked under a title's page it shows Locked.        assertTrue(File(stream, "You.kt").readText().contains("state.openHidden()"))        assertTrue(hidden.contains("if (!state.unlocked && state.askOnOpen) ask(); state.askOnOpen = false"))    }    @Test    fun hiddens_bin_row_is_drawn_like_a_row_and_opens_its_bin() {        // Core 4, M2: cinemas.hidden may end with the Bin row, its count and its act; absent while that Bin is empty.        val page = HiddenPage.of(Json.parseToJsonElement("""            {"title": "Hidden", "locked": false, "rows": [              {"id": "group:g1", "title": "Late Shows", "group": "g1", "count": 1, "hidden": true, "items": [{"table": "film", "id": "f1", "title": "Pine Hollow"}], "more": false},              {"id": "bin", "title": "Bin", "count": 2, "hidden": true, "act": {"method": "cinemas.bin", "request": {"hidden": true}}}            ]}        """).jsonObject)        val bin = page.rows.last()        assertEquals("bin", bin.id)        assertEquals(2, bin.count)        assertEquals("cinemas.bin", bin.act!!.method)        assertEquals(JsonPrimitive(true), bin.act!!.request["hidden"])        assertNull("a category's row opens its shelf", page.rows.first().act)        val none = HiddenPage.of(Json.parseToJsonElement("""{"title": "Hidden", "locked": false, "rows": [], "empty": "Nothing is hidden."}""").jsonObject)        assertTrue(none.rows.none { it.act != null })        // Drawn by the one Shelf; a tap calls the act, which opens the Bin with the core's request.        val home = File(stream, "Home.kt").readText()        assertTrue(home.contains("row.act?.let { state.openAct(it, row.title) } ?: state.stack.add(Screen.Shelf(row.id, row.title))"))        assertTrue(home.contains("if (row.act != null) Text(\"\${row.count}\""))        val open = state.substring(state.indexOf("fun openAct(")).substringBefore("\n    }\n")        assertTrue(open.contains("\"cinemas.bin\" -> openBin(act.request)"))        assertTrue(state.contains("data class Bin(val request: JsonObject"))    }    @Test    fun the_bin_draws_its_acts_from_the_answer_inside_and_outside() {        val inside = BinPage.of(Json.parseToJsonElement("""            {"title": "Bin", "hidden": true, "count": 1, "empty": null,             "items": [{"item": {"table": "film", "id": "f1", "title": "Pine Hollow"}, "line": "Moved to the Bin 27 Nov"}],             "restore": {"label": "Restore", "method": "cinemas.restore", "args": {}},             "erase": {"label": "Delete forever", "method": "cinemas.erase", "args": {"hidden": true}}}        """).jsonObject)        assertTrue(inside.hidden)        assertEquals("Moved to the Bin 27 Nov", inside.items.single().line)        assertEquals("Restore", inside.restore!!.label)        // One title: the act's args plus "id"; Empty: the args as given.        val one = inside.erase!!.forOne("f1")        assertEquals("cinemas.erase", one.method)        assertEquals(JsonPrimitive("f1"), one.args["id"])        assertEquals(JsonPrimitive(true), one.args["hidden"])        assertEquals(setOf("id"), inside.restore!!.forOne("f1").args.keys)        assertFalse(inside.erase!!.args.containsKey("id"))        val outside = BinPage.of(Json.parseToJsonElement("""            {"title": "Bin", "hidden": false, "count": 0, "items": [], "empty": "Nothing in the Bin.",             "restore": {"label": "Restore", "method": "cinemas.restore", "args": {}},             "erase": {"label": "Delete forever", "method": "cinemas.erase", "args": {}}}        """).jsonObject)        assertFalse(outside.hidden)        assertEquals("Nothing in the Bin.", outside.empty)        assertTrue(outside.erase!!.args.isEmpty())        // The page draws Restore, Delete forever and Empty from the answer, never its own calls; both deletes ask first.        val you = File(stream, "You.kt").readText()        val page = you.substring(you.indexOf("fun BinScreen(")).substringBefore("\n}\n")        assertTrue(page.contains("state.binAct(restore.forOne(item.id))"))        assertTrue(page.contains("state.sheet = Sheet.Erase(erase.forOne(item.id), item.title, 1)"))        assertTrue(page.contains("state.sheet = Sheet.Erase(erase, null, a.count)"))        assertFalse("no call written by the phone", page.contains("\"cinemas.restore\"") || page.contains("\"cinemas.erase\""))        val sheet = you.substring(you.indexOf("fun EraseSheet(")).substringBefore("\n}\n")        assertTrue(sheet.contains("state.sheet = null; state.later { state.binAct(sheet.call) }"))        assertTrue(sheet.contains("state.w(\"group_cancel\")"))        assertTrue(File(stream, "Sheets.kt").readText().contains("is Sheet.Erase -> EraseSheet(state, open)"))    }    @Test    fun the_bin_inside_hidden_goes_with_its_lock() {        // Stacked over Hidden it is inside it (secure); the lock takes it away and forgets what it read.        val lock = state.substring(state.indexOf("fun lock()")).substringBefore("\n    }\n")        assertTrue(lock.contains("bin = Loaded.Waiting"))        val load = state.substring(state.indexOf("suspend fun loadBin(")).substringBefore("\n    }\n")        assertTrue(load.contains("if (request[\"hidden\"] != null && !inHidden) { bin = Loaded.Waiting; return }"))        assertTrue(state.contains("is Screen.Bin -> loadBin(screen.request)"))    }}