@milesroxas/design-hub 0.11.1 → 0.12.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 (60) hide show
  1. package/README.md +4 -2
  2. package/core/prototype.test.ts +52 -0
  3. package/core/prototype.ts +59 -0
  4. package/core/review/model.ts +32 -3
  5. package/core/review/service.test.ts +240 -0
  6. package/core/review/service.ts +281 -50
  7. package/core/review/store.ts +10 -4
  8. package/dist/core/prototype.d.ts +50 -0
  9. package/dist/core/prototype.js +38 -0
  10. package/dist/core/review/model.d.ts +25 -1
  11. package/dist/core/review/model.js +18 -2
  12. package/dist/core/review/service.d.ts +98 -16
  13. package/dist/core/review/service.js +254 -45
  14. package/dist/core/review/store.d.ts +10 -4
  15. package/dist/scripts/client.d.ts +3 -3
  16. package/dist/scripts/client.js +52 -66
  17. package/dist/scripts/lib/dns.d.ts +32 -0
  18. package/dist/scripts/lib/dns.js +69 -1
  19. package/dist/scripts/lib/fake-host.d.ts +34 -9
  20. package/dist/scripts/lib/fake-host.js +7 -4
  21. package/dist/scripts/lib/review.d.ts +5 -2
  22. package/dist/scripts/lib/review.js +8 -2
  23. package/dist/scripts/lib/studios.d.ts +3 -1
  24. package/dist/scripts/lib/studios.js +2 -0
  25. package/dist/scripts/people.d.ts +8 -5
  26. package/dist/scripts/people.js +152 -41
  27. package/dist/scripts/share.js +1 -1
  28. package/dist/scripts/studio.d.ts +3 -3
  29. package/dist/scripts/studio.js +100 -10
  30. package/dist/server/api.d.ts +6 -8
  31. package/dist/server/api.js +129 -42
  32. package/dist/server/hosts.d.ts +3 -3
  33. package/dist/server/hosts.js +10 -10
  34. package/dist/server/session.d.ts +7 -0
  35. package/dist/server/session.js +8 -1
  36. package/dist/vercel/proxy.d.ts +7 -3
  37. package/dist/vercel/proxy.js +215 -49
  38. package/dist/vite/plugin.d.ts +6 -2
  39. package/dist/vite/plugin.js +21 -3
  40. package/package.json +1 -1
  41. package/shell/Index.tsx +29 -0
  42. package/shell/review/People.tsx +80 -61
  43. package/shell/review/Review.tsx +49 -1
  44. package/shell/review/ThreadCard.tsx +13 -1
  45. package/shell/review/client.ts +56 -22
  46. package/shell/review/review.css +1 -0
  47. package/shell/review/unsent.test.ts +63 -0
  48. package/shell/review/unsent.ts +64 -0
  49. package/shell/review/view.ts +22 -6
  50. package/shell/studio/ClientHub.tsx +9 -36
  51. package/shell/studio/StudioHub.tsx +293 -0
  52. package/shell/studio/hub.css +80 -5
  53. package/shell/studio/page.tsx +57 -0
  54. package/shell/studio/studio.html +14 -0
  55. package/shell/studio/studio.tsx +19 -0
  56. package/skills/miles-deploy/SKILL.md +16 -6
  57. package/skills/miles-design-hub/SKILL.md +3 -3
  58. package/skills/miles-review/SKILL.md +1 -1
  59. package/templates/studio/README.md +32 -13
  60. package/templates/studio/package.json +1 -1
package/README.md CHANGED
@@ -76,7 +76,9 @@ A speculation rule in `shell/index.html` prefetches a prototype's page when its
76
76
 
77
77
  A client's address is `<client>.<apex>` (or a codename under the apex, `client add --host`), and a project is `/<project>/` below it. `design-hub project link` writes `.design-hub/studio.json` in the project (studio, its id, client, project; committed). `design-hub share` builds the project with base `/<project>/`, uploads `dist/` under `c/<client>/<project>/<build>/` in the file store with a new random build id, checks every file is there, and only then points the project's record at that build, so a half-finished upload is never served; the live build and the two before it are kept. Every path on every client's host sits behind the gate, enforced on the server by Vercel Routing Middleware (`createStudioProxy` in `vercel/proxy.ts`, which the studio's `vercel.json` names as `proxy.entrypoint`). The host names the client, from one store lookup kept a minute per instance; a host no client has is the plain 404 before anything else is read. The files function and the review function run the gate's checks again themselves, in the order host, session, project, so neither trusts anything the gate set. A client-side gate was rejected because the bundle and the state behind it would still download.
78
78
 
79
- Every person is named, and signs in with a one-time link that Studio makes in the review panel's People tab, with `design-hub people add` in a linked project, or, for a client's first person and the owner, with `design-hub client add`. A link signs in on its client's host only.
79
+ Studio people have a host of their own, `studio.<apex>`, which `studio create` (or `studio deploy`, for an older studio) adds and proves as it proves a client's. Only Studio people sign in there; a Client, a cookie of a client's host, or none gets that host's sign-in page, word for word a client host's. Its page is the studio hub (`shell/studio/StudioHub.tsx`, built apart from the client hub under `_studio/`, so no client's host serves it): every client with its projects, an **Open** that hands the person off to the client's host (a one-time record in the client's keys, claimed once there within 60 seconds, which sets that host's own Studio cookie), and a **People** panel per client and one for the studio's own people, the review panel's People controls. Its review API answers `me`, `clients` and the people ops, each naming the client it is for with `client`, which the service checks as the handoff does (known, added, not archived) and answers as an unknown client otherwise; without `client` an op is the studio's own people. No client's host reads a `client` parameter.
80
+
81
+ Every person is named, and signs in with a one-time link that Studio makes in the review panel's People tab, on the studio hub, with `design-hub people` in a linked project, the studio folder or with `--studio`, or, for a client's first person, with `design-hub client add`. A Client's link is on their client's host, and signs in there only. A Studio person's is on `studio.<apex>` once it answers (on the linked client's host before then), and signs in on any host of the studio; the owner's first comes from the run that proves the studio host, and a new one from `people link <owner id> --studio <studio>`.
80
82
 
