@milesroxas/design-hub 0.9.0 → 0.11.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 (150) hide show
  1. package/README.md +13 -11
  2. package/core/controls.test.ts +176 -0
  3. package/core/controls.ts +195 -0
  4. package/core/files.test.ts +76 -0
  5. package/core/files.ts +131 -0
  6. package/core/prototype.test.ts +124 -7
  7. package/core/prototype.ts +133 -24
  8. package/core/review/model.ts +144 -2
  9. package/core/review/policy.test.ts +79 -22
  10. package/core/review/policy.ts +36 -8
  11. package/core/review/service.test.ts +1525 -322
  12. package/core/review/service.ts +810 -183
  13. package/core/review/store.test.ts +91 -0
  14. package/core/review/store.ts +143 -26
  15. package/dist/core/controls.d.ts +55 -0
  16. package/dist/core/controls.js +174 -0
  17. package/dist/core/files.d.ts +39 -0
  18. package/dist/core/files.js +107 -0
  19. package/dist/core/prototype.d.ts +61 -12
  20. package/dist/core/prototype.js +105 -26
  21. package/dist/core/review/model.d.ts +87 -2
  22. package/dist/core/review/model.js +97 -1
  23. package/dist/core/review/policy.d.ts +11 -4
  24. package/dist/core/review/policy.js +14 -2
  25. package/dist/core/review/service.d.ts +222 -43
  26. package/dist/core/review/service.js +727 -182
  27. package/dist/core/review/store.d.ts +84 -24
  28. package/dist/core/review/store.js +66 -26
  29. package/dist/scripts/bootstrap.d.ts +4 -34
  30. package/dist/scripts/bootstrap.js +9 -244
  31. package/dist/scripts/cli.js +6 -0
  32. package/dist/scripts/client.d.ts +45 -0
  33. package/dist/scripts/client.js +320 -0
  34. package/dist/scripts/commands.js +6 -0
  35. package/dist/scripts/comments.d.ts +8 -26
  36. package/dist/scripts/comments.js +27 -14
  37. package/dist/scripts/guard.d.ts +11 -0
  38. package/dist/scripts/guard.js +44 -0
  39. package/dist/scripts/lib/builds.d.ts +40 -0
  40. package/dist/scripts/lib/builds.js +198 -0
  41. package/dist/scripts/lib/cli.js +3 -1
  42. package/dist/scripts/lib/deploy.d.ts +6 -90
  43. package/dist/scripts/lib/deploy.js +24 -137
  44. package/dist/scripts/lib/dns.d.ts +49 -0
  45. package/dist/scripts/lib/dns.js +141 -0
  46. package/dist/scripts/lib/fake-host.d.ts +168 -0
  47. package/dist/scripts/lib/fake-host.js +244 -0
  48. package/dist/scripts/lib/host.d.ts +87 -0
  49. package/dist/scripts/lib/host.js +362 -0
  50. package/dist/scripts/lib/linked.d.ts +51 -0
  51. package/dist/scripts/lib/linked.js +124 -0
  52. package/dist/scripts/lib/names.d.ts +35 -0
  53. package/dist/scripts/lib/names.js +77 -0
  54. package/dist/scripts/lib/review.d.ts +23 -2
  55. package/dist/scripts/lib/review.js +32 -15
  56. package/dist/scripts/lib/rules.d.ts +1 -0
  57. package/dist/scripts/lib/rules.js +1 -0
  58. package/dist/scripts/lib/studio-deps.d.ts +35 -0
  59. package/dist/scripts/lib/studio-deps.js +79 -0
  60. package/dist/scripts/lib/studios.d.ts +70 -0
  61. package/dist/scripts/lib/studios.js +187 -0
  62. package/dist/scripts/lib/update.d.ts +33 -0
  63. package/dist/scripts/lib/update.js +40 -0
  64. package/dist/scripts/lib/vercel.d.ts +1 -23
  65. package/dist/scripts/lib/vercel.js +6 -81
  66. package/dist/scripts/people.d.ts +7 -11
  67. package/dist/scripts/people.js +51 -46
  68. package/dist/scripts/preflight.d.ts +8 -1
  69. package/dist/scripts/preflight.js +36 -19
  70. package/dist/scripts/project.d.ts +86 -0
  71. package/dist/scripts/project.js +141 -0
  72. package/dist/scripts/share.d.ts +105 -30
  73. package/dist/scripts/share.js +82 -154
  74. package/dist/scripts/skills.d.ts +18 -2
  75. package/dist/scripts/skills.js +22 -3
  76. package/dist/scripts/studio.d.ts +75 -0
  77. package/dist/scripts/studio.js +401 -0
  78. package/dist/scripts/upgrade.d.ts +10 -2
  79. package/dist/scripts/upgrade.js +44 -14
  80. package/dist/server/api.d.ts +22 -11
  81. package/dist/server/api.js +125 -44
  82. package/dist/server/blob.d.ts +10 -0
  83. package/dist/server/blob.js +53 -0
  84. package/dist/server/checks.js +2 -2
  85. package/dist/server/dev.js +4 -4
  86. package/dist/server/env.d.ts +2 -0
  87. package/dist/server/env.js +9 -0
  88. package/dist/server/fake-upstash.js +52 -3
  89. package/dist/server/hosts.d.ts +11 -0
  90. package/dist/server/hosts.js +31 -0
  91. package/dist/server/local.d.ts +7 -1
  92. package/dist/server/local.js +10 -1
  93. package/dist/server/mark.d.ts +41 -0
  94. package/dist/server/mark.js +111 -0
  95. package/dist/server/session.d.ts +17 -9
  96. package/dist/server/session.js +22 -45
  97. package/dist/server/upstash.js +20 -0
  98. package/dist/vercel/files.d.ts +11 -0
  99. package/dist/vercel/files.js +68 -0
  100. package/dist/vercel/index.d.ts +4 -3
  101. package/dist/vercel/index.js +12 -8
  102. package/dist/vercel/proxy.d.ts +69 -4
  103. package/dist/vercel/proxy.js +171 -107
  104. package/dist/vite/plugin.d.ts +24 -1
  105. package/dist/vite/plugin.js +46 -2
  106. package/kit/Artboard.tsx +21 -8
  107. package/kit/Space.tsx +4 -3
  108. package/kit/Variants.tsx +193 -65
  109. package/kit/ViewTools.tsx +77 -11
  110. package/kit/audience.ts +85 -0
  111. package/kit/dials.css +59 -0
  112. package/kit/dials.tsx +272 -35
  113. package/kit/harness.css +34 -0
  114. package/kit/prefs.ts +23 -1
  115. package/package.json +3 -2
  116. package/shell/ClientViewNote.tsx +31 -3
  117. package/shell/Guide.tsx +11 -5
  118. package/shell/Index.tsx +32 -10
  119. package/shell/Notice.tsx +1 -1
  120. package/shell/controls.ts +26 -0
  121. package/shell/hub.ts +5 -1
  122. package/shell/main.tsx +33 -6
  123. package/shell/review/Review.tsx +2 -2
  124. package/shell/review/ThreadCard.tsx +5 -5
  125. package/shell/review/client.ts +28 -11
  126. package/shell/review/view.test.ts +19 -0
  127. package/shell/review/view.ts +25 -0
  128. package/shell/shell.css +8 -0
  129. package/shell/studio/ClientHub.tsx +98 -0
  130. package/shell/studio/hub.css +48 -0
  131. package/shell/studio/index.html +14 -0
  132. package/shell/studio/main.tsx +17 -0
  133. package/skills/miles-brand/SKILL.md +11 -1
  134. package/skills/miles-deploy/SKILL.md +121 -64
  135. package/skills/miles-design-hub/SKILL.md +11 -9
  136. package/skills/miles-prototype/SKILL.md +34 -1
  137. package/skills/miles-review/SKILL.md +5 -7
  138. package/skills/miles-worktrees/SKILL.md +3 -3
  139. package/templates/conductor/settings.toml +1 -1
  140. package/templates/studio/.env.example +9 -0
  141. package/templates/studio/.vercelignore +5 -0
  142. package/templates/studio/README.md +84 -0
  143. package/templates/studio/_gitignore +7 -0
  144. package/templates/studio/api/files.ts +4 -0
  145. package/templates/studio/api/review.ts +4 -0
  146. package/templates/studio/build.mjs +4 -0
  147. package/templates/studio/package.json +16 -0
  148. package/templates/studio/proxy.ts +4 -0
  149. package/templates/studio/studio.json +9 -0
  150. package/templates/studio/vercel.json +27 -0
@@ -1,8 +1,8 @@
1
1
  import assert from 'node:assert/strict';
2
2
  import { beforeEach, test } from 'node:test';
3
- import { type Anchor, accessEnd, type Comment, type Person, threadLabel } from './model.ts';
4
- import { createReview, type Review, ReviewError } from './service.ts';
5
- import { KEY, type KV, memoryKV, type Snapshot } from './store.ts';
3
+ import { type Anchor, accessEnd, type Comment, type Person, type Scope, threadLabel } from './model.ts';
4
+ import { createReview, REUSE_SECONDS, type Review, ReviewError } from './service.ts';
5
+ import { type KV, keyOf, memoryKV, type Snapshot } from './store.ts';
6
6
 
7
7
  const anchor: Anchor = {
8
8
  prototype: 'sample-list',
@@ -14,6 +14,9 @@ const anchor: Anchor = {
14
14
  viewport: { width: 1440, height: 900 },
15
15
  };
16
16
 
17
+ /** Where these tests run: client Acme's rebrand. Client B's is another client's scope altogether. */
18
+ const A: Scope = { client: 'acme', project: 'rebrand' };
19
+
17
20
  let clock = 0;
18
21
  let review: Review;
19
22
  let studio: Person;
@@ -24,9 +27,35 @@ const DAY = 86_400_000;
24
27
  const rejects = (p: Promise<unknown>, code: string) =>
25
28
  assert.rejects(p, (e: unknown) => e instanceof ReviewError && e.code === code);
26
29
 
30
+ /**
31
+ * A store in which these clients are live, so ops run in them: an op in a
32
+ * client with no live record answers as an unknown id (`allow` in service.ts).
33
+ */
34
+ const live = (...slugs: string[]): Snapshot => ({
35
+ hashes: {
36
+ [keyOf('studio', 'clients')]: Object.fromEntries(
37
+ slugs.map((slug) => [
38
+ slug,
39
+ JSON.stringify({
40
+ slug,
41
+ host: `${slug}.studio.test`,
42
+ name: slug,
43
+ access: null,
44
+ createdAt: '2030-01-01T00:00:00.000Z',
45
+ archivedAt: null,
46
+ provenAt: null,
47
+ }),
48
+ ]),
49
+ ),
50
+ },
51
+ sets: {},
52
+ });
53
+ /** Clients A and B, live. */
54
+ const LIVE = live('acme', 'bravo');
55
+
27
56
  beforeEach(async () => {
28
57
  clock = Date.parse('2030-01-01T00:00:00Z');
29
- review = createReview(memoryKV(), {
58
+ review = createReview(memoryKV(LIVE), {
30
59
  now: () => {
31
60
  clock += 1000;
32
61
  return new Date(clock);
@@ -37,107 +66,108 @@ beforeEach(async () => {
37
66
  name: 'CLI',
38
67
  team: 'Studio',
39
68
  kind: 'studio',
69
+ client: null,
40
70
  email: null,
41
71
  accessEndsAt: null,
42
72
  createdAt: '',
43
73
  revokedAt: null,
44
74
  epoch: 0,
45
75
  } as Person;
46
- studio = (await review.addPerson(root, { name: 'Ed', team: 'Studio', kind: 'studio' })).person;
47
- const r = await review.addPerson(studio, { name: 'Rae', team: 'Acme' });
76
+ studio = (await review.addPerson(A, root, { name: 'Ed', team: 'Studio', kind: 'studio' })).person;
77
+ const r = await review.addPerson(A, studio, { name: 'Rae', team: 'Acme' });
48
78
  client = r.person;
49
79
  clientToken = r.token;
50
80
  // The prototype these tests comment on is client-facing, so a Client reaches its comments.
51
- await review.setAudience(studio, anchor.prototype, 'client');
81
+ await review.setAudience(A, studio, anchor.prototype, 'client');
52
82
  });
53
83
 
54
84
  test('a link works once, and looking at it uses nothing up', async () => {
55
- assert.equal((await review.peekLink(clientToken))?.id, client.id);
56
- assert.equal((await review.peekLink(clientToken))?.id, client.id);
57
- assert.equal((await review.useLink(clientToken))?.id, client.id);
58
- assert.equal(await review.useLink(clientToken), null);
59
- assert.equal(await review.peekLink(clientToken), null);
60
- assert.equal(await review.useLink('not a token'), null);
85
+ assert.equal((await review.peekLink(A, clientToken))?.id, client.id);
86
+ assert.equal((await review.peekLink(A, clientToken))?.id, client.id);
87
+ assert.equal((await review.useLink(A, clientToken))?.id, client.id);
88
+ assert.equal(await review.useLink(A, clientToken), null);
89
+ assert.equal(await review.peekLink(A, clientToken), null);
90
+ assert.equal(await review.useLink(A, 'not a token'), null);
61
91
  });
62
92
 
63
93
  test('two requests with one link at once sign in one device', async () => {
64
- const both = await Promise.all([review.useLink(clientToken), review.useLink(clientToken)]);
94
+ const both = await Promise.all([review.useLink(A, clientToken), review.useLink(A, clientToken)]);
65
95
  // Either request may win the claim; exactly one does.
66
96
  assert.deepEqual(both.map((p) => p?.id ?? null).sort(), [client.id, null].sort());
67
97
  });
68
98
 
69
99
  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);
100
+ const { token: second } = await review.newLink(A, studio, client.id);
101
+ const { token: third } = await review.newLink(A, studio, client.id);
72
102
  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);
103
+ assert.equal((await review.useLink(A, second))?.id, client.id);
104
+ assert.ok(await review.sessionPerson(A, client.id, 0));
105
+ assert.equal((await review.useLink(A, clientToken))?.epoch, 0);
106
+ assert.equal((await review.useLink(A, third))?.epoch, 0);
77
107
  });
78
108
 
79
109
  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);
110
+ const { token: spare } = await review.newLink(A, studio, client.id);
111
+ const { person, token } = await review.signOutEverywhere(A, studio, client.id);
82
112
  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));
113
+ assert.equal(await review.sessionPerson(A, client.id, 0), null);
114
+ assert.equal(await review.useLink(A, clientToken), null);
115
+ assert.equal(await review.useLink(A, spare), null);
116
+ assert.equal((await review.useLink(A, token))?.epoch, 1);
117
+ assert.ok(await review.sessionPerson(A, client.id, 1));
88
118
  });
89
119
 
90
120
  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');
121
+ await review.revoke(A, studio, client.id);
122
+ assert.equal(await review.sessionPerson(A, client.id, 1), null);
123
+ assert.equal(await review.peekLink(A, clientToken), null);
124
+ await rejects(review.newLink(A, studio, client.id), 'invalid');
125
+ await rejects(review.signOutEverywhere(A, studio, client.id), 'invalid');
126
+ await rejects(review.setAccess(A, studio, client.id, 'open'), 'invalid');
127
+ await rejects(review.newLink(A, studio, 'nobody'), 'not_found');
98
128
  });
99
129
 
100
130
  test('a store failure before the claim leaves the link usable', async () => {
101
- const kv = memoryKV();
131
+ const kv = memoryKV(LIVE);
102
132
  let failPeople = false;
103
133
  const flaky: KV = {
104
134
  ...kv,
105
135
  hget: async (key, field) => {
106
- if (failPeople && key === KEY.people) throw new Error('store down');
136
+ if (failPeople && key === keyOf(A, 'people')) throw new Error('store down');
107
137
  return kv.hget(key, field);
108
138
  },
109
139
  };
110
140
  const r = createReview(flaky);
111
141
  const root = { ...studio, id: 'cli' };
112
- const { token } = await r.addPerson(root, { name: 'Rae', team: 'Acme' });
142
+ const { token } = await r.addPerson(A, root, { name: 'Rae', team: 'Acme' });
113
143
  failPeople = true;
114
- await assert.rejects(r.useLink(token), /store down/);
144
+ await assert.rejects(r.useLink(A, token), /store down/);
115
145
  failPeople = false;
116
- assert.equal((await r.useLink(token))?.name, 'Rae');
146
+ assert.equal((await r.useLink(A, token))?.name, 'Rae');
117
147
  });
118
148
 
