SyncOutcomeTest.kt

11.5 KB · raw

1package com.peachpasswords.sync23import org.junit.Assert.assertEquals4import org.junit.Assert.assertNotEquals5import org.junit.Assert.assertNull6import org.junit.Assert.assertTrue7import org.junit.Test89/**10 * The indicator light is the whole point of this vocabulary, so the mapping is11 * swept exhaustively rather than sampled: a reason added later has to be given12 * a place here or the sweep fails.13 */14class SyncOutcomeTest {1516    private val succeeded = setOf(17        SyncOutcomeReason.COMPLETED_CHANGES,18        SyncOutcomeReason.COMPLETED_NO_CHANGES,19    )2021    @Test fun `only a real success is ever green, even with devices paired`() {22        for (reason in SyncOutcomeReason.entries) {23            val indicator = syncIndicator(SyncOutcomeSummary(reason = reason), pairedCount = 5)24            if (reason in succeeded) {25                assertEquals("$reason must be ok", SyncIndicator.OK, indicator)26            } else {27                assertNotEquals("$reason must never resolve to ok", SyncIndicator.OK, indicator)28            }29        }30    }3132    /**33     * The set itself, not just its self-consistency with the light: moving a34     * reason out of it would otherwise flip red to unlit with every other test35     * still green. "Paired, but nothing here can accept a sync" in particular36     * is a problem, not a quiet state.37     *38     * Network-class outcomes (relay/handshake/timeout/connection_lost) live in39     * [SyncOutcomeReason.isWarning] and resolve to the amber light, not red —40     * the background loop keeps trying, so a transient miss is a warning, not41     * a problem. VAULT_LOCKED joined them in v2.9.35 for the same reason: the42     * loop retries once the vault unlocks, so amber, not red.43     */44    @Test fun `the failing outcomes are exactly these`() {45        assertEquals(46            setOf(47                SyncOutcomeReason.NO_ELIGIBLE_HOST,48                SyncOutcomeReason.PRIVATE_RELAY_UNSET,49                // Android refused to start the runner: nothing synced, and the50                // person can do something about it.51                SyncOutcomeReason.FOREGROUND_START_BLOCKED,52                SyncOutcomeReason.VAULT_ID_MISMATCH,53                SyncOutcomeReason.PEER_PROTOCOL_OUTDATED,54                SyncOutcomeReason.RUN_FAILED,55            ),56            SyncOutcomeReason.entries.filter { it.isFailure }.toSet(),57        )58    }5960    @Test fun `the warning outcomes are the network class plus a locked vault`() {61        assertEquals(62            setOf(63                SyncOutcomeReason.VAULT_LOCKED,64                SyncOutcomeReason.RELAY_UNREACHABLE,65                SyncOutcomeReason.HANDSHAKE_FAILED,66                SyncOutcomeReason.SYNC_TIMEOUT,67                SyncOutcomeReason.CONNECTION_LOST,68            ),69            SyncOutcomeReason.entries.filter { it.isWarning }.toSet(),70        )71    }7273    @Test fun `every failure is red and every warning is amber, and nothing else is either`() {74        for (reason in SyncOutcomeReason.entries) {75            val indicator = syncIndicator(SyncOutcomeSummary(reason = reason), pairedCount = 1)76            assertEquals(77                "$reason: isFailure and the red light must agree",78                reason.isFailure,79                indicator == SyncIndicator.ERROR,80            )81            assertEquals(82                "$reason: isWarning and the amber light must agree",83                reason.isWarning,84                indicator == SyncIndicator.WARN,85            )86        }87    }8889    @Test fun `a run in flight pulses whatever the last outcome was`() {90        for (reason in SyncOutcomeReason.entries + null) {91            assertEquals(92                SyncIndicator.BUSY,93                syncIndicator(SyncOutcomeSummary(reason = reason, syncing = true), pairedCount = 0),94            )95        }96    }9798    @Test fun `a successful pair with nothing attempted is green, and nothing paired is unlit`() {99        assertEquals(SyncIndicator.OK, syncIndicator(SyncOutcomeSummary(), pairedCount = 1))100        assertEquals(SyncIndicator.IDLE, syncIndicator(SyncOutcomeSummary(), pairedCount = 0))101        // Paired but switched off is not a problem and not health — unlit.102        assertEquals(103            SyncIndicator.IDLE,104            syncIndicator(SyncOutcomeSummary(reason = SyncOutcomeReason.SYNC_DISABLED), pairedCount = 1),105        )106    }107108    @Test fun `a sync that could not run reads differently from one that found nothing`() {109        val ranAndFoundNothing = describeSyncOutcome(110            SyncOutcomeSummary(reason = SyncOutcomeReason.COMPLETED_NO_CHANGES),111        )112        val couldNotRun = describeSyncOutcome(113            SyncOutcomeSummary(reason = SyncOutcomeReason.NO_ELIGIBLE_HOST, detail = "Pixel [type=android]"),114        )115        assertEquals("Up to date — nothing to send or receive", ranAndFoundNothing)116        assertEquals(117            "Paired, but no device here can accept a sync (Pixel [type=android])",118            couldNotRun,119        )120    }121122    /** Word for word the browser's `COPY` table — the two clients sit side by side. */123    @Test fun `copy matches the browser`() {124        fun copy(reason: SyncOutcomeReason) = describeSyncOutcome(SyncOutcomeSummary(reason = reason))125        assertEquals("Not synced yet", describeSyncOutcome(SyncOutcomeSummary()))126        assertEquals("Synced", copy(SyncOutcomeReason.COMPLETED_CHANGES))127        assertEquals("Up to date — nothing to send or receive", copy(SyncOutcomeReason.COMPLETED_NO_CHANGES))128        assertEquals("No devices paired yet", copy(SyncOutcomeReason.NO_PAIRED_DEVICES))129        assertEquals("Paired, but no device here can accept a sync", copy(SyncOutcomeReason.NO_ELIGIBLE_HOST))130        assertEquals("Sync is switched off", copy(SyncOutcomeReason.SYNC_DISABLED))131        assertEquals("Vault was locked — unlock and sync again", copy(SyncOutcomeReason.VAULT_LOCKED))132        // Android-only, so there is no browser line to match — see the enum.133        assertEquals(134            "Android blocked Peach from starting sync in the background — open Peach and sync again",135            copy(SyncOutcomeReason.FOREGROUND_START_BLOCKED),136        )137        assertEquals("Couldn't reach the relay", copy(SyncOutcomeReason.RELAY_UNREACHABLE))138        assertEquals("Reached the relay, but the device didn't answer", copy(SyncOutcomeReason.HANDSHAKE_FAILED))139        assertEquals("That device holds a different vault", copy(SyncOutcomeReason.VAULT_ID_MISMATCH))140        assertEquals(141            "Your other device needs to update before it can sync",142            copy(SyncOutcomeReason.PEER_PROTOCOL_OUTDATED),143        )144        assertEquals("The device stopped responding mid-sync", copy(SyncOutcomeReason.SYNC_TIMEOUT))145        assertEquals("The connection dropped mid-sync — will retry", copy(SyncOutcomeReason.CONNECTION_LOST))146        assertEquals("Sync failed", copy(SyncOutcomeReason.RUN_FAILED))147    }148149    @Test fun `a completed sync says what moved`() {150        assertEquals(151            "Synced — 3 sent, 1 received",152            describeSyncOutcome(153                SyncOutcomeSummary(154                    reason = SyncOutcomeReason.COMPLETED_CHANGES,155                    entriesSent = 3,156                    entriesReceived = 1,157                ),158            ),159        )160    }161162    @Test fun `the label always carries the colour's claim in words`() {163        assertEquals(164            "Sync problem — Couldn't reach the relay",165            syncIndicatorLabel(166                SyncIndicator.ERROR,167                SyncOutcomeSummary(reason = SyncOutcomeReason.RELAY_UNREACHABLE),168            ),169        )170        assertEquals("Not syncing — Not synced yet", syncIndicatorLabel(SyncIndicator.IDLE, SyncOutcomeSummary()))171        assertTrue(syncIndicatorLabel(SyncIndicator.BUSY, SyncOutcomeSummary()).startsWith("Syncing"))172        assertTrue(syncIndicatorLabel(SyncIndicator.OK, SyncOutcomeSummary()).startsWith("Sync healthy"))173    }174175    @Test fun `opaque session errors are classified, not lumped into one failure`() {176        assertEquals(SyncOutcomeReason.VAULT_LOCKED, classifySyncError("VAULT_LOCKED"))177        assertEquals(SyncOutcomeReason.VAULT_ID_MISMATCH, classifySyncError("vault_id_mismatch"))178        assertEquals(SyncOutcomeReason.PEER_PROTOCOL_OUTDATED, classifySyncError("PEER_PROTOCOL_OUTDATED"))179        assertEquals(SyncOutcomeReason.SYNC_TIMEOUT, classifySyncError("session timed out after 30s"))180        assertEquals(181            SyncOutcomeReason.HANDSHAKE_FAILED,182            classifySyncError("peer identity does not match authenticated KK key"),183        )184        assertEquals(185            SyncOutcomeReason.HANDSHAKE_FAILED,186            classifySyncError("reconnect capability does not match relay room"),187        )188        assertEquals(SyncOutcomeReason.RELAY_UNREACHABLE, classifySyncError("relay_closed_1006"))189        assertEquals(SyncOutcomeReason.CONNECTION_LOST, classifySyncError("WebSocket closed"))190        assertEquals(SyncOutcomeReason.CONNECTION_LOST, classifySyncError("connection closed by peer"))191        assertEquals(SyncOutcomeReason.RUN_FAILED, classifySyncError("something nobody predicted"))192    }193194    @Test fun `a session outcome becomes the user-facing one`() {195        assertEquals(196            SyncOutcomeReason.COMPLETED_NO_CHANGES,197            SyncOutcome(SyncOutcomeStatus.COMPLETED).toSummary().reason,198        )199        assertEquals(200            SyncOutcomeReason.COMPLETED_CHANGES,201            SyncOutcome(SyncOutcomeStatus.COMPLETED, entriesReceived = 2).toSummary().reason,202        )203        // A deletion that crossed the wire is a change, not "nothing to do".204        assertEquals(205            SyncOutcomeReason.COMPLETED_CHANGES,206            SyncOutcome(SyncOutcomeStatus.COMPLETED, tombstonesSent = 1).toSummary().reason,207        )208        assertEquals(209            SyncOutcomeReason.VAULT_ID_MISMATCH,210            SyncOutcome(SyncOutcomeStatus.VAULT_MISMATCH).toSummary().reason,211        )212        assertEquals(213            SyncOutcomeReason.SYNC_TIMEOUT,214            SyncOutcome(SyncOutcomeStatus.TIMEOUT, error = "session timed out after 30s").toSummary().reason,215        )216        assertEquals(217            SyncOutcomeReason.RELAY_UNREACHABLE,218            SyncOutcome(SyncOutcomeStatus.ERRORED, error = "relay_receive_queue_full").toSummary().reason,219        )220        assertEquals(221            SyncOutcomeReason.PEER_PROTOCOL_OUTDATED,222            SyncOutcome(SyncOutcomeStatus.ERRORED, error = "PEER_PROTOCOL_OUTDATED").toSummary().reason,223        )224    }225226    @Test fun `the reason for a failure survives being killed`() {227        val failure = SyncOutcomeSummary(228            reason = SyncOutcomeReason.HANDSHAKE_FAILED,229            detail = "peer identity does not match authenticated KK key",230            entriesSent = 4,231            entriesReceived = 2,232            finishedAt = 1_700_000_000_000L,233        )234        assertEquals(failure, decodeSyncOutcome(encodeSyncOutcome(failure)))235    }236237    @Test fun `a process killed mid-sync does not come back claiming to be syncing`() {238        val inFlight = SyncOutcomeSummary(reason = SyncOutcomeReason.COMPLETED_CHANGES, syncing = true)239        val restored = decodeSyncOutcome(encodeSyncOutcome(inFlight))240        assertEquals(false, restored.syncing)241        assertEquals(SyncOutcomeReason.COMPLETED_CHANGES, restored.reason)242    }243244    @Test fun `nothing stored, or something unreadable, restores as nothing attempted`() {245        assertNull(decodeSyncOutcome(null).reason)246        assertNull(decodeSyncOutcome("{not json").reason)247        assertNull(decodeSyncOutcome("""{"reason":"A_REASON_FROM_THE_FUTURE"}""").reason)248    }249}