@milesroxas/design-hub 0.6.1 → 0.7.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 (55) hide show
  1. package/README.md +17 -7
  2. package/core/review/model.ts +90 -10
  3. package/core/review/policy.test.ts +62 -0
  4. package/core/review/policy.ts +13 -9
  5. package/core/review/service.test.ts +464 -54
  6. package/core/review/service.ts +168 -36
  7. package/core/review/store.ts +31 -7
  8. package/dist/core/review/model.d.ts +39 -7
  9. package/dist/core/review/model.js +73 -7
  10. package/dist/core/review/policy.d.ts +4 -1
  11. package/dist/core/review/policy.js +4 -4
  12. package/dist/core/review/service.d.ts +24 -6
  13. package/dist/core/review/service.js +155 -36
  14. package/dist/core/review/store.d.ts +15 -5
  15. package/dist/core/review/store.js +27 -6
  16. package/dist/scripts/bootstrap.d.ts +16 -14
  17. package/dist/scripts/bootstrap.js +56 -39
  18. package/dist/scripts/lib/deploy.d.ts +12 -4
  19. package/dist/scripts/lib/deploy.js +47 -13
  20. package/dist/scripts/lib/review.js +3 -3
  21. package/dist/scripts/lib/vercel.d.ts +8 -2
  22. package/dist/scripts/lib/vercel.js +23 -9
  23. package/dist/scripts/people.d.ts +16 -29
  24. package/dist/scripts/people.js +72 -39
  25. package/dist/scripts/preflight.js +2 -2
  26. package/dist/scripts/share.d.ts +2 -3
  27. package/dist/scripts/share.js +18 -10
  28. package/dist/server/api.d.ts +6 -2
  29. package/dist/server/api.js +53 -22
  30. package/dist/server/dev.js +3 -3
  31. package/dist/server/fake-upstash.js +3 -2
  32. package/dist/server/local.d.ts +2 -2
  33. package/dist/server/local.js +6 -4
  34. package/dist/server/session.d.ts +25 -10
  35. package/dist/server/session.js +69 -52
  36. package/dist/server/upstash.js +4 -3
  37. package/dist/vercel/proxy.d.ts +5 -3
  38. package/dist/vercel/proxy.js +183 -157
  39. package/dist/vite/plugin.js +5 -5
  40. package/kit/ui/Menu.tsx +14 -4
  41. package/kit/ui/ui.css +16 -3
  42. package/package.json +2 -2
  43. package/shell/review/Bar.tsx +5 -4
  44. package/shell/review/People.tsx +194 -55
  45. package/shell/review/Review.tsx +9 -8
  46. package/shell/review/ThreadCard.tsx +24 -21
  47. package/shell/review/client.ts +37 -18
  48. package/shell/review/review.css +9 -1
  49. package/skills/miles-deploy/SKILL.md +44 -16
  50. package/skills/miles-design-hub/SKILL.md +5 -5
  51. package/skills/miles-new-feature/SKILL.md +1 -1
  52. package/skills/miles-review/SKILL.md +7 -5
  53. package/skills/miles-worktrees/SKILL.md +1 -1
  54. package/templates/conductor/settings.toml +1 -1
  55. package/templates/guide.md +1 -1
@@ -1,8 +1,8 @@
1
1
  import assert from 'node:assert/strict';
2
2
  import { beforeEach, test } from 'node:test';
3
- import type { Anchor, Person } from './model.ts';
3
+ import { type Anchor, accessEnd, type Person } from './model.ts';
4
4
  import { createReview, type Review, ReviewError } from './service.ts';
5
- import { memoryKV } from './store.ts';
5
+ import { KEY, type KV, memoryKV, type Snapshot } from './store.ts';
6
6
 