81
83
  - **A link works once.** `GET /invite/<token>` shows who it is for and a Continue button, and uses nothing up, since mail and chat previews open links too. Continue is a same-origin `POST` that uses the link up, sets the session cookie and goes to `/`. The use is atomic: the store's delete is the claim, so of two requests with one link only the one whose delete removed it signs in. A used, expired or unknown link answers 404, as does a link for someone revoked or past their access end. An unused link lasts 14 days. The store keeps only a hash of each token (32 random bytes), so a read of the store signs nobody in.
82
84
  - **A second device gets a second link** (`design-hub people link <id>`), and the first stays signed in. **Sign out everywhere** (`people sign-out <id>`) bumps the person's epoch, drops their unused links and returns one new link. **Revoke** ends their access everywhere.
@@ -87,7 +89,7 @@ Every person is named, and signs in with a one-time link that Studio makes in th
87
89
  - **Internal prototypes.** Everything built from `prototypes/<slug>/` (its README, code, CSS, fixtures, lazy chunks and images) lands under `/_p/<slug>/`, its entry at `/_p/<slug>/entry.js`, and Studio's list of every slug at `/_p/all.js`; the shared bundle in `/assets/` names no prototype's folder (`vite/registry.ts`, `vite/boundary.ts`, `vite/settle.ts`; the build fails if a file breaks that). The exception is the walkthroughs: their compiled text and every feature's name sit in `/assets/` and reach every signed-in person, so a walkthrough must not name or describe internal work until that is built. A built file is owned by where its content comes from, never by who reads it, and each source is recorded as the file is written (`vite/settle.ts`): a stylesheet's CSS modules, an asset's original files, a worker's output; a file with no record and no fixed name fails the build. Nothing a prototype owns is inlined by the size limit, and nothing under `prototypes/` is ever the hub's: a file directly there, or in a folder without a README, is refused (shared code belongs in `kit/`). Any import or `new URL` (`?inline` included) into a prototype its importer does not own is refused in dev and in a build. Every stylesheet is read for reaches inside Vite's postcss run (`vite/css-boundary.ts`), each reference followed as a path and through Vite's own resolvers (aliases and package paths included); Sass, Less and Stylus are refused in prototypes and the project's `kit/`, since their imports resolve inside the preprocessor; and every worker bundle refuses a module under `prototypes/` and resolves `@kit/*`. After the bundle is settled the build reads what it wrote (`vite/leaks.ts`): a file outside `/_p/<slug>/` that holds a whole file of `prototypes/<slug>/` (its bytes, a base64 or SVG data URI of it anywhere in the file, or, for a file the build read, its whole text as a string in a script; 40 bytes or more) fails the build, unless the reader's owner has a source with the same content. It is a backstop for whole-file inlining: no part of a file (a line, a rule) is looked for. Vite is pinned to 8.3 (`~8.3.5`) because the boundary leans on its internals; `vite/build.test.ts` is what to run on a Vite upgrade. A file a prototype owns lives in its folder; the hub's and the kit's live in `/assets/` and are never copied into a prototype's folder; the same bytes from several owners become one copy per owner, each named from its owner's source. A file that reads what another prototype owns, or a hub or kit file that reads what a prototype owns, fails the build, naming both. A prototype that reaches into another (an import, a CSS `@import` or `url()`, a `new URL`) or starts a web worker fails the build. A Client asking for a prototype's page (`/<project>/<slug>/`) or anything under `/<project>/_p/<slug>/` gets 404 unless that prototype is client-facing, asked of the store on every such request; `/<project>/_p/all.js` is always 404 to a Client, and an internal prototype and one that does not exist answer byte for byte the same. Below a project, any first segment that is not the hub's own (`assets`, `api`, `guide`, `_gate`, `index.html`, `favicon.svg`), `_p` or a prototype slug gets the same 404. A path that could mean more than one file (a `.` or `..` segment, a backslash, a control character, an empty segment, a `%` left after decoding, or one that does not decode) goes nowhere for anyone, Studio included. Another client's project, and a project with no build, are the same 404, its review API too. A Client's comments on a prototype that is not client-facing answer as if it had none. Studio passes on the cookie's kind with no audience read. The client's hub at `/` and a project's `/assets/*`, `/guide/*` and review API pass on the session alone. `invite`, `api`, `_gate`, `_p`, `assets`, `guide`, `index.html` and `favicon.svg` are never a prototype's slug (`RESERVED` in `core/prototype.ts`).
88
90
  - **Signed out**, a page gets the sign-in page (401). It has no form: it says to open the link the studio sent, that each link signs in one device and then stops working, and to ask the studio for a new one for another device. Anything else gets a plain-text 401, an anonymous `POST` included.
89
91
 