119
149
  test('the store never holds a link token in clear', async () => {
120
150
  let seen: Snapshot = { hashes: {}, sets: {} };
121
151
  const r = createReview(
122
- memoryKV(undefined, (snapshot) => {
152
+ memoryKV(LIVE, (snapshot) => {
123
153
  seen = snapshot;
124
154
  }),
125
155
  );
126
156
  const root = { ...studio, id: 'cli' };
127
- const added = await r.addPerson(root, { name: 'Rae', team: 'Acme' });
157
+ const added = await r.addPerson(A, root, { name: 'Rae', team: 'Acme' });
128
158
  const tokens = [
129
159
  added.token,
130
- (await r.newLink(root, added.person.id)).token,
131
- (await r.signOutEverywhere(root, added.person.id)).token,
160
+ (await r.newLink(A, root, added.person.id)).token,
161
+ (await r.signOutEverywhere(A, root, added.person.id)).token,
132
162
  ];
133
163
  const stored = JSON.stringify(seen);
134
- assert.ok(Object.keys(seen.hashes[KEY.links] ?? {}).length > 0);
164
+ assert.ok(Object.keys(seen.hashes[keyOf(A, 'links')] ?? {}).length > 0);
135
165
  for (const token of tokens) assert.equal(stored.includes(token), false);
136
166
  });
137
167
 
138
168
  test('a revoke writes revocation and epoch, never the profile, before it drops their links', async () => {
139
169
  const writes: string[] = [];
140
- const kv = memoryKV();
170
+ const kv = memoryKV(LIVE);
141
171
  const watched: KV = {
142
172
  ...kv,
143
173
  hset: async (key, field, value) => {
@@ -155,10 +185,14 @@ test('a revoke writes revocation and epoch, never the profile, before it drops t
155
185
  };
156
186
  const r = createReview(watched);
157
187
  const root = { ...studio, id: 'cli' };
158
- const { person } = await r.addPerson(root, { name: 'Rae', team: 'Acme' });
188
+ const { person } = await r.addPerson(A, root, { name: 'Rae', team: 'Acme' });
159
189
  writes.length = 0;
160
- await r.revoke(root, person.id);
161
- assert.deepEqual(writes, [`set ${KEY.revoked}`, `incr ${KEY.epochs}`, `del ${KEY.links}`]);
190
+ await r.revoke(A, root, person.id);
191
+ assert.deepEqual(writes, [
192
+ `set ${keyOf(A, 'revoked')}`,
193
+ `incr ${keyOf(A, 'epochs')}`,
194
+ `del ${keyOf(A, 'links')}`,
195
+ ]);
162
196
  });
163
197
 
164
198
  /**
@@ -166,7 +200,7 @@ test('a revoke writes revocation and epoch, never the profile, before it drops t
166
200
  * until the test releases it: a real interleaving, not two calls in a row.
167
201
  */
168
202
  function pausingKV() {
169
- const kv = memoryKV();
203
+ const kv = memoryKV(LIVE);
170
204
  let arm = false;
171
205
  let reached = () => {};
172
206
  let release = () => {};
@@ -179,7 +213,7 @@ function pausingKV() {
179
213
  const store: KV = {
180
214
  ...kv,
181
215
  hset: async (key, field, value) => {
182
- if (arm && key === KEY.people) {
216
+ if (arm && key === keyOf(A, 'people')) {
183
217
  arm = false;
184
218
  reached();
185
219
  await gate;
@@ -194,11 +228,11 @@ async function racedSetAccess(between: (r: Review, root: Person, id: string) =>
194
228
  const { kv, store, armNextProfileWrite, paused, release } = pausingKV();
195
229
  const r = createReview(store);
196
230
  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);
231
+ const { person, token } = await r.addPerson(A, root, { name: 'Rae', team: 'Acme' });
232
+ const { token: spare } = await r.newLink(A, root, person.id);
199
233
  armNextProfileWrite();
200
234
  // setAccess reads the person (active, epoch 0), then waits at its write.
201
- const stale = r.setAccess(root, person.id, 'open');
235
+ const stale = r.setAccess(A, root, person.id, 'open');
202
236
  await paused;
203
237
  await between(r, root, person.id);
204
238
  release();
@@ -207,31 +241,33 @@ async function racedSetAccess(between: (r: Review, root: Person, id: string) =>
207
241
  }
208
242
 
209
243
  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);
244
+ const { r, kv, person, token, spare, answer } = await racedSetAccess((r, root, id) =>
245
+ r.revoke(A, root, id),
246
+ );
247
+ const now = await r.person(A, person.id);
212
248
  assert.ok(now?.revokedAt);
213
249
  assert.equal(now?.accessEndsAt, null);
214
250
  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);
251
+ assert.equal(await r.sessionPerson(A, person.id, 0), null);
252
+ assert.equal(await r.sessionPerson(A, person.id, now?.epoch ?? 0), null);
253
+ assert.equal(await r.peekLink(A, token), null);
254
+ assert.equal(await r.peekLink(A, spare), null);
255
+ assert.deepEqual(Object.keys(await kv.hgetall(keyOf(A, 'links'))), []);
256
+ assert.ok((await r.listPeople(A, studio)).find((p) => p.id === person.id)?.revokedAt);
221
257
  });
222
258
 
223
259
  test('a stale setAccess written after a sign out everywhere keeps the new epoch', async () => {
224
260
  let fresh = '';
225
261
  const { r, person, spare } = await racedSetAccess(async (r, root, id) => {
226
- fresh = (await r.signOutEverywhere(root, id)).token;
262
+ fresh = (await r.signOutEverywhere(A, root, id)).token;
227
263
  });
228
- const now = await r.person(person.id);
264
+ const now = await r.person(A, person.id);
229
265
  assert.equal(now?.epoch, 1);
230
266
  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);
267
+ assert.equal(await r.sessionPerson(A, person.id, 0), null);
268
+ assert.ok(await r.sessionPerson(A, person.id, 1));
269
+ assert.equal(await r.peekLink(A, spare), null);
270
+ assert.equal((await r.useLink(A, fresh))?.epoch, 1);
235
271
  });
236
272
 
237
273
  /** A pause point: the first call to `method` on `key` waits there until released. */
@@ -274,20 +310,20 @@ function pausing(kv: KV, points: Record<string, { method: 'hdel' | 'hgetall'; ke
274
310
  }
275
311
 
276
312
  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)],
313
+ ['a revoke', (r: Review, root: Person, id: string) => r.revoke(A, root, id)],
314
+ ['a sign out everywhere', (r: Review, root: Person, id: string) => r.signOutEverywhere(A, root, id)],
279
315
  ] as const)
280
316
  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 },
317
+ const { store, arm, pauses } = pausing(memoryKV(LIVE), {
318
+ claim: { method: 'hdel', key: keyOf(A, 'links') },
319
+ drop: { method: 'hgetall', key: keyOf(A, 'links') },
284
320
  });
285
321
  const r = createReview(store);
286
322
  const root = { ...studio, id: 'cli' };
287
- const { person, token } = await r.addPerson(root, { name: 'Rae', team: 'Acme' });
323
+ const { person, token } = await r.addPerson(A, root, { name: 'Rae', team: 'Acme' });
288
324
  // useLink checks the person (active, epoch 0), then waits at its claim.
289
325
  arm('claim');
290
- const using = r.useLink(token);
326
+ const using = r.useLink(A, token);
291
327
  await pauses.claim.reached;
292
328
  // The other call writes its revocation or epoch, then waits before it drops links.
293
329
  arm('drop');
@@ -301,44 +337,48 @@ for (const [what, run] of [
301
337
  });
302
338
 
303
339
  test('any revocation record means revoked, and an access end that does not parse means no access', async () => {
304
- const kv = memoryKV();
340
+ const kv = memoryKV(LIVE);
305
341
  const r = createReview(kv);
306
342
  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);
343
+ const a = (await r.addPerson(A, root, { name: 'Rae', team: 'Acme' })).person;
344
+ await kv.hset(keyOf(A, 'revoked'), a.id, '');
345
+ assert.ok((await r.person(A, a.id))?.revokedAt);
346
+ assert.equal(await r.sessionPerson(A, a.id, 0), null);
347
+ const b = await r.addPerson(A, root, { name: 'Oz', team: 'Acme' });
348
+ const profile = JSON.parse((await kv.hget(keyOf(A, 'people'), b.person.id)) ?? '{}');
349
+ await kv.hset(keyOf(A, 'people'), b.person.id, JSON.stringify({ ...profile, accessEndsAt: 'someday' }));
350
+ assert.equal(await r.sessionPerson(A, b.person.id, 0), null);
351
+ assert.equal(await r.peekLink(A, b.token), null);
352
+ assert.equal(await r.useLink(A, b.token), null);
317
353
  });
318
354
 
319
355
  test('a profile that is not a well-formed person is no person, and listPeople skips it', async () => {
320
- const kv = memoryKV();
356
+ const kv = memoryKV(LIVE);
321
357
  const r = createReview(kv);
322
358
  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: '' }));
359
+ const { person } = await r.addPerson(A, root, { name: 'Rae', team: 'Acme' });
360
+ await kv.hset(keyOf(A, 'people'), 'junk', 'not json');
361
+ await kv.hset(keyOf(A, 'people'), 'array', '[]');
362
+ await kv.hset(
363
+ keyOf(A, 'people'),
364
+ 'nokind',
365
+ JSON.stringify({ id: 'nokind', name: 'X', team: 'Y', createdAt: '' }),
366
+ );
327
367
  await kv.hset(
328
- KEY.people,
368
+ keyOf(A, 'people'),
329
369
  'oddend',
330
370
  JSON.stringify({
331
- ...JSON.parse((await kv.hget(KEY.people, person.id)) ?? '{}'),
371
+ ...JSON.parse((await kv.hget(keyOf(A, 'people'), person.id)) ?? '{}'),
332
372
  id: 'oddend',
333
373
  accessEndsAt: 5,
334
374
  }),
335
375
  );
336
376
  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);
377
+ assert.equal(await r.person(A, id), null);
378
+ assert.equal(await r.sessionPerson(A, id, 0), null);
339
379
  }
340
380
  assert.deepEqual(
341
- (await r.listPeople(root)).map((p) => p.id),
381
+ (await r.listPeople(A, root)).map((p) => p.id),
342
382
  [person.id],
343
383
  );
344
384
  });
@@ -346,29 +386,29 @@ test('a profile that is not a well-formed person is no person, and listPeople sk
346
386
  test('two sign outs everywhere at once both end the earlier sessions', async () => {
347
387
  const root = { ...studio, id: 'cli' };
348
388
  const [a, b] = await Promise.all([
349
- review.signOutEverywhere(root, client.id),
350
- review.signOutEverywhere(root, client.id),
389
+ review.signOutEverywhere(A, root, client.id),
390
+ review.signOutEverywhere(A, root, client.id),
351
391
  ]);
352
392
  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);
393
+ assert.equal((await review.person(A, client.id))?.epoch, 2);
394
+ assert.equal(await review.sessionPerson(A, client.id, 0), null);
355
395
  });
356
396
 
357
397
  test('a profile write never carries revocation or epoch', async () => {
358
- const kv = memoryKV();
398
+ const kv = memoryKV(LIVE);
359
399
  const r = createReview(kv);
360
400
  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)) ?? '{}');
401
+ const { person } = await r.addPerson(A, root, { name: 'Rae', team: 'Acme' });
402
+ await r.signOutEverywhere(A, root, person.id);
403
+ await r.setAccess(A, root, person.id, '6m');
404
+ const profile = JSON.parse((await kv.hget(keyOf(A, 'people'), person.id)) ?? '{}');
365
405
  assert.equal('epoch' in profile, false);
366
406
  assert.equal('revokedAt' in profile, false);
367
407
  // 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);
408
+ await kv.hset(keyOf(A, 'people'), person.id, JSON.stringify({ ...profile, epoch: 0, revokedAt: null }));
409
+ await r.revoke(A, root, person.id);
410
+ await kv.hset(keyOf(A, 'people'), person.id, JSON.stringify({ ...profile, epoch: 0, revokedAt: null }));
411
+ const now = await r.person(A, person.id);
372
412
  assert.ok(now?.revokedAt);
373
413
  assert.equal(now?.epoch, 2);
374
414
  });
@@ -383,133 +423,143 @@ test('access lengths land on the same day, or the last day of a shorter month',
383
423
 
384
424
  test('an unused link lasts 14 days', async () => {
385
425
  clock += 13 * DAY;
386
- assert.ok(await review.peekLink(clientToken));
426
+ assert.ok(await review.peekLink(A, clientToken));
387
427
  clock += 2 * DAY;
388
- assert.equal(await review.peekLink(clientToken), null);
389
- assert.equal(await review.useLink(clientToken), null);
428
+ assert.equal(await review.peekLink(A, clientToken), null);
429
+ assert.equal(await review.useLink(A, clientToken), null);
390
430
  });
391
431
 
392
432
  test('a Client gets the project default, Studio is open-ended, and a length given wins', async () => {
393
433
  assert.match(client.accessEndsAt ?? '', /^2030-04-01/);
394
434
  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;
435
+ assert.equal(await review.defaultAccess(A, studio), '3m');
436
+ assert.equal(await review.setDefaultAccess(A, studio, '6m'), '6m');
437
+ const later = (await review.addPerson(A, studio, { name: 'Oz', team: 'Acme' })).person;
398
438
  assert.match(later.accessEndsAt ?? '', /^2030-07-01/);
399
- const open = (await review.addPerson(studio, { name: 'Lu', team: 'Acme', access: 'open' })).person;
439
+ const open = (await review.addPerson(A, studio, { name: 'Lu', team: 'Acme', access: 'open' })).person;
400
440
  assert.equal(open.accessEndsAt, null);
401
- const short = (await review.addPerson(studio, { name: 'Jo', team: 'Studio', kind: 'studio', access: '3m' }))
402
- .person;
441
+ const short = (
442
+ await review.addPerson(A, studio, { name: 'Jo', team: 'Studio', kind: 'studio', access: '3m' })
443
+ ).person;
403
444
  assert.match(short.accessEndsAt ?? '', /^2030-04-01/);
404
445
  });
405
446
 
406
447
  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));
448
+ assert.ok(await review.sessionPerson(A, client.id, 0));
408
449
  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');
450
+ assert.equal(await review.sessionPerson(A, client.id, 0), null);
451
+ await rejects(review.newLink(A, studio, client.id), 'invalid');
452
+ const changed = await review.setAccess(A, studio, client.id, '6m');
412
453
  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);
454
+ assert.ok(await review.sessionPerson(A, client.id, 0));
455
+ assert.equal((await review.setAccess(A, studio, client.id, 'open')).accessEndsAt, null);
415
456
  });
416
457
 
417
458
  test('access lengths are checked and every problem is named', async () => {
418
459
  await assert.rejects(
419
- review.addPerson(studio, { name: '', team: 'Acme', access: '1y' }),
460
+ review.addPerson(A, studio, { name: '', team: 'Acme', access: '1y' }),
420
461
  (e: unknown) =>
421
462
  e instanceof ReviewError && e.problems.join() === 'name: required,access: one of 3m, 6m, open',
422
463
  );
423
- await rejects(review.setAccess(studio, client.id, 'forever'), 'invalid');
424
- await rejects(review.setDefaultAccess(studio, null), 'invalid');
464
+ await rejects(review.setAccess(A, studio, client.id, 'forever'), 'invalid');
465
+ await rejects(review.setDefaultAccess(A, studio, null), 'invalid');
425
466
  });
426
467
 
427
468
  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');
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');
437
- await rejects(review.revoke(studio, studio.id), 'invalid');
469
+ await rejects(review.addPerson(A, client, { name: 'X', team: 'Acme' }), 'forbidden');
470
+ await rejects(review.listPeople(A, client), 'forbidden');
471
+ await rejects(review.listPeople(A, null), 'unauthorized');
472
+ await rejects(review.newLink(A, client, client.id), 'forbidden');
473
+ await rejects(review.signOutEverywhere(A, client, client.id), 'forbidden');
474
+ await rejects(review.revoke(A, client, studio.id), 'forbidden');
475
+ await rejects(review.setAccess(A, client, client.id, 'open'), 'forbidden');
476
+ await rejects(review.defaultAccess(A, client), 'forbidden');
477
+ await rejects(review.setDefaultAccess(A, client, 'open'), 'forbidden');
478
+ await rejects(review.revoke(A, studio, studio.id), 'invalid');
438
479
  assert.deepEqual(
439
- (await review.listPeople(studio)).map((p) => p.name),
480
+ (await review.listPeople(A, studio)).map((p) => p.name),
440
481
  ['Rae', 'Ed'],
441
482
  );
442
483
  });
443
484
 
444
485
  test('a comment, its replies, resolving and deleting the thread', async () => {
445
- const root = await review.add(client, { body: ' Row height jumps on empty ', anchor });
486
+ const root = await review.add(A, client, { body: ' Row height jumps on empty ', anchor });
446
487
  assert.equal(root.body, 'Row height jumps on empty');
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 });
488
+ await review.reply(A, studio, { prototype: 'sample-list', thread: root.id, body: 'Fixed in v2' });
489
+ assert.equal((await review.listComments(A, client, 'sample-list')).length, 2);
490
+ const done = await review.resolve(A, client, { prototype: 'sample-list', id: root.id, resolved: true });
450
491
  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'), []);
492
+ assert.deepEqual((await review.remove(A, client, { prototype: 'sample-list', id: root.id })).length, 2);
493
+ assert.deepEqual(await review.listComments(A, studio, 'sample-list'), []);
453
494
  });
454
495
 
455
496
  test('a new person is a Client unless named Studio, and any other kind is invalid', async () => {
456
497
  assert.equal(client.kind, 'client');
457
498
  assert.equal(client.email, null);
458
499
  assert.equal(studio.kind, 'studio');
459
- await rejects(review.addPerson(studio, { name: 'X', team: 'Acme', kind: 'editor' }), 'invalid');
500
+ await rejects(review.addPerson(A, studio, { name: 'X', team: 'Acme', kind: 'editor' }), 'invalid');
460
501
  });
461
502
 
462
503
  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;
465
- const done = await review.resolve(other, { prototype: 'sample-list', id: c.id, resolved: true });
504
+ const c = await review.add(A, studio, { body: 'Spacing off', anchor, audience: 'client' });
505
+ const other = (await review.addPerson(A, studio, { name: 'Oz', team: 'Acme' })).person;
506
+ const done = await review.resolve(A, other, { prototype: 'sample-list', id: c.id, resolved: true });
466
507
  assert.equal(done.resolvedBy?.id, other.id);
467
- const open = await review.resolve(client, { prototype: 'sample-list', id: c.id, resolved: false });
508
+ const open = await review.resolve(A, client, { prototype: 'sample-list', id: c.id, resolved: false });
468
509
  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');
510
+ await rejects(review.remove(A, client, { prototype: 'sample-list', id: c.id }), 'forbidden');
511
+ const reply = await review.reply(A, other, { prototype: 'sample-list', thread: c.id, body: 'Agreed' });
512
+ await rejects(review.remove(A, client, { prototype: 'sample-list', id: reply.id }), 'forbidden');
472
513
  });
473
514
 
474
515
  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 });
516
+ const c = await review.add(A, client, { body: 'Wrong icon', anchor });
517
+ const done = await review.resolve(A, studio, { prototype: 'sample-list', id: c.id, resolved: true });
477
518
  assert.equal(done.resolvedBy?.id, studio.id);
478
- assert.deepEqual(await review.remove(studio, { prototype: 'sample-list', id: c.id }), [c.id]);
519
+ assert.deepEqual(await review.remove(A, studio, { prototype: 'sample-list', id: c.id }), [c.id]);
479
520
  });
480
521
 
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' };
522
+ test('records under an older key prefix are never read: the unscoped hub3 store starts clean', async () => {
523
+ const { client: _c, ...profile } = { ...client, id: 'old', name: 'Old' };
483
524
  const token = 'a'.repeat(43);
484
525
  const hash = Buffer.from(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(token))).toString(
485
526
  'hex',
486
527
  );
528
+ const link = JSON.stringify({ person: 'old', expiresAt: '2999-01-01T00:00:00Z' });
529
+ const comment = JSON.stringify({ id: 'x', thread: 'x', audience: 'client', anchor });
487
530
  const kv = memoryKV({
488
531
  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' }) },
532
+ ...LIVE.hashes,
533
+ 'hub3:people': { old: JSON.stringify(profile) },
534
+ 'hub3:links': { [hash]: link },
535
+ 'hub3:audiences': { 'sample-list': 'client', 'old-plan': 'client' },
536
+ 'hub3:comments:sample-list': { x: comment },
537
+ 'hub2:people': { old: JSON.stringify(profile) },
538
+ 'hub2:invites': { [hash]: link },
492
539
  },
493
- sets: { 'hub2:prototypes': ['sample-list'] },
540
+ sets: { 'hub3:prototypes': ['sample-list'], 'hub2:prototypes': ['sample-list'] },
494
541
  });
