@milesroxas/design-hub 0.7.0 → 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.
@@ -10,6 +10,13 @@ import { SLUG } from '../feature.ts';
10
10
  export const PERSON_KINDS = ['studio', 'client'] as const;
11
11
  export type PersonKind = (typeof PERSON_KINDS)[number];
12
12
 
13
+ /**
14
+ * Who a thread is for. Internal stays inside the studio; client-facing is
15
+ * read by everyone. Fixed when the thread is made: no op changes it.
16
+ */
17
+ const AUDIENCES = ['internal', 'client'] as const;
18
+ export type Audience = (typeof AUDIENCES)[number];
19
+
13
20
  /** How a comment is triaged: something broken, a design opinion, or the words. */
14
21
  export const KINDS = ['bug', 'taste', 'copy'] as const;
15
22
  export type Kind = (typeof KINDS)[number];
@@ -109,8 +116,27 @@ export type Comment = {
109
116
  status: 'open' | 'resolved';
110
117
  resolvedBy: Author | null;
111
118
  triage: Triage | null;
119
+ /** The thread's audience. Set on the root when it is made and copied onto every reply; never changed. */
120
+ audience: Audience;
121
+ /**
122
+ * The thread's number on its prototype, from 1, copied onto every reply. Never reused, so a
123
+ * deleted or hidden thread moves no other number. 0 for a thread stored before numbers existed.
124
+ */
125
+ number: number;
112
126
  };
113
127
 
128
+ /**
129
+ * A stored comment as the rules read it. A record with no audience, or one
130
+ * that is not a known value, is internal: a missing field never shows a
131
+ * Client anything. A missing or broken number reads as 0, no number.
132
+ */
133
+ export function readComment(json: string): Comment {
134
+ const c = JSON.parse(json) as Comment;
135
+ const audience: Audience = c.audience === 'client' ? 'client' : 'internal';
136
+ const number = Number.isInteger(c.number) && c.number > 0 ? c.number : 0;
137
+ return { ...c, audience, number };
138
+ }
139
+
114
140
  const isText = (v: unknown) => typeof v === 'string';
115
141
  const isTextOrNull = (v: unknown) => v === null || typeof v === 'string';
116
142
 
@@ -189,6 +215,22 @@ export function checkPersonInput(input: unknown): Checked<PersonInput> {
189
215
  : { ok: true, value: { name, team, kind: kind as PersonKind, access: access as Access | null } };
190
216
  }
191
217
 
