Andee lives on your phone or Mac and uses any app like you do—no integrations, no root, any model; memory and passwords never leave your device | Andee 住进你的手机或 Mac,像你一样操作任何 App——无需集成、无需 Root、支持任意模型,记忆和密码绝不离开设备。
test(desktop): pin the artifact-shelf invariants
booboosui80e5f8e883f5
diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Pages.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Pages.ktindex c4d1028..881964b 100644--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Pages.kt+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Pages.kt@@ -36,8 +36,11 @@ object Pages {private val entries = mutableListOf<Page>()private var nextId = 1L- private val dir get() = File(AppDirs.root, "pages").apply { mkdirs() }- private val indexFile get() = AppDirs.file("pages.json")+ /** Overridable for tests; the shelf's files live under here. */+ @Volatile var base: File = AppDirs.root++ private val dir get() = File(base, "pages").apply { mkdirs() }+ private val indexFile get() = File(base, "pages.json")init { load() }@@ -47,6 +50,13 @@ object Pages {/** Fired on the UI thread's behalf after the shelf changes; the panel reads [artifacts] on it. */@Volatile var onChange: () -> Unit = {}+ /** For tests: point the shelf at a fresh directory and drop everything in memory. */+ fun resetForTest(b: File) {+ base = b+ synchronized(lock) { entries.clear(); nextId = 1L }+ armedGroup = null+ }+fun beginTurn() { armedGroup = null }fun armGroup(id: Long) { armedGroup = id }@@ -62,8 +72,9 @@ object Pages {fun add(title: String, html: String, groupId: Long?): Page? {val (id, group, version) = synchronized(lock) {- val g = groupId ?: nextId- Triple(nextId, g, nextVersionOf(g))+ val id = nextId+++ val g = groupId ?: id+ Triple(id, g, nextVersionOf(g))}val file = runCatching { File(dir, "page_$id.html").also { it.writeText(html) } }.getOrElse {Log.w(TAG, "could not archive page: ${it.message}")@@ -111,7 +122,7 @@ object Pages {return add(source.title, html, groupId = source.groupId)}- private fun nextVersionOf(groupId: Long): Int = synchronized(lock) {+ internal fun nextVersionOf(groupId: Long): Int = synchronized(lock) {entries.filter { it.groupId == groupId }.maxOfOrNull { it.version }?.plus(1) ?: 1}diff --git a/desktop/src/test/kotlin/net/kuafuai/andee/desktop/PagesTest.kt b/desktop/src/test/kotlin/net/kuafuai/andee/desktop/PagesTest.ktnew file mode 100644index 0000000..042564f--- /dev/null+++ b/desktop/src/test/kotlin/net/kuafuai/andee/desktop/PagesTest.kt@@ -0,0 +1,87 @@+package net.kuafuai.andee.desktop++import org.junit.Assert.assertEquals+import org.junit.Assert.assertNotNull+import org.junit.Assert.assertNull+import org.junit.Assert.assertTrue+import org.junit.Test+import java.io.File++/**+ * The artifact-shelf invariants, the ones the phone hit with `ChatHistoryVersionsTest`:+ * one artifact = one group, versions only ever go up, and the revision target is+ * consumed exactly once — a turn that reads a page back and then draws two of+ * them gets a revision and then a new artifact, not two revisions of the first.+ */+class PagesTest {++ private fun fresh(): File = File.createTempFile("pages", "").also { it.delete(); it.mkdirs() }++ @Test+ fun `a new page is version 1 and its own group`() {+ Pages.resetForTest(fresh())+ val a = Pages.add("a", "<html>", null)!!+ assertEquals(1, a.version)+ assertEquals(a.id, a.groupId)+ }++ @Test+ fun `a revision gets the next version, not a new artifact`() {+ Pages.resetForTest(fresh())+ val a = Pages.add("a", "<html>", null)!!+ val a2 = Pages.add("a", "<html>", a.groupId)!!+ assertEquals(a.groupId, a2.groupId)+ assertEquals(2, a2.version)+ assertEquals(1, Pages.pages().size) // collapsed to one artifact+ assertEquals(2, Pages.versionsOf(a.groupId).size)+ }++ @Test+ fun `the revision target is consumed once`() {+ Pages.resetForTest(fresh())+ val a = Pages.add("a", "<html>", null)!!+ Pages.armGroup(a.groupId)+ assertEquals(a.groupId, Pages.armOrResolve(null)) // consumed by the first draw+ assertNull(Pages.armOrResolve(null)) // the next draw is a new artifact+ }++ @Test+ fun `an explicit revise wins and clears the armed target`() {+ Pages.resetForTest(fresh())+ val a = Pages.add("a", "<html>", null)!!+ val b = Pages.add("b", "<html>", null)!!+ Pages.armGroup(a.groupId)+ assertEquals(b.groupId, Pages.armOrResolve(b.groupId))+ assertNull(Pages.armOrResolve(null)) // armed was cleared, not just overridden+ }++ @Test+ fun `the armed target is cleared at the turn boundary`() {+ Pages.resetForTest(fresh())+ val a = Pages.add("a", "<html>", null)!!+ Pages.armGroup(a.groupId)+ Pages.beginTurn()+ assertNull(Pages.armOrResolve(null))+ }++ @Test+ fun `rollback files an old revision as the next version`() {+ Pages.resetForTest(fresh())+ val a = Pages.add("a", "v1", null)!!+ val a2 = Pages.add("a", "v2", a.groupId)!!+ val back = Pages.restoreVersion(a.id)!!+ assertEquals(a.groupId, back.groupId)+ assertEquals(3, back.version) // append-only, not v1 again+ assertTrue(Pages.versionsOf(a.groupId).any { it.id == a2.id })+ }++ @Test+ fun `an artifact with twenty revisions keeps the newest and drops the oldest file`() {+ Pages.resetForTest(fresh())+ var g: Long? = null+ for (i in 1..25) { val p = Pages.add("a", "<html>$i", g); g = p!!.groupId }+ val revs = Pages.versionsOf(g!!)+ assertTrue(revs.size <= 20)+ assertNotNull(revs.firstOrNull { it.version == 25 })+ }+}