@tallyui/core 3.20.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/dist/chunk-XF3RD6GX.js +515 -0
  2. package/dist/index.d.ts +41 -47
  3. package/dist/index.js +119 -101
  4. package/dist/server/index.cjs +1923 -0
  5. package/dist/server/index.d.cts +1039 -0
  6. package/dist/server/index.d.ts +46 -48
  7. package/dist/server/index.js +516 -206
  8. package/dist/{command-result-E_nH57WD.d.ts → tax-rate-DCJhfJbZ.d.ts} +442 -26
  9. package/package.json +10 -5
  10. package/src/commands.ts +12 -0
  11. package/src/context/money-format-context.tsx +50 -0
  12. package/src/coupons/woocommerce/to-coupon-configs.ts +5 -8
  13. package/src/fnv1a32.ts +14 -0
  14. package/src/index.ts +18 -5
  15. package/src/known-warnings.ts +23 -0
  16. package/src/money.ts +85 -2
  17. package/src/server/batch.ts +63 -27
  18. package/src/server/command-result.ts +28 -1
  19. package/src/server/display-signalling/contract.ts +203 -78
  20. package/src/server/display-signalling/memory-server.ts +71 -23
  21. package/src/server/fiscal-figures.ts +1 -1
  22. package/src/server/index.ts +5 -1
  23. package/src/server/money.ts +4 -1
  24. package/src/server/order-payload-shape.ts +96 -5
  25. package/src/server/refund-payload-shape.ts +5 -11
  26. package/src/server/register-payload-shape.ts +6 -12
  27. package/src/server/rejection-codes.ts +4 -4
  28. package/src/server/reopen-payload-shape.ts +25 -0
  29. package/src/server/shape-errors.ts +17 -0
  30. package/src/server/tax-rate.ts +13 -0
  31. package/src/server/update-payload-shape.ts +51 -0
  32. package/src/server/versions.ts +9 -2
  33. package/src/types/closures.ts +34 -0
  34. package/src/types/commands.ts +93 -8
  35. package/src/types/connector.ts +37 -4
  36. package/src/types/display-signalling.ts +30 -14
  37. package/src/types/index.ts +6 -3
  38. package/src/types/orders.ts +25 -3
  39. package/src/types/payment-methods.ts +17 -0
  40. package/src/types/stock.ts +2 -0
  41. package/src/types/store-settings.ts +7 -0
  42. package/src/types/traits/product.ts +19 -0
  43. package/dist/chunk-27L7XLTT.js +0 -235
@@ -1,5 +1,5 @@
1
1
  import type { SyncContext } from '../../types/connector';
2
- import type { DisplaySignalling, DisplaySignallingContext, DisplaySignallingErrorCode } from '../../types/display-signalling';
2
+ import type { DisplaySignalling, DisplaySignallingContext, DisplaySignallingErrorCode, TillDevice } from '../../types/display-signalling';
3
3
  import { sha256Hex, type DisplaySignallingHarness } from './memory-server';
4
4
 
5
5
  const MINUTE = 60_000, DAY = 24 * 60 * MINUTE;
@@ -35,33 +35,71 @@ function unusedCode(...used: string[]): string {
35
35
  }
36
36
  }
37
37
 
38
- interface World { h: DisplaySignallingHarness; s: DisplaySignalling; a: SyncContext; b: SyncContext; viewer: SyncContext }
38
+ const KEY_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
39
+ /** A random 22-character base64url key, which no device holds. */
40
+ const randomKey = (): string => Array.from(globalThis.crypto.getRandomValues(new Uint8Array(22)), (n) => KEY_ALPHABET[n & 63]).join('');
41
+
42
+ interface World { h: DisplaySignallingHarness; s: DisplaySignalling; a: SyncContext; b: SyncContext; viewer: SyncContext; da: TillDevice; db: TillDevice }
39
43
 
40
44
  async function world(make: () => Promise<DisplaySignallingHarness>): Promise<World> {
41
45
  const h = await make();
42
46
  const till = (cashierId: string, canPair = true) => h.till({ cashierId, canPair });
43
- return { h, s: h.signalling, a: await till('cashier-a'), b: await till('cashier-b'), viewer: await till('cashier-viewer', false) };
47
+ const [a, b, viewer] = [await till('cashier-a'), await till('cashier-b'), await till('cashier-viewer', false)];
48
+ const da = { deviceId: A, deviceKey: (await h.signalling.registerDevice(a, A)).deviceKey };
49
+ const db = { deviceId: B, deviceKey: (await h.signalling.registerDevice(b, B)).deviceKey };
50
+ return { h, s: h.signalling, a, b, viewer, da, db };
44
51
  }
45
52
 
