atagen

malice

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