7
7
  const anchor: Anchor = {
8
8
  prototype: 'sample-list',
@@ -16,9 +16,10 @@ const anchor: Anchor = {
16
16
 
17
17
  let clock = 0;
18
18
  let review: Review;
19
- let editor: Person;
20
- let reviewer: Person;
21
- let reviewerToken: string;
19
+ let studio: Person;
20
+ let client: Person;
21
+ let clientToken: string;
22
+ const DAY = 86_400_000;
22
23
 
23
24
  const rejects = (p: Promise<unknown>, code: string) =>
24
25
  assert.rejects(p, (e: unknown) => e instanceof ReviewError && e.code === code);
@@ -35,71 +36,480 @@ beforeEach(async () => {
35
36
  id: 'cli',
36
37
  name: 'CLI',
37
38
  team: 'Studio',
38
- role: 'editor',
39
+ kind: 'studio',
40
+ email: null,
41
+ accessEndsAt: null,
39
42
  createdAt: '',
40
43
  revokedAt: null,
41
44
  epoch: 0,
42
45
  } as Person;
43
- editor = (await review.invite(root, { name: 'Ed', team: 'Studio', role: 'editor' })).person;
44
- const r = await review.invite(editor, { name: 'Rae', team: 'Acme' });
45
- reviewer = r.person;
46
- reviewerToken = r.token;
46
+ studio = (await review.addPerson(root, { name: 'Ed', team: 'Studio', kind: 'studio' })).person;
47
+ const r = await review.addPerson(studio, { name: 'Rae', team: 'Acme' });
48
+ client = r.person;
49
+ clientToken = r.token;
47
50
  });
48
51
 
49
- test('an invite signs its person in until it is reissued or revoked', async () => {
50
- assert.equal((await review.acceptInvite(reviewerToken))?.id, reviewer.id);
51
- const { token } = await review.reissue(editor, reviewer.id);
52
- assert.equal(await review.acceptInvite(reviewerToken), null);
53
- assert.equal((await review.acceptInvite(token))?.epoch, 1);
54
- await review.revoke(editor, reviewer.id);
55
- assert.equal(await review.acceptInvite(token), null);
52
+ test('a link works once, and looking at it uses nothing up', async () => {
53
+ assert.equal((await review.peekLink(clientToken))?.id, client.id);
54
+ assert.equal((await review.peekLink(clientToken))?.id, client.id);
55
+ assert.equal((await review.useLink(clientToken))?.id, client.id);
56
+ assert.equal(await review.useLink(clientToken), null);
57
+ assert.equal(await review.peekLink(clientToken), null);
58
+ assert.equal(await review.useLink('not a token'), null);
56
59
  });
57
60
 
58
- test('a reissue or revoke ends every session signed with the old epoch', async () => {
59
- assert.ok(await review.sessionPerson(reviewer.id, 0));
60
- await review.reissue(editor, reviewer.id);
61
- assert.equal(await review.sessionPerson(reviewer.id, 0), null);
62
- assert.ok(await review.sessionPerson(reviewer.id, 1));
63
- await review.revoke(editor, reviewer.id);
64
- assert.equal(await review.sessionPerson(reviewer.id, 2), null);
61
+ test('two requests with one link at once sign in one device', async () => {
62
+ const both = await Promise.all([review.useLink(clientToken), review.useLink(clientToken)]);
63
+ assert.deepEqual(
64
+ both.map((p) => p?.id ?? null),
65
+ [client.id, null],
66
+ );
67
+ });
68
+
69
+ test('a new link is for another device: sessions and other links stay', async () => {
70
+ const { token: second } = await review.newLink(studio, client.id);
71
+ const { token: third } = await review.newLink(studio, client.id);
72
+ assert.notEqual(second, clientToken);
73
+ assert.equal((await review.useLink(second))?.id, client.id);
74
+ assert.ok(await review.sessionPerson(client.id, 0));
75
+ assert.equal((await review.useLink(clientToken))?.epoch, 0);
76
+ assert.equal((await review.useLink(third))?.epoch, 0);
77
+ });
78
+
79
+ test('sign out everywhere ends every session and unused link, and gives one new link', async () => {
80
+ const { token: spare } = await review.newLink(studio, client.id);
81
+ const { person, token } = await review.signOutEverywhere(studio, client.id);
82
+ assert.equal(person.epoch, 1);
83
+ assert.equal(await review.sessionPerson(client.id, 0), null);
84
+ assert.equal(await review.useLink(clientToken), null);
85
+ assert.equal(await review.useLink(spare), null);
86
+ assert.equal((await review.useLink(token))?.epoch, 1);
87
+ assert.ok(await review.sessionPerson(client.id, 1));
88
+ });
89
+
90
+ test('a revoke ends every session and link, and a revoked person gets no new link', async () => {
91
+ await review.revoke(studio, client.id);
92
+ assert.equal(await review.sessionPerson(client.id, 1), null);
93
+ assert.equal(await review.peekLink(clientToken), null);
94
+ await rejects(review.newLink(studio, client.id), 'invalid');
95
+ await rejects(review.signOutEverywhere(studio, client.id), 'invalid');
96
+ await rejects(review.setAccess(studio, client.id, 'open'), 'invalid');
97
+ await rejects(review.newLink(studio, 'nobody'), 'not_found');
98
+ });
99
+
100
+ test('a store failure before the claim leaves the link usable', async () => {
101
+ const kv = memoryKV();
102
+ let failPeople = false;
103
+ const flaky: KV = {
104
+ ...kv,
105
+ hget: async (key, field) => {
106
+ if (failPeople && key === KEY.people) throw new Error('store down');
107
+ return kv.hget(key, field);
108
+ },
109
+ };
110
+ const r = createReview(flaky);
111
+ const root = { ...studio, id: 'cli' };
112
+ const { token } = await r.addPerson(root, { name: 'Rae', team: 'Acme' });
113
+ failPeople = true;
114
+ await assert.rejects(r.useLink(token), /store down/);
115
+ failPeople = false;
116
+ assert.equal((await r.useLink(token))?.name, 'Rae');
117
+ });
118
+
119
+ test('the store never holds a link token in clear', async () => {
120
+ let seen: Snapshot = { hashes: {}, sets: {} };
121
+ const r = createReview(
122
+ memoryKV(undefined, (snapshot) => {
123
+ seen = snapshot;
124
+ }),
125
+ );
126
+ const root = { ...studio, id: 'cli' };
127
+ const added = await r.addPerson(root, { name: 'Rae', team: 'Acme' });
128
+ const tokens = [
129
+ added.token,
130
+ (await r.newLink(root, added.person.id)).token,
131
+ (await r.signOutEverywhere(root, added.person.id)).token,
132
+ ];
133
+ const stored = JSON.stringify(seen);
134
+ assert.ok(Object.keys(seen.hashes[KEY.links] ?? {}).length > 0);
135
+ for (const token of tokens) assert.equal(stored.includes(token), false);
136
+ });
137
+
138
+ test('a revoke writes revocation and epoch, never the profile, before it drops their links', async () => {
139
+ const writes: string[] = [];
140
+ const kv = memoryKV();
141
+ const watched: KV = {
142
+ ...kv,
143
+ hset: async (key, field, value) => {
144
+ writes.push(`set ${key}`);
145
+ return kv.hset(key, field, value);
146
+ },
147
+ hincrby: async (key, field, by) => {
148
+ writes.push(`incr ${key}`);
149
+ return kv.hincrby(key, field, by);
150
+ },
151
+ hdel: async (key, field) => {
152
+ writes.push(`del ${key}`);
153
+ return kv.hdel(key, field);
154
+ },
155
+ };
156
+ const r = createReview(watched);
157
+ const root = { ...studio, id: 'cli' };
158
+ const { person } = await r.addPerson(root, { name: 'Rae', team: 'Acme' });
159
+ writes.length = 0;
160
+ await r.revoke(root, person.id);
161
+ assert.deepEqual(writes, [`set ${KEY.revoked}`, `incr ${KEY.epochs}`, `del ${KEY.links}`]);
162
+ });
163
+
164
+ /**
165
+ * A store that holds the next profile write (setAccess's) after its read,
166
+ * until the test releases it: a real interleaving, not two calls in a row.
167
+ */
168
+ function pausingKV() {
169
+ const kv = memoryKV();
170
+ let arm = false;
171
+ let reached = () => {};
172
+ let release = () => {};
173
+ const paused = new Promise<void>((resolve) => {
174
+ reached = resolve;
175
+ });
176
+ const gate = new Promise<void>((resolve) => {
177
+ release = resolve;
178
+ });
179
+ const store: KV = {
180
+ ...kv,
181
+ hset: async (key, field, value) => {
182
+ if (arm && key === KEY.people) {
183
+ arm = false;
184
+ reached();
185
+ await gate;
186
+ }
187
+ return kv.hset(key, field, value);
188
+ },
189
+ };
190
+ return { kv, store, armNextProfileWrite: () => (arm = true), paused, release };
191
+ }
192
+
193
+ async function racedSetAccess(between: (r: Review, root: Person, id: string) => Promise<unknown>) {
194
+ const { kv, store, armNextProfileWrite, paused, release } = pausingKV();
195
+ const r = createReview(store);
196
+ const root = { ...studio, id: 'cli' };
197
+ const { person, token } = await r.addPerson(root, { name: 'Rae', team: 'Acme' });
198
+ const { token: spare } = await r.newLink(root, person.id);
199
+ armNextProfileWrite();
200
+ // setAccess reads the person (active, epoch 0), then waits at its write.
201
+ const stale = r.setAccess(root, person.id, 'open');
202
+ await paused;
203
+ await between(r, root, person.id);
204
+ release();
205
+ const answer = await stale;
206
+ return { r, kv, person, token, spare, answer };
207
+ }
208
+
209
+ test('a stale setAccess written after a revoke never revives the person', async () => {
210
+ const { r, kv, person, token, spare, answer } = await racedSetAccess((r, root, id) => r.revoke(root, id));
211
+ const now = await r.person(person.id);
212
+ assert.ok(now?.revokedAt);
213
+ assert.equal(now?.accessEndsAt, null);
214
+ assert.ok(answer.revokedAt);
215
+ assert.equal(await r.sessionPerson(person.id, 0), null);
216
+ assert.equal(await r.sessionPerson(person.id, now?.epoch ?? 0), null);
217
+ assert.equal(await r.peekLink(token), null);
218
+ assert.equal(await r.peekLink(spare), null);
219
+ assert.deepEqual(Object.keys(await kv.hgetall(KEY.links)), []);
220
+ assert.ok((await r.listPeople(studio)).find((p) => p.id === person.id)?.revokedAt);
221
+ });
222
+
223
+ test('a stale setAccess written after a sign out everywhere keeps the new epoch', async () => {
224
+ let fresh = '';
225
+ const { r, person, spare } = await racedSetAccess(async (r, root, id) => {
226
+ fresh = (await r.signOutEverywhere(root, id)).token;
227
+ });
228
+ const now = await r.person(person.id);
229
+ assert.equal(now?.epoch, 1);
230
+ assert.equal(now?.accessEndsAt, null);
231
+ assert.equal(await r.sessionPerson(person.id, 0), null);
232
+ assert.ok(await r.sessionPerson(person.id, 1));
233
+ assert.equal(await r.peekLink(spare), null);
234
+ assert.equal((await r.useLink(fresh))?.epoch, 1);
235
+ });
236
+
237
+ /** A pause point: the first call to `method` on `key` waits there until released. */
238
+ type Pause = { reached: Promise<void>; release: () => void };
239
+ function pausing(kv: KV, points: Record<string, { method: 'hdel' | 'hgetall'; key: string }>) {
240
+ const pauses: Record<string, Pause & { armed: boolean; hit: () => void; gate: Promise<void> }> = {};
241
+ for (const name of Object.keys(points)) {
242
+ let hit = () => {};
243
+ let release = () => {};
244
+ const reached = new Promise<void>((r) => {
245
+ hit = r;
246
+ });
247
+ const gate = new Promise<void>((r) => {
248
+ release = r;
249
+ });
250
+ pauses[name] = { reached, release, hit, gate, armed: false };
251
+ }
252
+ const wait = async (method: string, key: string) => {
253
+ for (const [name, at] of Object.entries(points)) {
254
+ const p = pauses[name];
255
+ if (p.armed && at.method === method && at.key === key) {
256
+ p.armed = false;
257
+ p.hit();
258
+ await p.gate;
259
+ }
260
+ }
261
+ };
262
+ const store: KV = {
263
+ ...kv,
264
+ hdel: async (key, field) => {
265
+ await wait('hdel', key);
266
+ return kv.hdel(key, field);
267
+ },
268
+ hgetall: async (key) => {
269
+ await wait('hgetall', key);
270
+ return kv.hgetall(key);
271
+ },
272
+ };
273
+ return { store, arm: (name: string) => (pauses[name].armed = true), pauses };
274
+ }
275
+
276
+ for (const [what, run] of [
277
+ ['a revoke', (r: Review, root: Person, id: string) => r.revoke(root, id)],
278
+ ['a sign out everywhere', (r: Review, root: Person, id: string) => r.signOutEverywhere(root, id)],
279
+ ] as const)
280
+ test(`a link used while ${what} lands signs nobody in`, async () => {
281
+ const { store, arm, pauses } = pausing(memoryKV(), {
282
+ claim: { method: 'hdel', key: KEY.links },
283
+ drop: { method: 'hgetall', key: KEY.links },
284
+ });
285
+ const r = createReview(store);
286
+ const root = { ...studio, id: 'cli' };
287
+ const { person, token } = await r.addPerson(root, { name: 'Rae', team: 'Acme' });
288
+ // useLink checks the person (active, epoch 0), then waits at its claim.
289
+ arm('claim');
290
+ const using = r.useLink(token);
291
+ await pauses.claim.reached;
292
+ // The other call writes its revocation or epoch, then waits before it drops links.
293
+ arm('drop');
294
+ const other = run(r, root, person.id);
295
+ await pauses.drop.reached;
296
+ // The claim wins the race to the link, but the person it checked is no longer current.
297
+ pauses.claim.release();
298
+ assert.equal(await using, null);
299
+ pauses.drop.release();
300
+ await other;
301
+ });
302
+
303
+ test('any revocation record means revoked, and an access end that does not parse means no access', async () => {
304
+ const kv = memoryKV();
305
+ const r = createReview(kv);
306
+ const root = { ...studio, id: 'cli' };
307
+ const a = (await r.addPerson(root, { name: 'Rae', team: 'Acme' })).person;
308
+ await kv.hset(KEY.revoked, a.id, '');
309
+ assert.ok((await r.person(a.id))?.revokedAt);
310
+ assert.equal(await r.sessionPerson(a.id, 0), null);
311
+ const b = await r.addPerson(root, { name: 'Oz', team: 'Acme' });
312
+ const profile = JSON.parse((await kv.hget(KEY.people, b.person.id)) ?? '{}');
313
+ await kv.hset(KEY.people, b.person.id, JSON.stringify({ ...profile, accessEndsAt: 'someday' }));
314
+ assert.equal(await r.sessionPerson(b.person.id, 0), null);
315
+ assert.equal(await r.peekLink(b.token), null);
316
+ assert.equal(await r.useLink(b.token), null);
317
+ });
318
+
319
+ test('a profile that is not a well-formed person is no person, and listPeople skips it', async () => {
320
+ const kv = memoryKV();
321
+ const r = createReview(kv);
322
+ const root = { ...studio, id: 'cli' };
323
+ const { person } = await r.addPerson(root, { name: 'Rae', team: 'Acme' });
324
+ await kv.hset(KEY.people, 'junk', 'not json');
325
+ await kv.hset(KEY.people, 'array', '[]');
326
+ await kv.hset(KEY.people, 'nokind', JSON.stringify({ id: 'nokind', name: 'X', team: 'Y', createdAt: '' }));
327
+ await kv.hset(
328
+ KEY.people,
329
+ 'oddend',
330
+ JSON.stringify({
331
+ ...JSON.parse((await kv.hget(KEY.people, person.id)) ?? '{}'),
332
+ id: 'oddend',
333
+ accessEndsAt: 5,
334
+ }),
335
+ );
336
+ for (const id of ['junk', 'array', 'nokind', 'oddend']) {
337
+ assert.equal(await r.person(id), null);
338
+ assert.equal(await r.sessionPerson(id, 0), null);
339
+ }
340
+ assert.deepEqual(
341
+ (await r.listPeople(root)).map((p) => p.id),
342
+ [person.id],
343
+ );
344
+ });
345
+
346
+ test('two sign outs everywhere at once both end the earlier sessions', async () => {
347
+ const root = { ...studio, id: 'cli' };
348
+ const [a, b] = await Promise.all([
349
+ review.signOutEverywhere(root, client.id),
350
+ review.signOutEverywhere(root, client.id),
351
+ ]);
352
+ assert.deepEqual([a.person.epoch, b.person.epoch].sort(), [1, 2]);
353
+ assert.equal((await review.person(client.id))?.epoch, 2);
354
+ assert.equal(await review.sessionPerson(client.id, 0), null);
355
+ });
356
+
357
+ test('a profile write never carries revocation or epoch', async () => {
358
+ const kv = memoryKV();
359
+ const r = createReview(kv);
360
+ const root = { ...studio, id: 'cli' };
361
+ const { person } = await r.addPerson(root, { name: 'Rae', team: 'Acme' });
362
+ await r.signOutEverywhere(root, person.id);
363
+ await r.setAccess(root, person.id, '6m');
364
+ const profile = JSON.parse((await kv.hget(KEY.people, person.id)) ?? '{}');
365
+ assert.equal('epoch' in profile, false);
366
+ assert.equal('revokedAt' in profile, false);
367
+ // A profile that does carry them (an older write, a hand edit) is overruled by the records that own them.
368
+ await kv.hset(KEY.people, person.id, JSON.stringify({ ...profile, epoch: 0, revokedAt: null }));
369
+ await r.revoke(root, person.id);
370
+ await kv.hset(KEY.people, person.id, JSON.stringify({ ...profile, epoch: 0, revokedAt: null }));
371
+ const now = await r.person(person.id);
372
+ assert.ok(now?.revokedAt);
373
+ assert.equal(now?.epoch, 2);
374
+ });
375
+
376
+ test('access lengths land on the same day, or the last day of a shorter month', () => {
377
+ assert.equal(accessEnd('3m', new Date('2030-11-30T09:00:00Z')), '2031-02-28T09:00:00.000Z');
378
+ assert.equal(accessEnd('6m', new Date('2030-08-31T09:00:00Z')), '2031-02-28T09:00:00.000Z');
379
+ assert.equal(accessEnd('3m', new Date('2031-11-30T09:00:00Z')), '2032-02-29T09:00:00.000Z');
380
+ assert.equal(accessEnd('6m', new Date('2030-01-15T09:00:00Z')), '2030-07-15T09:00:00.000Z');
381
+ assert.equal(accessEnd('open', new Date()), null);
382
+ });
383
+
384
+ test('an unused link lasts 14 days', async () => {
385
+ clock += 13 * DAY;
386
+ assert.ok(await review.peekLink(clientToken));
387
+ clock += 2 * DAY;
388
+ assert.equal(await review.peekLink(clientToken), null);
389
+ assert.equal(await review.useLink(clientToken), null);
390
+ });
391
+
392
+ test('a Client gets the project default, Studio is open-ended, and a length given wins', async () => {
393
+ assert.match(client.accessEndsAt ?? '', /^2030-04-01/);
394
+ assert.equal(studio.accessEndsAt, null);
395
+ assert.equal(await review.defaultAccess(studio), '3m');
396
+ assert.equal(await review.setDefaultAccess(studio, '6m'), '6m');
397
+ const later = (await review.addPerson(studio, { name: 'Oz', team: 'Acme' })).person;
398
+ assert.match(later.accessEndsAt ?? '', /^2030-07-01/);
399
+ const open = (await review.addPerson(studio, { name: 'Lu', team: 'Acme', access: 'open' })).person;
400
+ assert.equal(open.accessEndsAt, null);
401
+ const short = (await review.addPerson(studio, { name: 'Jo', team: 'Studio', kind: 'studio', access: '3m' }))
402
+ .person;
403
+ assert.match(short.accessEndsAt ?? '', /^2030-04-01/);
65
404
  });
66
405
 
67
- test('an invite expires', async () => {
68
- clock += 15 * 86_400_000;
69
- assert.equal(await review.acceptInvite(reviewerToken), null);
406
+ test('access ends sessions and links when it runs out, and Studio can change it', async () => {
407
+ assert.ok(await review.sessionPerson(client.id, 0));
408
+ clock += 91 * DAY;
409
+ assert.equal(await review.sessionPerson(client.id, 0), null);
410
+ await rejects(review.newLink(studio, client.id), 'invalid');
411
+ const changed = await review.setAccess(studio, client.id, '6m');
412
+ assert.ok((changed.accessEndsAt ?? '') > new Date(clock).toISOString());
413
+ assert.ok(await review.sessionPerson(client.id, 0));
414
+ assert.equal((await review.setAccess(studio, client.id, 'open')).accessEndsAt, null);
70
415
  });
71
416
 
72
- test('only editors manage people, and nobody revokes themselves', async () => {
73
- await rejects(review.invite(reviewer, { name: 'X', team: 'Acme' }), 'forbidden');
417
+ test('access lengths are checked and every problem is named', async () => {
418
+ await assert.rejects(
419
+ review.addPerson(studio, { name: '', team: 'Acme', access: '1y' }),
420
+ (e: unknown) =>
421
+ e instanceof ReviewError && e.problems.join() === 'name: required,access: one of 3m, 6m, open',
422
+ );
423
+ await rejects(review.setAccess(studio, client.id, 'forever'), 'invalid');
424
+ await rejects(review.setDefaultAccess(studio, null), 'invalid');
425
+ });
426
+
427
+ test('only Studio manages people, links, access and the default, and nobody revokes themselves', async () => {
428
+ await rejects(review.addPerson(client, { name: 'X', team: 'Acme' }), 'forbidden');
429
+ await rejects(review.listPeople(client), 'forbidden');
74
430
  await rejects(review.listPeople(null), 'unauthorized');
75
- await rejects(review.revoke(editor, editor.id), 'invalid');
431
+ await rejects(review.newLink(client, client.id), 'forbidden');
432
+ await rejects(review.signOutEverywhere(client, client.id), 'forbidden');
433
+ await rejects(review.revoke(client, studio.id), 'forbidden');
434
+ await rejects(review.setAccess(client, client.id, 'open'), 'forbidden');
435
+ await rejects(review.defaultAccess(client), 'forbidden');
436
+ await rejects(review.setDefaultAccess(client, 'open'), 'forbidden');
437
+ await rejects(review.revoke(studio, studio.id), 'invalid');
76
438
  assert.deepEqual(
77
- (await review.listPeople(editor)).map((p) => p.name),
439
+ (await review.listPeople(studio)).map((p) => p.name),
78
440
  ['Rae', 'Ed'],
79
441
  );
80
442
  });
81
443
 
82
444
  test('a comment, its replies, resolving and deleting the thread', async () => {
83
- const root = await review.add(reviewer, { body: ' Row height jumps on empty ', anchor });
445
+ const root = await review.add(client, { body: ' Row height jumps on empty ', anchor });
84
446
  assert.equal(root.body, 'Row height jumps on empty');
85
- await review.reply(editor, { prototype: 'sample-list', thread: root.id, body: 'Fixed in v2' });
86
- assert.equal((await review.listComments(reviewer, 'sample-list')).length, 2);
87
- const done = await review.resolve(reviewer, { prototype: 'sample-list', id: root.id, resolved: true });
88
- assert.equal(done.resolvedBy?.id, reviewer.id);
89
- assert.deepEqual((await review.remove(reviewer, { prototype: 'sample-list', id: root.id })).length, 2);
90
- assert.deepEqual(await review.listComments(editor, 'sample-list'), []);
91
- });
92
-
93
- test('people edit only their own words; editors resolve and triage anything', async () => {
94
- const c = await review.add(reviewer, { body: 'Typo in header', anchor });
95
- await rejects(review.edit(editor, { prototype: 'sample-list', id: c.id, body: 'x' }), 'forbidden');
96
- const other = (await review.invite(editor, { name: 'Oz', team: 'Acme' })).person;
97
- await rejects(review.resolve(other, { prototype: 'sample-list', id: c.id, resolved: true }), 'forbidden');
447
+ await review.reply(studio, { prototype: 'sample-list', thread: root.id, body: 'Fixed in v2' });
448
+ assert.equal((await review.listComments(client, 'sample-list')).length, 2);
449
+ const done = await review.resolve(client, { prototype: 'sample-list', id: root.id, resolved: true });
450
+ assert.equal(done.resolvedBy?.id, client.id);
451
+ assert.deepEqual((await review.remove(client, { prototype: 'sample-list', id: root.id })).length, 2);
452
+ assert.deepEqual(await review.listComments(studio, 'sample-list'), []);
453
+ });
454
+
455
+ test('a new person is a Client unless named Studio, and any other kind is invalid', async () => {
456
+ assert.equal(client.kind, 'client');
457
+ assert.equal(client.email, null);
458
+ assert.equal(studio.kind, 'studio');
459
+ await rejects(review.addPerson(studio, { name: 'X', team: 'Acme', kind: 'editor' }), 'invalid');
460
+ });
461
+
462
+ test('a Client resolves and reopens anyone’s thread, but deletes only their own', async () => {
463
+ const c = await review.add(studio, { body: 'Spacing off', anchor });
464
+ const other = (await review.addPerson(studio, { name: 'Oz', team: 'Acme' })).person;
465
+ const done = await review.resolve(other, { prototype: 'sample-list', id: c.id, resolved: true });
466
+ assert.equal(done.resolvedBy?.id, other.id);
467
+ const open = await review.resolve(client, { prototype: 'sample-list', id: c.id, resolved: false });
468
+ assert.equal(open.status, 'open');
469
+ await rejects(review.remove(client, { prototype: 'sample-list', id: c.id }), 'forbidden');
470
+ const reply = await review.reply(other, { prototype: 'sample-list', thread: c.id, body: 'Agreed' });
471
+ await rejects(review.remove(client, { prototype: 'sample-list', id: reply.id }), 'forbidden');
472
+ });
473
+
474
+ test('Studio resolves and deletes anyone’s thread', async () => {
475
+ const c = await review.add(client, { body: 'Wrong icon', anchor });
476
+ const done = await review.resolve(studio, { prototype: 'sample-list', id: c.id, resolved: true });
477
+ assert.equal(done.resolvedBy?.id, studio.id);
478
+ assert.deepEqual(await review.remove(studio, { prototype: 'sample-list', id: c.id }), [c.id]);
479
+ });
480
+
481
+ test('records under an older key prefix are never read, 0.6 links included', async () => {
482
+ const old: Person = { ...client, id: 'old', name: 'Old' };
483
+ const token = 'a'.repeat(43);
484
+ const hash = Buffer.from(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(token))).toString(
485
+ 'hex',
486
+ );
487
+ const kv = memoryKV({
488
+ hashes: {
489
+ 'hub2:people': { old: JSON.stringify(old) },
490
+ 'hub2:invites': { [hash]: JSON.stringify({ person: 'old', expiresAt: '2999-01-01T00:00:00Z' }) },
491
+ 'hub2:comments:sample-list': { x: JSON.stringify({ id: 'x', thread: 'x' }) },
492
+ },
493
+ sets: { 'hub2:prototypes': ['sample-list'] },
494
+ });
495
+ const fresh = createReview(kv);
496
+ assert.ok(KEY.people.startsWith('hub3:'));
497
+ assert.equal(await fresh.person('old'), null);
498
+ assert.equal(await fresh.sessionPerson('old', 0), null);
499
+ assert.equal(await fresh.peekLink(token), null);
500
+ assert.equal(await fresh.useLink(token), null);
501
+ assert.deepEqual(await fresh.allComments(), []);
502
+ assert.equal(await fresh.locate('x'), null);
503
+ });
504
+
505
+ test('people edit only their own words; Studio triages anything', async () => {
506
+ const c = await review.add(client, { body: 'Typo in header', anchor });
507
+ await rejects(review.edit(studio, { prototype: 'sample-list', id: c.id, body: 'x' }), 'forbidden');
98
508
  await rejects(
99
- review.retriage(reviewer, { prototype: 'sample-list', id: c.id, triage: { kind: 'copy' } }),
509
+ review.retriage(client, { prototype: 'sample-list', id: c.id, triage: { kind: 'copy' } }),
100
510
  'forbidden',
101
511
  );
102
- const t = await review.retriage(editor, {
512
+ const t = await review.retriage(studio, {
103
513
  prototype: 'sample-list',
104
514
  id: c.id,
105
515
  triage: { kind: 'copy', state: 'Empty' },
@@ -107,7 +517,7 @@ test('people edit only their own words; editors resolve and triage anything', as
107
517
  });
108
518
  assert.deepEqual(t.triage, { kind: 'copy', state: 'Empty', by: 'person', confidence: null });
109
519
  await rejects(
110
- review.retriage(editor, {
520
+ review.retriage(studio, {
111
521
  prototype: 'sample-list',
112
522
  id: c.id,
113
523
  triage: { state: 'Nope' },
@@ -117,15 +527,15 @@ test('people edit only their own words; editors resolve and triage anything', as
117
527
  );
118
528
  });
119
529
 
120
- test('a revoked person and a password-only visitor can do nothing', async () => {
121
- const gone = await review.revoke(editor, reviewer.id);
530
+ test('a revoked person and a visitor who is not signed in can do nothing', async () => {
531
+ const gone = await review.revoke(studio, client.id);
122
532
  await rejects(review.add(gone, { body: 'x', anchor }), 'forbidden');
123
533
  await rejects(review.add(null, { body: 'x', anchor }), 'unauthorized');
124
534
  });
125
535
 
126
536
  test('bad input names every problem', async () => {
127
537
  await assert.rejects(
128
- review.add(reviewer, { body: '', anchor: { ...anchor, url: 'x', offset: { x: 2, y: 0 } } }),
538
+ review.add(client, { body: '', anchor: { ...anchor, url: 'x', offset: { x: 2, y: 0 } } }),
129
539
  (e: unknown) => {
130
540
  assert.ok(e instanceof ReviewError);
131
541
  assert.deepEqual(
@@ -145,7 +555,7 @@ test('triage runs on add, and a failing triager never loses the comment', async
145
555
  return { kind: 'bug', state: states[0] ?? null, by: 'jev', confidence: 0.9 };
146
556
  },
147
557
  });
148
- const c = await triaged.add(reviewer, {
558
+ const c = await triaged.add(client, {
149
559
  body: 'Broken',
150
560
  anchor,
151
561
  states: ['Empty', 'Empty', ' Real list '],
@@ -153,11 +563,11 @@ test('triage runs on add, and a failing triager never loses the comment', async
153
563
  assert.deepEqual(seen, [['Empty', 'Real list']]);
154
564
  assert.equal(c.triage?.kind, 'bug');
155
565
  const failing = createReview(memoryKV(), { triage: async () => Promise.reject(new Error('down')) });
156
- assert.equal((await failing.add(reviewer, { body: 'Still saved', anchor })).triage, null);
566
+ assert.equal((await failing.add(client, { body: 'Still saved', anchor })).triage, null);
157
567
  });
158
568
 
159
569
  test('locate finds a comment by id alone', async () => {
160
- const c = await review.add(reviewer, { body: 'Here', anchor });
570
+ const c = await review.add(client, { body: 'Here', anchor });
161
571
  assert.equal((await review.locate(c.id))?.prototype, 'sample-list');
162
572
  assert.equal(await review.locate('nope'), null);
163
573
  });