218
+ /** A new thread's audience as asked: one of AUDIENCES, or null when none is given. */
219
+ export function checkAudience(input: unknown): Checked<Audience | null> {
220
+ if (input === undefined || input === null) return { ok: true, value: null };
221
+ return AUDIENCES.includes(input as Audience)
222
+ ? { ok: true, value: input as Audience }
223
+ : { ok: false, problems: [`audience: one of ${AUDIENCES.join(', ')}`] };
224
+ }
225
+
226
+ /** Whose view of the comments is asked for: a person kind, or null for the caller's own. */
227
+ export function checkViewAs(input: unknown): Checked<PersonKind | null> {
228
+ if (input === undefined || input === null) return { ok: true, value: null };
229
+ return PERSON_KINDS.includes(input as PersonKind)
230
+ ? { ok: true, value: input as PersonKind }
231
+ : { ok: false, problems: [`viewAs: one of ${PERSON_KINDS.join(', ')}`] };
232
+ }
233
+
192
234
  export function checkBody(body: unknown): Checked<string> {
193
235
  const problems: string[] = [];
194
236
  const value = text(body, 'body', LIMITS.body, problems);
@@ -1,7 +1,7 @@
1
1
  import assert from 'node:assert/strict';
2
2
  import { test } from 'node:test';
3
- import type { Comment, Person, PersonKind } from './model.ts';
4
- import { type Action, can } from './policy.ts';
3
+ import type { Audience, Comment, Person, PersonKind } from './model.ts';
4
+ import { type Action, audienceFor, can, viewOf, visibleTo } from './policy.ts';
5
5
 
6
6
  const person = (id: string, kind: PersonKind, revokedAt: string | null = null): Person => ({
7
7
  id,
@@ -15,9 +15,9 @@ const person = (id: string, kind: PersonKind, revokedAt: string | null = null):
15
15
  epoch: 0,
16
16
  });
17
17
 
18
- const thread = (author: string): Comment => ({
19
- id: 'c',
20
- thread: 'c',
18
+ const comment = (id: string, thread: string, author: string, audience: Audience): Comment => ({
19
+ id,
20
+ thread,
21
21
  author: { id: author, name: author, team: 'Acme' },
22
22
  body: 'x',
23
23
  createdAt: '',
@@ -26,37 +26,114 @@ const thread = (author: string): Comment => ({
26
26
  status: 'open',
27
27
  resolvedBy: null,
28
28
  triage: null,
29
+ audience,
30
+ number: 1,
29
31
  });
32
+ const thread = (author: string, audience: Audience = 'client') => comment('c', 'c', author, audience);
30
33
 
31
- // The rule table, one row per action: [Studio own, Studio other's, Client own, Client other's].
32
- const TABLE: [Action, boolean, boolean, boolean, boolean][] = [
33
- ['read', true, true, true, true],
34
- ['comment', true, true, true, true],
35
- ['edit', true, false, true, false],
36
- ['resolve', true, true, true, true],
37
- ['delete', true, true, true, false],
38
- ['triage', true, true, false, false],
39
- ['people', true, true, false, false],
34
+ // The rule table, one row per action and thread audience:
35
+ // [Studio own, Studio other's, Client own, Client other's].
36
+ const TABLE: [Action, Audience, boolean, boolean, boolean, boolean][] = [
37
+ ['read', 'client', true, true, true, true],
38
+ ['read', 'internal', true, true, false, false],
39
+ ['comment', 'client', true, true, true, true],
40
+ ['comment', 'internal', true, true, false, false],
41
+ ['edit', 'client', true, false, true, false],
42
+ ['edit', 'internal', true, false, false, false],
43
+ ['resolve', 'client', true, true, true, true],
44
+ ['resolve', 'internal', true, true, false, false],
45
+ ['delete', 'client', true, true, true, false],
46
+ ['delete', 'internal', true, true, false, false],
47
+ ['triage', 'client', true, true, false, false],
48
+ ['triage', 'internal', true, true, false, false],
49
+ ['people', 'client', true, true, false, false],
50
+ ['people', 'internal', true, true, false, false],
40
51
  ];
41
52
 
42
53
  const studio = person('s', 'studio');
43
54
  const client = person('c', 'client');
44
55
 
45
- for (const [action, studioOwn, studioOther, clientOwn, clientOther] of TABLE) {
46
- test(`${action}: Studio and Client, own and another's`, () => {
47
- assert.equal(can(studio, action, thread(studio.id)), studioOwn, 'Studio, own');
48
- assert.equal(can(studio, action, thread('someone')), studioOther, "Studio, another's");
49
- assert.equal(can(client, action, thread(client.id)), clientOwn, 'Client, own');
50
- assert.equal(can(client, action, thread('someone')), clientOther, "Client, another's");
56
+ for (const [action, audience, studioOwn, studioOther, clientOwn, clientOther] of TABLE) {
57
+ test(`${action} on a ${audience} thread: Studio and Client, own and another's`, () => {
58
+ assert.equal(can(studio, action, thread(studio.id, audience)), studioOwn, 'Studio, own');
59
+ assert.equal(can(studio, action, thread('someone', audience)), studioOther, "Studio, another's");
60
+ assert.equal(can(client, action, thread(client.id, audience)), clientOwn, 'Client, own');
61
+ assert.equal(can(client, action, thread('someone', audience)), clientOther, "Client, another's");
51
62
  });
52
63
 
53
- test(`${action}: nobody signed in and a revoked person may not`, () => {
64
+ test(`${action} on a ${audience} thread: nobody signed in and a revoked person may not`, () => {
54
65
  for (const kind of ['studio', 'client'] as const) {
55
66
  const gone = person('r', kind, '2030-01-01T00:00:00Z');
56
- assert.equal(can(gone, action, thread(gone.id)), false, `revoked ${kind}, own`);
57
- assert.equal(can(gone, action, thread('someone')), false, `revoked ${kind}, another's`);
67
+ assert.equal(can(gone, action, thread(gone.id, audience)), false, `revoked ${kind}, own`);
68
+ assert.equal(can(gone, action, thread('someone', audience)), false, `revoked ${kind}, another's`);
58
69
  }
59
- assert.equal(can(null, action, thread('someone')), false, 'null');
70
+ assert.equal(can(null, action, thread('someone', audience)), false, 'null');
60
71
  assert.equal(can(null, action), false, 'null, no subject');
61
72
  });
62
73
  }
74
+
75
+ test('with no thread, both kinds read and start one; only Studio triages and manages people', () => {
76
+ for (const action of ['read', 'comment'] as const) {
77
+ assert.equal(can(studio, action), true);
78
+ assert.equal(can(client, action), true);
79
+ }
80
+ for (const action of ['triage', 'people'] as const) {
81
+ assert.equal(can(studio, action), true);
82
+ assert.equal(can(client, action), false);
83
+ }
84
+ });
85
+
86
+ test('a reply is judged by its own author, and inside its thread’s audience', () => {
87
+ const root = thread(studio.id, 'client');
88
+ const mine = comment('r', 'c', client.id, 'client');
89
+ assert.equal(can(client, 'edit', root, mine), true);
90
+ assert.equal(can(client, 'delete', root, mine), true);
91
+ assert.equal(can(studio, 'edit', root, mine), false);
92
+ assert.equal(can(studio, 'delete', root, mine), true);
93
+ // A client-facing reply in an internal thread, or an internal reply in a client-facing one: never a Client's.
94
+ assert.equal(can(client, 'edit', thread(studio.id, 'internal'), mine), false);
95
+ assert.equal(can(client, 'read', root, comment('r', 'c', client.id, 'internal')), false);
96
+ });
97
+
98
+ test('a record without an audience is internal to the rules', () => {
99
+ const legacy = { ...thread(client.id), audience: undefined } as unknown as Comment;
100
+ assert.equal(can(client, 'read', legacy), false);
101
+ assert.equal(can(studio, 'read', legacy), true);
102
+ assert.deepEqual(visibleTo('client', [legacy]), []);
103
+ });
104
+
105
+ test('visibleTo: Studio sees everything; a Client, client-facing threads and their client-facing replies', () => {
106
+ const all = [
107
+ comment('i', 'i', 's', 'internal'),
108
+ comment('i1', 'i', 's', 'internal'),
109
+ comment('k', 'k', 'c', 'client'),
110
+ comment('k1', 'k', 's', 'client'),
111
+ comment('k2', 'k', 'c', 'client'),
112
+ // Nothing writes these, but a Client still never gets them.
113
+ comment('odd', 'k', 's', 'internal'),
114
+ comment('stray', 'i', 's', 'client'),
115
+ comment('orphan', 'gone', 'c', 'client'),
116
+ ];
117
+ assert.deepEqual(visibleTo('studio', all), all);
118
+ assert.deepEqual(
119
+ visibleTo('client', all).map((c) => c.id),
120
+ ['k', 'k1', 'k2'],
121
+ );
122
+ });
123
+
124
+ test('viewOf: Studio may narrow to a Client’s view; a Client is never widened', () => {
125
+ assert.equal(viewOf(studio), 'studio');
126
+ assert.equal(viewOf(studio, 'studio'), 'studio');
127
+ assert.equal(viewOf(studio, 'client'), 'client');
128
+ assert.equal(viewOf(client), 'client');
129
+ assert.equal(viewOf(client, 'studio'), 'client');
130
+ assert.equal(viewOf(null), 'client');
131
+ });
132
+
133
+ test('audienceFor: a Client’s thread is client-facing; Studio’s is internal unless asked', () => {
134
+ assert.equal(audienceFor(client, null), 'client');
135
+ assert.equal(audienceFor(client, 'internal'), 'client');
136
+ assert.equal(audienceFor(studio, null), 'internal');
137
+ assert.equal(audienceFor(studio, 'client'), 'client');
138
+ assert.equal(audienceFor(studio, 'internal'), 'internal');
139
+ });
@@ -1,19 +1,30 @@
1
1
  // Who may do what. Studio runs the review: it adds and revokes people,
2
2
  // deletes any comment and corrects triage. A Client comments, replies, edits
3
- // and deletes what they wrote, and resolves or reopens any thread, because
4
- // clients work alongside others in their own organisation. A revoked person,
5
- // or a visitor who is not signed in, may do nothing here.
6
- import type { Comment, Person } from './model.ts';
3
+ // and deletes what they wrote, and resolves or reopens any client-facing
4
+ // thread, because clients work alongside others in their own organisation.
5
+ // An internal thread does not exist for a Client: every action on one is
6
+ // refused. A revoked person, or a visitor who is not signed in, may do
7
+ // nothing here.
8
+ import type { Audience, Comment, Person, PersonKind } from './model.ts';
7
9
 
8
10
  export type Action = 'read' | 'comment' | 'edit' | 'delete' | 'resolve' | 'triage' | 'people';
9
11
 
10
12
  /** Only what the rules read, so the browser's signed-in person (no epoch) is checked by the same rules. */
11
13
  type Actor = Pick<Person, 'id' | 'kind' | 'revokedAt'>;
12
14
 
13
- export function can(actor: Actor | null, action: Action, subject?: Comment): boolean {
15
+ /** Whether a comment reaches a Client. Only the exact value counts, so a record without one stays inside. */
16
+ const clientFacing = (c: Pick<Comment, 'audience'>) => c.audience === 'client';
17
+
18
+ /**
19
+ * `root` is the thread acted on, `subject` the comment in it when that is a
20
+ * reply (edit or delete one); without `subject`, the root is the subject.
21
+ * Without a root, the action is on no thread: listing, or a new thread.
22
+ */
23
+ export function can(actor: Actor | null, action: Action, root?: Comment, subject?: Comment): boolean {
14
24
  if (!actor || actor.revokedAt) return false;
15
25
  const studio = actor.kind === 'studio';
16
- const own = subject?.author.id === actor.id;
26
+ if (!studio && [root, subject].some((c) => c && !clientFacing(c))) return false;
27
+ const own = (subject ?? root)?.author.id === actor.id;
17
28
  switch (action) {
18
29
  case 'read':
19
30
  case 'comment':
@@ -28,3 +39,28 @@ export function can(actor: Actor | null, action: Action, subject?: Comment): boo
28
39
  return studio;
29
40
  }
30
41
  }
42
+
43
+ /**
44
+ * The view a person gets: their own kind, or a narrower one they ask for.
45
+ * Studio may look as a Client does (Client view); a Client asking for
46
+ * Studio's still gets a Client's, so the view is never wider than the person.
47
+ */
48
+ export function viewOf(actor: Pick<Person, 'kind'> | null, viewAs?: PersonKind | null): PersonKind {
49
+ return actor?.kind === 'studio' && viewAs !== 'client' ? 'studio' : 'client';
50
+ }
51
+
52
+ /**
53
+ * The comments a view may see. Studio sees all. A Client sees a client-facing
54
+ * thread and its client-facing replies; a reply whose thread is not among
55
+ * them is not seen either.
56
+ */
57
+ export function visibleTo(viewAs: PersonKind, comments: Comment[]): Comment[] {
58
+ if (viewAs === 'studio') return comments;
59
+ const roots = new Set(comments.filter((c) => c.thread === c.id && clientFacing(c)).map((c) => c.id));
60
+ return comments.filter((c) => clientFacing(c) && roots.has(c.thread));
61
+ }
62
+
63
+ /** A new thread's audience: a Client's is always client-facing; Studio's is internal unless Studio asks otherwise. */
64
+ export function audienceFor(actor: Pick<Person, 'kind'>, asked: Audience | null): Audience {
65
+ return actor.kind === 'studio' ? (asked ?? 'internal') : 'client';
66
+ }
@@ -459,8 +459,8 @@ test('a new person is a Client unless named Studio, and any other kind is invali
459
459
  await rejects(review.addPerson(studio, { name: 'X', team: 'Acme', kind: 'editor' }), 'invalid');
460
460
  });
461
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 });
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
464
  const other = (await review.addPerson(studio, { name: 'Oz', team: 'Acme' })).person;
465
465
  const done = await review.resolve(other, { prototype: 'sample-list', id: c.id, resolved: true });
466
466
  assert.equal(done.resolvedBy?.id, other.id);
@@ -498,8 +498,8 @@ test('records under an older key prefix are never read, 0.6 links included', asy
498
498
  assert.equal(await fresh.sessionPerson('old', 0), null);
499
499
  assert.equal(await fresh.peekLink(token), null);
500
500
  assert.equal(await fresh.useLink(token), null);
501
- assert.deepEqual(await fresh.allComments(), []);
502
- assert.equal(await fresh.locate('x'), null);
501
+ assert.deepEqual(await fresh.allComments(studio), []);
502
+ assert.equal(await fresh.locate(studio, 'x'), null);
503
503
  });
504
504
 
505
505
  test('people edit only their own words; Studio triages anything', async () => {
@@ -568,6 +568,172 @@ test('triage runs on add, and a failing triager never loses the comment', async
568
568
 
569
569
  test('locate finds a comment by id alone', async () => {
570
570
  const c = await review.add(client, { body: 'Here', anchor });
571
- assert.equal((await review.locate(c.id))?.prototype, 'sample-list');
572
- 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');
573
739
  });