aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-android/app/src/test/kotlin/org/meshbay
diff options
context:
space:
mode:
Diffstat (limited to 'packages/meshbay-android/app/src/test/kotlin/org/meshbay')
-rw-r--r--packages/meshbay-android/app/src/test/kotlin/org/meshbay/client/DocSyncTest.kt55
1 files changed, 55 insertions, 0 deletions
diff --git a/packages/meshbay-android/app/src/test/kotlin/org/meshbay/client/DocSyncTest.kt b/packages/meshbay-android/app/src/test/kotlin/org/meshbay/client/DocSyncTest.kt
new file mode 100644
index 0000000..276f1fd
--- /dev/null
+++ b/packages/meshbay-android/app/src/test/kotlin/org/meshbay/client/DocSyncTest.kt
@@ -0,0 +1,55 @@
+package org.meshbay.client
+
+import org.json.JSONObject
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertNotEquals
+import org.junit.Assert.assertNull
+import org.junit.Assert.assertThrows
+import org.junit.Test
+import org.meshbay.client.phonesync.DocConfig
+import org.meshbay.client.phonesync.DocPlan
+import java.util.TimeZone
+
+class DocSyncTest {
+ private val utc = TimeZone.getTimeZone("UTC")
+ // 2026-10-09 12:00:00 UTC
+ private val oct9 = 1791547200000L
+
+ private fun settings(account: String = "bob", folder: String = "Backups") = JSONObject()
+ .put("account", account).put("groupId", "g1").put("groupName", "Mine").put("owner", "bob")
+ .put("folder", folder)
+
+ @Test fun `each kind goes into a folder of its own named for the account`() {
+ val c = DocConfig.fromJson(settings(folder = "/Backups/Phone/"))
+ assertEquals("Backups/Phone/bob-contacts", DocPlan.dirFor(c, "contacts"))
+ assertEquals("Backups/Phone/bob-messages", DocPlan.dirFor(c, "messages"))
+ }
+
+ @Test fun `a file is named for when it was taken, in the phone's time`() {
+ assertEquals("contacts-2026-10-09-1200.vcf", DocPlan.nameFor("contacts", "vcf", oct9, utc))
+ assertEquals("contacts-2026-10-09-1400.vcf",
+ DocPlan.nameFor("contacts", "vcf", oct9, TimeZone.getTimeZone("Europe/Paris")))
+ }
+
+ @Test fun `a folder that climbs out or an account that is not a name is refused`() {
+ for (bad in listOf("../etc", "Backups/../x", "Backups//x", "./x", "")) {
+ assertThrows(bad, IllegalArgumentException::class.java) { DocConfig.fromJson(settings(folder = bad)) }
+ }
+ for (bad in listOf("../bob", "bob/x", "", "-bob")) {
+ assertThrows(bad, IllegalArgumentException::class.java) { DocConfig.fromJson(settings(account = bad)) }
+ }
+ }
+
+ @Test fun `settings survive being stored`() {
+ val c = DocConfig.fromJson(settings())
+ assertEquals(c, DocConfig.parse(c.toJson().toString()))
+ assertNull(DocConfig.parse("{not json"))
+ }
+
+ @Test fun `another group or folder is another backup`() {
+ val a = DocConfig.fromJson(settings())
+ assertNotEquals(a.ledgerKey, DocConfig.fromJson(settings(folder = "Other")).ledgerKey)
+ assertNotEquals(a.ledgerKey, DocConfig.fromJson(settings().put("groupId", "g2")).ledgerKey)
+ assertEquals(a.ledgerKey, DocConfig.fromJson(settings().put("groupName", "Renamed")).ledgerKey)
+ }
+}