SyncOutcomeTest.kt

11.7 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                // §11: a device that owes a secure reset, or whose generation55                // cannot be read, has not synced and must be told so.56                SyncOutcomeReason.RESET_REQUIRED,57                SyncOutcomeReason.SYNC_GENERATION_UNKNOWN,58                SyncOutcomeReason.RUN_FAILED,59            ),60            SyncOutcomeReason.entries.filter { it.isFailure }.toSet(),61        )62    }6364    @Test fun `the warning outcomes are the network class plus a locked vault`() {65        assertEquals(66            setOf(67                SyncOutcomeReason.VAULT_LOCKED,68                SyncOutcomeReason.RELAY_UNREACHABLE,69                SyncOutcomeReason.HANDSHAKE_FAILED,70                SyncOutcomeReason.SYNC_TIMEOUT,71                SyncOutcomeReason.CONNECTION_LOST,72            ),73            SyncOutcomeReason.entries.filter { it.isWarning }.toSet(),74        )75    }7677    @Test fun `every failure is red and every warning is amber, and nothing else is either`() {78        for (reason in SyncOutcomeReason.entries) {79            val indicator = syncIndicator(SyncOutcomeSummary(reason = reason), pairedCount = 1)80            assertEquals(81                "$reason: isFailure and the red light must agree",82                reason.isFailure,83                indicator == SyncIndicator.ERROR,84            )85            assertEquals(86                "$reason: isWarning and the amber light must agree",87                reason.isWarning,88                indicator == SyncIndicator.WARN,89            )90        }91    }9293    @Test fun `a run in flight pulses whatever the last outcome was`() {94        for (reason in SyncOutcomeReason.entries + null) {95            assertEquals(96                SyncIndicator.BUSY,97                syncIndicator(SyncOutcomeSummary(reason = reason, syncing = true), pairedCount = 0),98            )99        }100    }101102    @Test fun `a successful pair with nothing attempted is green, and nothing paired is unlit`() {103        assertEquals(SyncIndicator.OK, syncIndicator(SyncOutcomeSummary(), pairedCount = 1))104        assertEquals(SyncIndicator.IDLE, syncIndicator(SyncOutcomeSummary(), pairedCount = 0))105        // Paired but switched off is not a problem and not health — unlit.106        assertEquals(107            SyncIndicator.IDLE,108            syncIndicator(SyncOutcomeSummary(reason = SyncOutcomeReason.SYNC_DISABLED), pairedCount = 1),109        )110    }111112    @Test fun `a sync that could not run reads differently from one that found nothing`() {113        val ranAndFoundNothing = describeSyncOutcome(114            SyncOutcomeSummary(reason = SyncOutcomeReason.COMPLETED_NO_CHANGES),115        )116        val couldNotRun = describeSyncOutcome(117            SyncOutcomeSummary(reason = SyncOutcomeReason.NO_ELIGIBLE_HOST, detail = "Pixel [type=android]"),118        )119        assertEquals("Up to date — nothing to send or receive", ranAndFoundNothing)120        assertEquals(121            "Paired, but no device here can accept a sync (Pixel [type=android])",122            couldNotRun,123        )124    }125126    /** Word for word the browser's `COPY` table — the two clients sit side by side. */127    @Test fun `copy matches the browser`() {128        fun copy(reason: SyncOutcomeReason) = describeSyncOutcome(SyncOutcomeSummary(reason = reason))129        assertEquals("Not synced yet", describeSyncOutcome(SyncOutcomeSummary()))130        assertEquals("Synced", copy(SyncOutcomeReason.COMPLETED_CHANGES))131        assertEquals("Up to date — nothing to send or receive", copy(SyncOutcomeReason.COMPLETED_NO_CHANGES))132        assertEquals("No devices paired yet", copy(SyncOutcomeReason.NO_PAIRED_DEVICES))133        assertEquals("Paired, but no device here can accept a sync", copy(SyncOutcomeReason.NO_ELIGIBLE_HOST))134        assertEquals("Sync is switched off", copy(SyncOutcomeReason.SYNC_DISABLED))135        assertEquals("Vault was locked — unlock and sync again", copy(SyncOutcomeReason.VAULT_LOCKED))136        // Android-only, so there is no browser line to match — see the enum.137        assertEquals(138            "Android blocked Peach from starting sync in the background — open Peach and sync again",139            copy(SyncOutcomeReason.FOREGROUND_START_BLOCKED),140        )141        assertEquals("Couldn't reach the relay", copy(SyncOutcomeReason.RELAY_UNREACHABLE))142        assertEquals("Reached the relay, but the device didn't answer", copy(SyncOutcomeReason.HANDSHAKE_FAILED))143        assertEquals("That device holds a different vault", copy(SyncOutcomeReason.VAULT_ID_MISMATCH))144        assertEquals(145            "Update Peach on both devices, then re-pair them",146            copy(SyncOutcomeReason.PEER_PROTOCOL_OUTDATED),147        )148        assertEquals("The device stopped responding mid-sync", copy(SyncOutcomeReason.SYNC_TIMEOUT))149        assertEquals("The connection dropped mid-sync — will retry", copy(SyncOutcomeReason.CONNECTION_LOST))150        assertEquals("Sync failed", copy(SyncOutcomeReason.RUN_FAILED))151    }152153    @Test fun `a completed sync says what moved`() {154        assertEquals(155            "Synced — 3 sent, 1 received",156            describeSyncOutcome(157                SyncOutcomeSummary(158                    reason = SyncOutcomeReason.COMPLETED_CHANGES,159                    entriesSent = 3,160                    entriesReceived = 1,161                ),162            ),163        )164    }165166    @Test fun `the label always carries the colour's claim in words`() {167        assertEquals(168            "Sync problem — Couldn't reach the relay",169            syncIndicatorLabel(170                SyncIndicator.ERROR,171                SyncOutcomeSummary(reason = SyncOutcomeReason.RELAY_UNREACHABLE),172            ),173        )174        assertEquals("Not syncing — Not synced yet", syncIndicatorLabel(SyncIndicator.IDLE, SyncOutcomeSummary()))175        assertTrue(syncIndicatorLabel(SyncIndicator.BUSY, SyncOutcomeSummary()).startsWith("Syncing"))176        assertTrue(syncIndicatorLabel(SyncIndicator.OK, SyncOutcomeSummary()).startsWith("Sync healthy"))177    }178179    @Test fun `opaque session errors are classified, not lumped into one failure`() {180        assertEquals(SyncOutcomeReason.VAULT_LOCKED, classifySyncError("VAULT_LOCKED"))181        assertEquals(SyncOutcomeReason.VAULT_ID_MISMATCH, classifySyncError("vault_id_mismatch"))182        assertEquals(SyncOutcomeReason.PEER_PROTOCOL_OUTDATED, classifySyncError("PEER_PROTOCOL_OUTDATED"))183        assertEquals(SyncOutcomeReason.SYNC_TIMEOUT, classifySyncError("session timed out after 30s"))184        assertEquals(185            SyncOutcomeReason.HANDSHAKE_FAILED,186            classifySyncError("peer identity does not match authenticated KK key"),187        )188        assertEquals(189            SyncOutcomeReason.HANDSHAKE_FAILED,190            classifySyncError("reconnect capability does not match relay room"),191        )192        assertEquals(SyncOutcomeReason.RELAY_UNREACHABLE, classifySyncError("relay_closed_1006"))193        assertEquals(SyncOutcomeReason.CONNECTION_LOST, classifySyncError("WebSocket closed"))194        assertEquals(SyncOutcomeReason.CONNECTION_LOST, classifySyncError("connection closed by peer"))195        assertEquals(SyncOutcomeReason.RUN_FAILED, classifySyncError("something nobody predicted"))196    }197198    @Test fun `a session outcome becomes the user-facing one`() {199        assertEquals(200            SyncOutcomeReason.COMPLETED_NO_CHANGES,201            SyncOutcome(SyncOutcomeStatus.COMPLETED).toSummary().reason,202        )203        assertEquals(204            SyncOutcomeReason.COMPLETED_CHANGES,205            SyncOutcome(SyncOutcomeStatus.COMPLETED, entriesReceived = 2).toSummary().reason,206        )207        // A deletion that crossed the wire is a change, not "nothing to do".208        assertEquals(209            SyncOutcomeReason.COMPLETED_CHANGES,210            SyncOutcome(SyncOutcomeStatus.COMPLETED, tombstonesSent = 1).toSummary().reason,211        )212        assertEquals(213            SyncOutcomeReason.VAULT_ID_MISMATCH,214            SyncOutcome(SyncOutcomeStatus.VAULT_MISMATCH).toSummary().reason,215        )216        assertEquals(217            SyncOutcomeReason.SYNC_TIMEOUT,218            SyncOutcome(SyncOutcomeStatus.TIMEOUT, error = "session timed out after 30s").toSummary().reason,219        )220        assertEquals(221            SyncOutcomeReason.RELAY_UNREACHABLE,222            SyncOutcome(SyncOutcomeStatus.ERRORED, error = "relay_receive_queue_full").toSummary().reason,223        )224        assertEquals(225            SyncOutcomeReason.PEER_PROTOCOL_OUTDATED,226            SyncOutcome(SyncOutcomeStatus.ERRORED, error = "PEER_PROTOCOL_OUTDATED").toSummary().reason,227        )228    }229230    @Test fun `the reason for a failure survives being killed`() {231        val failure = SyncOutcomeSummary(232            reason = SyncOutcomeReason.HANDSHAKE_FAILED,233            detail = "peer identity does not match authenticated KK key",234            entriesSent = 4,235            entriesReceived = 2,236            finishedAt = 1_700_000_000_000L,237        )238        assertEquals(failure, decodeSyncOutcome(encodeSyncOutcome(failure)))239    }240241    @Test fun `a process killed mid-sync does not come back claiming to be syncing`() {242        val inFlight = SyncOutcomeSummary(reason = SyncOutcomeReason.COMPLETED_CHANGES, syncing = true)243        val restored = decodeSyncOutcome(encodeSyncOutcome(inFlight))244        assertEquals(false, restored.syncing)245        assertEquals(SyncOutcomeReason.COMPLETED_CHANGES, restored.reason)246    }247248    @Test fun `nothing stored, or something unreadable, restores as nothing attempted`() {249        assertNull(decodeSyncOutcome(null).reason)250        assertNull(decodeSyncOutcome("{not json").reason)251        assertNull(decodeSyncOutcome("""{"reason":"A_REASON_FROM_THE_FUTURE"}""").reason)252    }253}