46
- /** Claims a fresh code from `deviceId`, giving the claim and its pending pairing's id. */
47
- async function claimed(w: World, ctx: SyncContext, deviceId: string, d: DisplaySignallingContext, name: string, token?: string) {
48
- const { code } = await w.s.mint(ctx, deviceId);
53
+ /** Claims a fresh code from `device`, giving the claim and its pending pairing's id. */
54
+ async function claimed(w: World, ctx: SyncContext, device: TillDevice, d: DisplaySignallingContext, name: string, token?: string) {
55
+ const { code } = await w.s.mint(ctx, device);
49
56
  const claim = await w.s.claim(d, { code, displayName: name, ...(token ? { token } : {}) });
50
- const entry = (await w.s.pending(ctx, deviceId)).find((p) => p.checkCode === claim.checkCode);
57
+ const entry = (await w.s.pending(ctx, device)).find((p) => p.checkCode === claim.checkCode);
51
58
  check(entry && entry.displayName === name, `the pending listing lacks the claim by ${name}`);
52
59
  return { code, claim, claimId: entry.claimId };
53
60
  }
54
61
 
55
- /** Pairs a display with `deviceId` end to end: mint, claim, confirm, collect. */
56
- async function paired(w: World, ctx: SyncContext, deviceId: string, source: string, name: string, token?: string) {
62
+ /** Pairs a display with `device` end to end: mint, claim, confirm, collect. */
63
+ async function paired(w: World, ctx: SyncContext, device: TillDevice, source: string, name: string, token?: string) {
57
64
  const d = w.h.display(source);
58
- const { claim, claimId } = await claimed(w, ctx, deviceId, d, name, token);
59
- await w.s.confirm(ctx, deviceId, claimId);
65
+ const { claim, claimId } = await claimed(w, ctx, device, d, name, token);
66
+ await w.s.confirm(ctx, device, claimId);
60
67
  const result = await w.s.collect(d, claim.claimSecret);
61
68
  check(result.status === 'paired', `collect after the confirm gave ${result.status}`);
62
69
  return { d, claim, displayId: result.displayId, token: result.token };
63
70
  }
64
71
 
72
+ /** A's display with a snapshot and a signal waiting for the till, and a pending claim: something for every till call to touch. */
73
+ async function armed(w: World) {
74
+ const shown = await paired(w, w.a, w.da, '192.0.2.1', 'Counter');
75
+ await w.s.postSnapshot(w.a, w.da, shown.displayId, { v: 1 });
76
+ await w.s.displaySendSignal(shown.d, shown.token, { type: 'answer', payload: 'a1' });
77
+ const { claimId } = await claimed(w, w.a, w.da, w.h.display('192.0.2.2'), 'Waiting');
78
+ return { ...shown, claimId };
79
+ }
80
+
81
+ /** Each of the nine till-side calls, made by cashier A with `device`. */
82
+ const tillCalls = (w: World, device: TillDevice, displayId: string, claimId: string): Array<[string, () => Promise<unknown>]> => [
83
+ ['mint', () => w.s.mint(w.a, device)], ['pending', () => w.s.pending(w.a, device)],
84
+ ['confirm', () => w.s.confirm(w.a, device, claimId)], ['decline', () => w.s.decline(w.a, device, claimId)],
85
+ ['list', () => w.s.list(w.a, device)], ['forget', () => w.s.forget(w.a, device, displayId)],
86
+ ['sendSignal', () => w.s.sendSignal(w.a, device, displayId, { type: 'offer', payload: 'o2' })],
87
+ ['receiveSignals', () => w.s.receiveSignals(w.a, device)], ['postSnapshot', () => w.s.postSnapshot(w.a, device, displayId, { v: 2 })],
88
+ ];
89
+
90
+ /** The four devices no till call accepts: no key, a random key, B's key on A's id, and A's key on an id never registered. */
91
+ const refusedDevices = (w: World): Array<[string, TillDevice]> => [
92
+ ['a missing key', { deviceId: A, deviceKey: '' }], ['a wrong key', { deviceId: A, deviceKey: randomKey() }],
93
+ ["B's key on A's id", { deviceId: A, deviceKey: w.db.deviceKey }], ['an unregistered id', { deviceId: 'till-never', deviceKey: w.da.deviceKey }],
94
+ ];
95
+
96
+ /** Seeds a pre-migration till and lets cashier A's register adopt it. */
97
+ async function adopted(w: World) {
98
+ const legacy = await w.h.seedLegacyDevice('till-legacy');
99
+ const device = { deviceId: 'till-legacy', deviceKey: (await w.s.registerDevice(w.a, 'till-legacy')).deviceKey };
100
+ return { ...legacy, device };
101
+ }
102
+
65
103
  /**
66
104
  * The security floor of ADR-085 d4–d6 and d8, and #705, as runner-agnostic cases: each builds a fresh harness and
67
105
  * throws on failure. Each plugin's connector runs it against its own routes:
@@ -69,28 +107,28 @@ async function paired(w: World, ctx: SyncContext, deviceId: string, source: stri
69
107
  */
70
108
  export function displaySignallingContract(makeHarness: () => Promise<DisplaySignallingHarness>): Array<{ name: string; run: () => Promise<void> }> {
71
109
  const cases: Record<string, (w: World) => Promise<void>> = {
72
- 'mint without the capability is forbidden and makes no code': async ({ h, s, viewer }) => {
73
- await expectRefusal(s.mint(viewer, A), 'forbidden', 'mint without the capability');
110
+ 'mint without the capability is forbidden and makes no code': async ({ h, s, viewer, da }) => {
111
+ await expectRefusal(s.mint(viewer, da), 'forbidden', 'mint without the capability');
74
112
  check((await h.storedCounts()).codes === 0, 'a refused mint stored a code');
75
113
  },
76
- 'a minted code is at least 6 digits and expires within 10 minutes': async ({ h, s, a }) => {
77
- const { code, expiresAt } = await s.mint(a, A);
114
+ 'a minted code is at least 6 digits and expires within 10 minutes': async ({ h, s, a, da }) => {
115
+ const { code, expiresAt } = await s.mint(a, da);
78
116
  check(/^\d{6,}$/.test(code), `code ${code} is not 6 or more digits`);
79
117
  check(!Number.isNaN(Date.parse(expiresAt)), `expiresAt ${expiresAt} is not a date`);
80
118
  await h.advance(10 * MINUTE);
81
119
  await expectRefusal(s.claim(h.display('192.0.2.1'), { code, displayName: 'Late' }), 'claim_refused', 'a claim 10 minutes after the mint');
82
120
  },
83
- 'a device holds at most 5 unspent codes, and expired codes stop counting': async ({ h, s, a }) => {
84
- for (let i = 0; i < 5; i += 1) await s.mint(a, A);
85
- await expectRefusal(s.mint(a, A), 'limit', 'the 6th unspent code');
121
+ 'a device holds at most 5 unspent codes, and expired codes stop counting': async ({ h, s, a, da }) => {
122
+ for (let i = 0; i < 5; i += 1) await s.mint(a, da);
123
+ await expectRefusal(s.mint(a, da), 'limit', 'the 6th unspent code');
86
124
  await h.advance(10 * MINUTE);
87
- await s.mint(a, A);
125
+ await s.mint(a, da);
88
126
  },
89
- 'spent, expired and unknown codes get the same claim refusal': async ({ h, s, a }) => {
90
- const spent = (await s.mint(a, A)).code;
127
+ 'spent, expired and unknown codes get the same claim refusal': async ({ h, s, a, da }) => {
128
+ const spent = (await s.mint(a, da)).code;
91
129
  await s.claim(h.display('192.0.2.1'), { code: spent, displayName: 'First' });
92
130
  const again = await expectRefusal(s.claim(h.display('192.0.2.2'), { code: spent, displayName: 'Second' }), 'claim_refused', 'a spent code');
93
- const expired = (await s.mint(a, A)).code;
131
+ const expired = (await s.mint(a, da)).code;
94
132
  await h.advance(10 * MINUTE);
95
133
  const late = await expectRefusal(s.claim(h.display('192.0.2.3'), { code: expired, displayName: 'Late' }), 'claim_refused', 'an expired code');
96
134
  const unknown = await expectRefusal(s.claim(h.display('192.0.2.4'), { code: unusedCode(spent, expired), displayName: 'Guess' }), 'claim_refused', 'an unknown code');
@@ -99,23 +137,23 @@ export function displaySignallingContract(makeHarness: () => Promise<DisplaySign
99
137
  },
100
138
  'a claim issues no token: collect gives pending with its check code': async (w) => {
101
139
  const d = w.h.display('192.0.2.1');
102
- const { claim } = await claimed(w, w.a, A, d, 'Counter');
140
+ const { claim } = await claimed(w, w.a, w.da, d, 'Counter');
103
141
  const result = await w.s.collect(d, claim.claimSecret);
104
142
  check(result.status === 'pending' && result.checkCode === claim.checkCode, `collect before the confirm gave ${JSON.stringify(result)}`);
105
143
  },
106
144
  'the check code matches on both screens, and two live claims never share one': async (w) => {
107
145
  const d = w.h.display('192.0.2.1');
108
- const first = await claimed(w, w.a, A, d, 'One');
146
+ const first = await claimed(w, w.a, w.da, d, 'One');
109
147
  check(/^\d{4}$/.test(first.claim.checkCode), `check code ${first.claim.checkCode} is not 4 digits`);
110
- const listed = (await w.s.pending(w.a, A)).find((p) => p.claimId === first.claimId);
148
+ const listed = (await w.s.pending(w.a, w.da)).find((p) => p.claimId === first.claimId);
111
149
  const collected = await w.s.collect(d, first.claim.claimSecret);
112
150
  check(listed?.checkCode === first.claim.checkCode, 'the till lists another check code');
113
151
  check(collected.status === 'pending' && collected.checkCode === first.claim.checkCode, 'collect gives another check code');
114
- const second = await claimed(w, w.a, A, w.h.display('192.0.2.2'), 'Two');
152
+ const second = await claimed(w, w.a, w.da, w.h.display('192.0.2.2'), 'Two');
115
153
  check(second.claim.checkCode !== first.claim.checkCode, 'two live claims share a check code');
116
154
  },
117
155
  'a confirm issues the token once, then collect is refused like an unknown secret': async (w) => {
118
- const { d, claim, token } = await paired(w, w.a, A, '192.0.2.1', 'Counter');
156
+ const { d, claim, token } = await paired(w, w.a, w.da, '192.0.2.1', 'Counter');
119
157
  check(SECRET.test(token), 'the token is not 22+ base64url characters');
120
158
  const again = await expectRefusal(w.s.collect(d, claim.claimSecret), 'collect_refused', 'a second collect');
121
159
  const unknown = await expectRefusal(w.s.collect(d, 'AAAAAAAAAAAAAAAAAAAAAA'), 'collect_refused', 'an unknown secret');
@@ -123,32 +161,32 @@ export function displaySignallingContract(makeHarness: () => Promise<DisplaySign
123
161
  },
124
162
  'a decline, or an unconfirmed claim whose code expires, issues nothing': async (w) => {
125
163
  const d = w.h.display('192.0.2.1');
126
- const declined = await claimed(w, w.a, A, d, 'Declined');
127
- await w.s.decline(w.a, A, declined.claimId);
164
+ const declined = await claimed(w, w.a, w.da, d, 'Declined');
165
+ await w.s.decline(w.a, w.da, declined.claimId);
128
166
  await expectRefusal(w.s.collect(d, declined.claim.claimSecret), 'collect_refused', 'collect after a decline');
129
- const lapsed = await claimed(w, w.a, A, d, 'Lapsed');
167
+ const lapsed = await claimed(w, w.a, w.da, d, 'Lapsed');
130
168
  await w.h.advance(10 * MINUTE);
131
169
  await expectRefusal(w.s.collect(d, lapsed.claim.claimSecret), 'collect_refused', 'collect after the code expired');
132
170
  check((await w.h.storedCounts()).pending === 0, 'a pending pairing outlived its decline or its code');
133
171
  },
134
172
  'a confirm needs the capability and the minting device': async (w) => {
135
173
  const d = w.h.display('192.0.2.1');
136
- const { claim, claimId } = await claimed(w, w.a, A, d, 'Counter');
137
- await expectRefusal(w.s.confirm(w.viewer, A, claimId), 'forbidden', 'a confirm without the capability');
138
- await expectRefusal(w.s.confirm(w.b, B, claimId), 'not_found', 'a confirm from another device');
174
+ const { claim, claimId } = await claimed(w, w.a, w.da, d, 'Counter');
175
+ await expectRefusal(w.s.confirm(w.viewer, w.da, claimId), 'forbidden', 'a confirm without the capability');
176
+ await expectRefusal(w.s.confirm(w.b, w.db, claimId), 'not_found', 'a confirm from another device');
139
177
  check((await w.s.collect(d, claim.claimSecret)).status === 'pending', 'a refused confirm changed the claim');
140
178
  },
141
- 'one source is limited to 10 failed claims a minute, and a limited claim spends nothing': async ({ h, s, a }) => {
142
- const { code } = await s.mint(a, A);
179
+ 'one source is limited to 10 failed claims a minute, and a limited claim spends nothing': async ({ h, s, a, da }) => {
180
+ const { code } = await s.mint(a, da);
143
181
  const guesser = h.display('198.51.100.7');
144
182
  for (let i = 0; i < 10; i += 1) await expectRefusal(s.claim(guesser, { code: unusedCode(code), displayName: 'Guess' }), 'claim_refused', 'a wrong code');
145
183
  await expectRefusal(s.claim(guesser, { code, displayName: 'Guess' }), 'claim_refused', 'a valid code from a limited source');
146
184
  await s.claim(h.display('198.51.100.8'), { code, displayName: 'Other' });
147
185
  await h.advance(MINUTE);
148
- await s.claim(guesser, { code: (await s.mint(a, A)).code, displayName: 'Later' });
186
+ await s.claim(guesser, { code: (await s.mint(a, da)).code, displayName: 'Later' });
149
187
  },
150
- 'two IPv6 addresses in one /64 share a limit': async ({ h, s, a }) => {
151
- const { code } = await s.mint(a, A);
188
+ 'two IPv6 addresses in one /64 share a limit': async ({ h, s, a, da }) => {
189
+ const { code } = await s.mint(a, da);
152
190
  const one = h.display('2001:db8:0:1::5');
153
191
  const two = h.display('2001:db8:0:1:ffff::9');
154
192
  for (let i = 0; i < 10; i += 1) await expectRefusal(s.claim(i % 2 ? two : one, { code: unusedCode(code), displayName: 'Guess' }), 'claim_refused', 'a wrong code');
@@ -156,64 +194,64 @@ export function displaySignallingContract(makeHarness: () => Promise<DisplaySign
156
194
  await expectRefusal(s.claim(two, { code, displayName: 'Guess' }), 'claim_refused', 'a valid code from a limited /64');
157
195
  await s.claim(h.display('2001:db8:0:2::5'), { code, displayName: 'Other' });
158
196
  },
159
- '100 failed claims store-wide pause every claim for 10 minutes': async ({ h, s, a }) => {
160
- const { code } = await s.mint(a, A);
197
+ '100 failed claims store-wide pause every claim for 10 minutes': async ({ h, s, a, da }) => {
198
+ const { code } = await s.mint(a, da);
161
199
  // 9 failures per address, so no source reaches its own limit of 10.
162
200
  for (let i = 0; i < 100; i += 1) await expectRefusal(s.claim(h.display(`203.0.113.${Math.floor(i / 9) + 1}`), { code: unusedCode(code), displayName: 'Guess' }), 'claim_refused', 'a wrong code');
163
201
  await expectRefusal(s.claim(h.display('203.0.113.200'), { code, displayName: 'Shop' }), 'claim_refused', 'a valid code while paused');
164
202
  await h.advance(10 * MINUTE);
165
- await s.claim(h.display('203.0.113.201'), { code: (await s.mint(a, A)).code, displayName: 'Shop' });
203
+ await s.claim(h.display('203.0.113.201'), { code: (await s.mint(a, da)).code, displayName: 'Shop' });
166
204
  },
167
205
  'the claim secret and the token are stored only as hashes': async (w) => {
168
- const { claim, token } = await paired(w, w.a, A, '192.0.2.1', 'Counter');
206
+ const { claim, token } = await paired(w, w.a, w.da, '192.0.2.1', 'Counter');
169
207
  const text = await w.h.storedText();
170
208
  check(SECRET.test(claim.claimSecret), 'the claim secret is not 22+ base64url characters');
171
209
  check(!text.includes(claim.claimSecret) && !text.includes(token), 'the claim secret or the token is stored');
172
210
  check(text.includes(await sha256Hex(token)), "the token's SHA-256 hex is not stored");
173
211
  },
174
212
  'the registry lists a display on its owning device only, and tracks connected': async (w) => {
175
- const { d, displayId, token } = await paired(w, w.a, A, '192.0.2.1', 'Counter');
176
- const row = (await w.s.list(w.a, A)).find((r) => r.displayId === displayId);
213
+ const { d, displayId, token } = await paired(w, w.a, w.da, '192.0.2.1', 'Counter');
214
+ const row = (await w.s.list(w.a, w.da)).find((r) => r.displayId === displayId);
177
215
  check(row?.name === 'Counter' && row.cashierId === 'cashier-a', `the owner lists ${JSON.stringify(row)}`);
178
- check(!(await w.s.list(w.b, B)).some((r) => r.displayId === displayId), 'another device lists the display');
216
+ check(!(await w.s.list(w.b, w.db)).some((r) => r.displayId === displayId), 'another device lists the display');
179
217
  await w.s.displayReceiveSignals(d, token);
180
- check((await w.s.list(w.a, A))[0]?.connected === true, 'a display just seen is not connected');
218
+ check((await w.s.list(w.a, w.da))[0]?.connected === true, 'a display just seen is not connected');
181
219
  await w.h.advance(31_000);
182
- check((await w.s.list(w.a, A))[0]?.connected === false, 'a display unseen for 31 s is connected');
220
+ check((await w.s.list(w.a, w.da))[0]?.connected === false, 'a display unseen for 31 s is connected');
183
221
  },
184
222
  'forget revokes the token, the row and the snapshot, for the owner only': async (w) => {
185
- const { d, displayId, token } = await paired(w, w.a, A, '192.0.2.1', 'Counter');
186
- await w.s.postSnapshot(w.a, A, displayId, { v: 1 });
223
+ const { d, displayId, token } = await paired(w, w.a, w.da, '192.0.2.1', 'Counter');
224
+ await w.s.postSnapshot(w.a, w.da, displayId, { v: 1 });
187
225
  await w.s.displaySendSignal(d, token, { type: 'answer', payload: 'a1' });
188
- await expectRefusal(w.s.forget(w.b, B, displayId), 'not_found', 'forget from another device');
189
- check((await w.s.list(w.a, A)).length === 1, 'a refused forget removed the display');
190
- await w.s.forget(w.a, A, displayId);
226
+ await expectRefusal(w.s.forget(w.b, w.db, displayId), 'not_found', 'forget from another device');
227
+ check((await w.s.list(w.a, w.da)).length === 1, 'a refused forget removed the display');
228
+ await w.s.forget(w.a, w.da, displayId);
191
229
  await expectRefusal(w.s.displayReceiveSignals(d, token), 'token_refused', 'a forgotten token');
192
- check((await w.s.list(w.a, A)).length === 0, 'the owner still lists a forgotten display');
193
- check((await w.s.receiveSignals(w.a, A)).every((m) => m.displayId !== displayId), 'the till received from a forgotten display');
230
+ check((await w.s.list(w.a, w.da)).length === 0, 'the owner still lists a forgotten display');
231
+ check((await w.s.receiveSignals(w.a, w.da)).every((m) => m.displayId !== displayId), 'the till received from a forgotten display');
194
232
  const counts = await w.h.storedCounts();
195
233
  check(counts.displays === 0 && counts.snapshots === 0, `forget left ${JSON.stringify(counts)}`);
196
234
  },
197
235
  'a display confirmed by a second till moves to it': async (w) => {
198
- const first = await paired(w, w.a, A, '192.0.2.1', 'Counter');
199
- await w.s.postSnapshot(w.a, A, first.displayId, { v: 1 });
200
- await w.s.sendSignal(w.a, A, first.displayId, { type: 'offer', payload: 'o1' });
236
+ const first = await paired(w, w.a, w.da, '192.0.2.1', 'Counter');
237
+ await w.s.postSnapshot(w.a, w.da, first.displayId, { v: 1 });
238
+ await w.s.sendSignal(w.a, w.da, first.displayId, { type: 'offer', payload: 'o1' });
201
239
  await w.s.displaySendSignal(first.d, first.token, { type: 'answer', payload: 'a1' });
202
- const moved = await paired(w, w.b, B, '192.0.2.1', 'Counter', first.token);
240
+ const moved = await paired(w, w.b, w.db, '192.0.2.1', 'Counter', first.token);
203
241
  check((await w.s.readSnapshot(moved.d, moved.token)) === null, "the first till's snapshot survived the move");
204
242
  check((await w.s.displayReceiveSignals(moved.d, moved.token)).length === 0, "the first till's signal survived the move");
205
243
  check((await w.h.storedCounts()).snapshots === 0, 'a snapshot is stored after the move');
206
- check((await w.s.receiveSignals(w.a, A)).every((m) => m.displayId !== first.displayId), 'the first till received from a moved display');
207
- check((await w.s.list(w.a, A)).length === 0, 'the first till still lists a moved display');
208
- check((await w.s.list(w.b, B)).some((r) => r.displayId === first.displayId) && moved.displayId === first.displayId, 'the moved display has a new id');
244
+ check((await w.s.receiveSignals(w.a, w.da)).every((m) => m.displayId !== first.displayId), 'the first till received from a moved display');
245
+ check((await w.s.list(w.a, w.da)).length === 0, 'the first till still lists a moved display');
246
+ check((await w.s.list(w.b, w.db)).some((r) => r.displayId === first.displayId) && moved.displayId === first.displayId, 'the moved display has a new id');
209
247
  await expectRefusal(w.s.readSnapshot(first.d, first.token), 'token_refused', 'the old token after a move');
210
248
  },
211
249
  'signals arrive once and in order, and only at their own display or till': async (w) => {
212
- const x = await paired(w, w.a, A, '192.0.2.1', 'X');
213
- const y = await paired(w, w.a, A, '192.0.2.2', 'Y');
214
- const z = await paired(w, w.b, B, '192.0.2.3', 'Z');
215
- await w.s.sendSignal(w.a, A, x.displayId, { type: 'offer', payload: 'o1' });
216
- await w.s.sendSignal(w.a, A, x.displayId, { type: 'ice', payload: 'i1' });
250
+ const x = await paired(w, w.a, w.da, '192.0.2.1', 'X');
251
+ const y = await paired(w, w.a, w.da, '192.0.2.2', 'Y');
252
+ const z = await paired(w, w.b, w.db, '192.0.2.3', 'Z');
253
+ await w.s.sendSignal(w.a, w.da, x.displayId, { type: 'offer', payload: 'o1' });
254
+ await w.s.sendSignal(w.a, w.da, x.displayId, { type: 'ice', payload: 'i1' });
217
255
  const atX = await w.s.displayReceiveSignals(x.d, x.token);
218
256
  check(atX.map((m) => m.payload).join() === 'o1,i1', `display X received ${JSON.stringify(atX)}`);
219
257
  check((await w.s.displayReceiveSignals(x.d, x.token)).length === 0, 'display X received a message twice');
@@ -221,34 +259,121 @@ export function displaySignallingContract(makeHarness: () => Promise<DisplaySign
221
259
  await w.s.displaySendSignal(x.d, x.token, { type: 'answer', payload: 'a1' });
222
260
  await w.s.displaySendSignal(x.d, x.token, { type: 'ice', payload: 'i2' });
223
261
  await w.s.displaySendSignal(z.d, z.token, { type: 'answer', payload: 'z1' });
224
- check((await w.s.receiveSignals(w.b, B)).every((m) => m.displayId === z.displayId), "device B received device A's display");
225
- const atA = await w.s.receiveSignals(w.a, A);
262
+ check((await w.s.receiveSignals(w.b, w.db)).every((m) => m.displayId === z.displayId), "device B received device A's display");
263
+ const atA = await w.s.receiveSignals(w.a, w.da);
226
264
  check(atA.map((m) => `${m.displayId === x.displayId}:${m.payload}`).join() === 'true:a1,true:i2', `till A received ${JSON.stringify(atA)}`);
227
- check((await w.s.receiveSignals(w.a, A)).length === 0, 'till A received a message twice');
265
+ check((await w.s.receiveSignals(w.a, w.da)).length === 0, 'till A received a message twice');
228
266
  },
229
267
  'the snapshot needs the token, keeps the latest body for 60 s, and refuses over 200 KiB': async (w) => {
230
- const { d, displayId, token } = await paired(w, w.a, A, '192.0.2.1', 'Counter');
268
+ const { d, displayId, token } = await paired(w, w.a, w.da, '192.0.2.1', 'Counter');
231
269
  await expectRefusal(w.s.readSnapshot(d, 'not-a-token-not-a-token'), 'token_refused', 'a read without the token');
232
- await w.s.postSnapshot(w.a, A, displayId, { seq: 1 });
233
- await w.s.postSnapshot(w.a, A, displayId, { seq: 2 });
270
+ await w.s.postSnapshot(w.a, w.da, displayId, { seq: 1 });
271
+ await w.s.postSnapshot(w.a, w.da, displayId, { seq: 2 });
234
272
  check(JSON.stringify(await w.s.readSnapshot(d, token)) === '{"seq":2}', 'the latest snapshot is not served');
235
273
  await w.h.advance(61_000);
236
274
  check((await w.s.readSnapshot(d, token)) === null, 'a snapshot outlived 60 s');
237
- await w.s.postSnapshot(w.a, A, displayId, { seq: 3 });
238
- await expectRefusal(w.s.postSnapshot(w.a, A, displayId, { pad: 'x'.repeat(204_800) }), 'too_large', 'a snapshot over 200 KiB');
275
+ await w.s.postSnapshot(w.a, w.da, displayId, { seq: 3 });
276
+ await expectRefusal(w.s.postSnapshot(w.a, w.da, displayId, { pad: 'x'.repeat(204_800) }), 'too_large', 'a snapshot over 200 KiB');
239
277
  check(JSON.stringify(await w.s.readSnapshot(d, token)) === '{"seq":3}', 'a refused snapshot replaced the previous one');
240
278
  },
241
279
  'expired codes and displays unseen for 30 days are pruned': async (w) => {
242
- const { d, token } = await paired(w, w.a, A, '192.0.2.1', 'Counter');
243
- await w.s.mint(w.a, A);
280
+ const { d, token } = await paired(w, w.a, w.da, '192.0.2.1', 'Counter');
281
+ await w.s.mint(w.a, w.da);
244
282
  await w.h.advance(10 * MINUTE);
245
- await w.s.list(w.a, A);
283
+ await w.s.list(w.a, w.da);
246
284
  check((await w.h.storedCounts()).codes === 0, 'an expired code is still stored');
247
285
  await w.h.advance(30 * DAY);
248
- await w.s.list(w.a, A);
286
+ await w.s.list(w.a, w.da);
249
287
  check((await w.h.storedCounts()).displays === 0, 'a display unseen for 30 days is still stored');
250
288
  await expectRefusal(w.s.readSnapshot(d, token), 'token_refused', 'a pruned display');
251
289
  },
290
+ 'K1: registerDevice needs the capability, and issues a 22+ character base64url key': async (w) => {
291
+ const before = (await w.h.storedCounts()).devices;
292
+ await expectRefusal(w.s.registerDevice(w.viewer, 'till-c'), 'forbidden', 'a register without the capability');
293
+ check((await w.h.storedCounts()).devices === before, 'a refused register stored a device');
294
+ check(SECRET.test((await w.s.registerDevice(w.a, 'till-c')).deviceKey), 'the device key is not 22+ base64url characters');
295
+ },
296
+ 'K2: an id that holds a key cannot register again, by any cashier': async (w) => {
297
+ await expectRefusal(w.s.registerDevice(w.a, A), 'device_refused', 'a second register by the same cashier');
298
+ await expectRefusal(w.s.registerDevice(w.b, A), 'device_refused', 'a second register by another cashier');
299
+ await w.s.list(w.a, w.da);
300
+ },
301
+ 'K3: every till call refuses a missing, wrong, swapped or unregistered key the same way': async (w) => {
302
+ const { displayId, claimId } = await armed(w);
303
+ for (const [method] of tillCalls(w, w.da, displayId, claimId)) {
304
+ const refusals: Error[] = [];
305
+ for (const [what, device] of refusedDevices(w)) {
306
+ const call = tillCalls(w, device, displayId, claimId).find(([name]) => name === method)![1];
307
+ refusals.push(await expectRefusal(call(), 'device_refused', `${method} with ${what}`));
308
+ }
309
+ refusals.forEach((refusal, i) => sameRefusal(refusals[0], refusal, `${method}: refusal ${i}`));
310
+ }
311
+ },
312
+ 'K4: a refused till call has no effect': async (w) => {
313
+ const { d, token, displayId, claimId } = await armed(w);
314
+ const before = JSON.stringify(await w.h.storedCounts());
315
+ for (const [, device] of refusedDevices(w)) {
316
+ for (const [method, call] of tillCalls(w, device, displayId, claimId)) await expectRefusal(call(), 'device_refused', method);
317
+ }
318
+ const after = JSON.stringify(await w.h.storedCounts());
319
+ check(after === before, `refused calls changed the stored counts from ${before} to ${after}`);
320
+ check(JSON.stringify(await w.s.readSnapshot(d, token)) === '{"v":1}', 'a refused postSnapshot replaced the snapshot');
321
+ check((await w.s.displayReceiveSignals(d, token)).length === 0, 'a refused sendSignal queued a message');
322
+ check((await w.s.receiveSignals(w.a, w.da)).map((m) => m.payload).join() === 'a1', 'a refused receiveSignals drained the inbox');
323
+ },
324
+ 'K5: a wrong key gets device_refused, never not_found, on an owned and on a foreign display': async (w) => {
325
+ const own = await paired(w, w.a, w.da, '192.0.2.1', 'Own');
326
+ const foreign = await paired(w, w.b, w.db, '192.0.2.2', 'Foreign');
327
+ const wrong = { deviceId: A, deviceKey: w.db.deviceKey };
328
+ const onOwn = await expectRefusal(w.s.forget(w.a, wrong, own.displayId), 'device_refused', 'a wrong key on an owned display');
329
+ const onForeign = await expectRefusal(w.s.forget(w.a, wrong, foreign.displayId), 'device_refused', 'a wrong key on a foreign display');
330
+ sameRefusal(onOwn, onForeign, 'owned and foreign displays');
331
+ await expectRefusal(w.s.postSnapshot(w.a, wrong, foreign.displayId, { v: 1 }), 'device_refused', 'a wrong key posting to a foreign display');
332
+ },
333
+ 'K6: the device key is stored only as its SHA-256 hex': async (w) => {
334
+ const text = await w.h.storedText();
335
+ check(!text.includes(w.da.deviceKey) && !text.includes(w.db.deviceKey), 'a device key is stored');
336
+ check(text.includes(await sha256Hex(w.da.deviceKey)), "the device key's SHA-256 hex is not stored");
337
+ },
338
+ 'K7: a pre-migration till adopts its displays at its first register': async (w) => {
339
+ const legacy = await w.h.seedLegacyDevice('till-legacy');
340
+ await expectRefusal(w.s.registerDevice(w.viewer, 'till-legacy'), 'forbidden', 'a register without the capability');
341
+ check((await w.h.adoptionLog()).length === 0, 'a refused register adopted');
342
+ const before = JSON.stringify(await w.h.storedCounts());
343
+ for (const [what, deviceKey] of [['no key', ''], ['a random key', randomKey()]] as const) {
344
+ const keyless = { deviceId: 'till-legacy', deviceKey };
345
+ await expectRefusal(w.s.list(w.a, keyless), 'device_refused', `list with ${what} before the adoption`);
346
+ await expectRefusal(w.s.mint(w.a, keyless), 'device_refused', `mint with ${what} before the adoption`);
347
+ }
348
+ const after = JSON.stringify(await w.h.storedCounts());
349
+ check(after === before, `refused pre-adoption calls changed the stored counts from ${before} to ${after}`);
350
+ const device = { deviceId: 'till-legacy', deviceKey: (await w.s.registerDevice(w.a, 'till-legacy')).deviceKey };
351
+ check(SECRET.test(device.deviceKey), 'the adopting key is not 22+ base64url characters');
352
+ check((await w.s.list(w.a, device)).some((r) => r.displayId === legacy.displayId), 'the adopted till does not list its display');
353
+ await w.s.readSnapshot(w.h.display('192.0.2.1'), legacy.token);
354
+ const log = JSON.stringify(await w.h.adoptionLog());
355
+ check(log === JSON.stringify([{ deviceId: 'till-legacy', cashierId: 'cashier-a' }]), `the adoption log is ${log}`);
356
+ },
357
+ 'K8: an adoption cannot repeat': async (w) => {
358
+ const { device } = await adopted(w);
359
+ await expectRefusal(w.s.registerDevice(w.b, 'till-legacy'), 'device_refused', 'a second adoption by another cashier');
360
+ await expectRefusal(w.s.registerDevice(w.a, 'till-legacy'), 'device_refused', 'a second adoption by the same cashier');
361
+ check((await w.h.adoptionLog()).length === 1, 'the adoption log has more than one entry');
362
+ await w.s.list(w.a, device);
363
+ },
364
+ 'K9: a device that owns nothing and makes no till call for over 30 days is pruned, and its id registers again': async (w) => {
365
+ const old = { deviceId: 'till-idle', deviceKey: (await w.s.registerDevice(w.a, 'till-idle')).deviceKey };
366
+ const shown = await paired(w, w.a, w.da, '192.0.2.1', 'Counter');
367
+ await w.h.advance(29 * DAY);
368
+ await expectRefusal(w.s.list(w.a, { deviceId: 'till-idle', deviceKey: randomKey() }), 'device_refused', 'a wrong key on an idle device');
369
+ await w.s.readSnapshot(shown.d, shown.token);
370
+ await w.h.advance(2 * DAY);
371
+ check((await w.s.list(w.a, w.da)).some((r) => r.displayId === shown.displayId), 'a device that owns a live row was pruned');
372
+ const fresh = (await w.s.registerDevice(w.a, 'till-idle')).deviceKey;
373
+ check(fresh !== old.deviceKey, 'the re-registered id got its old key');
374
+ await expectRefusal(w.s.list(w.a, old), 'device_refused', 'the key of a pruned device');
375
+ await w.s.list(w.a, { deviceId: 'till-idle', deviceKey: fresh });
376
+ },
252
377
  };
253
378
  return Object.entries(cases).map(([name, run]) => ({ name, run: async () => run(await world(makeHarness)) }));
254
379
  }