@milesroxas/design-hub 0.6.2 → 0.8.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 (56) hide show
  1. package/README.md +16 -6
  2. package/core/review/model.ts +124 -6
  3. package/core/review/policy.test.ts +103 -24
  4. package/core/review/policy.ts +43 -7
  5. package/core/review/service.test.ts +569 -33
  6. package/core/review/service.ts +272 -76
  7. package/core/review/store.ts +24 -4
  8. package/dist/core/review/model.d.ts +59 -4
  9. package/dist/core/review/model.js +98 -4
  10. package/dist/core/review/policy.d.ts +21 -2
  11. package/dist/core/review/policy.js +34 -2
  12. package/dist/core/review/service.d.ts +39 -11
  13. package/dist/core/review/service.js +237 -77
  14. package/dist/core/review/store.d.ts +17 -4
  15. package/dist/core/review/store.js +21 -3
  16. package/dist/scripts/bootstrap.d.ts +16 -14
  17. package/dist/scripts/bootstrap.js +56 -39
  18. package/dist/scripts/comments.d.ts +10 -2
  19. package/dist/scripts/comments.js +18 -7
  20. package/dist/scripts/lib/deploy.d.ts +12 -4
  21. package/dist/scripts/lib/deploy.js +47 -13
  22. package/dist/scripts/lib/vercel.d.ts +8 -2
  23. package/dist/scripts/lib/vercel.js +23 -9
  24. package/dist/scripts/people.d.ts +13 -26
  25. package/dist/scripts/people.js +68 -35
  26. package/dist/scripts/preflight.js +2 -2
  27. package/dist/scripts/share.d.ts +2 -3
  28. package/dist/scripts/share.js +18 -10
  29. package/dist/server/api.d.ts +6 -2
  30. package/dist/server/api.js +65 -24
  31. package/dist/server/fake-upstash.js +3 -2
  32. package/dist/server/local.js +2 -0
  33. package/dist/server/session.d.ts +25 -10
  34. package/dist/server/session.js +69 -52
  35. package/dist/server/upstash.js +4 -3
  36. package/dist/vercel/proxy.d.ts +5 -3
  37. package/dist/vercel/proxy.js +183 -157
  38. package/dist/vite/plugin.js +5 -5
  39. package/kit/prefs.ts +19 -2
  40. package/package.json +2 -2
  41. package/shell/review/Bar.tsx +97 -4
  42. package/shell/review/People.tsx +178 -45
  43. package/shell/review/Pins.tsx +39 -24
  44. package/shell/review/Review.tsx +113 -29
  45. package/shell/review/ThreadCard.tsx +25 -13
  46. package/shell/review/client.ts +64 -23
  47. package/shell/review/review.css +16 -2
  48. package/shell/review/view.test.ts +73 -1
  49. package/shell/review/view.ts +49 -3
  50. package/skills/miles-deploy/SKILL.md +44 -16
  51. package/skills/miles-design-hub/SKILL.md +5 -5
  52. package/skills/miles-new-feature/SKILL.md +1 -1
  53. package/skills/miles-review/SKILL.md +10 -6
  54. package/skills/miles-worktrees/SKILL.md +1 -1
  55. package/templates/conductor/settings.toml +1 -1
  56. 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 { KEY, 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',
@@ -19,6 +19,7 @@ let review: Review;
19
19
  let studio: Person;
20
20
  let client: Person;
21
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);
@@ -36,43 +37,403 @@ beforeEach(async () => {
36
37
  name: 'CLI',
37
38
  team: 'Studio',
38
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
- studio = (await review.invite(root, { name: 'Ed', team: 'Studio', kind: 'studio' })).person;
44
- const r = await review.invite(studio, { name: 'Rae', team: 'Acme' });
46
+ studio = (await review.addPerson(root, { name: 'Ed', team: 'Studio', kind: 'studio' })).person;
47
+ const r = await review.addPerson(studio, { name: 'Rae', team: 'Acme' });
45
48
  client = r.person;
46
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(clientToken))?.id, client.id);
51
- const { token } = await review.reissue(studio, client.id);
52
- assert.equal(await review.acceptInvite(clientToken), null);
53
- assert.equal((await review.acceptInvite(token))?.epoch, 1);
54
- await review.revoke(studio, client.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);
59
+ });
60
+
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
+ );
56
67
  });