90
- The studio site reads its env from the Vercel project, never committed: `GATE_SECRET`, at least 32 random characters that sign the session cookie, and the database's and file store's credentials, which `studio create` sets and copies into the studio folder's `.env` (`templates/studio/.env.example` names them). With the secret or the database missing, every host answers 503 "This site is not set up yet.", which names no command, studio or client, so a misconfigured site never serves anything open. The gate pages ship a hash-based Content Security Policy with no `unsafe-inline` and no script, plus `font-src 'self'` and `img-src 'self'`, and send no referrer. They wear the hub's neutral look in Geist, so two files pass the gate without a session: `/_gate/geist.woff2` (the latin subset, emitted at that fixed path) and `/favicon.svg`. Nothing else does. Every response carries `X-Robots-Tag: noindex`, so no search engine lists the site. The signed-out page names no client. Under `design-hub dev` there is no gate; `/_gate/` previews the sign-in page, `?page=link` a link's page and `?page=used` a used one.
92
+ The studio site reads its env from the Vercel project, never committed: `GATE_SECRET`, at least 32 random characters that sign the session cookie, and the database's and file store's credentials, which `studio create` sets and copies into the studio folder's `.env` (`templates/studio/.env.example` names them). With the secret or the database missing, every host answers 503 "This site is not set up yet.", which names no command, studio or client, so a misconfigured site never serves anything open. The gate pages ship a hash-based Content Security Policy with no `unsafe-inline` and no script, plus `font-src 'self'` and `img-src 'self'`, and send no referrer. They wear the hub's neutral look in Geist, so two files pass the gate without a session: `/_gate/geist.woff2` (the latin subset, emitted at that fixed path) and `/favicon.svg`. Nothing else does. Every response carries `X-Robots-Tag: noindex`, so no search engine lists the site. The signed-out page names no client. Under `design-hub dev` there is no gate; `/_gate/` previews the sign-in page, `?page=link` a link's page, `?page=used` a used one and `?page=stopped` the studio host's page for an Open that cannot start. On the studio host the used page's way in says the clients, not the prototypes.
91
93
 
92
94
  ## Comments and people
93
95
 
@@ -11,6 +11,8 @@ import {
11
11
  RESERVED,
12
12
  RESERVED_CLIENTS,
13
13
  routeOf,
14
+ STUDIO_PAGE,
15
+ studioRouteOf,
14
16
  } from './prototype.ts';
15
17
 
