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