57
68
 
58
- test('a reissue or revoke ends every session signed with the old epoch', async () => {
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);
59
74
  assert.ok(await review.sessionPerson(client.id, 0));
60
- await review.reissue(studio, client.id);
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);
61
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);
62
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 () => {
63
91
  await review.revoke(studio, client.id);
64
- assert.equal(await review.sessionPerson(client.id, 2), null);
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(clientToken), 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 Studio manages people, and nobody revokes themselves', async () => {
73
- await rejects(review.invite(client, { 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');
74
429
  await rejects(review.listPeople(client), 'forbidden');
75
430
  await rejects(review.listPeople(null), 'unauthorized');
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');
76
437
  await rejects(review.revoke(studio, studio.id), 'invalid');
77
438
  assert.deepEqual(
78
439
  (await review.listPeople(studio)).map((p) => p.name),
@@ -93,13 +454,14 @@ test('a comment, its replies, resolving and deleting the thread', async () => {
93
454
 
94
455
  test('a new person is a Client unless named Studio, and any other kind is invalid', async () => {
95
456
  assert.equal(client.kind, 'client');
457
+ assert.equal(client.email, null);
96
458
  assert.equal(studio.kind, 'studio');
97
- await rejects(review.invite(studio, { name: 'X', team: 'Acme', kind: 'editor' }), 'invalid');
459
+ await rejects(review.addPerson(studio, { name: 'X', team: 'Acme', kind: 'editor' }), 'invalid');
98
460
  });
99
461
 
100
- test('a Client resolves and reopens anyone’s thread, but deletes only their own', async () => {
101
- const c = await review.add(studio, { body: 'Spacing off', anchor });
102
- const other = (await review.invite(studio, { name: 'Oz', team: 'Acme' })).person;
462
+ test('a Client resolves and reopens anyone’s client-facing thread, but deletes only their own', async () => {
463
+ const c = await review.add(studio, { body: 'Spacing off', anchor, audience: 'client' });
464
+ const other = (await review.addPerson(studio, { name: 'Oz', team: 'Acme' })).person;
103
465
  const done = await review.resolve(other, { prototype: 'sample-list', id: c.id, resolved: true });
104
466
  assert.equal(done.resolvedBy?.id, other.id);
105
467
  const open = await review.resolve(client, { prototype: 'sample-list', id: c.id, resolved: false });
@@ -116,20 +478,28 @@ test('Studio resolves and deletes anyone’s thread', async () => {
116
478
  assert.deepEqual(await review.remove(studio, { prototype: 'sample-list', id: c.id }), [c.id]);
117
479
  });
118
480
 
119
- test('records under the old key prefix are never read', async () => {
481
+ test('records under an older key prefix are never read, 0.6 links included', async () => {
120
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
+ );
121
487
  const kv = memoryKV({
122
488
  hashes: {
123
- 'hub:people': { old: JSON.stringify(old) },
124
- 'hub:comments:sample-list': { x: JSON.stringify({ id: 'x', thread: 'x' }) },
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' }) },
125
492
  },
126
- sets: { 'hub:prototypes': ['sample-list'] },
493
+ sets: { 'hub2:prototypes': ['sample-list'] },
127
494
  });
128
495
  const fresh = createReview(kv);
129
- assert.ok(KEY.people.startsWith('hub2:'));
496
+ assert.ok(KEY.people.startsWith('hub3:'));
130
497
  assert.equal(await fresh.person('old'), null);
131
- assert.deepEqual(await fresh.allComments(), []);
132
- assert.equal(await fresh.locate('x'), 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(studio), []);
502
+ assert.equal(await fresh.locate(studio, 'x'), null);
133
503
  });
134
504
 
135
505
  test('people edit only their own words; Studio triages anything', async () => {
@@ -157,7 +527,7 @@ test('people edit only their own words; Studio triages anything', async () => {
157
527
  );
158
528
  });
159
529
 
160
- test('a revoked person and a password-only visitor can do nothing', async () => {
530
+ test('a revoked person and a visitor who is not signed in can do nothing', async () => {
161
531
  const gone = await review.revoke(studio, client.id);
162
532
  await rejects(review.add(gone, { body: 'x', anchor }), 'forbidden');
163
533
  await rejects(review.add(null, { body: 'x', anchor }), 'unauthorized');
@@ -198,6 +568,172 @@ test('triage runs on add, and a failing triager never loses the comment', async
198
568
 
199
569
  test('locate finds a comment by id alone', async () => {
200
570
  const c = await review.add(client, { body: 'Here', anchor });
201
- assert.equal((await review.locate(c.id))?.prototype, 'sample-list');
202
- assert.equal(await review.locate('nope'), null);
571
+ assert.equal((await review.locate(studio, c.id))?.prototype, 'sample-list');
572
+ assert.equal((await review.locate(client, c.id))?.prototype, 'sample-list');
573
+ assert.equal(await review.locate(studio, 'nope'), null);
574
+ await rejects(review.locate(null, c.id), 'unauthorized');
575
+ });
576
+
577
+ // Slice 4: internal comments. One internal thread with a Studio reply, one
578
+ // client-facing thread with replies from both kinds.
579
+ async function twoThreads() {
580
+ const at = { prototype: 'sample-list' };
581
+ const internal = await review.add(studio, { body: 'Spacing token is wrong', anchor });
582
+ const internalReply = await review.reply(studio, { ...at, thread: internal.id, body: 'Fix before Friday' });
583
+ const shared = await review.add(client, { body: 'Header feels heavy', anchor });
584
+ const studioReply = await review.reply(studio, { ...at, thread: shared.id, body: 'Trying a lighter one' });
585
+ const clientReply = await review.reply(client, { ...at, thread: shared.id, body: 'Thanks' });
586
+ return { internal, internalReply, shared, studioReply, clientReply };
587
+ }
588
+
589
+ test('audience: Studio’s thread is internal, a Client’s client-facing, and replies follow the thread', async () => {
590
+ const t = await twoThreads();
591
+ assert.deepEqual(
592
+ [t.internal, t.internalReply, t.shared, t.studioReply, t.clientReply].map((c) => c.audience),
593
+ ['internal', 'internal', 'client', 'client', 'client'],
594
+ );
595
+ const asked = await review.add(studio, { body: 'For the client', anchor, audience: 'client' });
596
+ assert.equal(asked.audience, 'client');
597
+ assert.equal(
598
+ (await review.add(studio, { body: 'Ours', anchor, audience: 'internal' })).audience,
599
+ 'internal',
600
+ );
601
+ await rejects(review.add(studio, { body: 'x', anchor, audience: 'everyone' }), 'invalid');
602
+ });
603
+
604
+ test('a Client cannot make an internal thread by asking for one', async () => {
605
+ const c = await review.add(client, { body: 'Mine', anchor, audience: 'internal' });
606
+ assert.equal(c.audience, 'client');
607
+ assert.ok((await review.listComments(client, 'sample-list')).some((x) => x.id === c.id));
608
+ });
609
+
610
+ test('a Client’s list holds no internal thread or reply; Studio’s holds all', async () => {
611
+ const t = await twoThreads();
612
+ const ids = (cs: { id: string }[]) => cs.map((c) => c.id);
613
+ assert.deepEqual(ids(await review.listComments(client, 'sample-list')), [
614
+ t.shared.id,
615
+ t.studioReply.id,
616
+ t.clientReply.id,
617
+ ]);
618
+ assert.equal((await review.listComments(studio, 'sample-list')).length, 5);
619
+ assert.deepEqual(ids(await review.allComments(client)), [t.shared.id, t.studioReply.id, t.clientReply.id]);
620
+ assert.equal((await review.allComments(studio)).length, 5);
621
+ });
622
+
623
+ test('Client view gives Studio exactly a Client’s list, and never widens a Client’s', async () => {
624
+ await twoThreads();
625
+ const asClient = await review.listComments(client, 'sample-list');
626
+ assert.deepEqual(await review.listComments(studio, 'sample-list', 'client'), asClient);
627
+ assert.deepEqual(await review.listComments(client, 'sample-list', 'studio'), asClient);
628
+ assert.equal((await review.listComments(studio, 'sample-list', 'studio')).length, 5);
629
+ await rejects(review.listComments(studio, 'sample-list', 'editor'), 'invalid');
630
+ });
631
+
632
+ test('a Client acting on an internal id is told it does not exist, by every op', async () => {
633
+ const t = await twoThreads();
634
+ const at = { prototype: 'sample-list' };
635
+ for (const id of [t.internal.id, t.internalReply.id]) {
636
+ await rejects(review.reply(client, { ...at, thread: id, body: 'x' }), 'not_found');
637
+ await rejects(review.edit(client, { ...at, id, body: 'x' }), 'not_found');
638
+ await rejects(review.remove(client, { ...at, id }), 'not_found');
639
+ await rejects(review.resolve(client, { ...at, id, resolved: true }), 'not_found');
640
+ await rejects(review.resolve(client, { ...at, id, resolved: false }), 'not_found');
641
+ // Triage is Studio's whatever the id, so a Client is refused before any lookup.
642
+ await rejects(review.retriage(client, { ...at, id, triage: { kind: 'bug' } }), 'forbidden');
643
+ assert.equal(await review.locate(client, id), null);
644
+ assert.ok(await review.locate(studio, id));
645
+ }
646
+ // The same answer as an id that is not there at all.
647
+ await rejects(review.edit(client, { ...at, id: 'nope', body: 'x' }), 'not_found');
648
+ // Nothing changed.
649
+ const after = await review.listComments(studio, 'sample-list');
650
+ assert.equal(after.length, 5);
651
+ assert.equal(after.find((c) => c.id === t.internal.id)?.status, 'open');
652
+ assert.equal(after.find((c) => c.id === t.internalReply.id)?.body, 'Fix before Friday');
653
+ });
654
+
655
+ test('a signed-out caller is refused before any comment is looked up', async () => {
656
+ const t = await twoThreads();
657
+ const at = { prototype: 'sample-list' };
658
+ for (const id of [t.internal.id, 'nope']) {
659
+ await rejects(review.edit(null, { ...at, id, body: 'x' }), 'unauthorized');
660
+ await rejects(review.remove(null, { ...at, id }), 'unauthorized');
661
+ await rejects(review.resolve(null, { ...at, id, resolved: true }), 'unauthorized');
662
+ await rejects(review.reply(null, { ...at, thread: id, body: 'x' }), 'unauthorized');
663
+ }
664
+ await rejects(review.listComments(null, 'sample-list'), 'unauthorized');
665
+ await rejects(review.allComments(null), 'unauthorized');
666
+ });
667
+
668
+ test('a Client still works on client-facing threads, Studio’s replies included', async () => {
669
+ const t = await twoThreads();
670
+ const at = { prototype: 'sample-list' };
671
+ assert.equal((await review.resolve(client, { ...at, id: t.shared.id, resolved: true })).status, 'resolved');
672
+ assert.equal(
673
+ (await review.edit(client, { ...at, id: t.clientReply.id, body: 'Thank you' })).body,
674
+ 'Thank you',
675
+ );
676
+ await rejects(review.edit(client, { ...at, id: t.studioReply.id, body: 'x' }), 'forbidden');
677
+ await rejects(review.remove(client, { ...at, id: t.studioReply.id }), 'forbidden');
678
+ assert.deepEqual(await review.remove(client, { ...at, id: t.clientReply.id }), [t.clientReply.id]);
679
+ });
680
+
681
+ test('numbers are fixed: a delete or a hidden thread moves no other number, and none is reused', async () => {
682
+ const t = await twoThreads();
683
+ const third = await review.add(client, { body: 'Third', anchor });
684
+ assert.deepEqual([t.internal.number, t.shared.number, third.number], [1, 2, 3]);
685
+ // Replies carry their thread's number.
686
+ assert.deepEqual([t.internalReply.number, t.studioReply.number], [1, 2]);
687
+ // A Client sees 2 and 3: the internal thread is hidden and leaves a gap.
688
+ const roots = (cs: { id: string; thread: string; number: number }[]) =>
689
+ cs.filter((c) => c.id === c.thread).map((c) => c.number);
690
+ assert.deepEqual(roots(await review.listComments(client, 'sample-list')), [2, 3]);
691
+ await review.remove(studio, { prototype: 'sample-list', id: t.shared.id });
692
+ assert.deepEqual(roots(await review.listComments(studio, 'sample-list')), [1, 3]);
693
+ assert.deepEqual(roots(await review.listComments(client, 'sample-list')), [3]);
694
+ assert.equal((await review.add(client, { body: 'Fourth', anchor })).number, 4);
695
+ });
696
+
697
+ test('numbers count per prototype', async () => {
698
+ const a = await review.add(client, { body: 'A1', anchor });
699
+ const b = await review.add(client, { body: 'B1', anchor: { ...anchor, prototype: 'other' } });
700
+ const a2 = await review.add(studio, { body: 'A2', anchor });
701
+ assert.deepEqual([a.number, b.number, a2.number], [1, 1, 2]);
702
+ });
703
+
704
+ test('a stored comment without an audience is internal, and one without a number has none', async () => {
705
+ const kv = memoryKV();
706
+ const r = createReview(kv);
707
+ const root = { ...studio, id: 'cli' };
708
+ const rae = (await r.addPerson(root, { name: 'Rae', team: 'Acme' })).person;
709
+ const legacy = {
710
+ id: 'old',
711
+ thread: 'old',
712
+ author: { id: rae.id, name: 'Rae', team: 'Acme' },
713
+ body: 'From before audiences',
714
+ createdAt: '2030-01-01T00:00:00Z',
715
+ editedAt: null,
716
+ anchor,
717
+ status: 'open',
718
+ resolvedBy: null,
719
+ triage: null,
720
+ };
721
+ await kv.sadd(KEY.prototypes, 'sample-list');
722
+ await kv.hset(KEY.comments('sample-list'), 'old', JSON.stringify(legacy));
723
+ await kv.hset(
724
+ KEY.comments('sample-list'),
725
+ 'odd',
726
+ JSON.stringify({ ...legacy, id: 'odd', thread: 'odd', audience: 'everyone' }),
727
+ );
728
+ const seen = await r.listComments(root, 'sample-list');
729
+ assert.deepEqual(
730
+ seen.map((c) => [c.id, c.audience, c.number]),
731
+ [
732
+ ['odd', 'internal', 0],
733
+ ['old', 'internal', 0],
734
+ ],
735
+ );
736
+ assert.deepEqual(await r.listComments(rae, 'sample-list'), []);
737
+ assert.equal(await r.locate(rae, 'old'), null);
738
+ await rejects(r.edit(rae, { prototype: 'sample-list', id: 'old', body: 'x' }), 'not_found');
203
739
  });