4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 1 | import assert from 'node:assert/strict'; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 2 | import { test } from 'node:test'; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 3 | import { isDismissed, recoveryPrompt, requestPersistence, setDismissed } from '../src/lib/protection-model.ts'; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 4 | |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 5 | const state = (recovery: 'Unknown' | 'Disabled' | 'Incomplete' | 'Enabled') => ({ |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 6 | device_id: 'D', |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 7 | device_verified: true, |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 8 | recovery, |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 9 | key_backup: 'Disabled' |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 10 | }); |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 11 | |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 12 | const browser = { device: false, standalone: false }; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 13 | const device = { device: true, standalone: false }; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 14 | const installed = { device: true, standalone: true }; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 15 | |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 16 | test('recovery prompt follows recovery state', () => { |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 17 | assert.equal(recoveryPrompt(null, device, false), null); |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 18 | assert.equal(recoveryPrompt(state('Unknown'), device, false), null); |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 19 | assert.equal(recoveryPrompt(state('Enabled'), device, false), null); |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 20 | assert.equal(recoveryPrompt(state('Incomplete'), device, true), 'recover'); |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 21 | assert.equal(recoveryPrompt(state('Disabled'), device, false), 'setup'); |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 22 | }); |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 23 | |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 24 | test('only the device build in a browser tab may dismiss the setup prompt', () => { |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 25 | assert.equal(recoveryPrompt(state('Disabled'), device, true), null); |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 26 | assert.equal(recoveryPrompt(state('Disabled'), installed, true), 'setup'); |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 27 | assert.equal(recoveryPrompt(state('Disabled'), browser, true), 'setup'); |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 28 | }); |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 29 | |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 30 | test('dismissals are per account', () => { |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 31 | const data = new Map<string, string>(); |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 32 | const store = { getItem: (k: string) => data.get(k) ?? null, setItem: (k: string, v: string) => void data.set(k, v) }; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 33 | setDismissed(store, 'storage', '@a:x'); |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 34 | assert.equal(isDismissed(store, 'storage', '@a:x'), true); |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 35 | assert.equal(isDismissed(store, 'storage', '@b:x'), false); |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 36 | assert.equal(isDismissed(store, 'recovery', '@a:x'), false); |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 37 | }); |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 38 | |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 39 | test('persistence is requested only when not already granted', async () => { |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 40 | let asked = 0; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 41 | const denied = { |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 42 | persisted: async () => false, |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 43 | persist: async () => (asked++, false), |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 44 | estimate: async () => ({ usage: 5, quota: 100 }) |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 45 | }; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 46 | assert.deepEqual(await requestPersistence(denied), { persisted: false, usage: 5, quota: 100 }); |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 47 | assert.equal(asked, 1); |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 48 | const granted = { persisted: async () => true, persist: async () => (asked++, true) }; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 49 | assert.deepEqual(await requestPersistence(granted), { persisted: true, usage: null, quota: null }); |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 50 | assert.equal(asked, 1); |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 51 | assert.equal(await requestPersistence(undefined), null); |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 52 | }); |