495
542
  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(studio), []);
502
- assert.equal(await fresh.locate(studio, 'x'), null);
543
+ for (const key of [keyOf('studio', 'people'), keyOf(A, 'people'), keyOf(A, 'comments', 'x')])
544
+ assert.ok(key.startsWith('hub4:'), key);
545
+ assert.equal(await fresh.person(A, 'old'), null);
546
+ assert.equal(await fresh.sessionPerson(A, 'old', 0), null);
547
+ assert.equal(await fresh.peekLink(A, token), null);
548
+ assert.equal(await fresh.useLink(A, token), null);
549
+ assert.deepEqual(await fresh.allComments(A, studio), []);
550
+ assert.equal(await fresh.locate(A, studio, 'x'), null);
551
+ assert.equal(await fresh.audienceOf(A, 'old-plan'), 'internal');
552
+ assert.deepEqual((await fresh.visiblePrototypes(A, studio)).clientFacing, []);
503
553
  });
504
554
 
505
555
  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');
556
+ const c = await review.add(A, client, { body: 'Typo in header', anchor });
557
+ await rejects(review.edit(A, studio, { prototype: 'sample-list', id: c.id, body: 'x' }), 'forbidden');
508
558
  await rejects(
509
- review.retriage(client, { prototype: 'sample-list', id: c.id, triage: { kind: 'copy' } }),
559
+ review.retriage(A, client, { prototype: 'sample-list', id: c.id, triage: { kind: 'copy' } }),
510
560
  'forbidden',
511
561
  );
