package com.cottondb.sync import com.cottondb.shell.FirstSync import com.cottondb.shell.Loading import com.cottondb.shell.SyncSeen import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonObject import org.junit.Assert.assertEquals import org.junit.Assert.assertNull import org.junit.Assert.assertTrue import org.junit.Test import java.io.File /** * FIRST SYNC (owner, 26 Nov 2026, from the iPhone: Post said "No mail here yet", "0 mails" and "None yet" while his copy * came down): while the first pull runs, or a pass catches up, an empty place says it is loading in the core's words; * and every Sync page says the one seen line, read live. */ class FirstSyncTest { private val repo = generateSequence(File("").absoluteFile) { it.parentFile }.first { File(it, "apps/registry.json").isFile } private fun home(text: String) = Json.parseToJsonElement(text) as JsonObject private val upToDate = SyncSeen("up_to_date", "Synced just now") /** The first pull's own line first; else the seen line while it catches up; else the empty state may stand. */ @Test fun an_empty_place_says_loading_only_while_the_copy_comes_down() { val bringing = home("""{"bringing":{"words":"Loading your data… 42%","percent":42}}""") val catching = SyncSeen("catching_up", "Syncing… 1,200 of 3,108", percent = 38) assertEquals(Loading("Loading your data… 42%", 42), FirstSync.of(bringing, catching)) assertEquals(Loading("Syncing… 1,200 of 3,108", 38), FirstSync.of(home("""{"bringing":null}"""), catching)) assertEquals(Loading("Syncing… 1,200 of 3,108", 38), FirstSync.of(null, catching)) // A copy that has caught up once says its empty places as they are while a later pass runs. assertNull(FirstSync.of(home("""{"bringing":null,"device":{"rows":4341,"caught_up_at":"2026-11-26T10:17:52.720Z"}}"""), catching)) assertEquals(Loading("Syncing… 1,200 of 3,108", 38), FirstSync.of(home("""{"bringing":null,"device":{"rows":46,"caught_up_at":null}}"""), catching)) assertNull(FirstSync.of(home("""{}"""), SyncSeen("up_to_date", "Synced just now"))) assertNull(FirstSync.of(home("""{}"""), SyncSeen("waiting", "1 change waiting to sync"))) assertNull(FirstSync.of(home("""{}"""), SyncSeen("unreached", "Can't reach Cotton right now · trying again"))) assertNull(FirstSync.of(null, null)) // The moment between a sign-in landing and `shell.home` saying so: signed in, never caught up, the boot set's words. assertEquals(Loading("Loading your data…", null), FirstSync.of(home("""{"stand":"local","bringing":null,"device":{"caught_up_at":null}}"""), SyncSeen("signed_out", "Signed out"), signedIn = true, bringing = "Loading your data…")) assertNull(FirstSync.of(home("""{"stand":"local","bringing":null,"device":{"caught_up_at":null}}"""), SyncSeen("signed_out", "Signed out"), signedIn = false, bringing = "Loading your data…")) assertNull(FirstSync.of(home("""{"device":{"caught_up_at":"2026-11-26T10:17:52.720Z"}}"""), upToDate, signedIn = true, bringing = "Loading your data…")) } /** The shell's Sync page and its row say the seen line, live, never only the settings' answer read once. */ @Test fun the_shells_sync_page_says_the_one_line() { val settings = File(repo, "apps/android/shell/src/main/kotlin/com/cottondb/shell/SettingsHome.kt").readText() // Changes that cannot be sent (UNSENT HEAL) are said over it while any stand; else the seen line (SyncStuckTest). assertTrue(settings.contains("skin.Big(SyncStuck.line(stuck) ?: seen?.line ?: sync?.str(\"title\")")) assertTrue(settings.contains("SyncStuck.row(settings?.obj(\"sync\")?.obj(\"stuck\"), seen?.line, row.str(\"line\"))")) assertTrue(settings.contains("val seen = rememberSyncSeen(shell)")) assertTrue(settings.contains("seen = seen,")) // A screen's first frame takes a line whose pass already ran its moment, so an empty state never flashes first. val seen = File(repo, "apps/android/shell/src/main/kotlin/com/cottondb/shell/SyncSeen.kt").readText() assertTrue(seen.contains("FirstSync.of(shell.home, said, signedIn, shell.lines.line(\"boot\", \"home.bringing\"))")) // The loading bar's moving piece stays inside the bar (it drew past its left edge in Comms' padded place). assertTrue(File(repo, "apps/android/shell/src/main/kotlin/com/cottondb/shell/SettingsParts.kt").readText().contains("Modifier.fillMaxWidth().height(2.dp).clipToBounds().testTag(\"loading-bar\")")) assertTrue(seen.contains("SyncSeen.of(shell.seen)?.takeIf { SyncSeen.wait(it, null, System.currentTimeMillis()) == null }")) } }