SyncOutcomeTest.kt

9.3 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    @Test fun `the failing outcomes are exactly these`() {39        assertEquals(40            setOf(41                SyncOutcomeReason.NO_ELIGIBLE_HOST,42                SyncOutcomeReason.PRIVATE_RELAY_UNSET,43                SyncOutcomeReason.VAULT_LOCKED,44                SyncOutcomeReason.RELAY_UNREACHABLE,45                SyncOutcomeReason.HANDSHAKE_FAILED,46                SyncOutcomeReason.VAULT_ID_MISMATCH,47                SyncOutcomeReason.SYNC_TIMEOUT,48                SyncOutcomeReason.RUN_FAILED,49            ),50            SyncOutcomeReason.entries.filter { it.isFailure }.toSet(),51        )52    }5354    @Test fun `every failure is red and nothing else is`() {55        for (reason in SyncOutcomeReason.entries) {56            val indicator = syncIndicator(SyncOutcomeSummary(reason = reason), pairedCount = 1)57            assertEquals(58                "$reason: isFailure and the red light must agree",59                reason.isFailure,60                indicator == SyncIndicator.ERROR,61            )62        }63    }6465    @Test fun `a run in flight pulses whatever the last outcome was`() {66        for (reason in SyncOutcomeReason.entries + null) {67            assertEquals(68                SyncIndicator.BUSY,69                syncIndicator(SyncOutcomeSummary(reason = reason, syncing = true), pairedCount = 0),70            )71        }72    }7374    @Test fun `a successful pair with nothing attempted is green, and nothing paired is unlit`() {75        assertEquals(SyncIndicator.OK, syncIndicator(SyncOutcomeSummary(), pairedCount = 1))76        assertEquals(SyncIndicator.IDLE, syncIndicator(SyncOutcomeSummary(), pairedCount = 0))77        // Paired but switched off is not a problem and not health — unlit.78        assertEquals(79            SyncIndicator.IDLE,80            syncIndicator(SyncOutcomeSummary(reason = SyncOutcomeReason.SYNC_DISABLED), pairedCount = 1),81        )82    }8384    @Test fun `a sync that could not run reads differently from one that found nothing`() {85        val ranAndFoundNothing = describeSyncOutcome(86            SyncOutcomeSummary(reason = SyncOutcomeReason.COMPLETED_NO_CHANGES),87        )88        val couldNotRun = describeSyncOutcome(89            SyncOutcomeSummary(reason = SyncOutcomeReason.NO_ELIGIBLE_HOST, detail = "Pixel [type=android]"),90        )91        assertEquals("Up to date — nothing to send or receive", ranAndFoundNothing)92        assertEquals(93            "Paired, but no device here can accept a sync (Pixel [type=android])",94            couldNotRun,95        )96    }9798    /** Word for word the browser's `COPY` table — the two clients sit side by side. */99    @Test fun `copy matches the browser`() {100        fun copy(reason: SyncOutcomeReason) = describeSyncOutcome(SyncOutcomeSummary(reason = reason))101        assertEquals("Not synced yet", describeSyncOutcome(SyncOutcomeSummary()))102        assertEquals("Synced", copy(SyncOutcomeReason.COMPLETED_CHANGES))103        assertEquals("Up to date — nothing to send or receive", copy(SyncOutcomeReason.COMPLETED_NO_CHANGES))104        assertEquals("No devices paired yet", copy(SyncOutcomeReason.NO_PAIRED_DEVICES))105        assertEquals("Paired, but no device here can accept a sync", copy(SyncOutcomeReason.NO_ELIGIBLE_HOST))106        assertEquals("Sync is switched off", copy(SyncOutcomeReason.SYNC_DISABLED))107        assertEquals("Vault was locked — unlock and sync again", copy(SyncOutcomeReason.VAULT_LOCKED))108        assertEquals("Couldn't reach the relay", copy(SyncOutcomeReason.RELAY_UNREACHABLE))109        assertEquals("Reached the relay, but the device didn't answer", copy(SyncOutcomeReason.HANDSHAKE_FAILED))110        assertEquals("That device holds a different vault", copy(SyncOutcomeReason.VAULT_ID_MISMATCH))111        assertEquals("The device stopped responding mid-sync", copy(SyncOutcomeReason.SYNC_TIMEOUT))112        assertEquals("Sync failed", copy(SyncOutcomeReason.RUN_FAILED))113    }114115    @Test fun `a completed sync says what moved`() {116        assertEquals(117            "Synced — 3 sent, 1 received",118            describeSyncOutcome(119                SyncOutcomeSummary(120                    reason = SyncOutcomeReason.COMPLETED_CHANGES,121                    entriesSent = 3,122                    entriesReceived = 1,123                ),124            ),125        )126    }127128    @Test fun `the label always carries the colour's claim in words`() {129        assertEquals(130            "Sync problem — Couldn't reach the relay",131            syncIndicatorLabel(132                SyncIndicator.ERROR,133                SyncOutcomeSummary(reason = SyncOutcomeReason.RELAY_UNREACHABLE),134            ),135        )136        assertEquals("Not syncing — Not synced yet", syncIndicatorLabel(SyncIndicator.IDLE, SyncOutcomeSummary()))137        assertTrue(syncIndicatorLabel(SyncIndicator.BUSY, SyncOutcomeSummary()).startsWith("Syncing"))138        assertTrue(syncIndicatorLabel(SyncIndicator.OK, SyncOutcomeSummary()).startsWith("Sync healthy"))139    }140141    @Test fun `opaque session errors are classified, not lumped into one failure`() {142        assertEquals(SyncOutcomeReason.VAULT_LOCKED, classifySyncError("VAULT_LOCKED"))143        assertEquals(SyncOutcomeReason.VAULT_ID_MISMATCH, classifySyncError("vault_id_mismatch"))144        assertEquals(SyncOutcomeReason.SYNC_TIMEOUT, classifySyncError("session timed out after 30s"))145        assertEquals(146            SyncOutcomeReason.HANDSHAKE_FAILED,147            classifySyncError("peer identity does not match authenticated KK key"),148        )149        assertEquals(150            SyncOutcomeReason.HANDSHAKE_FAILED,151            classifySyncError("reconnect capability does not match relay room"),152        )153        assertEquals(SyncOutcomeReason.RELAY_UNREACHABLE, classifySyncError("relay_closed_1006"))154        assertEquals(SyncOutcomeReason.RUN_FAILED, classifySyncError("something nobody predicted"))155    }156157    @Test fun `a session outcome becomes the user-facing one`() {158        assertEquals(159            SyncOutcomeReason.COMPLETED_NO_CHANGES,160            SyncOutcome(SyncOutcomeStatus.COMPLETED).toSummary().reason,161        )162        assertEquals(163            SyncOutcomeReason.COMPLETED_CHANGES,164            SyncOutcome(SyncOutcomeStatus.COMPLETED, entriesReceived = 2).toSummary().reason,165        )166        // A deletion that crossed the wire is a change, not "nothing to do".167        assertEquals(168            SyncOutcomeReason.COMPLETED_CHANGES,169            SyncOutcome(SyncOutcomeStatus.COMPLETED, tombstonesSent = 1).toSummary().reason,170        )171        assertEquals(172            SyncOutcomeReason.VAULT_ID_MISMATCH,173            SyncOutcome(SyncOutcomeStatus.VAULT_MISMATCH).toSummary().reason,174        )175        assertEquals(176            SyncOutcomeReason.SYNC_TIMEOUT,177            SyncOutcome(SyncOutcomeStatus.TIMEOUT, error = "session timed out after 30s").toSummary().reason,178        )179        assertEquals(180            SyncOutcomeReason.RELAY_UNREACHABLE,181            SyncOutcome(SyncOutcomeStatus.ERRORED, error = "relay_receive_queue_full").toSummary().reason,182        )183    }184185    @Test fun `the reason for a failure survives being killed`() {186        val failure = SyncOutcomeSummary(187            reason = SyncOutcomeReason.HANDSHAKE_FAILED,188            detail = "peer identity does not match authenticated KK key",189            entriesSent = 4,190            entriesReceived = 2,191            finishedAt = 1_700_000_000_000L,192        )193        assertEquals(failure, decodeSyncOutcome(encodeSyncOutcome(failure)))194    }195196    @Test fun `a process killed mid-sync does not come back claiming to be syncing`() {197        val inFlight = SyncOutcomeSummary(reason = SyncOutcomeReason.COMPLETED_CHANGES, syncing = true)198        val restored = decodeSyncOutcome(encodeSyncOutcome(inFlight))199        assertEquals(false, restored.syncing)200        assertEquals(SyncOutcomeReason.COMPLETED_CHANGES, restored.reason)201    }202203    @Test fun `nothing stored, or something unreadable, restores as nothing attempted`() {204        assertNull(decodeSyncOutcome(null).reason)205        assertNull(decodeSyncOutcome("{not json").reason)206        assertNull(decodeSyncOutcome("""{"reason":"A_REASON_FROM_THE_FUTURE"}""").reason)207    }208}