package com.cottondb.sync import com.cottondb.shell.SyncSeen import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertNull import org.junit.Assert.assertTrue import org.junit.Test import java.io.File /** * SYNC SEEN, one source (coordinator, 26 Nov 2026; launch-ios 23cc5732c0): Android reads the line the iPhone reads, the * core's `seen` (`shell_account::seen_of`, the same on `sync.status` and `shell.home`), and keeps the iPhone's promises * (SyncSeenTests.swift): held back until a pass has run `showAfterMs`, `spoken` said once, read on loop events, placed always. */ class SyncSeenTest { private val repo = generateSequence(File("").absoluteFile) { it.parentFile }.first { File(it, "apps/registry.json").isFile } private fun seen(text: String) = Json.parseToJsonElement(text) as JsonObject private val upToDate = SyncSeen("up_to_date", "Synced at 9:53 am") private fun catching(since: Long?) = SyncSeen("catching_up", "Syncing… 1,200 of 3,000", "Syncing…", 1_000, since, 40) /** The line is the core's `seen`, every field of it; none when it says no line. */ @Test fun the_line_is_the_cores_seen() { val read = SyncSeen.of(seen("""{"state":"catching_up","line":"Syncing… 1,200 of 3,000","spoken":"Syncing…","showAfterMs":1000,"since":1764100000000,"percent":40}""")) assertEquals(catching(1_764_100_000_000), read) assertEquals("Synced just now", SyncSeen.of(seen("""{"state":"up_to_date","line":"Synced just now","spoken":"Synced just now","showAfterMs":1000}"""))?.spoken) assertEquals("Signed out", SyncSeen.of(seen("""{"state":"signed_out","line":"Signed out"}"""))?.spoken) assertNull(SyncSeen.of(seen("""{"state":"up_to_date","line":""}"""))) assertNull(SyncSeen.of(null)) assertFalse(SyncSeen("waiting", "1 change waiting to sync").catchingUp) } /** No flicker: a catching-up line waits until its pass has run showAfterMs; already catching up, or any other line, shows at once. */ @Test fun a_pass_under_its_wait_never_flickers_the_line() { val now = 1_764_100_000_000L assertEquals(700L, SyncSeen.wait(catching(now - 300), upToDate, now)) assertNull(SyncSeen.wait(catching(now - 1_500), upToDate, now)) assertNull(SyncSeen.wait(catching(now - 300), catching(now - 5_000), now)) assertNull(SyncSeen.wait(catching(null), upToDate, now)) assertNull(SyncSeen.wait(upToDate, catching(now - 300), now)) assertNull(SyncSeen.wait(SyncSeen("unreached", "Can't reach Cotton right now · trying again"), upToDate, now)) assertEquals(2_000L, SyncSeen.CATCHING_UP_READ_MS) assertEquals(60_000L, SyncSeen.READ_MS) } /** * The words are the shell's `boot` `account.tile` set (the iPhone's too), the shell reads `sync.status` after loop * and data events, and every home places the line always: never behind a test on the line it has not read yet. */ @Test fun one_source_read_on_events_and_placed_always() { val words = Json.parseToJsonElement(File(repo, "crates/cotton-core/kits/shell/lines/boot.json").readText()).jsonObject["account"]!!.jsonObject["tile"]!!.jsonObject listOf("signed_out", "waiting", "waiting_one", "syncing", "syncing_of", "unreached", "synced_now", "synced", "stopped") .forEach { assertTrue(it, words.containsKey(it)) } val shell = File(repo, "apps/android/shell/src/main/kotlin/com/cottondb/shell/Shell.kt").readText() assertTrue(shell.contains("core.obj(\"sync.status\")")) assertTrue(shell.contains("if (event.kind == \"data\" || event.kind == \"client\") readSeenSoon()")) val homes = mapOf( "notes" to "notes/kotlin/com/cottondb/notes/NotesSurface.kt", "days" to "days/kotlin/com/cottondb/days/DaysSurface.kt", "story" to "story/kotlin/com/cottondb/story/ink/HomeScreen.kt", "messages" to "messages/kotlin/com/cottondb/messages/warm/ChatRoot.kt", "keys" to "keys/kotlin/com/cottondb/keys/secrets/ListScreen.kt", "mail" to "mail/kotlin/com/cottondb/mail/Home.kt", ) homes.forEach { (app, path) -> val text = File(repo, "apps/android/app/src/$path").readText() assertTrue(app, text.contains("SyncSeenLine(")) assertFalse(app, text.contains("SyncSeen.of(shell.home)") || text.contains("SyncSeen.of(state.shell.home)")) } } }