16
18
  test('a prototype or project slug is kebab-case and never a first segment the gate answers', () => {
@@ -198,3 +200,53 @@ test('anything else on a studio host goes nowhere: odd paths, the reserved names
198
200
  for (const host of ['', 'localhost', 'a b'])
199
201
  assert.deepEqual(routeOf(host, '/rebrand/'), { host: '', kind: 'none' }, host);
200
202
  });
203
+
204
+ test('a handoff’s claim is one path at a client host’s root; anything near it is a hub path or nothing', () => {
205
+ const token = 'h'.repeat(43);
206
+ assert.deepEqual(routeOf('acme.example.com', `/_gate/handoff/${token}`), {
207
+ host: 'acme.example.com',
208
+ kind: 'handoff',
209
+ token,
210
+ });
211
+ for (const path of [
212
+ `/_gate/handoff/${token}/`,
213
+ '/_gate/handoff/short',
214
+ '/_gate/handoff',
215
+ `/rebrand/_gate/handoff/${token}`,
216
+ ])
217
+ assert.notEqual(routeOf('acme.example.com', path).kind, 'handoff', path);
218
+ });
219
+
220
+ test('on the studio’s own host only its hub, its bundle, its API, a link, the gate’s assets and the handoff go anywhere', () => {
221
+ const token = 'l'.repeat(43);
222
+ const cases: [string, string][] = [
223
+ ['/', 'page'],
224
+ [`/invite/${token}`, 'link'],
225
+ ['/_gate/geist.woff2', 'asset'],
226
+ ['/favicon.svg', 'asset'],
227
+ ['/_studio/studio-abc.js', 'file'],
228
+ ['/_studio/geist-latin.woff2', 'file'],
229
+ ['/api/review', 'api'],
230
+ ['/_gate/handoff', 'handoff'],
231
+ [`/_gate/proof/${token}`, 'proof'],
232
+ ];
233
+ for (const [path, kind] of cases) assert.equal(studioRouteOf(path).kind, kind, path);
234
+ for (const path of [
235
+ STUDIO_PAGE,
236
+ '/index.html',
237
+ '/assets/index.js',
238
+ '/rebrand/',
239
+ '/api/files',
240
+ '/api/review/',
241
+ '/_studio',
242
+ '/_studio/',
243
+ '/_studio/.%09./x',
244
+ '/_studio/%2e%2e/x',
245
+ '/_gate/proof/x',
246
+ `/_gate/handoff/${token}`,
247
+ ])
248
+ assert.equal(studioRouteOf(path).kind, 'none', path);
249
+ // A client host never reaches the studio hub's files.
250
+ for (const path of [STUDIO_PAGE, '/_studio/studio-abc.js'])
251
+ assert.equal(routeOf('acme.example.com', path).kind, 'none', path);
252
+ });
package/core/prototype.ts CHANGED
@@ -109,6 +109,14 @@ const GATE_ASSETS: readonly string[] = [GATE_FONT, '/favicon.svg'];
109
109
  */
110
110
  export const PROOF_PATH = /^\/_gate\/proof\/([\w-]{20,100})$/;
111
111
  export const proofPath = (ask: string) => `/_gate/proof/${ask}`;
112
+ /**
113
+ * A handoff's claim, on a client's host: the studio's host sends a Studio
114
+ * person here with a one-time token, and the gate signs them in on this host.
115
+ */
116
+ const HANDOFF_PATH = /^\/_gate\/handoff\/([\w-]{20,100})$/;
117
+ export const handoffPath = (token: string) => `/_gate/handoff/${token}`;
118
+ /** Where the studio hub asks for a handoff, on the studio's own host: a POST of the client and the project. */
119
+ export const HANDOFF_START = '/_gate/handoff';
112
120
  /**
113
121
  * The studio site's functions, at a client host's root: the review API, and
114
122
  * the files function the gate rewrites a project's files to. The gate passes
@@ -121,6 +129,7 @@ const FUNCTIONS: readonly string[] = ['review', 'files'];
121
129
  *
122
130
  * - `none`: nothing; the gate's plain 404, whoever asks.
123
131
  * - `link`: `/invite/<token>`, at the host root only.
132
+ * - `handoff`: `/_gate/handoff/<token>`, a handoff's claim, at the host root only.
124
133
  * - `asset`: the gate's font and the favicon, served before sign-in.
125
134
  * - `hub`: the client's hub page and its own files (`/`, `/index.html`, `/assets/…`, `/_gate/…`).
126
135
  * - `api`: a function at the root (`project` null), or a project's review API, `/<project>/api/review`.
@@ -130,6 +139,7 @@ const FUNCTIONS: readonly string[] = ['review', 'files'];
130
139
  export type Where =
131
140
  | { kind: 'none' }
132
141
  | { kind: 'link'; token: string }
142
+ | { kind: 'handoff'; token: string }
133
143
  | { kind: 'asset' }
134
144
  | { kind: 'hub' }
135
145
  | { kind: 'api'; project: string | null }
@@ -155,6 +165,8 @@ function projectRoute(project: string, rest: string[]): Where {
155
165
  function where(pathname: string): Where {
156
166
  const linked = pathname.match(LINK_PATH);
157
167
  if (linked) return { kind: 'link', token: linked[1] };
168
+ const handoff = pathname.match(HANDOFF_PATH);
169
+ if (handoff) return { kind: 'handoff', token: handoff[1] };
158
170
  if (GATE_ASSETS.includes(pathname)) return { kind: 'asset' };
159
171
  const parts = segments(pathname);
160
172
  if (!parts) return { kind: 'none' };
@@ -167,6 +179,53 @@ function where(pathname: string): Where {
167
179
  return isProjectSlug(first) ? projectRoute(first, rest) : { kind: 'none' };
168
180
  }
169
181
 
182
+ /**
183
+ * The studio hub's own files, which the studio site's build puts beside the
184
+ * client hub (studioConfig in vite/plugin.ts): its page, and its bundle under
185
+ * one folder. Neither is a path a client's host answers: `_studio` and
186
+ * `studio.html` are no project slug, so routeOf sends both nowhere.
187
+ */
188
+ export const STUDIO_DIR = '_studio';
189
+ export const STUDIO_PAGE = '/studio.html';
190
+
191
+ /**
192
+ * Where a request on the studio's own host (`studio.<apex>`) goes. Only
193
+ * Studio signs in here, and nothing of a client's host is answered:
194
+ *
195
+ * - `none`: nothing; the gate's plain 404.
196
+ * - `link`: `/invite/<token>`, a Studio person's link.
197
+ * - `asset`: the gate's font and the favicon, served before sign-in.
198
+ * - `page`: `/`, the studio hub.
199
+ * - `file`: the studio hub's bundle, below `/_studio/`.
200
+ * - `api`: `/api/review`, where the studio hub reads every client.
201
+ * - `handoff`: `/_gate/handoff`, where the studio hub asks to open a client.
202
+ * - `proof`: `/_gate/proof/<ask>`, the host proof `studio create` and
203
+ * `studio deploy` ask the studio's own host for, as `client add` asks a client's.
204
+ */
205
+ export type StudioWhere =
206
+ | { kind: 'none' }
207
+ | { kind: 'link'; token: string }
208
+ | { kind: 'asset' }
209
+ | { kind: 'page' }
210
+ | { kind: 'file' }
211
+ | { kind: 'api' }
212
+ | { kind: 'handoff' }
213
+ | { kind: 'proof'; ask: string };
214
+
215
+ /** The one place a request's path on the studio's own host is read. It fails closed, as routeOf does. */
216
+ export function studioRouteOf(pathname: string): StudioWhere {
217
+ const linked = pathname.match(LINK_PATH);
218
+ if (linked) return { kind: 'link', token: linked[1] };
219
+ if (GATE_ASSETS.includes(pathname)) return { kind: 'asset' };
220
+ if (pathname === '/') return { kind: 'page' };
221
+ if (pathname === '/api/review') return { kind: 'api' };
222
+ if (pathname === HANDOFF_START) return { kind: 'handoff' };
223
+ const proof = pathname.match(PROOF_PATH);
224
+ if (proof) return { kind: 'proof', ask: proof[1] };
225
+ const parts = segments(pathname);
226
+ return parts && parts.length > 1 && parts[0] === STUDIO_DIR ? { kind: 'file' } : { kind: 'none' };
227
+ }
228
+
170
229
  /**
171
230
  * The one place a studio site reads a request's path. It fails closed: a
172
231
  * path that could mean more than one file (`segments`) goes nowhere. Which client a host is, is the store's to say (a host may be a
@@ -40,6 +40,17 @@ export const DEFAULT_ACCESS: Access = '3m';
40
40
  * archiving a client, or adding one, takes effect on every instance within it.
41
41
  */
42
42
  export const HOST_SECONDS = 60;
43
+ /** How long a handoff lasts: Studio opens a client from the studio's host, and the client's host claims it within this. */
44
+ export const HANDOFF_SECONDS = 60;
45
+
46
+ /**
47
+ * The studio's own host (`studio.<apex>`): its entry in the store's `hosts`,
48
+ * in place of a client's slug, and the client a session on it is signed to.
49
+ * Never a client's slug (RESERVED_CLIENTS), so no client host is ever it.
50
+ */
51
+ export const STUDIO_SITE = 'studio';
52
+ /** What a studio site's host serves: one client, the studio's own host, or nothing. */
53
+ export type Site = Client | typeof STUDIO_SITE | null;
43
54
 
44
55
  const ACCESS_MONTHS: Record<Exclude<Access, 'open'>, number> = { '3m': 3, '6m': 6 };
45
56
 
@@ -140,6 +151,15 @@ export type Project = { slug: string; client: string; name: string; build: strin
140
151
  /** A one-time sign-in link, stored under the hash of its token and deleted when used. */
141
152
  export type Link = { person: string; expiresAt: string };
142
153
 
154
+ /**
155
+ * A handoff: a Studio person, signed in on the studio's host, opening one
156
+ * client's hub (or one of its projects) on that client's host. Stored with
157
+ * the client under the hash of its token, deleted when its host claims it,
158
+ * good for HANDOFF_SECONDS. `project` is a project of the client with a
159
+ * build when it was made, or null for the client's hub.
160
+ */
161
+ export type Handoff = { person: string; project: string | null; expiresAt: string };
162
+
143
163
  /** A comment's author as it was when they wrote it, so a revoked person's comments still read. */
144
164
  export type Author = Pick<Person, 'id' | 'name' | 'team'>;
145
165
 
@@ -273,13 +293,14 @@ export function checkAccess(input: unknown): Checked<Access> {
273
293
  return isAccess(input) ? { ok: true, value: input } : { ok: false, problems: [ACCESS_PROBLEM] };
274
294
  }
275
295
 
276
- export type PersonInput = Pick<Person, 'name' | 'team' | 'kind'> & { access: Access | null };
296
+ /** `team` absent: the service fills it, the studio's name for Studio and the client's for a Client. */
297
+ export type PersonInput = Pick<Person, 'name' | 'kind'> & { team?: string; access: Access | null };
277
298
 
278
299
  export function checkPersonInput(input: unknown): Checked<PersonInput> {
279
300
  const i = (input ?? {}) as Record<string, unknown>;
280
301
  const problems: string[] = [];
281
302
  const name = text(i.name, 'name', LIMITS.name, problems);
282
- const team = text(i.team, 'team', LIMITS.team, problems);
303
+ const team = i.team === undefined ? undefined : text(i.team, 'team', LIMITS.team, problems);
283
304
  // Most people added are on the client's side; Studio is the deliberate choice.
284
305
  const kind = i.kind ?? 'client';
285
306
  if (!PERSON_KINDS.includes(kind as PersonKind)) problems.push(`kind: one of ${PERSON_KINDS.join(', ')}`);
@@ -288,7 +309,15 @@ export function checkPersonInput(input: unknown): Checked<PersonInput> {
288
309
  if (access !== null && !isAccess(access)) problems.push(ACCESS_PROBLEM);
289
310
  return problems.length
290
311
  ? { ok: false, problems }
291
- : { ok: true, value: { name, team, kind: kind as PersonKind, access: access as Access | null } };
312
+ : {
313
+ ok: true,
314
+ value: {
315
+ name,
316
+ ...(team === undefined ? {} : { team }),
317
+ kind: kind as PersonKind,
318
+ access: access as Access | null,
319
+ },
320
+ };
292
321
  }
293
322
 
294
323
  export type ClientInput = Pick<Client, 'slug' | 'host' | 'name'>;
@@ -81,6 +81,22 @@ beforeEach(async () => {
81
81
  await review.setAudience(A, studio, anchor.prototype, 'client');
82
82
  });
83
83
 
84
+ test('without a team, a Studio person gets the studio’s, from whoever adds them, and a Client their client’s name', async () => {
85
+ const mo = (await review.addPerson(A, studio, { name: 'Mo', kind: 'studio' })).person;
86
+ assert.equal(mo.team, studio.team);
87
+ const kim = (await review.addPerson(A, studio, { name: 'Kim' })).person;
88
+ assert.equal(kim.team, 'acme');
89
+ assert.equal((await review.addPerson(B, studio, { name: 'Lu' })).person.team, 'bravo');
90
+ // A team given still wins, and an empty one is still refused.
91
+ assert.equal(
92
+ (await review.addPerson(A, studio, { name: 'Jo', team: 'Freelance' })).person.team,
93
+ 'Freelance',
94
+ );
95
+ await rejects(review.addPerson(A, studio, { name: 'Al', team: ' ' }), 'invalid');
96
+ // The studio's first person has no one to take a team from.
97
+ await rejects(review.addStudioPerson(studio, { name: 'Owner' }), 'invalid');
98
+ });
99
+
84
100
  test('a link works once, and looking at it uses nothing up', async () => {
85
101
  assert.equal((await review.peekLink(A, clientToken))?.id, client.id);
86
102
  assert.equal((await review.peekLink(A, clientToken))?.id, client.id);
@@ -1179,6 +1195,13 @@ const STUDIO_WIDE: [string, (actor: Person | null) => Promise<unknown>][] = [
1179
1195
  ['studioAccess', (a) => review.studioAccess(a)],
1180
1196
  ['setStudioAccess', (a) => review.setStudioAccess(a, 'open')],
1181
1197
  ['clientWait', (a) => review.clientWait(a, { slug: 'acme', host: 'acme.studio.test' })],
1198
+ ['setStudioHost', (a) => review.setStudioHost(a, 'studio.studio.test')],
1199
+ ['listClients', (a) => review.listClients(a)],
1200
+ ['startHandoff', (a) => review.startHandoff(a, { client: 'acme', project: null })],
1201
+ ['studioClient', (a) => review.studioClient(a, 'acme')],
1202
+ ['studioHostProven', (a) => review.studioHostProven(a)],
1203
+ ['startStudioProof', (a) => review.startStudioProof(a)],
1204
+ ['endStudioProof', (a) => review.endStudioProof(a)],
1182
1205
  ];
1183
1206
  /** The ops that name an id, whose answer to an unknown one is what out of scope must match. */
1184
1207
  const BY_ID = new Set([
@@ -1202,8 +1225,11 @@ const ACTORLESS = [
1202
1225
  'useLink',
1203
1226
  'audienceOf',
1204
1227
  'clientOfHost',
1228
+ 'siteOfHost',
1205
1229
  'projectOf',
1206
1230
  'proofAnswer',
1231
+ 'claimHandoff',
1232
+ 'studioProofAnswer',
1207
1233
  ];
1208
1234
 
1209
1235
  test('every op of the review service is in the cross-client test or named as having no actor', () => {
@@ -2124,3 +2150,217 @@ test('a project points only at a build started for it, never one started before
2124
2150
  await review.addProject(acme, studio, { slug: 'atlas', name: 'Atlas' });
2125
2151
  await rejects(review.setBuild({ client: 'acme', project: 'atlas' }, studio, fast), 'invalid');
2126
2152
  });
2153
+
2154
+ // ── The studio's own host and the handoff ──────────────────────────────────
2155
+
2156
+ /** A store with acme added (its host proven) and bravo still waiting for its host, on a clock a test moves. */
2157
+ async function handoffStudio() {
2158
+ const kv = memoryKV();
2159
+ let t = Date.parse('2030-01-01T00:00:00Z');
2160
+ const r = createReview(kv, { now: () => new Date(t) });
2161
+ const ed = await r.addStudioPerson({ ...studio, id: 'cli' }, { name: 'Ed', team: 'Studio' });
2162
+ await r.addClient(ed, { slug: 'acme', host: 'acme.studio.test', name: 'Acme' });
2163
+ await r.addClient(ed, { slug: 'bravo', host: 'bravo.studio.test', name: 'Bravo' });
2164
+ await r.endProof({ client: 'acme' }, ed);
2165
+ await r.addProject({ client: 'acme' }, ed, { slug: 'rebrand', name: 'Rebrand' });
2166
+ await r.addProject({ client: 'acme' }, ed, { slug: 'draft', name: 'Draft' });
2167
+ await r.startBuild({ client: 'acme', project: 'rebrand' }, ed, 'a'.repeat(32));
2168
+ await r.setBuild({ client: 'acme', project: 'rebrand' }, ed, 'a'.repeat(32));
2169
+ return { kv, r, ed, move: (ms: number) => (t += ms) };
2170
+ }
2171
+
2172
+ test('the client a studio-host op names is an added, live client of the studio, or not found as an unknown one; Studio only', async () => {
2173
+ const { r, ed } = await handoffStudio();
2174
+ assert.equal((await r.studioClient(ed, 'acme')).host, 'acme.studio.test');
2175
+ // bravo still waits for its host; zeta is unknown; the rest are no client's slug.
2176
+ for (const slug of ['bravo', 'zeta', 'studio', 'site', '../acme', 'ACME', '', null, 42, { client: 'acme' }])
2177
+ await rejects(r.studioClient(ed, slug), 'not_found');
2178
+ const rae = await r.addPerson({ client: 'acme' }, ed, { name: 'Rae', team: 'Acme' });
2179
+ await rejects(r.studioClient(rae.person, 'acme'), 'forbidden');
2180
+ await rejects(r.studioClient(null, 'acme'), 'unauthorized');
2181
+ await r.archiveClient({ client: 'acme' }, ed);
2182
+ await rejects(r.studioClient(ed, 'acme'), 'not_found');
2183
+ });
2184
+
2185
+ test('people on the studio’s own host are the studio’s alone, Studio only, and no Client is made or found there', async () => {
2186
+ const { kv, r, ed } = await handoffStudio();
2187
+ const at = 'studio' as const;
2188
+ const rae = await r.addPerson({ client: 'acme' }, ed, { name: 'Rae', team: 'Acme' });
2189
+ const sam = await r.addPerson(at, ed, { name: 'Sam', kind: 'studio' });
2190
+ assert.deepEqual([sam.person.client, sam.person.kind, sam.person.team], [null, 'studio', 'Studio']);
2191
+ assert.equal(sam.person.accessEndsAt, null);
2192
+ assert.equal((await r.useLink(at, sam.token))?.id, sam.person.id);
2193
+ assert.deepEqual(
2194
+ (await r.listPeople(at, ed)).map((p) => p.name),
2195
+ ['Ed', 'Sam'],
2196
+ );
2197
+ // A Client belongs to a client: none is named here, so none is made.
2198
+ const before = JSON.stringify(await kv.hgetall(keyOf('studio', 'people')));
2199
+ await rejects(r.addPerson(at, ed, { name: 'Oz', team: 'Acme', kind: 'client' }), 'invalid');
2200
+ assert.equal(JSON.stringify(await kv.hgetall(keyOf('studio', 'people'))), before);
2201
+ // A Client's id is no one here, for every op by id.
2202
+ await rejects(r.newLink(at, ed, rae.person.id), 'not_found');
2203
+ await rejects(r.signOutEverywhere(at, ed, rae.person.id), 'not_found');
2204
+ await rejects(r.revoke(at, ed, rae.person.id), 'not_found');
2205
+ await rejects(r.setAccess(at, ed, rae.person.id, '6m'), 'not_found');
2206
+ assert.equal(
2207
+ (await r.listPeople({ client: 'acme' }, ed)).some((p) => p.id === rae.person.id),
2208
+ true,
2209
+ );
2210
+ // Each op on the studio's own people works there.
2211
+ assert.match((await r.newLink(at, ed, sam.person.id)).token, /^[\w-]{43}$/);
2212
+ assert.equal((await r.signOutEverywhere(at, ed, sam.person.id)).person.epoch, 1);
2213
+ assert.notEqual((await r.setAccess(at, ed, sam.person.id, '3m')).accessEndsAt, null);
2214
+ assert.ok((await r.revoke(at, ed, sam.person.id)).revokedAt);
2215
+ await rejects(r.revoke(at, ed, ed.id), 'invalid');
2216
+ // A Client, even signed in, and a caller signed out get nothing, and nothing is written.
2217
+ const snapshot = JSON.stringify(await kv.hgetall(keyOf('studio', 'people')));
2218
+ for (const [who, actor, code] of [
2219
+ ['a Client', rae.person, 'forbidden'],
2220
+ ['signed out', null, 'unauthorized'],
2221
+ ] as const) {
2222
+ await rejects(r.listPeople(at, actor), code);
2223
+ await rejects(r.addPerson(at, actor, { name: 'Oz', kind: 'studio' }), code);
2224
+ await rejects(r.newLink(at, actor, ed.id), code);
2225
+ await rejects(r.signOutEverywhere(at, actor, ed.id), code);
2226
+ await rejects(r.revoke(at, actor, ed.id), code);
2227
+ await rejects(r.setAccess(at, actor, ed.id, 'open'), code);
2228
+ assert.equal(JSON.stringify(await kv.hgetall(keyOf('studio', 'people'))), snapshot, who);
2229
+ }
2230
+ });
2231
+
2232
+ test('the studio’s own host is Studio’s to set, once; it is no client’s, and no client may take it', async () => {
2233
+ const { r, ed } = await handoffStudio();
2234
+ const rae = await r.addPerson({ client: 'acme' }, ed, { name: 'Rae', team: 'Acme' });
2235
+ await rejects(r.setStudioHost(rae.person, 'studio.studio.test'), 'forbidden');
2236
+ assert.equal(await r.siteOfHost('studio.studio.test'), null);
2237
+ assert.equal(await r.setStudioHost(ed, 'studio.studio.test'), 'added');
2238
+ assert.equal(await r.setStudioHost(ed, 'studio.studio.test'), 'kept');
2239
+ assert.equal(await r.siteOfHost('STUDIO.studio.test.'), 'studio');
2240
+ assert.equal(await r.clientOfHost('studio.studio.test'), null);
2241
+ assert.equal(((await r.siteOfHost('acme.studio.test')) as { slug: string }).slug, 'acme');
2242
+ await rejects(r.setStudioHost(ed, 'acme.studio.test'), 'invalid');
2243
+ await rejects(r.setStudioHost(ed, 'Studio.studio.test'), 'invalid');
2244
+ await rejects(r.addClient(ed, { slug: 'zeta', host: 'studio.studio.test', name: 'Z' }), 'invalid');
2245
+ });
2246
+
2247
+ test('on the studio’s own host only Studio signs in: a Client’s link and session are nobody there', async () => {
2248
+ const { r, ed } = await handoffStudio();
2249
+ const rae = await r.addPerson({ client: 'acme' }, ed, { name: 'Rae', team: 'Acme' });
2250
+ assert.equal(await r.peekLink('studio', rae.token), null);
2251
+ assert.equal(await r.useLink('studio', rae.token), null);
2252
+ assert.equal(await r.sessionPerson('studio', rae.person.id, 0), null);
2253
+ // Rae's link was never used up by the tries there.
2254
+ assert.equal((await r.peekLink({ client: 'acme' }, rae.token))?.id, rae.person.id);
2255
+ const { token } = await r.newStudioLink(ed, ed.id);
2256
+ assert.equal((await r.peekLink('studio', token))?.id, ed.id);
2257
+ assert.equal((await r.useLink('studio', token))?.id, ed.id);
2258
+ assert.equal(await r.useLink('studio', token), null);
2259
+ assert.equal((await r.sessionPerson('studio', ed.id, 0))?.id, ed.id);
2260
+ assert.equal(await r.sessionPerson('studio', ed.id, 1), null);
2261
+ });
2262
+
2263
+ test('the studio lists every client with all its projects, by name, archived ones too; Studio only', async () => {
2264
+ const { r, ed } = await handoffStudio();
2265
+ const listed = await r.listClients(ed);
2266
+ assert.deepEqual(
2267
+ listed.map(({ client, projects }) => [
2268
+ client.slug,
2269
+ client.provenAt !== null,
2270
+ projects.map((p) => p.slug),
2271
+ ]),
2272
+ [
2273
+ ['acme', true, ['draft', 'rebrand']],
2274
+ ['bravo', false, []],
2275
+ ],
2276
+ );
2277
+ await r.archiveClient({ client: 'bravo' }, ed);
2278
+ assert.ok((await r.listClients(ed))[1].client.archivedAt);
2279
+ const rae = await r.addPerson({ client: 'acme' }, ed, { name: 'Rae', team: 'Acme' });
2280
+ await rejects(r.listClients(rae.person), 'forbidden');
2281
+ });
2282
+
2283
+ test('a handoff opens one added client once, on its own host only, within a minute', async () => {
2284
+ const { kv, r, ed, move } = await handoffStudio();
2285
+ const acme = { client: 'acme' };
2286
+ const made = await r.startHandoff(ed, { client: 'acme', project: null });
2287
+ assert.equal(made.host, 'acme.studio.test');
2288
+ assert.match(made.token, /^[\w-]{43}$/);
2289
+ // The store keeps the token's hash, never the token.
2290
+ assert.doesNotMatch(JSON.stringify(await kv.hgetall(keyOf(acme, 'handoffs'))), new RegExp(made.token));
2291
+ // Another client's host, a malformed token or the local store's client finds nothing, and uses nothing up.
2292
+ for (const scope of [{ client: 'bravo' }, { client: 'site' }, { client: 'zeta' }])
2293
+ assert.equal(await r.claimHandoff(scope, made.token), null, scope.client);
2294
+ assert.equal(await r.claimHandoff(acme, 'short'), null);
2295
+ const claimed = await r.claimHandoff(acme, made.token);
2296
+ assert.equal(claimed?.person.id, ed.id);
2297
+ assert.equal(claimed?.project, null);
2298
+ assert.equal(await r.claimHandoff(acme, made.token), null);
2299
+ // A project of the client with a build goes with it; past HANDOFF_SECONDS it is nothing.
2300
+ const onto = await r.startHandoff(ed, { client: 'acme', project: 'rebrand' });
2301
+ assert.equal((await r.claimHandoff(acme, onto.token))?.project, 'rebrand');
2302
+ const late = await r.startHandoff(ed, { client: 'acme', project: null });
2303
+ move(60_000);
2304
+ assert.equal(await r.claimHandoff(acme, late.token), null);
2305
+ // Two claims at once: one person.
2306
+ const twice = await r.startHandoff(ed, { client: 'acme', project: null });
2307
+ const both = await Promise.all([r.claimHandoff(acme, twice.token), r.claimHandoff(acme, twice.token)]);
2308
+ assert.equal(both.filter(Boolean).length, 1);
2309
+ // Unclaimed handoffs past their time go as new ones are made; claimed ones are gone already.
2310
+ await r.startHandoff(ed, { client: 'acme', project: null });
2311
+ assert.equal(Object.keys(await kv.hgetall(keyOf(acme, 'handoffs'))).length, 1);
2312
+ move(60_000);
2313
+ await r.startHandoff(ed, { client: 'acme', project: null });
2314
+ assert.equal(Object.keys(await kv.hgetall(keyOf(acme, 'handoffs'))).length, 1);
2315
+ });
2316
+
2317
+ test('no handoff for a client still being added, archived or unknown, or a project it has not built; Studio only', async () => {
2318
+ const { r, ed } = await handoffStudio();
2319
+ const asks: { client: unknown; project: unknown }[] = [
2320
+ { client: 'bravo', project: null },
2321
+ { client: 'zeta', project: null },
2322
+ { client: 'studio', project: null },
2323
+ { client: 'site', project: null },
2324
+ { client: '../acme', project: null },
2325
+ { client: 'acme', project: 'draft' },
2326
+ { client: 'acme', project: 'nope' },
2327
+ { client: 'acme', project: '../bravo' },
2328
+ { client: 'acme', project: 'invite' },
2329
+ ];
2330
+ for (const ask of asks) await rejects(r.startHandoff(ed, ask), 'not_found');
2331
+ const rae = await r.addPerson({ client: 'acme' }, ed, { name: 'Rae', team: 'Acme' });
2332
+ await rejects(r.startHandoff(rae.person, { client: 'acme', project: null }), 'forbidden');
2333
+ await rejects(r.startHandoff(null, { client: 'acme', project: null }), 'unauthorized');
2334
+ // Made, then the client archived, or the person revoked: the claim gives nothing.
2335
+ const made = await r.startHandoff(ed, { client: 'acme', project: null });
2336
+ const jo = await r.addStudioPerson(ed, { name: 'Jo', team: 'Studio' });
2337
+ const byJo = await r.startHandoff(jo, { client: 'acme', project: null });
2338
+ await r.revoke({ client: 'acme' }, ed, jo.id);
2339
+ assert.equal(await r.claimHandoff({ client: 'acme' }, byJo.token), null);
2340
+ await r.archiveClient({ client: 'acme' }, ed);
2341
+ assert.equal(await r.claimHandoff({ client: 'acme' }, made.token), null);
2342
+ await rejects(r.startHandoff(ed, { client: 'acme', project: null }), 'not_found');
2343
+ });
2344
+
2345
+ test('the studio host’s proof is Studio’s to start and end, answers only its own ask, keeps no ask, and marks the host proven once', async () => {
2346
+ const { kv, r, ed } = await handoffStudio();
2347
+ assert.equal(await r.studioHostProven(ed), false);
2348
+ const { ask, answer } = await r.startStudioProof(ed);
2349
+ assert.notEqual(ask, answer);
2350
+ assert.equal(await r.studioProofAnswer(ask), answer);
2351
+ assert.doesNotMatch(JSON.stringify(await kv.hgetall(keyOf('studio', 'proof'))), new RegExp(ask));
2352
+ for (const given of [answer, 'x'.repeat(43), 'short'])
2353
+ assert.equal(await r.studioProofAnswer(given), null, given);
2354
+ // A client's proof is never the studio's, nor the other way.
2355
+ const client = await r.startProof({ client: 'bravo' }, ed);
2356
+ assert.equal(await r.studioProofAnswer(client.ask), null);
2357
+ assert.equal(await r.proofAnswer({ client: 'bravo' }, ask), null);
2358
+ const next = await r.startStudioProof(ed);
2359
+ assert.equal(await r.studioProofAnswer(ask), null);
2360
+ await r.endStudioProof(ed);
2361
+ assert.equal(await r.studioProofAnswer(next.ask), null);
2362
+ assert.equal(await r.studioHostProven(ed), true);
2363
+ const at = await kv.hget(keyOf('studio', 'settings'), 'hostProvenAt');
2364
+ await r.endStudioProof(ed);
2365
+ assert.equal(await kv.hget(keyOf('studio', 'settings'), 'hostProvenAt'), at);
2366
+ });