aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-android/app/src/test/kotlin/org/meshbay/client/ManifestLogTest.kt
blob: bb3a67f1a67d1032b8e4a5578a3d31362b5ba446 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package org.meshbay.client

import org.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.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.meshbay.client.phonesync.ManifestLog
import java.io.File
import java.util.TimeZone

class ManifestLogTest {
    @get:Rule val tmp = TemporaryFolder()

    private fun line(n: Int) = JSONObject().put("node", "d/$n.jpg").put("source", "DCIM/Camera/$n.jpg")

    private fun nodes(f: File) = f.readLines().filter { it.isNotBlank() }.map { JSONObject(it).getString("node") }

    @Test fun `nothing waiting means nothing to send`() {
        val log = ManifestLog(File(tmp.root, "m"))
        assertFalse(log.waiting())
        assertNull(log.issue())
    }

    @Test fun `what was sent is described once the node has the manifest, and only then forgotten`() {
        val log = ManifestLog(File(tmp.root, "m"))
        log.append(line(1)); log.append(line(2))
        assertTrue(log.waiting())
        assertEquals(listOf("d/1.jpg", "d/2.jpg"), nodes(log.issue()!!))
        log.confirm()
        assertFalse(log.waiting())
        assertNull(log.issue())
    }

    @Test fun `a manifest that did not arrive goes again, with what came since`() {
        val log = ManifestLog(File(tmp.root, "m"))
        log.append(line(1))
        log.issue()                      // sent, never confirmed: the run was cut short
        log.append(line(2))
        val again = ManifestLog(File(tmp.root, "m"))   // a restart in between
        assertTrue(again.waiting())
        assertEquals(listOf("d/1.jpg", "d/2.jpg"), nodes(again.issue()!!))
    }

    @Test fun `manifests are named for when they were written`() {
        assertEquals("manifest-2026-10-09-120000.jsonl",
                     ManifestLog.nameFor(1791547200000L, TimeZone.getTimeZone("UTC")))
    }
}