512
- const t = await review.retriage(studio, {
562
+ const t = await review.retriage(A, studio, {
513
563
  prototype: 'sample-list',
514
564
  id: c.id,
515
565
  triage: { kind: 'copy', state: 'Empty' },
@@ -517,7 +567,7 @@ test('people edit only their own words; Studio triages anything', async () => {
517
567
  });
518
568
  assert.deepEqual(t.triage, { kind: 'copy', state: 'Empty', by: 'person', confidence: null });
519
569
  await rejects(
520
- review.retriage(studio, {
570
+ review.retriage(A, studio, {
521
571
  prototype: 'sample-list',
522
572
  id: c.id,
523
573
  triage: { state: 'Nope' },
@@ -528,14 +578,14 @@ test('people edit only their own words; Studio triages anything', async () => {
528
578
  });
529
579
 
530
580
  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);
532
- await rejects(review.add(gone, { body: 'x', anchor }), 'forbidden');
533
- await rejects(review.add(null, { body: 'x', anchor }), 'unauthorized');
581
+ const gone = await review.revoke(A, studio, client.id);
582
+ await rejects(review.add(A, gone, { body: 'x', anchor }), 'forbidden');
583
+ await rejects(review.add(A, null, { body: 'x', anchor }), 'unauthorized');
534
584
  });
535
585
 
536
586
  test('bad input names every problem', async () => {
537
587
  await assert.rejects(
538
- review.add(client, { body: '', anchor: { ...anchor, url: 'x', offset: { x: 2, y: 0 } } }),
588
+ review.add(A, client, { body: '', anchor: { ...anchor, url: 'x', offset: { x: 2, y: 0 } } }),
539
589
  (e: unknown) => {
540
590
  assert.ok(e instanceof ReviewError);
541
591
  assert.deepEqual(
@@ -549,42 +599,50 @@ test('bad input names every problem', async () => {
549
599
 
550
600
  test('triage runs on add, and a failing triager never loses the comment', async () => {
551
601
  const seen: string[][] = [];
552
- const triaged = createReview(memoryKV(), {
602
+ const triaged = createReview(memoryKV(LIVE), {
553
603
  triage: async ({ states }) => {
554
604
  seen.push(states);
555
605
  return { kind: 'bug', state: states[0] ?? null, by: 'jev', confidence: 0.9 };
556
606
  },
557
607
  });
558
- await triaged.setAudience(studio, anchor.prototype, 'client');
559
- const c = await triaged.add(client, {
608
+ await triaged.setAudience(A, studio, anchor.prototype, 'client');
609
+ const c = await triaged.add(A, client, {
560
610
  body: 'Broken',
561
611
  anchor,
562
612
  states: ['Empty', 'Empty', ' Real list '],
563
613
  });
564
614
  assert.deepEqual(seen, [['Empty', 'Real list']]);
565
615
  assert.equal(c.triage?.kind, 'bug');
566
- const failing = createReview(memoryKV(), { triage: async () => Promise.reject(new Error('down')) });
567
- await failing.setAudience(studio, anchor.prototype, 'client');
568
- assert.equal((await failing.add(client, { body: 'Still saved', anchor })).triage, null);
616
+ const failing = createReview(memoryKV(LIVE), { triage: async () => Promise.reject(new Error('down')) });
617
+ await failing.setAudience(A, studio, anchor.prototype, 'client');
618
+ assert.equal((await failing.add(A, client, { body: 'Still saved', anchor })).triage, null);
569
619
  });
570
620
 
571
621
  test('locate finds a comment by id alone', async () => {
572
- const c = await review.add(client, { body: 'Here', anchor });
573
- assert.equal((await review.locate(studio, c.id))?.prototype, 'sample-list');
574
- assert.equal((await review.locate(client, c.id))?.prototype, 'sample-list');
575
- assert.equal(await review.locate(studio, 'nope'), null);
576
- await rejects(review.locate(null, c.id), 'unauthorized');
622
+ const c = await review.add(A, client, { body: 'Here', anchor });
623
+ assert.equal((await review.locate(A, studio, c.id))?.prototype, 'sample-list');
624
+ assert.equal((await review.locate(A, client, c.id))?.prototype, 'sample-list');
625
+ assert.equal(await review.locate(A, studio, 'nope'), null);
626
+ await rejects(review.locate(A, null, c.id), 'unauthorized');
577
627
  });
578
628
 
579
629
  // Slice 4: internal comments. One internal thread with a Studio reply, one
580
630
  // client-facing thread with replies from both kinds.
581
631
  async function twoThreads() {
582
632
  const at = { prototype: 'sample-list' };
583
- const internal = await review.add(studio, { body: 'Spacing token is wrong', anchor });
584
- const internalReply = await review.reply(studio, { ...at, thread: internal.id, body: 'Fix before Friday' });
585
- const shared = await review.add(client, { body: 'Header feels heavy', anchor });
586
- const studioReply = await review.reply(studio, { ...at, thread: shared.id, body: 'Trying a lighter one' });
587
- const clientReply = await review.reply(client, { ...at, thread: shared.id, body: 'Thanks' });
633
+ const internal = await review.add(A, studio, { body: 'Spacing token is wrong', anchor });
634
+ const internalReply = await review.reply(A, studio, {
635
+ ...at,
636
+ thread: internal.id,
637
+ body: 'Fix before Friday',
638
+ });
639
+ const shared = await review.add(A, client, { body: 'Header feels heavy', anchor });
640
+ const studioReply = await review.reply(A, studio, {
641
+ ...at,
642
+ thread: shared.id,
643
+ body: 'Trying a lighter one',
644
+ });
645
+ const clientReply = await review.reply(A, client, { ...at, thread: shared.id, body: 'Thanks' });
588
646
  return { internal, internalReply, shared, studioReply, clientReply };
589
647
  }
590
648
 
@@ -594,61 +652,65 @@ test('audience: Studio’s thread is internal, a Client’s client-facing, and r
594
652
  [t.internal, t.internalReply, t.shared, t.studioReply, t.clientReply].map((c) => c.audience),
595
653
  ['internal', 'internal', 'client', 'client', 'client'],
596
654
  );
597
- const asked = await review.add(studio, { body: 'For the client', anchor, audience: 'client' });
655
+ const asked = await review.add(A, studio, { body: 'For the client', anchor, audience: 'client' });
598
656
  assert.equal(asked.audience, 'client');
599
657
  assert.equal(
600
- (await review.add(studio, { body: 'Ours', anchor, audience: 'internal' })).audience,
658
+ (await review.add(A, studio, { body: 'Ours', anchor, audience: 'internal' })).audience,
601
659
  'internal',
602
660
  );
603
- await rejects(review.add(studio, { body: 'x', anchor, audience: 'everyone' }), 'invalid');
661
+ await rejects(review.add(A, studio, { body: 'x', anchor, audience: 'everyone' }), 'invalid');
604
662
  });
605
663
 
606
664
  test('a Client cannot make an internal thread by asking for one', async () => {
607
- const c = await review.add(client, { body: 'Mine', anchor, audience: 'internal' });
665
+ const c = await review.add(A, client, { body: 'Mine', anchor, audience: 'internal' });
608
666
  assert.equal(c.audience, 'client');
609
- assert.ok((await review.listComments(client, 'sample-list')).some((x) => x.id === c.id));
667
+ assert.ok((await review.listComments(A, client, 'sample-list')).some((x) => x.id === c.id));
610
668
  });
611
669
 
612
670
  test('a Client’s list holds no internal thread or reply; Studio’s holds all', async () => {
613
671
  const t = await twoThreads();
614
672
  const ids = (cs: { id: string }[]) => cs.map((c) => c.id);
615
- assert.deepEqual(ids(await review.listComments(client, 'sample-list')), [
673
+ assert.deepEqual(ids(await review.listComments(A, client, 'sample-list')), [
674
+ t.shared.id,
675
+ t.studioReply.id,
676
+ t.clientReply.id,
677
+ ]);
678
+ assert.equal((await review.listComments(A, studio, 'sample-list')).length, 5);
679
+ assert.deepEqual(ids(await review.allComments(A, client)), [
616
680
  t.shared.id,
617
681
  t.studioReply.id,
618
682
  t.clientReply.id,
619
683
  ]);
620
- assert.equal((await review.listComments(studio, 'sample-list')).length, 5);
621
- assert.deepEqual(ids(await review.allComments(client)), [t.shared.id, t.studioReply.id, t.clientReply.id]);
622
- assert.equal((await review.allComments(studio)).length, 5);
684
+ assert.equal((await review.allComments(A, studio)).length, 5);
623
685
  });
624
686
 
625
687
  test('Client view gives Studio exactly a Client’s list, and never widens a Client’s', async () => {
626
688
  await twoThreads();
627
- const asClient = await review.listComments(client, 'sample-list');
628
- assert.deepEqual(await review.listComments(studio, 'sample-list', 'client'), asClient);
629
- assert.deepEqual(await review.listComments(client, 'sample-list', 'studio'), asClient);
630
- assert.equal((await review.listComments(studio, 'sample-list', 'studio')).length, 5);
631
- await rejects(review.listComments(studio, 'sample-list', 'editor'), 'invalid');
689
+ const asClient = await review.listComments(A, client, 'sample-list');
690
+ assert.deepEqual(await review.listComments(A, studio, 'sample-list', 'client'), asClient);
691
+ assert.deepEqual(await review.listComments(A, client, 'sample-list', 'studio'), asClient);
692
+ assert.equal((await review.listComments(A, studio, 'sample-list', 'studio')).length, 5);
693
+ await rejects(review.listComments(A, studio, 'sample-list', 'editor'), 'invalid');
632
694
  });
633
695
 
634
696
  test('a Client acting on an internal id is told it does not exist, by every op', async () => {
635
697
  const t = await twoThreads();
636
698
  const at = { prototype: 'sample-list' };
637
699
  for (const id of [t.internal.id, t.internalReply.id]) {
638
- await rejects(review.reply(client, { ...at, thread: id, body: 'x' }), 'not_found');
639
- await rejects(review.edit(client, { ...at, id, body: 'x' }), 'not_found');
640
- await rejects(review.remove(client, { ...at, id }), 'not_found');
641
- await rejects(review.resolve(client, { ...at, id, resolved: true }), 'not_found');
642
- await rejects(review.resolve(client, { ...at, id, resolved: false }), 'not_found');
700
+ await rejects(review.reply(A, client, { ...at, thread: id, body: 'x' }), 'not_found');
701
+ await rejects(review.edit(A, client, { ...at, id, body: 'x' }), 'not_found');
702
+ await rejects(review.remove(A, client, { ...at, id }), 'not_found');
703
+ await rejects(review.resolve(A, client, { ...at, id, resolved: true }), 'not_found');
704
+ await rejects(review.resolve(A, client, { ...at, id, resolved: false }), 'not_found');
643
705
  // Triage is Studio's whatever the id, so a Client is refused before any lookup.
644
- await rejects(review.retriage(client, { ...at, id, triage: { kind: 'bug' } }), 'forbidden');
645
- assert.equal(await review.locate(client, id), null);
646
- assert.ok(await review.locate(studio, id));
706
+ await rejects(review.retriage(A, client, { ...at, id, triage: { kind: 'bug' } }), 'forbidden');
707
+ assert.equal(await review.locate(A, client, id), null);
708
+ assert.ok(await review.locate(A, studio, id));
647
709
  }
648
710
  // The same answer as an id that is not there at all.
649
- await rejects(review.edit(client, { ...at, id: 'nope', body: 'x' }), 'not_found');
711
+ await rejects(review.edit(A, client, { ...at, id: 'nope', body: 'x' }), 'not_found');
650
712
  // Nothing changed.
651
- const after = await review.listComments(studio, 'sample-list');
713
+ const after = await review.listComments(A, studio, 'sample-list');
652
714
  assert.equal(after.length, 5);
653
715
  assert.equal(after.find((c) => c.id === t.internal.id)?.status, 'open');
654
716
  assert.equal(after.find((c) => c.id === t.internalReply.id)?.body, 'Fix before Friday');
@@ -658,32 +720,35 @@ test('a signed-out caller is refused before any comment is looked up', async ()
658
720
  const t = await twoThreads();
659
721
  const at = { prototype: 'sample-list' };
660
722
  for (const id of [t.internal.id, 'nope']) {
661
- await rejects(review.edit(null, { ...at, id, body: 'x' }), 'unauthorized');
662
- await rejects(review.remove(null, { ...at, id }), 'unauthorized');
663
- await rejects(review.resolve(null, { ...at, id, resolved: true }), 'unauthorized');
664
- await rejects(review.reply(null, { ...at, thread: id, body: 'x' }), 'unauthorized');
723
+ await rejects(review.edit(A, null, { ...at, id, body: 'x' }), 'unauthorized');
724
+ await rejects(review.remove(A, null, { ...at, id }), 'unauthorized');
725
+ await rejects(review.resolve(A, null, { ...at, id, resolved: true }), 'unauthorized');
726
+ await rejects(review.reply(A, null, { ...at, thread: id, body: 'x' }), 'unauthorized');
665
727
  }
666
- await rejects(review.listComments(null, 'sample-list'), 'unauthorized');
667
- await rejects(review.allComments(null), 'unauthorized');
728
+ await rejects(review.listComments(A, null, 'sample-list'), 'unauthorized');
729
+ await rejects(review.allComments(A, null), 'unauthorized');
668
730
  });
669
731
 
670
732
  test('a Client still works on client-facing threads, Studio’s replies included', async () => {
671
733
  const t = await twoThreads();
672
734
  const at = { prototype: 'sample-list' };
673
- assert.equal((await review.resolve(client, { ...at, id: t.shared.id, resolved: true })).status, 'resolved');
674
735
  assert.equal(
675
- (await review.edit(client, { ...at, id: t.clientReply.id, body: 'Thank you' })).body,
736
+ (await review.resolve(A, client, { ...at, id: t.shared.id, resolved: true })).status,
737
+ 'resolved',
738
+ );
739
+ assert.equal(
740
+ (await review.edit(A, client, { ...at, id: t.clientReply.id, body: 'Thank you' })).body,
676
741
  'Thank you',
677
742
  );
678
- await rejects(review.edit(client, { ...at, id: t.studioReply.id, body: 'x' }), 'forbidden');
679
- await rejects(review.remove(client, { ...at, id: t.studioReply.id }), 'forbidden');
680
- assert.deepEqual(await review.remove(client, { ...at, id: t.clientReply.id }), [t.clientReply.id]);
743
+ await rejects(review.edit(A, client, { ...at, id: t.studioReply.id, body: 'x' }), 'forbidden');
744
+ await rejects(review.remove(A, client, { ...at, id: t.studioReply.id }), 'forbidden');
745
+ assert.deepEqual(await review.remove(A, client, { ...at, id: t.clientReply.id }), [t.clientReply.id]);
681
746
  });
682
747
 
683
748
  test('numbers are fixed: internal and client-facing threads count apart, and none moves or is reused', async () => {
684
749
  const t = await twoThreads();
685
- const third = await review.add(client, { body: 'Third', anchor });
686
- const ours = await review.add(studio, { body: 'Ours too', anchor });
750
+ const third = await review.add(A, client, { body: 'Third', anchor });
751
+ const ours = await review.add(A, studio, { body: 'Ours too', anchor });
687
752
  assert.deepEqual(
688
753
  [t.internal, t.shared, third, ours].map((c) => [c.audience, c.number]),
689
754
  [
@@ -698,23 +763,23 @@ test('numbers are fixed: internal and client-facing threads count apart, and non
698
763
  assert.deepEqual([t.internalReply.number, t.studioReply.number], [1, 1]);
699
764
  // A Client sees 1 and 2: no gap where an internal thread sits.
700
765
  const roots = (cs: Comment[]) => cs.filter((c) => c.id === c.thread).map(threadLabel);
701
- assert.deepEqual(roots(await review.listComments(client, 'sample-list')), ['1', '2']);
702
- await review.remove(studio, { prototype: 'sample-list', id: t.shared.id });
703
- assert.deepEqual(roots(await review.listComments(studio, 'sample-list')), ['I1', '2', 'I2']);
704
- assert.deepEqual(roots(await review.listComments(client, 'sample-list')), ['2']);
705
- assert.equal((await review.add(client, { body: 'Fourth', anchor })).number, 3);
766
+ assert.deepEqual(roots(await review.listComments(A, client, 'sample-list')), ['1', '2']);
767
+ await review.remove(A, studio, { prototype: 'sample-list', id: t.shared.id });
768
+ assert.deepEqual(roots(await review.listComments(A, studio, 'sample-list')), ['I1', '2', 'I2']);
769
+ assert.deepEqual(roots(await review.listComments(A, client, 'sample-list')), ['2']);
770
+ assert.equal((await review.add(A, client, { body: 'Fourth', anchor })).number, 3);
706
771
  });
707
772
 
708
773
  test('numbers count per prototype', async () => {
709
- await review.setAudience(studio, 'other', 'client');
710
- const a = await review.add(client, { body: 'A1', anchor });
711
- const b = await review.add(client, { body: 'B1', anchor: { ...anchor, prototype: 'other' } });
712
- const a2 = await review.add(studio, { body: 'A2', anchor, audience: 'client' });
774
+ await review.setAudience(A, studio, 'other', 'client');
775
+ const a = await review.add(A, client, { body: 'A1', anchor });
776
+ const b = await review.add(A, client, { body: 'B1', anchor: { ...anchor, prototype: 'other' } });
777
+ const a2 = await review.add(A, studio, { body: 'A2', anchor, audience: 'client' });
713
778
  assert.deepEqual([a.number, b.number, a2.number], [1, 1, 2]);
714
779
  });
715
780
 
716
781
  test('the internal count starts past internal threads numbered while the two shared one count', async () => {
717
- const kv = memoryKV();
782
+ const kv = memoryKV(LIVE);
718
783
  const r = createReview(kv);
719
784
  const root = { ...studio, id: 'cli' };
720
785
  // Under one shared count: internal 1, client-facing 2, internal 3.
@@ -723,11 +788,11 @@ test('the internal count starts past internal threads numbered while the two sha
723
788
  { id: 'b', audience: 'client', number: 2 },
724
789
  { id: 'c', audience: 'internal', number: 3 },
725
790
  ];
726
- await kv.sadd(KEY.prototypes, 'sample-list');
727
- await kv.hset(KEY.counters, 'sample-list', '3');
791
+ await kv.sadd(keyOf(A, 'prototypes'), 'sample-list');
792
+ await kv.hset(keyOf(A, 'counters'), 'sample-list', '3');
728
793
  for (const c of shared)
729
794
  await kv.hset(
730
- KEY.comments('sample-list'),
795
+ keyOf(A, 'comments', 'sample-list'),
731
796
  c.id,
732
797
  JSON.stringify({
733
798
  ...c,
@@ -742,17 +807,17 @@ test('the internal count starts past internal threads numbered while the two sha
742
807
  triage: null,
743
808
  }),
744
809
  );
745
- const next = await r.add(root, { body: 'After', anchor });
810
+ const next = await r.add(A, root, { body: 'After', anchor });
746
811
  assert.deepEqual([next.audience, next.number], ['internal', 4]);
747
- assert.equal((await r.add(root, { body: 'Again', anchor })).number, 5);
748
- assert.equal((await r.add(root, { body: 'For them', anchor, audience: 'client' })).number, 4);
812
+ assert.equal((await r.add(A, root, { body: 'Again', anchor })).number, 5);
813
+ assert.equal((await r.add(A, root, { body: 'For them', anchor, audience: 'client' })).number, 4);
749
814
  });
750
815
 
751
816
  test('a stored comment without an audience is internal, and one without a number has none', async () => {
752
- const kv = memoryKV();
817
+ const kv = memoryKV(LIVE);
753
818
  const r = createReview(kv);
754
819
  const root = { ...studio, id: 'cli' };
755
- const rae = (await r.addPerson(root, { name: 'Rae', team: 'Acme' })).person;
820
+ const rae = (await r.addPerson(A, root, { name: 'Rae', team: 'Acme' })).person;
756
821
  const legacy = {
757
822
  id: 'old',
758
823
  thread: 'old',
@@ -765,14 +830,14 @@ test('a stored comment without an audience is internal, and one without a number
765
830
  resolvedBy: null,
766
831
  triage: null,
767
832
  };
768
- await kv.sadd(KEY.prototypes, 'sample-list');
769
- await kv.hset(KEY.comments('sample-list'), 'old', JSON.stringify(legacy));
833
+ await kv.sadd(keyOf(A, 'prototypes'), 'sample-list');
834
+ await kv.hset(keyOf(A, 'comments', 'sample-list'), 'old', JSON.stringify(legacy));
770
835
  await kv.hset(
771
- KEY.comments('sample-list'),
836
+ keyOf(A, 'comments', 'sample-list'),
772
837
  'odd',
773
838
  JSON.stringify({ ...legacy, id: 'odd', thread: 'odd', audience: 'everyone' }),
774
839
  );
775
- const seen = await r.listComments(root, 'sample-list');
840
+ const seen = await r.listComments(A, root, 'sample-list');
776
841
  assert.deepEqual(
777
842
  seen.map((c) => [c.id, c.audience, c.number]),
778
843
  [
@@ -780,136 +845,142 @@ test('a stored comment without an audience is internal, and one without a number
780
845
  ['old', 'internal', 0],
781
846
  ],
782
847
  );
783
- assert.deepEqual(await r.listComments(rae, 'sample-list'), []);
784
- assert.equal(await r.locate(rae, 'old'), null);
785
- await rejects(r.edit(rae, { prototype: 'sample-list', id: 'old', body: 'x' }), 'not_found');
848
+ assert.deepEqual(await r.listComments(A, rae, 'sample-list'), []);
849
+ assert.equal(await r.locate(A, rae, 'old'), null);
850
+ await rejects(r.edit(A, rae, { prototype: 'sample-list', id: 'old', body: 'x' }), 'not_found');
786
851
  });
787
852
 
788
853
  test('a prototype is internal until Studio marks it client-facing, and internal again when unmarked', async () => {
789
- assert.equal(await review.audienceOf('secret-plan'), 'internal');
790
- assert.equal(await review.setAudience(studio, 'secret-plan', 'client'), 'client');
791
- assert.equal(await review.audienceOf('secret-plan'), 'client');
792
- assert.equal(await review.audienceOf('sample-page'), 'internal');
793
- assert.equal(await review.setAudience(studio, 'secret-plan', 'internal'), 'internal');
794
- assert.equal(await review.audienceOf('secret-plan'), 'internal');
854
+ assert.equal(await review.audienceOf(A, 'secret-plan'), 'internal');
855
+ assert.equal(await review.setAudience(A, studio, 'secret-plan', 'client'), 'client');
856
+ assert.equal(await review.audienceOf(A, 'secret-plan'), 'client');
857
+ assert.equal(await review.audienceOf(A, 'sample-page'), 'internal');
858
+ assert.equal(await review.setAudience(A, studio, 'secret-plan', 'internal'), 'internal');
859
+ assert.equal(await review.audienceOf(A, 'secret-plan'), 'internal');
795
860
  // Marking internal twice, or one never marked, is no error.
796
- assert.equal(await review.setAudience(studio, 'sample-page', 'internal'), 'internal');
861
+ assert.equal(await review.setAudience(A, studio, 'sample-page', 'internal'), 'internal');
797
862
  });
798
863
 
799
864
  test('only client-facing is stored: internal deletes the field, and absent or garbage reads as internal', async () => {
800
- const kv = memoryKV();
865
+ const kv = memoryKV(LIVE);
801
866
  const own = createReview(kv);
802
- await own.setAudience(studio, 'secret-plan', 'client');
803
- assert.deepEqual(await kv.hgetall(KEY.audiences), { 'secret-plan': 'client' });
804
- await own.setAudience(studio, 'secret-plan', 'internal');
805
- assert.deepEqual(await kv.hgetall(KEY.audiences), {});
867
+ await own.setAudience(A, studio, 'secret-plan', 'client');
868
+ assert.deepEqual(await kv.hgetall(keyOf(A, 'audiences')), { 'secret-plan': 'client' });
869
+ await own.setAudience(A, studio, 'secret-plan', 'internal');
870
+ assert.deepEqual(await kv.hgetall(keyOf(A, 'audiences')), {});
806
871
  for (const garbage of ['Client', 'internal', '', '"client"', 'true', ' client']) {
807
- await kv.hset(KEY.audiences, 'secret-plan', garbage);
808
- assert.equal(await own.audienceOf('secret-plan'), 'internal', garbage);
872
+ await kv.hset(keyOf(A, 'audiences'), 'secret-plan', garbage);
873
+ assert.equal(await own.audienceOf(A, 'secret-plan'), 'internal', garbage);
809
874
  }
810
- await kv.hset(KEY.audiences, 'sample-page', 'yes');
811
- await kv.hset(KEY.audiences, 'guide', 'client');
812
- await kv.hset(KEY.audiences, 'Not_A_Slug', 'client');
813
- await kv.hset(KEY.audiences, 'sample-social', 'client');
814
- assert.deepEqual(await own.visiblePrototypes(studio), { view: 'studio', clientFacing: ['sample-social'] });
875
+ await kv.hset(keyOf(A, 'audiences'), 'sample-page', 'yes');
876
+ await kv.hset(keyOf(A, 'audiences'), 'guide', 'client');
877
+ await kv.hset(keyOf(A, 'audiences'), 'Not_A_Slug', 'client');
878
+ await kv.hset(keyOf(A, 'audiences'), 'sample-social', 'client');
879
+ assert.deepEqual(await own.visiblePrototypes(A, studio), {
880
+ view: 'studio',
881
+ clientFacing: ['sample-social'],
882
+ });
815
883
  // A name that can never be a prototype is internal without a store read.
816
- assert.equal(await own.audienceOf('guide'), 'internal');
817
- assert.equal(await own.audienceOf('all.js'), 'internal');
884
+ assert.equal(await own.audienceOf(A, 'guide'), 'internal');
885
+ assert.equal(await own.audienceOf(A, 'all.js'), 'internal');
818
886
  });
819
887
 
820
888
  test('only Studio sets an audience: a Client is forbidden, a signed-out caller unauthorized, whatever the input', async () => {
821
- await rejects(review.setAudience(client, 'secret-plan', 'client'), 'forbidden');
822
- await rejects(review.setAudience(client, 'nope!', 'garbage'), 'forbidden');
823
- await rejects(review.setAudience(null, 'secret-plan', 'client'), 'unauthorized');
824
- const revoked = await review.revoke(studio, client.id);
825
- await rejects(review.setAudience({ ...revoked, kind: 'studio' }, 'secret-plan', 'client'), 'forbidden');
826
- assert.equal(await review.audienceOf('secret-plan'), 'internal');
889
+ await rejects(review.setAudience(A, client, 'secret-plan', 'client'), 'forbidden');
890
+ await rejects(review.setAudience(A, client, 'nope!', 'garbage'), 'forbidden');
891
+ await rejects(review.setAudience(A, null, 'secret-plan', 'client'), 'unauthorized');
892
+ const revoked = await review.revoke(A, studio, client.id);
893
+ await rejects(
894
+ review.setAudience(A, { ...revoked, kind: 'studio', client: null }, 'secret-plan', 'client'),
895
+ 'forbidden',
896
+ );
897
+ assert.equal(await review.audienceOf(A, 'secret-plan'), 'internal');
827
898
  });
828
899
 
829
900
  test('a prototype’s audience is checked: the slug and the value, every problem named', async () => {
830
901
  await assert.rejects(
831
- review.setAudience(studio, 'Bad Slug', 'everyone'),
902
+ review.setAudience(A, studio, 'Bad Slug', 'everyone'),
832
903
  (e: unknown) =>
833
904
  e instanceof ReviewError &&
834
905
  e.code === 'invalid' &&
835
906
  e.problems.join('|') === 'prototype: a prototype slug|audience: one of internal, client',
836
907
  );
837
908
  for (const reserved of ['guide', 'api', 'assets'])
838
- await rejects(review.setAudience(studio, reserved, 'client'), 'invalid');
839
- await rejects(review.setAudience(studio, 'sample-list', null), 'invalid');
840
- await rejects(review.setAudience(studio, 'sample-list', undefined), 'invalid');
909
+ await rejects(review.setAudience(A, studio, reserved, 'client'), 'invalid');
910
+ await rejects(review.setAudience(A, studio, 'sample-list', null), 'invalid');
911
+ await rejects(review.setAudience(A, studio, 'sample-list', undefined), 'invalid');
841
912
  });
842
913
 
843
914
  test('the index read: Studio’s view, Client view, and a Client’s, each with the client-facing slugs', async () => {
844
- await review.setAudience(studio, 'sample-page', 'client');
845
- await review.setAudience(studio, 'sample-list', 'client');
915
+ await review.setAudience(A, studio, 'sample-page', 'client');
916
+ await review.setAudience(A, studio, 'sample-list', 'client');
846
917
  const facing = ['sample-list', 'sample-page'];
847
- assert.deepEqual(await review.visiblePrototypes(studio), { view: 'studio', clientFacing: facing });
848
- assert.deepEqual(await review.visiblePrototypes(studio, 'studio'), {
918
+ assert.deepEqual(await review.visiblePrototypes(A, studio), { view: 'studio', clientFacing: facing });
919
+ assert.deepEqual(await review.visiblePrototypes(A, studio, 'studio'), {
849
920
  view: 'studio',
850
921
  clientFacing: facing,
851
922
  });
852
- assert.deepEqual(await review.visiblePrototypes(studio, 'client'), {
923
+ assert.deepEqual(await review.visiblePrototypes(A, studio, 'client'), {
853
924
  view: 'client',
854
925
  clientFacing: facing,
855
926
  });
856
927
  // A Client asking for Studio's view still gets a Client's.
857
- assert.deepEqual(await review.visiblePrototypes(client), { view: 'client', clientFacing: facing });
858
- assert.deepEqual(await review.visiblePrototypes(client, 'studio'), {
928
+ assert.deepEqual(await review.visiblePrototypes(A, client), { view: 'client', clientFacing: facing });
929
+ assert.deepEqual(await review.visiblePrototypes(A, client, 'studio'), {
859
930
  view: 'client',
860
931
  clientFacing: facing,
861
932
  });
862
- await rejects(review.visiblePrototypes(studio, 'everyone'), 'invalid');
863
- await rejects(review.visiblePrototypes(null), 'unauthorized');
864
- await rejects(review.visiblePrototypes(null, 'client'), 'unauthorized');
933
+ await rejects(review.visiblePrototypes(A, studio, 'everyone'), 'invalid');
934
+ await rejects(review.visiblePrototypes(A, null), 'unauthorized');
935
+ await rejects(review.visiblePrototypes(A, null, 'client'), 'unauthorized');
865
936
  });
866
937
 
867
938
  /** A client-facing thread with a Client's reply on it, then the prototype switched back to internal. */
868
939
  async function onInternalPrototype() {
869
- const root = await review.add(studio, { body: 'For the client', anchor, audience: 'client' });
870
- const reply = await review.reply(client, { prototype: anchor.prototype, thread: root.id, body: 'Seen' });
871
- await review.setAudience(studio, anchor.prototype, 'internal');
940
+ const root = await review.add(A, studio, { body: 'For the client', anchor, audience: 'client' });
941
+ const reply = await review.reply(A, client, { prototype: anchor.prototype, thread: root.id, body: 'Seen' });
942
+ await review.setAudience(A, studio, anchor.prototype, 'internal');
872
943
  return { root, reply };
873
944
  }
874
945
 
875
946
  test('a Client’s list of an internal prototype is empty, as if it had no comments, and so is Studio’s Client view', async () => {
876
947
  await onInternalPrototype();
877
- assert.deepEqual(await review.listComments(client, anchor.prototype), []);
878
- assert.deepEqual(await review.listComments(client, anchor.prototype, 'studio'), []);
879
- assert.deepEqual(await review.listComments(studio, anchor.prototype, 'client'), []);
948
+ assert.deepEqual(await review.listComments(A, client, anchor.prototype), []);
949
+ assert.deepEqual(await review.listComments(A, client, anchor.prototype, 'studio'), []);
950
+ assert.deepEqual(await review.listComments(A, studio, anchor.prototype, 'client'), []);
880
951
  // The same answer as a prototype nobody made.
881
- assert.deepEqual(await review.listComments(client, 'no-such-thing'), []);
882
- assert.equal((await review.listComments(studio, anchor.prototype)).length, 2);
883
- assert.deepEqual(await review.allComments(client), []);
884
- assert.equal((await review.allComments(studio)).length, 2);
952
+ assert.deepEqual(await review.listComments(A, client, 'no-such-thing'), []);
953
+ assert.equal((await review.listComments(A, studio, anchor.prototype)).length, 2);
954
+ assert.deepEqual(await review.allComments(A, client), []);
955
+ assert.equal((await review.allComments(A, studio)).length, 2);
885
956
  // Client-facing again: the thread is back for them, with its reply.
886
- await review.setAudience(studio, anchor.prototype, 'client');
887
- assert.equal((await review.listComments(client, anchor.prototype)).length, 2);
957
+ await review.setAudience(A, studio, anchor.prototype, 'client');
958
+ assert.equal((await review.listComments(A, client, anchor.prototype)).length, 2);
888
959
  });
889
960
 
890
961
  test('a Client acting on a client-facing thread of an internal prototype is told it does not exist, by every op', async () => {
891
962
  const { root, reply } = await onInternalPrototype();
892
963
  const at = { prototype: anchor.prototype };
893
964
  for (const id of [root.id, reply.id]) {
894
- await rejects(review.reply(client, { ...at, thread: id, body: 'x' }), 'not_found');
895
- await rejects(review.edit(client, { ...at, id, body: 'x' }), 'not_found');
896
- await rejects(review.remove(client, { ...at, id }), 'not_found');
897
- await rejects(review.resolve(client, { ...at, id, resolved: true }), 'not_found');
898
- await rejects(review.resolve(client, { ...at, id, resolved: false }), 'not_found');
899
- await rejects(review.retriage(client, { ...at, id, triage: { kind: 'bug' } }), 'forbidden');
900
- assert.equal(await review.locate(client, id), null);
901
- assert.ok(await review.locate(studio, id));
965
+ await rejects(review.reply(A, client, { ...at, thread: id, body: 'x' }), 'not_found');
966
+ await rejects(review.edit(A, client, { ...at, id, body: 'x' }), 'not_found');
967
+ await rejects(review.remove(A, client, { ...at, id }), 'not_found');
968
+ await rejects(review.resolve(A, client, { ...at, id, resolved: true }), 'not_found');
969
+ await rejects(review.resolve(A, client, { ...at, id, resolved: false }), 'not_found');
970
+ await rejects(review.retriage(A, client, { ...at, id, triage: { kind: 'bug' } }), 'forbidden');
971
+ assert.equal(await review.locate(A, client, id), null);
972
+ assert.ok(await review.locate(A, studio, id));
902
973
  }
903
974
  // The same answers as an id, and a prototype, that are not there at all.
904
- await rejects(review.edit(client, { ...at, id: 'nope', body: 'x' }), 'not_found');
905
- await rejects(review.edit(client, { prototype: 'no-such-thing', id: 'nope', body: 'x' }), 'not_found');
975
+ await rejects(review.edit(A, client, { ...at, id: 'nope', body: 'x' }), 'not_found');
976
+ await rejects(review.edit(A, client, { prototype: 'no-such-thing', id: 'nope', body: 'x' }), 'not_found');
906
977
  // A new thread there is refused the same way, and nothing is written or counted.
907
- await rejects(review.add(client, { body: 'Hello?', anchor }), 'not_found');
978
+ await rejects(review.add(A, client, { body: 'Hello?', anchor }), 'not_found');
908
979
  await rejects(
909
- review.add(client, { body: 'Hello?', anchor: { ...anchor, prototype: 'no-such-thing' } }),
980
+ review.add(A, client, { body: 'Hello?', anchor: { ...anchor, prototype: 'no-such-thing' } }),
910
981
  'not_found',
911
982
  );
912
- const after = await review.listComments(studio, anchor.prototype);
983
+ const after = await review.listComments(A, studio, anchor.prototype);
913
984
  assert.deepEqual(
914
985
  after.map((c) => [c.body, c.status]),
915
986
  [
@@ -918,6 +989,1138 @@ test('a Client acting on a client-facing thread of an internal prototype is told
918
989
  ],
919
990
  );
920
991
  // Studio, writing as Studio from Client view, still works there.
921
- assert.ok(await review.add(studio, { body: 'Internal note', anchor }));
922
- assert.ok(await review.reply(studio, { ...at, thread: root.id, body: 'Studio reply' }));
992
+ assert.ok(await review.add(A, studio, { body: 'Internal note', anchor }));
993
+ assert.ok(await review.reply(A, studio, { ...at, thread: root.id, body: 'Studio reply' }));
994
+ });
995
+
996
+ // Slice 6: what Studio marks on a prototype's dials and variants.
997
+ test('controls: Studio marks a dial either way and a variant on or off, one field each', async () => {
998
+ const kv = memoryKV(LIVE);
999
+ const own = createReview(kv);
1000
+ const mark = (kind: string, id: string, audience: string) =>
1001
+ own.setControl(A, studio, { prototype: 'sample-list', kind, id, audience });
1002
+ assert.deepEqual(await own.controlsOf(A, studio, 'sample-list'), {
1003
+ view: 'studio',
1004
+ dials: {},
1005
+ variants: [],
1006
+ });
1007
+ assert.deepEqual(await mark('dial', 'Review/density', 'client'), {
1008
+ kind: 'dial',
1009
+ id: 'Review/density',
1010
+ audience: 'client',
1011
+ });
1012
+ // Internal is stored for a dial: it overrules a client() in the code.
1013
+ await mark('dial', 'Review/avatars', 'internal');
1014
+ await mark('variant', 'Lift', 'client');
1015
+ await mark('variant', 'Sink', 'client');
1016
+ await mark('variant', 'Sink', 'internal');
1017
+ // Unmarking a variant never marked is no error.
1018
+ await mark('variant', 'Baseline', 'internal');
1019
+ assert.deepEqual(await kv.hgetall(keyOf(A, 'controls', 'sample-list')), {
1020
+ 'dial:Review/density': 'client',
1021
+ 'dial:Review/avatars': 'internal',
1022
+ 'variant:Lift': 'client',
1023
+ });
1024
+ assert.deepEqual(await own.controlsOf(A, studio, 'sample-list'), {
1025
+ view: 'studio',
1026
+ dials: { 'Review/density': 'client', 'Review/avatars': 'internal' },
1027
+ variants: ['Lift'],
1028
+ });
1029
+ // Marks are per prototype.
1030
+ assert.deepEqual(await own.controlsOf(A, studio, 'sample-page'), {
1031
+ view: 'studio',
1032
+ dials: {},
1033
+ variants: [],
1034
+ });
1035
+ });
1036
+
1037
+ test('controls: only Studio marks, and every field is checked', async () => {
1038
+ const input = { prototype: 'sample-list', kind: 'dial', id: 'Review/density', audience: 'client' };
1039
+ await rejects(review.setControl(A, client, input), 'forbidden');
1040
+ await rejects(review.setControl(A, null, input), 'unauthorized');
1041
+ await assert.rejects(
1042
+ review.setControl(A, studio, { prototype: 'guide', kind: 'knob', id: ' x', audience: 'all' }),
1043
+ (e: unknown) =>
1044
+ e instanceof ReviewError &&
1045
+ e.code === 'invalid' &&
1046
+ e.problems.join('|') ===
1047
+ [
1048
+ 'prototype: a prototype slug',
1049
+ 'kind: one of dial, variant',
1050
+ 'id: the control’s name, one line of at most 200 characters',
1051
+ 'audience: one of internal, client',
1052
+ ].join('|'),
1053
+ );
1054
+ assert.deepEqual((await review.controlsOf(A, studio, 'sample-list')).dials, {});
1055
+ });
1056
+
1057
+ test('controls: a Client reads the marks of a client-facing prototype, and none of an internal one', async () => {
1058
+ await review.setControl(A, studio, {
1059
+ prototype: 'sample-list',
1060
+ kind: 'dial',
1061
+ id: 'Review/density',
1062
+ audience: 'client',
1063
+ });
1064
+ await review.setControl(A, studio, {
1065
+ prototype: 'secret-plan',
1066
+ kind: 'variant',
1067
+ id: 'Lift',
1068
+ audience: 'client',
1069
+ });
1070
+ assert.deepEqual(await review.controlsOf(A, client, 'sample-list'), {
1071
+ view: 'client',
1072
+ dials: { 'Review/density': 'client' },
1073
+ variants: [],
1074
+ });
1075
+ // The same answer as for a prototype that does not exist.
1076
+ await rejects(review.controlsOf(A, client, 'secret-plan'), 'not_found');
1077
+ await rejects(review.controlsOf(A, client, 'never-made'), 'not_found');
1078
+ await rejects(review.controlsOf(A, null, 'sample-list'), 'unauthorized');
1079
+ await rejects(review.controlsOf(A, studio, 'guide'), 'invalid');
1080
+ assert.deepEqual((await review.controlsOf(A, studio, 'secret-plan')).variants, ['Lift']);
1081
+ });
1082
+
1083
+ // Scopes: one client never reaches another's anything.
1084
+ const B: Scope = { client: 'bravo', project: 'launch' };
1085
+ /** A client and project nobody ever wrote to. */
1086
+ const EMPTY: Scope = { client: 'zulu', project: 'void' };
1087
+
1088
+ /** What an op answered: its value, or its error's code, message and problems. */
1089
+ const outcome = (p: Promise<unknown>) =>
1090
+ p.then(
1091
+ (value) => ({ value }),
1092
+ (e: unknown) => {
1093
+ if (!(e instanceof ReviewError)) throw e;
1094
+ return { code: e.code, message: e.message, problems: e.problems };
1095
+ },
1096
+ );
1097
+
1098
+ /** Client B's own things: a Client, a client-facing thread with a reply, and a marked dial. */
1099
+ async function bravo() {
1100
+ await review.addClient(studio, { slug: 'bravo', host: 'bravo.studio.test', name: 'Bravo' });
1101
+ await review.addProject(B, studio, { slug: 'launch', name: 'Launch' });
1102
+ await review.startBuild(B, studio, 'b'.repeat(16));
1103
+ await review.setBuild(B, studio, 'b'.repeat(16));
1104
+ const bee = await review.addPerson(B, studio, { name: 'Bea', team: 'Bravo' });
1105
+ await review.setAudience(B, studio, anchor.prototype, 'client');
1106
+ const thread = await review.add(B, bee.person, { body: 'Bravo’s note', anchor });
1107
+ const reply = await review.reply(B, studio, {
1108
+ prototype: anchor.prototype,
1109
+ thread: thread.id,
1110
+ body: 'Seen',
1111
+ });
1112
+ await review.setControl(B, studio, {
1113
+ prototype: anchor.prototype,
1114
+ kind: 'dial',
1115
+ id: 'Card/x',
1116
+ audience: 'client',
1117
+ });
1118
+ return { bee, thread, reply };
1119
+ }
1120
+
1121
+ type Ids = { person: string; thread: string; token: string };
1122
+ const at = (ids: Ids) => ({ prototype: anchor.prototype, id: ids.thread });
1123
+ /** Every op with an actor, each run with the ids it names. */
1124
+ const OPS: [string, (s: Scope, actor: Person, ids: Ids) => Promise<unknown>][] = [
1125
+ ['listPeople', (s, a) => review.listPeople(s, a)],
1126
+ ['addPerson', (s, a) => review.addPerson(s, a, { name: 'Oz', team: 'Bravo' })],
1127
+ ['newLink', (s, a, ids) => review.newLink(s, a, ids.person)],
1128
+ ['signOutEverywhere', (s, a, ids) => review.signOutEverywhere(s, a, ids.person)],
1129
+ ['revoke', (s, a, ids) => review.revoke(s, a, ids.person)],
1130
+ ['setAccess', (s, a, ids) => review.setAccess(s, a, ids.person, 'open')],
1131
+ ['defaultAccess', (s, a) => review.defaultAccess(s, a)],
1132
+ ['setDefaultAccess', (s, a) => review.setDefaultAccess(s, a, 'open')],
1133
+ ['listComments', (s, a) => review.listComments(s, a, anchor.prototype)],
1134
+ ['allComments', (s, a) => review.allComments(s, a)],
1135
+ ['add', (s, a) => review.add(s, a, { body: 'x', anchor })],
1136
+ [
1137
+ 'reply',
1138
+ (s, a, ids) => review.reply(s, a, { prototype: anchor.prototype, thread: ids.thread, body: 'x' }),
1139
+ ],
1140
+ ['edit', (s, a, ids) => review.edit(s, a, { ...at(ids), body: 'x' })],
1141
+ ['remove', (s, a, ids) => review.remove(s, a, at(ids))],
1142
+ ['resolve', (s, a, ids) => review.resolve(s, a, { ...at(ids), resolved: true })],
1143
+ ['retriage', (s, a, ids) => review.retriage(s, a, { ...at(ids), triage: { kind: 'bug' } })],
1144
+ ['setAudience', (s, a) => review.setAudience(s, a, anchor.prototype, 'internal')],
1145
+ ['controlsOf', (s, a) => review.controlsOf(s, a, anchor.prototype)],
1146
+ [
1147
+ 'setControl',
1148
+ (s, a) =>
1149
+ review.setControl(s, a, {
1150
+ prototype: anchor.prototype,
1151
+ kind: 'dial',
1152
+ id: 'Card/x',
1153
+ audience: 'internal',
1154
+ }),
1155
+ ],
1156
+ ['visiblePrototypes', (s, a) => review.visiblePrototypes(s, a)],
1157
+ ['locate', (s, a, ids) => review.locate(s, a, ids.thread)],
1158
+ ['listProjects', (s, a) => review.listProjects(s, a)],
1159
+ ['addProject', (s, a) => review.addProject(s, a, { slug: 'next', name: 'Next' })],
1160
+ ['startBuild', (s, a) => review.startBuild(s, a, 'c'.repeat(16))],
1161
+ ['setBuild', (s, a) => review.setBuild(s, a, 'c'.repeat(16))],
1162
+ ['buildsOf', (s, a) => review.buildsOf(s, a)],
1163
+ ['forgetBuilds', (s, a) => review.forgetBuilds(s, a, ['c'.repeat(16)])],
1164
+ [
1165
+ 'addClient',
1166
+ (s, a) => review.addClient(a, { slug: s.client, host: `${s.client}.elsewhere.test`, name: 'X' }),
1167
+ ],
1168
+ ['clientRecord', (s, a) => review.clientRecord(s, a)],
1169
+ ['setClientAccess', (s, a) => review.setClientAccess(s, a, 'open')],
1170
+ ['archiveClient', (s, a) => review.archiveClient(s, a)],
1171
+ ['forgetClient', (s, a) => review.forgetClient(s, a)],
1172
+ ['startProof', (s, a) => review.startProof(s, a)],
1173
+ ['endProof', (s, a) => review.endProof(s, a)],
1174
+ ];
1175
+ /** The ops on the studio as a whole, which name no client: Studio's alone, refused to a Client before anything is read. */
1176
+ const STUDIO_WIDE: [string, (actor: Person | null) => Promise<unknown>][] = [
1177
+ ['addStudioPerson', (a) => review.addStudioPerson(a, { name: 'Oz', team: 'Studio' })],
1178
+ ['newStudioLink', (a) => review.newStudioLink(a, studio.id)],
1179
+ ['studioAccess', (a) => review.studioAccess(a)],
1180
+ ['setStudioAccess', (a) => review.setStudioAccess(a, 'open')],
1181
+ ['clientWait', (a) => review.clientWait(a, { slug: 'acme', host: 'acme.studio.test' })],
1182
+ ];
1183
+ /** The ops that name an id, whose answer to an unknown one is what out of scope must match. */
1184
+ const BY_ID = new Set([
1185
+ 'newLink',
1186
+ 'signOutEverywhere',
1187
+ 'revoke',
1188
+ 'setAccess',
1189
+ 'reply',
1190
+ 'edit',
1191
+ 'remove',
1192
+ 'resolve',
1193
+ 'retriage',
1194
+ 'locate',
1195
+ ]);
1196
+ const UNKNOWN: Ids = { person: 'nope', thread: 'nope', token: 'b'.repeat(43) };
1197
+ /** The ops that take no actor, so the cross-client test cannot run them as client A’s person. */
1198
+ const ACTORLESS = [
1199
+ 'person',
1200
+ 'sessionPerson',
1201
+ 'peekLink',
1202
+ 'useLink',
1203
+ 'audienceOf',
1204
+ 'clientOfHost',
1205
+ 'projectOf',
1206
+ 'proofAnswer',
1207
+ ];
1208
+
1209
+ test('every op of the review service is in the cross-client test or named as having no actor', () => {
1210
+ const listed = [...OPS.map(([name]) => name), ...STUDIO_WIDE.map(([name]) => name), ...ACTORLESS].sort();
1211
+ assert.deepEqual(Object.keys(createReview(memoryKV())).sort(), listed);
1212
+ });
1213
+
1214
+ test('every op from client A’s person on client B’s scope answers as an unknown id does, and changes nothing', async () => {
1215
+ let snapshot = '';
1216
+ review = createReview(
1217
+ memoryKV(live('acme'), (s) => {
1218
+ snapshot = JSON.stringify(s);
1219
+ }),
1220
+ );
1221
+ studio = (
1222
+ await review.addPerson(A, { ...studio, id: 'cli' }, { name: 'Ed', team: 'Studio', kind: 'studio' })
1223
+ ).person;
1224
+ client = (await review.addPerson(A, studio, { name: 'Rae', team: 'Acme' })).person;
1225
+ const b = await bravo();
1226
+ const real: Ids = { person: b.bee.person.id, thread: b.thread.id, token: b.bee.token };
1227
+ for (const ids of [real, { ...real, thread: b.reply.id }, { ...real, person: studio.id }]) {
1228
+ for (const [name, op] of OPS) {
1229
+ const before = snapshot;
1230
+ const out = await outcome(op(B, client, ids));
1231
+ assert.equal(snapshot, before, `${name} wrote nothing`);
1232
+ // The same answer whether B holds anything or not.
1233
+ assert.deepEqual(out, await outcome(op(EMPTY, client, ids)), `${name}: B and an empty client`);
1234
+ if (BY_ID.has(name))
1235
+ assert.deepEqual(out, await outcome(op(B, studio, UNKNOWN)), `${name}: an unknown id`);
1236
+ if (name === 'locate') assert.deepEqual(out, { value: null });
1237
+ else assert.equal((out as { code?: string }).code, 'not_found', name);
1238
+ }
1239
+ }
1240
+ // B's things are all still there, for B.
1241
+ assert.equal((await review.listComments(B, b.bee.person, anchor.prototype)).length, 2);
1242
+ assert.equal((await review.peekLink(B, b.bee.token))?.id, b.bee.person.id);
1243
+ });
1244
+
1245
+ /** The ops on a client's record itself, which run while it is absent or archived: adding, reading, archiving, forgetting it. */
1246
+ const ON_RECORD = new Set(['addClient', 'clientRecord', 'archiveClient', 'forgetClient']);
1247
+
1248
+ test('every op in a client archived, forgotten or never added answers as an unknown id, and writes nothing', async () => {
1249
+ let snapshot = '';
1250
+ review = createReview(
1251
+ memoryKV(undefined, (s) => {
1252
+ snapshot = JSON.stringify(s);
1253
+ }),
1254
+ );
1255
+ studio = await review.addStudioPerson({ ...studio, id: 'cli' }, { name: 'Ed', team: 'Studio' });
1256
+ const b = await bravo();
1257
+ const real: Ids = { person: b.bee.person.id, thread: b.thread.id, token: b.bee.token };
1258
+ const dead = async (state: string) => {
1259
+ for (const ids of [real, UNKNOWN])
1260
+ for (const [name, op] of OPS.filter(([name]) => !ON_RECORD.has(name))) {
1261
+ const scope = state === 'never added' ? EMPTY : B;
1262
+ const before = snapshot;
1263
+ const out = await outcome(op(scope, studio, ids));
1264
+ assert.equal(snapshot, before, `${state}: ${name} wrote nothing`);
1265
+ if (name === 'locate') assert.deepEqual(out, { value: null }, `${state}: ${name}`);
1266
+ else assert.equal((out as { code?: string }).code, 'not_found', `${state}: ${name}`);
1267
+ }
1268
+ };
1269
+ await review.archiveClient(B, studio);
1270
+ await dead('archived');
1271
+ await review.forgetClient(B, studio);
1272
+ await dead('forgotten');
1273
+ await dead('never added');
1274
+ });
1275
+
1276
+ test('a client added over keys an earlier client of its slug left starts clean, every kind of key', async () => {
1277
+ let seen: Snapshot = { hashes: {}, sets: {} };
1278
+ const kv = memoryKV(undefined, (s) => {
1279
+ seen = s;
1280
+ });
1281
+ const r = createReview(kv);
1282
+ const ed = await r.addStudioPerson({ ...studio, id: 'cli' }, { name: 'Ed', team: 'Studio' });
1283
+ // An earlier acme with one of everything: people, a revocation, an epoch, links, a project and
1284
+ // its keys, keys of a project no record names, its own default access, a host proof under way.
1285
+ await r.addClient(ed, { slug: 'acme', host: 'acme.studio.test', name: 'Old' });
1286
+ await r.addProject({ client: 'acme' }, ed, { slug: 'rebrand', name: 'Rebrand' });
1287
+ await r.startBuild(A, ed, 'a'.repeat(16));
1288
+ await r.setBuild(A, ed, 'a'.repeat(16));
1289
+ const bob = await r.addPerson(A, ed, { name: 'Bob', team: 'Old' });
1290
+ const gone = await r.addPerson(A, ed, { name: 'Gone', team: 'Old' });
1291
+ await r.revoke(A, ed, gone.person.id);
1292
+ await r.signOutEverywhere(A, ed, bob.person.id);
1293
+ const spare = await r.newLink(A, ed, bob.person.id);
1294
+ const unlisted = { client: 'acme', project: 'atlas' };
1295
+ for (const at of [A, unlisted]) {
1296
+ await r.setAudience(at, ed, anchor.prototype, 'client');
1297
+ await r.add(at, bob.person, { body: 'Old note', anchor });
1298
+ await r.setControl(at, ed, { prototype: 'marked-only', kind: 'dial', id: 'Card/x', audience: 'client' });
1299
+ }
1300
+ await r.setDefaultAccess(A, ed, 'open');
1301
+ await r.startProof(A, ed);
1302
+ // The record goes and the keys stay, as if the archive had never swept them, or a write landed after it.
1303
+ await kv.hdel(keyOf('studio', 'clients'), 'acme');
1304
+ await kv.hdel(keyOf('studio', 'hosts'), 'acme.studio.test');
1305
+ await kv.hdel(keyOf('studio', 'claims'), 'client:acme');
1306
+ await kv.hdel(keyOf('studio', 'claims'), 'host:acme.studio.test');
1307
+ // And no index names what is left: no project list, no prototype set, no audiences.
1308
+ await kv.del(keyOf({ client: 'acme' }, 'projects'));
1309
+ for (const at of [A, unlisted]) {
1310
+ await kv.del(keyOf(at, 'prototypes'));
1311
+ await kv.del(keyOf(at, 'audiences'));
1312
+ }
1313
+ const left = (s: Snapshot) =>
1314
+ [
1315
+ ...Object.entries(s.hashes).filter(([, v]) => Object.keys(v).length),
1316
+ ...Object.entries(s.sets).filter(([, v]) => v.length),
1317
+ ]
1318
+ .map(([k]) => k)
1319
+ .filter((k) => k.startsWith('hub4:c:acme:'));
1320
+ assert.ok(left(seen).length >= 10, 'every kind of key is there to begin with');
1321
+
1322
+ const fresh = await r.addClient(ed, { slug: 'acme', host: 'acme.studio.test', name: 'New' });
1323
+ assert.equal(fresh.access, null);
1324
+ // Nothing of the client is left, a project no record named included.
1325
+ assert.deepEqual(left(seen), []);
1326
+ assert.deepEqual(await r.listPeople(A, ed), [ed]);
1327
+ assert.equal(await r.useLink(A, bob.token), null);
1328
+ assert.equal(await r.useLink(A, spare.token), null);
1329
+ assert.equal(await r.sessionPerson(A, bob.person.id, 1), null);
1330
+ assert.equal(await r.person(A, gone.person.id), null);
1331
+ assert.deepEqual(await r.listProjects({ client: 'acme' }, ed), []);
1332
+ assert.equal(await r.projectOf(A), null);
1333
+ assert.equal(await r.defaultAccess(A, ed), '3m');
1334
+ assert.deepEqual(await kv.hgetall(keyOf({ client: 'acme' }, 'proof')), {});
1335
+ await r.addProject({ client: 'acme' }, ed, { slug: 'rebrand', name: 'Rebrand' });
1336
+ await r.addProject({ client: 'acme' }, ed, { slug: 'atlas', name: 'Atlas' });
1337
+ assert.deepEqual(
1338
+ left(seen).filter((k) => k.includes(':p:')),
1339
+ [],
1340
+ );
1341
+ for (const at of [A, unlisted]) {
1342
+ assert.deepEqual(await r.listComments(at, ed, anchor.prototype), []);
1343
+ assert.equal(await r.audienceOf(at, anchor.prototype), 'internal');
1344
+ assert.deepEqual((await r.controlsOf(at, ed, 'marked-only')).dials, {});
1345
+ assert.equal((await r.add(at, ed, { body: 'New', anchor })).number, 1);
1346
+ }
1347
+ });
1348
+
1349
+ test('the local store’s client, which has no record, runs every op as before, and no studio may add a client by its name', async () => {
1350
+ const kv = memoryKV();
1351
+ const r = createReview(kv);
1352
+ const SITE: Scope = { client: 'site', project: 'site' };
1353
+ const ed = (
1354
+ await r.addPerson(SITE, { ...studio, id: 'cli' }, { name: 'Ed', team: 'Studio', kind: 'studio' })
1355
+ ).person;
1356
+ const rae = await r.addPerson(SITE, ed, { name: 'Rae', team: 'Acme' });
1357
+ assert.equal((await r.useLink(SITE, rae.token))?.name, 'Rae');
1358
+ assert.equal((await r.newLink(SITE, ed, rae.person.id)).person.id, rae.person.id);
1359
+ await r.setAudience(SITE, ed, anchor.prototype, 'client');
1360
+ const thread = await r.add(SITE, rae.person, { body: 'x', anchor });
1361
+ await r.reply(SITE, ed, { prototype: anchor.prototype, thread: thread.id, body: 'y' });
1362
+ await r.resolve(SITE, rae.person, { prototype: anchor.prototype, id: thread.id, resolved: true });
1363
+ assert.equal((await r.listComments(SITE, rae.person, anchor.prototype)).length, 2);
1364
+ assert.equal((await r.locate(SITE, ed, thread.id))?.prototype, anchor.prototype);
1365
+ // Its default access is the studio's, read and set.
1366
+ assert.equal(await r.setDefaultAccess(SITE, ed, 'open'), 'open');
1367
+ assert.equal(await r.studioAccess(ed), 'open');
1368
+ assert.equal(await r.defaultAccess(SITE, ed), 'open');
1369
+ assert.ok((await r.revoke(SITE, ed, rae.person.id)).revokedAt);
1370
+ assert.equal(await kv.hget(keyOf('studio', 'clients'), 'site'), null);
1371
+ await rejects(r.addClient(ed, { slug: 'site', host: 'site.studio.test', name: 'Site' }), 'invalid');
1372
+ });
1373
+
1374
+ test('a session, a link and a person of client A are nobody on client B; Studio is someone on both', async () => {
1375
+ const bee = await review.addPerson(B, studio, { name: 'Bea', team: 'Bravo' });
1376
+ assert.equal(await review.person(B, client.id), null);
1377
+ assert.equal(await review.sessionPerson(B, client.id, 0), null);
1378
+ assert.equal(await review.peekLink(B, clientToken), null);
1379
+ assert.equal(await review.useLink(B, clientToken), null);
1380
+ assert.equal(await review.useLink(A, bee.token), null);
1381
+ // Refused elsewhere, the link still works once where it belongs.
1382
+ assert.equal((await review.useLink(A, clientToken))?.id, client.id);
1383
+ assert.equal((await review.useLink(B, bee.token))?.client, 'bravo');
1384
+ assert.ok(await review.sessionPerson(A, client.id, 0));
1385
+ // A Studio person and their link reach every client.
1386
+ const ed = await review.addPerson(A, studio, { name: 'Jo', team: 'Studio', kind: 'studio' });
1387
+ assert.equal(ed.person.client, null);
1388
+ assert.ok(await review.sessionPerson(B, studio.id, 0));
1389
+ assert.ok(await review.sessionPerson(EMPTY, studio.id, 0));
1390
+ assert.equal((await review.useLink(B, ed.token))?.id, ed.person.id);
1391
+ // People lists hold the scope's Clients and Studio, never another client's.
1392
+ const names = async (s: Scope) => (await review.listPeople(s, studio)).map((p) => p.name).sort();
1393
+ assert.deepEqual(await names(A), ['Ed', 'Jo', 'Rae']);
1394
+ assert.deepEqual(await names(B), ['Bea', 'Ed', 'Jo']);
1395
+ // Studio manages B's person from B's scope only.
1396
+ await rejects(review.revoke(A, studio, bee.person.id), 'not_found');
1397
+ assert.ok((await review.revoke(B, studio, bee.person.id)).revokedAt);
1398
+ });
1399
+
1400
+ test('a project’s comments, numbers, marks and audiences are its own, the same slug elsewhere included', async () => {
1401
+ const A2: Scope = { client: 'acme', project: 'other' };
1402
+ for (const s of [B, A2]) await review.setAudience(s, studio, anchor.prototype, 'client');
1403
+ const a = await review.add(A, client, { body: 'A', anchor });
1404
+ const b = await review.add(B, studio, { body: 'B', anchor, audience: 'client' });
1405
+ const a2 = await review.add(A2, client, { body: 'A2', anchor });
1406
+ assert.deepEqual([a.number, b.number, a2.number], [1, 1, 1]);
1407
+ const bodies = async (s: Scope) =>
1408
+ (await review.listComments(s, studio, anchor.prototype)).map((c) => c.body);
1409
+ assert.deepEqual([await bodies(A), await bodies(B), await bodies(A2)], [['A'], ['B'], ['A2']]);
1410
+ await rejects(review.edit(A, studio, { prototype: anchor.prototype, id: b.id, body: 'x' }), 'not_found');
1411
+ assert.equal(await review.locate(A, studio, b.id), null);
1412
+ await review.setAudience(B, studio, anchor.prototype, 'internal');
1413
+ assert.equal(await review.audienceOf(A, anchor.prototype), 'client');
1414
+ assert.equal(await review.audienceOf(B, anchor.prototype), 'internal');
1415
+ await review.setControl(B, studio, {
1416
+ prototype: anchor.prototype,
1417
+ kind: 'variant',
1418
+ id: 'Lift',
1419
+ audience: 'client',
1420
+ });
1421
+ assert.deepEqual((await review.controlsOf(A, studio, anchor.prototype)).variants, []);
1422
+ assert.deepEqual((await review.controlsOf(B, studio, anchor.prototype)).variants, ['Lift']);
1423
+ // A client's default access length is its own.
1424
+ await review.setDefaultAccess(B, studio, 'open');
1425
+ assert.equal(await review.defaultAccess(A, studio), '3m');
1426
+ });
1427
+
1428
+ test('every record lives under its own scope’s keys, and Studio’s under the studio’s', async () => {
1429
+ let seen: Snapshot = { hashes: {}, sets: {} };
1430
+ review = createReview(
1431
+ memoryKV(undefined, (s) => {
1432
+ seen = s;
1433
+ }),
1434
+ );
1435
+ await review.addClient({ ...studio, id: 'cli' }, { slug: 'acme', host: 'acme.studio.test', name: 'Acme' });
1436
+ const ed = (
1437
+ await review.addPerson(A, { ...studio, id: 'cli' }, { name: 'Ed', team: 'Studio', kind: 'studio' })
1438
+ ).person;
1439
+ await review.addProject(A, ed, { slug: 'rebrand', name: 'Rebrand' });
1440
+ const rae = (await review.addPerson(A, ed, { name: 'Rae', team: 'Acme' })).person;
1441
+ await review.setAudience(A, ed, anchor.prototype, 'client');
1442
+ await review.add(A, rae, { body: 'x', anchor });
1443
+ await review.setControl(A, ed, {
1444
+ prototype: anchor.prototype,
1445
+ kind: 'dial',
1446
+ id: 'Card/x',
1447
+ audience: 'client',
1448
+ });
1449
+ await review.setDefaultAccess(A, ed, '6m');
1450
+ await review.revoke(A, ed, rae.id);
1451
+ assert.deepEqual(Object.keys(seen.hashes).sort(), [
1452
+ 'hub4:c:acme:epochs',
1453
+ 'hub4:c:acme:links',
1454
+ 'hub4:c:acme:p:rebrand:audiences',
1455
+ 'hub4:c:acme:p:rebrand:comments:sample-list',
1456
+ 'hub4:c:acme:p:rebrand:controls:sample-list',
1457
+ 'hub4:c:acme:p:rebrand:counters',
1458
+ 'hub4:c:acme:people',
1459
+ 'hub4:c:acme:projects',
1460
+ 'hub4:c:acme:revoked',
1461
+ 'hub4:claims',
1462
+ 'hub4:clients',
1463
+ 'hub4:hosts',
1464
+ 'hub4:studio:links',
1465
+ 'hub4:studio:people',
1466
+ ]);
1467
+ assert.deepEqual(Object.keys(seen.sets), ['hub4:c:acme:p:rebrand:prototypes']);
1468
+ // A client's default access lives on its record.
1469
+ assert.equal(JSON.parse(seen.hashes['hub4:clients'].acme).access, '6m');
1470
+ // The client is where the records are, never a field in the profile.
1471
+ assert.equal('client' in JSON.parse(seen.hashes['hub4:c:acme:people'][rae.id]), false);
1472
+ });
1473
+
1474
+ test('a profile stored at the wrong home is no person: a Client with the studio, Studio with a client', async () => {
1475
+ const kv = memoryKV(LIVE);
1476
+ const r = createReview(kv);
1477
+ const rae = (await r.addPerson(A, studio, { name: 'Rae', team: 'Acme' })).person;
1478
+ const profile = JSON.parse((await kv.hget(keyOf(A, 'people'), rae.id)) ?? '{}');
1479
+ await kv.hset(keyOf('studio', 'people'), 'moved', JSON.stringify({ ...profile, id: 'moved' }));
1480
+ await kv.hset(keyOf(A, 'people'), 'boss', JSON.stringify({ ...profile, id: 'boss', kind: 'studio' }));
1481
+ for (const id of ['moved', 'boss']) {
1482
+ assert.equal(await r.person(A, id), null, id);
1483
+ assert.equal(await r.sessionPerson(A, id, 0), null, id);
1484
+ }
1485
+ assert.deepEqual(
1486
+ (await r.listPeople(A, studio)).map((p) => p.id),
1487
+ [rae.id],
1488
+ );
1489
+ });
1490
+
1491
+ test('a host names its client while the client is live and its records agree; anything else is no client', async () => {
1492
+ review = createReview(memoryKV());
1493
+ const acme = await review.addClient(studio, { slug: 'acme', host: 'acme.studio.test', name: 'Acme' });
1494
+ assert.deepEqual(acme, {
1495
+ slug: 'acme',
1496
+ host: 'acme.studio.test',
1497
+ name: 'Acme',
1498
+ access: null,
1499
+ createdAt: acme.createdAt,
1500
+ archivedAt: null,
1501
+ provenAt: null,
1502
+ });
1503
+ assert.equal((await review.clientOfHost('acme.studio.test'))?.slug, 'acme');
1504
+ assert.equal((await review.clientOfHost('ACME.studio.test:443'))?.slug, 'acme');
1505
+ for (const host of ['', 'nope.studio.test', 'studio.test', 'acme.studio.test.evil', 'a b'])
1506
+ assert.equal(await review.clientOfHost(host), null, host);
1507
+ // A host table entry the client's own record does not agree with, or an archived client, is no client.
1508
+ const kv = memoryKV();
1509
+ const r = createReview(kv);
1510
+ await r.addClient(studio, { slug: 'acme', host: 'acme.studio.test', name: 'Acme' });
1511
+ await kv.hset(keyOf('studio', 'hosts'), 'alias.studio.test', 'acme');
1512
+ await kv.hset(keyOf('studio', 'hosts'), 'odd.studio.test', 'ACME:x');
1513
+ assert.equal(await r.clientOfHost('alias.studio.test'), null);
1514
+ assert.equal(await r.clientOfHost('odd.studio.test'), null);
1515
+ const stored = JSON.parse((await kv.hget(keyOf('studio', 'clients'), 'acme')) ?? '{}');
1516
+ await kv.hset(keyOf('studio', 'clients'), 'acme', JSON.stringify({ ...stored, archivedAt: 'then' }));
1517
+ assert.equal(await r.clientOfHost('acme.studio.test'), null);
1518
+ await kv.hset(keyOf('studio', 'clients'), 'acme', 'not json');
1519
+ assert.equal(await r.clientOfHost('acme.studio.test'), null);
1520
+ });
1521
+
1522
+ test('a client is Studio’s to add, with a slug and a host nobody has, and never a name the studio keeps', async () => {
1523
+ review = createReview(memoryKV());
1524
+ await review.addClient(studio, { slug: 'acme', host: 'acme.studio.test', name: 'Acme' });
1525
+ await rejects(review.addClient(studio, { slug: 'acme', host: 'other.studio.test', name: 'A' }), 'invalid');
1526
+ await rejects(review.addClient(studio, { slug: 'other', host: 'acme.studio.test', name: 'A' }), 'invalid');
1527
+ for (const slug of ['studio', 'www', 'api', 'site', 'Acme', 'a:b', ''])
1528
+ await rejects(
1529
+ review.addClient(studio, { slug, host: `x${slug.length}.studio.test`, name: 'X' }),
1530
+ 'invalid',
1531
+ );
1532
+ for (const host of ['ACME2.studio.test', 'nodot', 'a b.test', 5])
1533
+ await rejects(review.addClient(studio, { slug: 'acme-two', host, name: 'X' }), 'invalid');
1534
+ await rejects(review.addClient(client, { slug: 'acme', host: 'z.studio.test', name: 'X' }), 'forbidden');
1535
+ await rejects(review.addClient(null, { slug: 'zeta', host: 'z.studio.test', name: 'X' }), 'unauthorized');
1536
+ });
1537
+
1538
+ test('a project is its client’s: listed with a build, opened only from its own client', async () => {
1539
+ review = createReview(memoryKV());
1540
+ const acme = { client: 'acme' };
1541
+ await rejects(review.addProject(acme, studio, { slug: 'rebrand', name: 'Rebrand' }), 'not_found');
1542
+ await review.addClient(studio, { slug: 'acme', host: 'acme.studio.test', name: 'Acme' });
1543
+ const made = await review.addProject(acme, studio, { slug: 'rebrand', name: 'Rebrand' });
1544
+ assert.deepEqual(made, {
1545
+ slug: 'rebrand',
1546
+ client: 'acme',
1547
+ name: 'Rebrand',
1548
+ build: null,
1549
+ createdAt: made.createdAt,
1550
+ });
1551
+ await review.addProject(acme, studio, { slug: 'atlas', name: 'Atlas' });
1552
+ await rejects(review.addProject(acme, studio, { slug: 'rebrand', name: 'Again' }), 'invalid');
1553
+ for (const slug of ['invite', 'api', '_gate', '_p', 'assets', 'guide', 'index.html', 'favicon.svg', 'A'])
1554
+ await rejects(review.addProject(acme, studio, { slug, name: 'X' }), 'invalid');
1555
+ await rejects(review.addProject(acme, client, { slug: 'mine', name: 'X' }), 'forbidden');
1556
+ // Nothing is listed until it has a build; then by name.
1557
+ assert.deepEqual(await review.listProjects(acme, client), []);
1558
+ await rejects(review.startBuild(A, studio, '1'), 'invalid');
1559
+ await rejects(review.setBuild(A, studio, '1'), 'invalid');
1560
+ await rejects(review.startBuild(A, studio, 'B'.repeat(16)), 'invalid');
1561
+ await rejects(review.setBuild(A, studio, 'B'.repeat(16)), 'invalid');
1562
+ await rejects(review.startBuild({ client: 'acme', project: 'gone' }, studio, 'a'.repeat(16)), 'not_found');
1563
+ await rejects(review.setBuild({ client: 'acme', project: 'gone' }, studio, 'a'.repeat(16)), 'not_found');
1564
+ await rejects(review.startBuild(A, client, 'a'.repeat(16)), 'forbidden');
1565
+ await rejects(review.setBuild(A, client, 'a'.repeat(16)), 'forbidden');
1566
+ await review.startBuild(A, studio, 'a'.repeat(16));
1567
+ assert.equal((await review.setBuild(A, studio, 'a'.repeat(16))).build, 'a'.repeat(16));
1568
+ await review.startBuild({ client: 'acme', project: 'atlas' }, studio, 'b'.repeat(16));
1569
+ await review.setBuild({ client: 'acme', project: 'atlas' }, studio, 'b'.repeat(16));
1570
+ assert.deepEqual(
1571
+ (await review.listProjects(acme, client)).map((p) => p.slug),
1572
+ ['atlas', 'rebrand'],
1573
+ );
1574
+ await rejects(review.listProjects(acme, null), 'unauthorized');
1575
+ // Another client has none of them, by any name.
1576
+ assert.equal((await review.projectOf(A))?.build, 'a'.repeat(16));
1577
+ for (const scope of [
1578
+ { client: 'bravo', project: 'rebrand' },
1579
+ { client: 'acme', project: 'gone' },
1580
+ { client: 'acme', project: 'a:b' },
1581
+ { client: 'acme' },
1582
+ { client: 'studio', project: 'rebrand' },
1583
+ ])
1584
+ assert.equal(await review.projectOf(scope), null, JSON.stringify(scope));
1585
+ });
1586
+
1587
+ test('a Client is refused every studio-wide op, and a signed-out caller is unauthorized, with nothing written', async () => {
1588
+ let snapshot = '';
1589
+ review = createReview(
1590
+ memoryKV(LIVE, (x) => {
1591
+ snapshot = JSON.stringify(x);
1592
+ }),
1593
+ );
1594
+ studio = await review.addStudioPerson({ ...studio, id: 'cli' }, { name: 'Ed', team: 'Studio' });
1595
+ client = (await review.addPerson(A, studio, { name: 'Rae', team: 'Acme' })).person;
1596
+ for (const [name, op] of STUDIO_WIDE) {
1597
+ const before = snapshot;
1598
+ assert.equal(((await outcome(op(client))) as { code?: string }).code, 'forbidden', name);
1599
+ assert.equal(((await outcome(op(null))) as { code?: string }).code, 'unauthorized', name);
1600
+ assert.equal(snapshot, before, `${name} wrote nothing`);
1601
+ }
1602
+ });
1603
+
1604
+ test('a Studio person added studio-wide reaches every client, and each of their links signs in once', async () => {
1605
+ const jo = await review.addStudioPerson(studio, { name: 'Jo', team: 'Studio', kind: 'client' });
1606
+ assert.equal(jo.kind, 'studio');
1607
+ assert.equal(jo.client, null);
1608
+ assert.equal(jo.accessEndsAt, null);
1609
+ const onA = await review.newStudioLink(studio, jo.id);
1610
+ const onB = await review.newStudioLink(studio, jo.id);
1611
+ // Two links, one per client host: neither cancels the other, and each works once.
1612
+ assert.equal((await review.useLink(A, onA.token))?.id, jo.id);
1613
+ assert.equal((await review.useLink(B, onB.token))?.id, jo.id);
1614
+ assert.equal(await review.useLink(B, onB.token), null);
1615
+ assert.equal(await review.useLink(A, onA.token), null);
1616
+ await rejects(review.newStudioLink(studio, client.id), 'not_found');
1617
+ await rejects(review.newStudioLink(studio, 'nope'), 'not_found');
1618
+ await review.revoke(A, studio, jo.id);
1619
+ await rejects(review.newStudioLink(studio, jo.id), 'invalid');
1620
+ });
1621
+
1622
+ test('access resolves the person’s own, then the client’s, then the studio’s, then 3m (S13)', async () => {
1623
+ // The day each new person's access ends; the clock starts at 2030-01-01.
1624
+ const ends = async (input: Record<string, unknown>, scope: Scope = A) =>
1625
+ (await review.addPerson(scope, studio, { team: 'Acme', ...input })).person.accessEndsAt?.slice(0, 10) ??
1626
+ null;
1627
+ assert.equal(await review.studioAccess(studio), '3m');
1628
+ assert.equal(await review.defaultAccess(A, studio), '3m');
1629
+ assert.equal(await ends({ name: 'P' }), '2030-04-01');
1630
+ assert.equal(await review.setStudioAccess(studio, '6m'), '6m');
1631
+ assert.equal(await review.defaultAccess(A, studio), '6m');
1632
+ assert.equal(await ends({ name: 'P2' }), '2030-07-01');
1633
+ assert.equal((await review.setClientAccess(A, studio, 'open')).access, 'open');
1634
+ assert.equal(await review.defaultAccess(A, studio), 'open');
1635
+ assert.equal(await ends({ name: 'Q' }), null);
1636
+ // Another client without its own takes the studio's.
1637
+ assert.equal(await review.defaultAccess(B, studio), '6m');
1638
+ assert.equal(await ends({ name: 'Bo', team: 'Bravo' }, B), '2030-07-01');
1639
+ // The person's own length wins over both, and Studio stays open-ended.
1640
+ assert.equal(await ends({ name: 'R', access: '3m' }), '2030-04-01');
1641
+ assert.equal(await ends({ name: 'S', team: 'Studio', kind: 'studio' }), null);
1642
+ // null hands it back to the studio's; anything else is refused.
1643
+ assert.equal((await review.setClientAccess(A, studio, null)).access, null);
1644
+ assert.equal(await review.defaultAccess(A, studio), '6m');
1645
+ await rejects(review.setClientAccess(A, studio, '9m'), 'invalid');
1646
+ await rejects(review.setClientAccess({ client: 'nobody' }, studio, '6m'), 'not_found');
1647
+ await rejects(review.setClientAccess(A, client, '6m'), 'forbidden');
1648
+ assert.equal((await review.clientRecord(A, studio)).access, null);
1649
+ });
1650
+
1651
+ test('the People tab’s default access reads and sets the same value: the client’s on its host, the studio’s in the local store', async () => {
1652
+ await review.setStudioAccess(studio, '6m');
1653
+ assert.equal(await review.defaultAccess(A, studio), '6m');
1654
+ // Set on the client's scope, it is the client's own: what defaultAccess reads there, and no other client's.
1655
+ assert.equal(await review.setDefaultAccess(A, studio, 'open'), 'open');
1656
+ assert.equal(await review.defaultAccess(A, studio), 'open');
1657
+ assert.equal((await review.clientRecord(A, studio)).access, 'open');
1658
+ assert.equal(await review.studioAccess(studio), '6m');
1659
+ assert.equal(await review.defaultAccess(B, studio), '6m');
1660
+ // A new Client of the client gets it.
1661
+ const p = await review.addPerson(A, studio, { name: 'Q', team: 'Acme' });
1662
+ assert.equal(p.person.accessEndsAt, null);
1663
+ // A scope with no client record (the local store's) reads and sets the studio's.
1664
+ const SITE = { client: 'site', project: 'site' };
1665
+ assert.equal(await review.setDefaultAccess(SITE, studio, '3m'), '3m');
1666
+ assert.equal(await review.defaultAccess(SITE, studio), '3m');
1667
+ assert.equal(await review.studioAccess(studio), '3m');
1668
+ assert.equal(await review.defaultAccess(A, studio), 'open');
1669
+ // Only a Studio person sets it.
1670
+ await rejects(review.setDefaultAccess(A, client, '3m'), 'forbidden');
1671
+ assert.equal(await review.defaultAccess(A, studio), 'open');
1672
+ });
1673
+
1674
+ test('of two adds at once, one gets the slug and one the host; the other is refused and holds nothing', async () => {
1675
+ review = createReview(memoryKV());
1676
+ const make = (slug: string, host: string) => outcome(review.addClient(studio, { slug, host, name: slug }));
1677
+ const sameSlug = await Promise.all([make('acme', 'one.studio.test'), make('acme', 'two.studio.test')]);
1678
+ assert.equal(sameSlug.filter((o) => 'value' in o).length, 1);
1679
+ const sameHost = await Promise.all([make('bravo', 'b.studio.test'), make('charlie', 'b.studio.test')]);
1680
+ assert.equal(sameHost.filter((o) => 'value' in o).length, 1);
1681
+ // The loser's slug or host was let go: it can be added again with a free one.
1682
+ const lostHost = sameSlug.find((o) => 'code' in o)
1683
+ ? (sameSlug.find((o) => 'value' in o) as { value: { host: string } }).value.host
1684
+ : '';
1685
+ const freeHost = lostHost === 'one.studio.test' ? 'two.studio.test' : 'one.studio.test';
1686
+ const lostSlug =
1687
+ (sameHost.find((o) => 'value' in o) as { value: { slug: string } }).value.slug === 'bravo'
1688
+ ? 'charlie'
1689
+ : 'bravo';
1690
+ assert.equal(
1691
+ (await review.addClient(studio, { slug: lostSlug, host: freeHost, name: 'X' })).host,
1692
+ freeHost,
1693
+ );
1694
+ });
1695
+
1696
+ test('archiving a client removes every key it has, so the slug and host start clean when added again', async () => {
1697
+ const kv = memoryKV();
1698
+ const tick = () => {
1699
+ clock += 1000;
1700
+ return new Date(clock);
1701
+ };
1702
+ const r = createReview(kv, { now: tick });
1703
+ const ed = await r.addStudioPerson({ ...studio, id: 'cli' }, { name: 'Ed', team: 'Studio' });
1704
+ const acme = await r.addClient(ed, { slug: 'acme', host: 'acme.studio.test', name: 'Acme' });
1705
+ await r.addClient(ed, { slug: 'bravo', host: 'bravo.studio.test', name: 'Bravo' });
1706
+ await r.addProject({ client: 'acme' }, ed, { slug: 'rebrand', name: 'Rebrand' });
1707
+ await r.startBuild(A, ed, 'a'.repeat(16));
1708
+ await r.setBuild(A, ed, 'a'.repeat(16));
1709
+ const rae = await r.addPerson(A, ed, { name: 'Rae', team: 'Acme' });
1710
+ await r.setAudience(A, ed, anchor.prototype, 'client');
1711
+ await r.add(A, rae.person, { body: 'x', anchor });
1712
+ await r.setControl(A, ed, { prototype: 'marked-only', kind: 'dial', id: 'Card/x', audience: 'client' });
1713
+ await r.setAudience(A, ed, 'shared-only', 'client');
1714
+ await r.setClientAccess(A, ed, 'open');
1715
+ await r.startProof(A, ed);
1716
+ const bravoPerson = await r.addPerson(B, ed, { name: 'Bea', team: 'Bravo' });
1717
+ const before = await Promise.all([kv.hgetall(keyOf('studio', 'people')), kv.hgetall(keyOf(B, 'people'))]);
1718
+
1719
+ const archived = await r.archiveClient(A, ed);
1720
+ assert.deepEqual(archived, {
1721
+ client: { ...acme, access: 'open', archivedAt: archived?.client.archivedAt },
1722
+ ownHost: true,
1723
+ });
1724
+ assert.ok(archived?.client.archivedAt);
1725
+ assert.equal(await r.clientOfHost('acme.studio.test'), null);
1726
+ const acmeKeys = [
1727
+ keyOf(A, 'people'),
1728
+ keyOf(A, 'links'),
1729
+ keyOf({ client: 'acme' }, 'projects'),
1730
+ keyOf(A, 'audiences'),
1731
+ keyOf(A, 'counters'),
1732
+ keyOf(A, 'comments', anchor.prototype),
1733
+ keyOf(A, 'controls', 'marked-only'),
1734
+ keyOf({ client: 'acme' }, 'proof'),
1735
+ ];
1736
+ for (const key of acmeKeys) assert.deepEqual(await kv.hgetall(key), {}, key);
1737
+ assert.deepEqual(await kv.smembers(keyOf(A, 'prototypes')), []);
1738
+ // The record stays, archived and naming its host, until forgetClient: run again, it still knows the host.
1739
+ assert.equal((await r.archiveClient(A, ed))?.client.host, 'acme.studio.test');
1740
+ await rejects(r.addClient(ed, { slug: 'acme', host: 'acme.studio.test', name: 'Acme' }), 'invalid');
1741
+ await r.forgetClient(A, ed);
1742
+ assert.equal(await kv.hget(keyOf('studio', 'claims'), 'client:acme'), null);
1743
+ assert.equal(await kv.hget(keyOf('studio', 'claims'), 'host:acme.studio.test'), null);
1744
+ // Nothing of the studio's or another client's went with it.
1745
+ assert.deepEqual(
1746
+ await Promise.all([kv.hgetall(keyOf('studio', 'people')), kv.hgetall(keyOf(B, 'people'))]),
1747
+ before,
1748
+ );
1749
+ assert.equal((await r.clientOfHost('bravo.studio.test'))?.slug, 'bravo');
1750
+ assert.equal((await r.useLink(B, bravoPerson.token))?.name, 'Bea');
1751
+ // Running it again finds nothing and is no error.
1752
+ assert.equal(await r.archiveClient(A, ed), null);
1753
+ await r.forgetClient(A, ed);
1754
+ // Until every gate's cached answer for the host has run out, neither the slug nor the host comes back.
1755
+ assert.ok((await r.clientWait(ed, { slug: 'acme', host: 'elsewhere.studio.test' })) > 60);
1756
+ assert.ok((await r.clientWait(ed, { slug: 'other', host: 'acme.studio.test' })) > 60);
1757
+ assert.equal(await r.clientWait(ed, { slug: 'other', host: 'elsewhere.studio.test' }), 0);
1758
+ for (const input of [
1759
+ { slug: 'acme', host: 'acme.studio.test' },
1760
+ { slug: 'acme', host: 'elsewhere.studio.test' },
1761
+ { slug: 'other', host: 'acme.studio.test' },
1762
+ ])
1763
+ await assert.rejects(r.addClient(ed, { ...input, name: 'X' }), (e: ReviewError) => {
1764
+ assert.match(e.message, /archived moments ago: add it again in \d+ seconds/);
1765
+ return true;
1766
+ });
1767
+ // A refused add holds nothing: no record, and no claim on the slug or host.
1768
+ assert.equal(await r.clientOfHost('elsewhere.studio.test'), null);
1769
+ assert.equal(await kv.hget(keyOf('studio', 'clients'), 'other'), null);
1770
+ assert.equal(await kv.hget(keyOf('studio', 'claims'), 'client:other'), null);
1771
+ clock += REUSE_SECONDS * 1000;
1772
+ assert.equal(await r.clientWait(ed, { slug: 'acme', host: 'acme.studio.test' }), 0);
1773
+
1774
+ // The slug and host again: a new client, with no person, project, comment or access of the old one.
1775
+ const again = await r.addClient(ed, { slug: 'acme', host: 'acme.studio.test', name: 'Acme' });
1776
+ assert.notEqual(again.createdAt, acme.createdAt);
1777
+ assert.equal(again.access, null);
1778
+ assert.equal(await r.useLink(A, rae.token), null);
1779
+ assert.equal(await r.sessionPerson(A, rae.person.id, 0), null);
1780
+ assert.deepEqual(await r.listProjects({ client: 'acme' }, ed), []);
1781
+ assert.equal(await r.projectOf(A), null);
1782
+ assert.deepEqual(await r.listComments(A, ed, anchor.prototype), []);
1783
+ assert.equal(await r.audienceOf(A, 'shared-only'), 'internal');
1784
+ await rejects(r.archiveClient(A, rae.person), 'forbidden');
1785
+ });
1786
+
1787
+ test('an archive keeps a host another client has, and frees a slug or host whose add stopped part way', async () => {
1788
+ const kv = memoryKV();
1789
+ let now = Date.parse('2030-01-01T00:00:00Z');
1790
+ const r = createReview(kv, { now: () => new Date(now) });
1791
+ const record = (slug: string, host: string) =>
1792
+ kv.hset(
1793
+ keyOf('studio', 'clients'),
1794
+ slug,
1795
+ JSON.stringify({
1796
+ slug,
1797
+ host,
1798
+ name: slug,
1799
+ access: null,
1800
+ createdAt: 'x',
1801
+ archivedAt: null,
1802
+ provenAt: null,
1803
+ }),
1804
+ );
1805
+ const claims = keyOf('studio', 'claims');
1806
+ await r.addClient(studio, { slug: 'bravo', host: 'shared.studio.test', name: 'Bravo' });
1807
+ // A client record whose host the table gives to another: archiving it leaves that host alone.
1808
+ await record('acme', 'shared.studio.test');
1809
+ assert.equal((await r.archiveClient({ client: 'acme' }, studio))?.ownHost, false);
1810
+ await r.forgetClient({ client: 'acme' }, studio);
1811
+ assert.equal((await r.clientOfHost('shared.studio.test'))?.slug, 'bravo');
1812
+ assert.equal(await kv.hget(claims, 'host:shared.studio.test'), '1');
1813
+ // Nor a host a live client's record names, though no host entry serves it yet: an add under way.
1814
+ await record('carol', 'open.studio.test');
1815
+ await kv.hincrby(claims, 'host:open.studio.test', 1);
1816
+ await record('dave', 'open.studio.test');
1817
+ assert.equal((await r.archiveClient({ client: 'dave' }, studio))?.ownHost, false);
1818
+ await r.forgetClient({ client: 'dave' }, studio);
1819
+ assert.equal(await kv.hget(claims, 'host:open.studio.test'), '1');
1820
+ // A slug claimed by an add that never wrote its record is refused until it is archived.
1821
+ await kv.hincrby(claims, 'client:zeta', 1);
1822
+ await rejects(r.addClient(studio, { slug: 'zeta', host: 'z.studio.test', name: 'Z' }), 'invalid');
1823
+ assert.equal(await r.archiveClient({ client: 'zeta' }, studio), null);
1824
+ await r.forgetClient({ client: 'zeta' }, studio);
1825
+ assert.equal((await r.addClient(studio, { slug: 'zeta', host: 'z.studio.test', name: 'Z' })).slug, 'zeta');
1826
+ // An add that stopped after its claims and before its host entry: the host answers "already serves"
1827
+ // for every other slug, until the stopped one is archived, which frees the host as well as the slug.
1828
+ await kv.hincrby(claims, 'client:yara', 1);
1829
+ await record('yara', 'y.studio.test');
1830
+ await kv.hincrby(claims, 'host:y.studio.test', 1);
1831
+ await rejects(r.addClient(studio, { slug: 'other', host: 'y.studio.test', name: 'O' }), 'invalid');
1832
+ assert.equal((await r.archiveClient({ client: 'yara' }, studio))?.ownHost, true);
1833
+ await r.forgetClient({ client: 'yara' }, studio);
1834
+ assert.equal(await kv.hget(claims, 'host:y.studio.test'), null);
1835
+ assert.equal(await kv.hget(claims, 'client:yara'), null);
1836
+ now += REUSE_SECONDS * 1000;
1837
+ assert.equal(
1838
+ (await r.addClient(studio, { slug: 'other', host: 'y.studio.test', name: 'O' })).host,
1839
+ 'y.studio.test',
1840
+ );
1841
+ assert.equal((await r.addClient(studio, { slug: 'yara', host: 'y2.studio.test', name: 'Y' })).slug, 'yara');
1842
+ // forgetClient refuses a client that is not archived.
1843
+ await rejects(r.forgetClient({ client: 'bravo' }, studio), 'invalid');
1844
+ assert.equal((await r.clientOfHost('shared.studio.test'))?.slug, 'bravo');
1845
+ });
1846
+
1847
+ test('a host proof is Studio’s to start and end, answers only its own ask in its own client, and keeps no ask', async () => {
1848
+ const kv = memoryKV();
1849
+ const r = createReview(kv);
1850
+ await r.addClient(studio, { slug: 'acme', host: 'acme.studio.test', name: 'Acme' });
1851
+ await r.addClient(studio, { slug: 'bravo', host: 'bravo.studio.test', name: 'Bravo' });
1852
+ await rejects(r.startProof({ client: 'zeta' }, studio), 'not_found');
1853
+ const rae = await r.addPerson(A, studio, { name: 'Rae', team: 'Acme' });
1854
+ await rejects(r.startProof(A, rae.person), 'forbidden');
1855
+ await rejects(r.startProof(A, null), 'unauthorized');
1856
+ const { ask, answer } = await r.startProof(A, studio);
1857
+ assert.match(ask, /^[\w-]{43}$/);
1858
+ assert.notEqual(ask, answer);
1859
+ assert.equal(await r.proofAnswer(A, ask), answer);
1860
+ // The store holds the ask's hash, never the ask.
1861
+ assert.doesNotMatch(JSON.stringify(await kv.hgetall(keyOf(A, 'proof'))), new RegExp(ask));
1862
+ for (const [scope, given] of [
1863
+ [B, ask],
1864
+ [A, answer],
1865
+ [A, 'x'.repeat(43)],
1866
+ [A, 'short'],
1867
+ [{ client: 'site' }, ask],
1868
+ ] as const)
1869
+ assert.equal(await r.proofAnswer(scope, given), null, `${scope.client} ${given}`);
1870
+ await rejects(r.endProof(A, rae.person), 'forbidden');
1871
+ // Started, the client is still being added; ended, it is marked proven, once.
1872
+ assert.equal((await r.clientRecord(A, studio)).provenAt, null);
1873
+ const proven = await r.endProof(A, studio);
1874
+ assert.ok(proven.provenAt);
1875
+ assert.equal(await r.proofAnswer(A, ask), null);
1876
+ assert.deepEqual(await r.clientRecord(A, studio), proven);
1877
+ assert.deepEqual(await r.endProof(A, studio), proven);
1878
+ assert.equal((await r.clientRecord(B, studio)).provenAt, null);
1879
+ });
1880
+
1881
+ /**
1882
+ * A store that holds the next write to a comments key back until it is let
1883
+ * go: a request that passed the live check before an archive, and lands after
1884
+ * the archive swept the project list, the prototype set and the audiences.
1885
+ */
1886
+ function holdingKV(kv: KV) {
1887
+ let armed = false;
1888
+ let reached = () => {};
1889
+ let go = () => {};
1890
+ const waiting = new Promise<void>((r) => {
1891
+ reached = r;
1892
+ });
1893
+ const released = new Promise<void>((r) => {
1894
+ go = r;
1895
+ });
1896
+ const held: KV = {
1897
+ ...kv,
1898
+ hset: async (key, field, value) => {
1899
+ if (armed && key.includes(':comments:')) {
1900
+ armed = false;
1901
+ reached();
1902
+ await released;
1903
+ }
1904
+ return kv.hset(key, field, value);
1905
+ },
1906
+ };
1907
+ return {
1908
+ kv: held,
1909
+ arm: () => {
1910
+ armed = true;
1911
+ },
1912
+ waiting,
1913
+ release: () => go(),
1914
+ };
1915
+ }
1916
+
1917
+ for (const lands of ['during the archive', 'after the forget'] as const)
1918
+ test(`a write that passed the live check before an archive and lands ${lands} is gone before the slug is a client again`, async () => {
1919
+ let at = Date.parse('2030-01-01T00:00:00Z');
1920
+ const deps = {
1921
+ now: () => {
1922
+ at += 1000;
1923
+ return new Date(at);
1924
+ },
1925
+ };
1926
+ const kv = memoryKV();
1927
+ const held = holdingKV(kv);
1928
+ const site = createReview(held.kv, deps);
1929
+ const cli = createReview(kv, deps);
1930
+ const ed = await cli.addStudioPerson({ ...studio, id: 'cli' }, { name: 'Ed', team: 'Studio' });
1931
+ const company = async (name: string) => {
1932
+ await cli.addClient(ed, { slug: 'acme', host: 'acme.studio.test', name });
1933
+ await cli.addProject({ client: 'acme' }, ed, { slug: 'rebrand', name: 'Rebrand' });
1934
+ await cli.startBuild(A, ed, 'a'.repeat(16));
1935
+ await cli.setBuild(A, ed, 'a'.repeat(16));
1936
+ await cli.setAudience(A, ed, anchor.prototype, 'client');
1937
+ return (await cli.addPerson(A, ed, { name: `${name} person`, team: name })).person;
1938
+ };
1939
+ const rae = await company('Old Co');
1940
+ const thread = await cli.add(A, rae, { body: 'Old Co plan', anchor });
1941
+ // Rae resolves her thread: the live check passes, and the write is held.
1942
+ held.arm();
1943
+ const resolving = site.resolve(A, rae, { prototype: anchor.prototype, id: thread.id, resolved: true });
1944
+ await held.waiting;
1945
+ await cli.archiveClient({ client: 'acme' }, ed);
1946
+ if (lands === 'during the archive') {
1947
+ held.release();
1948
+ await resolving;
1949
+ }
1950
+ await cli.forgetClient({ client: 'acme' }, ed);
1951
+ if (lands === 'after the forget') {
1952
+ held.release();
1953
+ await resolving;
1954
+ }
1955
+ const comments = keyOf(A, 'comments', anchor.prototype);
1956
+ assert.equal(Object.keys(await kv.hgetall(comments)).length, lands === 'during the archive' ? 0 : 1);
1957
+ assert.deepEqual(await kv.smembers(keyOf(A, 'prototypes')), [], 'no index names the write');
1958
+
1959
+ // After the hold, the slug, the project and the client-facing prototype, for another company.
1960
+ at += REUSE_SECONDS * 1000;
1961
+ const zed = await company('New Co');
1962
+ assert.deepEqual(await kv.hgetall(comments), {});
1963
+ assert.deepEqual(await cli.listComments(A, zed, anchor.prototype), []);
1964
+ assert.deepEqual(await cli.allComments(A, ed), []);
1965
+ });
1966
+
1967
+ test('a sweep reaches its own scope’s keys only: never acme-two’s, a sibling project’s, the studio’s or the directory’s', async () => {
1968
+ let at = Date.parse('2030-01-01T00:00:00Z');
1969
+ let seen: Snapshot = { hashes: {}, sets: {} };
1970
+ const kv = memoryKV(undefined, (s) => {
1971
+ seen = s;
1972
+ });
1973
+ const r = createReview(kv, {
1974
+ now: () => {
1975
+ at += 1000;
1976
+ return new Date(at);
1977
+ },
1978
+ });
1979
+ const ed = await r.addStudioPerson({ ...studio, id: 'cli' }, { name: 'Ed', team: 'Studio' });
1980
+ await r.setStudioAccess(ed, '6m');
1981
+ await kv.hset(keyOf('studio', 'mark'), 'id', 'studio-one');
1982
+ const PROJECTS = ['rebrand', 'rebrand-two'];
1983
+ const fill = async (slug: string) => {
1984
+ await r.addClient(ed, { slug, host: `${slug}.studio.test`, name: slug });
1985
+ const person = (await r.addPerson({ client: slug }, ed, { name: 'Rae', team: slug })).person;
1986
+ for (const project of PROJECTS) {
1987
+ const scope = { client: slug, project };
1988
+ await r.addProject({ client: slug }, ed, { slug: project, name: project });
1989
+ await r.setAudience(scope, ed, anchor.prototype, 'client');
1990
+ await r.add(scope, person, { body: `${slug} ${project}`, anchor });
1991
+ await r.setControl(scope, ed, {
1992
+ prototype: anchor.prototype,
1993
+ kind: 'dial',
1994
+ id: 'Card/x',
1995
+ audience: 'client',
1996
+ });
1997
+ }
1998
+ return person;
1999
+ };
2000
+ await fill('acme');
2001
+ await fill('acme-two');
2002
+ await r.startProof({ client: 'acme-two' }, ed);
2003
+ // Everything outside acme's own prefix, but the directory's acme fields, which archive and add rewrite.
2004
+ const outside = () =>
2005
+ JSON.stringify(
2006
+ Object.entries({ ...seen.hashes, ...seen.sets })
2007
+ .filter(([k]) => !k.startsWith('hub4:c:acme:'))
2008
+ .map(([k, v]) => [
2009
+ k,
2010
+ Array.isArray(v)
2011
+ ? v
2012
+ : Object.entries(v).filter(
2013
+ ([f]) => !['acme', 'client:acme', 'host:acme.studio.test', 'acme.studio.test'].includes(f),
2014
+ ),
2015
+ ])
2016
+ .filter(([, v]) => v.length),
2017
+ );
2018
+ const before = outside();
2019
+ assert.match(before, /hub4:c:acme-two:p:rebrand:comments:sample-list/);
2020
+
2021
+ await r.archiveClient({ client: 'acme' }, ed);
2022
+ await r.forgetClient({ client: 'acme' }, ed);
2023
+ at += REUSE_SECONDS * 1000;
2024
+ await r.addClient(ed, { slug: 'acme', host: 'acme.studio.test', name: 'New' });
2025
+ assert.equal(outside(), before);
2026
+ assert.deepEqual(
2027
+ Object.keys(seen.hashes).filter(
2028
+ (k) => k.startsWith('hub4:c:acme:') && Object.keys(seen.hashes[k]).length,
2029
+ ),
2030
+ [],
2031
+ );
2032
+ assert.equal((await r.clientOfHost('acme-two.studio.test'))?.slug, 'acme-two');
2033
+ assert.equal(await r.studioAccess(ed), '6m');
2034
+ assert.equal(await kv.hget(keyOf('studio', 'mark'), 'id'), 'studio-one');
2035
+
2036
+ // A project added sweeps its own prefix: never its client's other projects, rebrand-two beside rebrand.
2037
+ await r.addProject({ client: 'acme' }, ed, { slug: 'rebrand-two', name: 'Two' });
2038
+ const zed = (await r.addPerson(A, ed, { name: 'Zed', team: 'New' })).person;
2039
+ const two = { client: 'acme', project: 'rebrand-two' };
2040
+ await r.setAudience(two, ed, anchor.prototype, 'client');
2041
+ await r.add(two, zed, { body: 'kept', anchor });
2042
+ await kv.hset(keyOf(A, 'comments', anchor.prototype), 'stray', '{}');
2043
+ await r.addProject({ client: 'acme' }, ed, { slug: 'rebrand', name: 'Rebrand' });
2044
+ assert.deepEqual(await kv.hgetall(keyOf(A, 'comments', anchor.prototype)), {});
2045
+ assert.deepEqual(
2046
+ (await r.listComments(two, zed, anchor.prototype)).map((c) => c.body),
2047
+ ['kept'],
2048
+ );
2049
+ assert.deepEqual((await r.listPeople(A, ed)).map((p) => p.name).sort(), ['Ed', 'Zed']);
2050
+ assert.equal(outside(), before);
2051
+ });
2052
+
2053
+ test('the local store is never swept: archive and forget of its client change nothing, and a project added in it deletes nothing', async () => {
2054
+ let snapshot = '';
2055
+ const kv = memoryKV(undefined, (s) => {
2056
+ snapshot = JSON.stringify(s);
2057
+ });
2058
+ const r = createReview(kv);
2059
+ const SITE: Scope = { client: 'site', project: 'site' };
2060
+ const ed = (
2061
+ await r.addPerson(SITE, { ...studio, id: 'cli' }, { name: 'Ed', team: 'Studio', kind: 'studio' })
2062
+ ).person;
2063
+ const rae = await r.addPerson(SITE, ed, { name: 'Rae', team: 'Acme' });
2064
+ await r.setAudience(SITE, ed, anchor.prototype, 'client');
2065
+ await r.add(SITE, rae.person, { body: 'kept', anchor });
2066
+ const before = snapshot;
2067
+ assert.match(before, /hub4:c:site:p:site:comments:sample-list/);
2068
+ await rejects(r.archiveClient({ client: 'site' }, ed), 'not_found');
2069
+ await rejects(r.forgetClient(SITE, ed), 'not_found');
2070
+ for (const slug of ['site', 'other'])
2071
+ await assert.rejects(r.addProject({ client: 'site' }, ed, { slug, name: slug }), /local store/);
2072
+ assert.equal(snapshot, before);
2073
+ assert.deepEqual(
2074
+ (await r.listComments(SITE, rae.person, anchor.prototype)).map((c) => c.body),
2075
+ ['kept'],
2076
+ );
2077
+ assert.equal(await r.audienceOf(SITE, anchor.prototype), 'client');
2078
+ });
2079
+
2080
+ test('a client default set while the client’s record goes is refused, and never lands as the studio’s', async () => {
2081
+ const kv = memoryKV();
2082
+ const r = createReview(kv);
2083
+ await r.addClient(studio, { slug: 'acme', host: 'acme.studio.test', name: 'Acme' });
2084
+ // The record is there for the live check, and gone by the time the default is written: a forget landed between.
2085
+ let reads = 0;
2086
+ const racing: KV = {
2087
+ ...kv,
2088
+ hget: async (key, field) =>
2089
+ key === keyOf('studio', 'clients') && field === 'acme' && reads++ > 0 ? null : kv.hget(key, field),
2090
+ };
2091
+ await rejects(createReview(racing).setDefaultAccess(A, studio, 'open'), 'not_found');
2092
+ assert.equal(await kv.hget(keyOf('studio', 'settings'), 'access'), null);
2093
+ assert.equal(await r.studioAccess(studio), '3m');
2094
+ assert.equal((await r.clientRecord(A, studio)).access, null);
2095
+ });
2096
+
2097
+ test('a project points only at a build started for it, never one started before the live one, and pruning never forgets the live one', async () => {
2098
+ const acme = { client: 'acme' };
2099
+ await review.addProject(acme, studio, { slug: 'rebrand', name: 'Rebrand' });
2100
+ const [first, slow, fast] = ['1', '2', '3'].map((d) => d.repeat(16));
2101
+ // Not started for this project: refused.
2102
+ await rejects(review.setBuild(A, studio, first), 'invalid');
2103
+ for (const b of [first, slow, fast]) await review.startBuild(A, studio, b);
2104
+ // An id is started once.
2105
+ await rejects(review.startBuild(A, studio, first), 'invalid');
2106
+ await review.setBuild(A, studio, first);
2107
+ // The share started last goes live first; the one started before it then can never.
2108
+ await review.setBuild(A, studio, fast);
2109
+ await rejects(review.setBuild(A, studio, slow), 'invalid');
2110
+ assert.equal((await review.projectOf(A))?.build, fast);
2111
+ // The same build again is no move back.
2112
+ assert.equal((await review.setBuild(A, studio, fast)).build, fast);
2113
+ assert.deepEqual(await review.buildsOf(A, studio), {
2114
+ live: fast,
2115
+ builds: [
2116
+ { build: first, n: 1, live: true },
2117
+ { build: slow, n: 2, live: false },
2118
+ { build: fast, n: 3, live: true },
2119
+ ],
2120
+ });
2121
+ await review.forgetBuilds(A, studio, [first, slow, fast]);
2122
+ assert.deepEqual((await review.buildsOf(A, studio)).builds, [{ build: fast, n: 3, live: true }]);
2123
+ // A build of another project, or of this one before it was made again, is none of its own.
2124
+ await review.addProject(acme, studio, { slug: 'atlas', name: 'Atlas' });
2125
+ await rejects(review.setBuild({ client: 'acme', project: 'atlas' }, studio, fast), 'invalid');
923
2126
  });