create-ailk 0.46.5 → 0.47.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 (30) hide show
  1. package/component-catalog.json +90 -7
  2. package/dist/lib/env-catalog.js +4 -0
  3. package/package.json +3 -3
  4. package/templates/apps/api/.env.example +18 -0
  5. package/templates/apps/api/CLAUDE.md +7 -2
  6. package/templates/apps/api/src/__tests__/vercel-handler.test.ts +176 -2
  7. package/templates/apps/api/src/lib/__mocks__/prisma.ts +31 -0
  8. package/templates/apps/api/src/vercel-handler.ts +32 -2
  9. package/templates/apps/mcp/.env.example +8 -0
  10. package/templates/apps/mcp/CLAUDE.md +2 -2
  11. package/templates/apps/web/CLAUDE.md +31 -3
  12. package/templates/apps/web/app/[locale]/layout.tsx +98 -37
  13. package/templates/apps/web/app/llms.txt/__tests__/site-listing.test.ts +13 -3
  14. package/templates/apps/web/lib/__tests__/grid-read.test.ts +212 -0
  15. package/templates/apps/web/lib/__tests__/hub-read.test.ts +90 -0
  16. package/templates/apps/web/lib/__tests__/hub-site-config.test.ts +394 -10
  17. package/templates/apps/web/lib/__tests__/page-chrome-navigation.test.ts +194 -0
  18. package/templates/apps/web/lib/__tests__/pricing-page.test.ts +210 -0
  19. package/templates/apps/web/lib/__tests__/site-identity-feature-list.test.ts +19 -1
  20. package/templates/apps/web/lib/__tests__/site-navigation.test.ts +288 -0
  21. package/templates/apps/web/lib/grid-read.ts +115 -0
  22. package/templates/apps/web/lib/hub-read.ts +35 -0
  23. package/templates/apps/web/lib/hub-site-config.ts +154 -29
  24. package/templates/apps/web/lib/in-page-blocks.ts +11 -67
  25. package/templates/apps/web/lib/in-page-magnet.ts +5 -24
  26. package/templates/apps/web/lib/page-chrome.ts +70 -3
  27. package/templates/apps/web/lib/pricing-page.ts +33 -2
  28. package/templates/apps/web/lib/site-navigation.ts +208 -0
  29. package/templates/apps/web/next.config.mjs +19 -6
  30. package/versions.json +51 -51
@@ -0,0 +1,90 @@
1
+ /**
2
+ * lib/hub-read.ts — the shared hub read (#8731), a leaf module since #12385.
3
+ *
4
+ * `grid-read.ts` once imported `sharedHubRead` from `in-page-magnet.ts`, and
5
+ * `pricing-page.ts` → `grid-read.ts` → `in-page-magnet.ts` →
6
+ * `contact.actions.ts` → `site-pages.config.tsx` → `pricing-page.ts` closed a
7
+ * cycle that stopped `next build` collecting page data. The read now lives
8
+ * here, importing no other apps/web module, and `in-page-magnet.ts`
9
+ * re-exports it under the same names.
10
+ */
11
+ import { readFileSync } from "node:fs";
12
+ import path from "node:path";
13
+
14
+ jest.mock("@working-theory/api-client", () => ({
15
+ flows: { get: jest.fn() },
16
+ leads: { captureStatus: jest.fn() },
17
+ }));
18
+ jest.mock("@working-theory/site-pages/pages/contact/contact.actions", () => ({
19
+ captureLeadAction: jest.fn(),
20
+ }));
21
+ // eslint-disable-next-line no-restricted-syntax -- test-seam mock: a "use server" boundary in-page-magnet only binds
22
+ jest.mock("../newsletter.actions", () => ({
23
+ subscribeNewsletterAction: jest.fn(),
24
+ }));
25
+
26
+ import { HUB_READ_TTL_MS, sharedHubRead } from "../hub-read";
27
+ import * as inPageMagnet from "../in-page-magnet";
28
+
29
+ /** Every module specifier a source file names: static and dynamic imports, re-exports and requires. */
30
+ function specifiers(file: string): string[] {
31
+ const source = readFileSync(path.join(__dirname, "..", file), "utf8");
32
+ const found: string[] = [];
33
+ for (const match of source.matchAll(/(?:\bfrom\s*|\bimport\s*\(\s*|\bimport\s+|\brequire\s*\(\s*)(["'`])([^"'`]+)\1/g)) {
34
+ found.push(match[2]!);
35
+ }
36
+ return found;
37
+ }
38
+
39
+ describe("hub-read.ts is a leaf (#12385)", () => {
40
+ // No package import either: the #12385 cycle ran through one
41
+ // (`@working-theory/site-pages`' contact actions read `site-pages.config.tsx`).
42
+ it("imports nothing at all", () => {
43
+ expect(specifiers("hub-read.ts")).toEqual([]);
44
+ });
45
+ });
46
+
47
+ describe("in-page-magnet.ts re-exports the shared hub read under the same names (#12385)", () => {
48
+ it("exports the very same sharedHubRead and HUB_READ_TTL_MS", () => {
49
+ expect(inPageMagnet.sharedHubRead).toBe(sharedHubRead);
50
+ expect(inPageMagnet.HUB_READ_TTL_MS).toBe(HUB_READ_TTL_MS);
51
+ expect(HUB_READ_TTL_MS).toBe(15_000);
52
+ });
53
+ });
54
+
55
+ describe("sharedHubRead (#8731)", () => {
56
+ afterEach(() => {
57
+ jest.useRealTimers();
58
+ });
59
+
60
+ it("shares one in-flight read between concurrent calls", async () => {
61
+ const read = jest.fn(async () => "answer");
62
+ const [a, b] = await Promise.all([sharedHubRead("hub-read:concurrent", read), sharedHubRead("hub-read:concurrent", read)]);
63
+ expect([a, b]).toEqual(["answer", "answer"]);
64
+ expect(read).toHaveBeenCalledTimes(1);
65
+ });
66
+
67
+ it("shares a null answer too, so a burst against an unreachable hub is one read", async () => {
68
+ const read = jest.fn(async () => null);
69
+ await sharedHubRead("hub-read:null", read);
70
+ await expect(sharedHubRead("hub-read:null", read)).resolves.toBeNull();
71
+ expect(read).toHaveBeenCalledTimes(1);
72
+ });
73
+
74
+ it("keeps each key's read apart", async () => {
75
+ await expect(sharedHubRead("hub-read:a", async () => "a")).resolves.toBe("a");
76
+ await expect(sharedHubRead("hub-read:b", async () => "b")).resolves.toBe("b");
77
+ });
78
+
79
+ it("reads afresh once HUB_READ_TTL_MS has passed, and not before", async () => {
80
+ jest.useFakeTimers({ now: 1_000_000 });
81
+ const read = jest.fn(async () => "fresh");
82
+ await sharedHubRead("hub-read:ttl", read);
83
+ jest.setSystemTime(1_000_000 + HUB_READ_TTL_MS - 1);
84
+ await sharedHubRead("hub-read:ttl", read);
85
+ expect(read).toHaveBeenCalledTimes(1);
86
+ jest.setSystemTime(1_000_000 + HUB_READ_TTL_MS);
87
+ await sharedHubRead("hub-read:ttl", read);
88
+ expect(read).toHaveBeenCalledTimes(2);
89
+ });
90
+ });
@@ -4,15 +4,21 @@
4
4
 
5
5
  /**
6
6
  * The hub read of the site's identity and seo (#11456, spec
7
- * docs/specs/11456-a-hub-connected-site-reads-spec.md D3; AC-2, AC-3).
7
+ * docs/specs/11456-a-hub-connected-site-reads-spec.md D3; AC-2, AC-3), and
8
+ * the keyless read of a single-tenant site (#12390, spec
9
+ * docs/specs/12390-a-single-tenant-site-shows-what-its-admin-saves-spec.md
10
+ * D2, D3; AC-4, AC-5).
8
11
  *
9
12
  * External boundaries doubled:
10
13
  * - `@working-theory/api-client`'s `siteConfig.get` — the HTTP call to the
11
- * hub (`sendsSpokeKey` stays real, so the switch is the env's);
12
- * - `next/cache`'s `unstable_cache` — a fake with Next's write rule: a
13
- * RESOLVED callback is written, a THROWING one writes nothing and the
14
- * last entry is served (`unstable-cache.js` `cacheNewResult`).
14
+ * hub (`sendsSpokeKey`, `apiBaseConfigured` and `isSafeTransport` stay
15
+ * real, so the switch is the env's);
16
+ * - `next/cache`'s `unstable_cache` — a fake with Next's rules: an entry is
17
+ * served while it stands, a RESOLVED callback is written, a THROWING one
18
+ * writes nothing and the last entry is served (`unstable-cache.js`
19
+ * `cacheNewResult`).
15
20
  */
21
+ import { createHash } from "node:crypto";
16
22
  import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
17
23
  import { tmpdir } from "node:os";
18
24
  import { join } from "node:path";
@@ -33,6 +39,7 @@ const mockUnstableCache = jest.fn(
33
39
  ) =>
34
40
  async () => {
35
41
  const key = keyParts.join("|");
42
+ if (mockCacheStore.has(key)) return mockCacheStore.get(key);
36
43
  try {
37
44
  const value = await fn();
38
45
  mockCacheStore.set(key, value);
@@ -52,6 +59,7 @@ import { siteConfigTag } from "@working-theory/page-renderer/edge";
52
59
 
53
60
  import {
54
61
  __resetHubSiteForTests,
62
+ HUB_SITE_RETRY_SECONDS,
55
63
  HUB_SITE_REVALIDATE_SECONDS,
56
64
  HUB_SITE_TIMEOUT_MS,
57
65
  heldHubSite,
@@ -70,7 +78,14 @@ const HUB_IDENTITY = {
70
78
  const HUB_SEO = { version: "1", robots: { index: false } };
71
79
  const ok = (data: unknown) => ({ ok: true, body: data });
72
80
 
73
- const ENV_KEYS = ["CONTENT_ADAPTER", "AILK_SPOKE_KEY", "AILK_API_URL", "AILK_CONTENT_ROOT"] as const;
81
+ const ENV_KEYS = [
82
+ "CONTENT_ADAPTER",
83
+ "AILK_SPOKE_KEY",
84
+ "AILK_API_URL",
85
+ "NEXT_PUBLIC_AILK_API_URL",
86
+ "NEXT_PUBLIC_API_URL",
87
+ "AILK_CONTENT_ROOT",
88
+ ] as const;
74
89
  const savedEnv = new Map(ENV_KEYS.map((k) => [k, process.env[k]]));
75
90
  let root: string;
76
91
 
@@ -79,6 +94,19 @@ function hubConnect(): void {
79
94
  process.env.AILK_API_URL = "https://hub.example";
80
95
  }
81
96
 
97
+ /** A single-tenant site: its own API's address, no spoke key (#12390). */
98
+ function keylessConnect(address = "https://api.solo.example"): void {
99
+ delete process.env.AILK_SPOKE_KEY;
100
+ process.env.AILK_API_URL = address;
101
+ }
102
+
103
+ /** The warn lines a keyless refusal wrote. */
104
+ function refusalWarns(): unknown[][] {
105
+ return jest
106
+ .mocked(console.warn)
107
+ .mock.calls.filter((call) => String(call[0]).includes("refused"));
108
+ }
109
+
82
110
  beforeEach(() => {
83
111
  mockGet.mockReset();
84
112
  mockUnstableCache.mockClear();
@@ -113,17 +141,49 @@ describe("hubConnected (#11456 D3, #11515)", () => {
113
141
  process.env.CONTENT_ADAPTER = "database";
114
142
  expect(hubConnected()).toBe(true);
115
143
  delete process.env.AILK_SPOKE_KEY;
144
+ delete process.env.AILK_API_URL;
116
145
  expect(hubConnected()).toBe(false);
117
146
  });
118
147
  });
119
148
 
149
+ describe("hubConnected — a key or a configured API address (#12390 D2, AC-4)", () => {
150
+ it("is true with a spoke key and no address (the loopback default carries the key)", () => {
151
+ process.env.AILK_SPOKE_KEY = KEY;
152
+ expect(hubConnected()).toBe(true);
153
+ });
154
+
155
+ it.each([
156
+ ["an https: address", "AILK_API_URL", "https://api.solo.example"],
157
+ ["a loopback http: address", "AILK_API_URL", "http://127.0.0.1:4000"],
158
+ ["localhost on the browser name", "NEXT_PUBLIC_AILK_API_URL", "http://localhost:3001"],
159
+ ["an https: address on the deprecated name", "NEXT_PUBLIC_API_URL", "https://api.solo.example"],
160
+ ])("is true with %s and no key", (_label, name, address) => {
161
+ process.env[name] = address;
162
+ expect(hubConnected()).toBe(true);
163
+ });
164
+
165
+ it("is false with an http: address on another host and no key — never clear text to a remote host", () => {
166
+ process.env.AILK_API_URL = "http://api.solo.example";
167
+ expect(hubConnected()).toBe(false);
168
+ });
169
+
170
+ it("is false with neither; readHubSite answers undefined and never calls siteConfig.get", async () => {
171
+ expect(hubConnected()).toBe(false);
172
+ await expect(readHubSite()).resolves.toBeUndefined();
173
+ expect(mockGet).not.toHaveBeenCalled();
174
+ expect(mockUnstableCache).not.toHaveBeenCalled();
175
+ expect(heldHubSite()).toBeUndefined();
176
+ });
177
+ });
178
+
120
179
  describe("readHubSite (#11456 D3, AC-3)", () => {
121
180
  it.each([
122
181
  ["CONTENT_ADAPTER unset", () => undefined],
123
182
  ["CONTENT_ADAPTER=database", () => (process.env.CONTENT_ADAPTER = "database")],
124
- ])("with no AILK_SPOKE_KEY and %s answers undefined and never calls the hub", async (_label, arrange) => {
183
+ ])("with no AILK_SPOKE_KEY, no API address and %s answers undefined and never calls the hub", async (_label, arrange) => {
125
184
  hubConnect();
126
185
  delete process.env.AILK_SPOKE_KEY;
186
+ delete process.env.AILK_API_URL;
127
187
  arrange();
128
188
  await expect(readHubSite()).resolves.toBeUndefined();
129
189
  expect(mockGet).not.toHaveBeenCalled();
@@ -133,7 +193,7 @@ describe("readHubSite (#11456 D3, AC-3)", () => {
133
193
  it("with a spoke key and CONTENT_ADAPTER unset reads the hub (#11515)", async () => {
134
194
  hubConnect();
135
195
  mockGet.mockResolvedValue(ok({ identity: HUB_IDENTITY, seo: HUB_SEO }));
136
- await expect(readHubSite()).resolves.toEqual({ identity: HUB_IDENTITY, seo: HUB_SEO });
196
+ await expect(readHubSite()).resolves.toEqual({ identity: HUB_IDENTITY, seo: HUB_SEO, navigation: null });
137
197
  expect(mockGet).toHaveBeenCalledTimes(1);
138
198
  });
139
199
 
@@ -141,7 +201,7 @@ describe("readHubSite (#11456 D3, AC-3)", () => {
141
201
  hubConnect();
142
202
  mockGet.mockResolvedValue(ok({ identity: HUB_IDENTITY, seo: HUB_SEO }));
143
203
 
144
- await expect(readHubSite()).resolves.toEqual({ identity: HUB_IDENTITY, seo: HUB_SEO });
204
+ await expect(readHubSite()).resolves.toEqual({ identity: HUB_IDENTITY, seo: HUB_SEO, navigation: null });
145
205
 
146
206
  expect(mockUnstableCache).toHaveBeenCalledTimes(1);
147
207
  const [, keyParts, options] = mockUnstableCache.mock.calls[0]!;
@@ -168,7 +228,7 @@ describe("readHubSite (#11456 D3, AC-3)", () => {
168
228
  mockGet.mockResolvedValue(
169
229
  ok({ identity: { kind: "Organization", name: "" }, seo: { version: "1", robots: { index: "no" } } }),
170
230
  );
171
- await expect(readHubSite()).resolves.toEqual({ identity: null, seo: null });
231
+ await expect(readHubSite()).resolves.toEqual({ identity: null, seo: null, navigation: null });
172
232
  });
173
233
 
174
234
  it.each([
@@ -204,6 +264,155 @@ describe("readHubSite (#11456 D3, AC-3)", () => {
204
264
  });
205
265
  });
206
266
 
267
+ describe("readHubSite — a spoke's read is unchanged (#12390 AC-5)", () => {
268
+ it("a keyed 401 throws: nothing written, the last good value served, and the next request reads again", async () => {
269
+ hubConnect();
270
+ mockGet.mockResolvedValue(ok({ identity: HUB_IDENTITY, seo: HUB_SEO }));
271
+ await readHubSite();
272
+ mockCacheStore.clear(); // a purged tag: the next read misses
273
+ mockGet.mockReset().mockResolvedValue({ ok: false, status: 401, body: {} });
274
+
275
+ await expect(readHubSite()).resolves.toEqual({ identity: HUB_IDENTITY, seo: HUB_SEO, navigation: null });
276
+ expect(mockCacheStore.size).toBe(0);
277
+ expect(refusalWarns()).toHaveLength(0);
278
+
279
+ await readHubSite();
280
+ expect(mockGet).toHaveBeenCalledTimes(2);
281
+ });
282
+
283
+ it("a keyed read's key parts are today's: the key's digest", async () => {
284
+ hubConnect();
285
+ mockGet.mockResolvedValue(ok({ identity: HUB_IDENTITY, seo: HUB_SEO }));
286
+ await readHubSite();
287
+ const [, keyParts] = mockUnstableCache.mock.calls[0]!;
288
+ expect(keyParts).toEqual([
289
+ "ailk-hub-site-config",
290
+ "default",
291
+ createHash("sha256").update(KEY, "utf8").digest("hex").slice(0, 16),
292
+ "nav1",
293
+ ]);
294
+ });
295
+ });
296
+
297
+ describe("readHubSite — a keyless single-tenant site (#12390 D2, D3, AC-5)", () => {
298
+ it("a 200's rows are parsed and returned as for a spoke", async () => {
299
+ keylessConnect();
300
+ mockGet.mockResolvedValue(
301
+ ok({ identity: HUB_IDENTITY, seo: { version: "1", robots: { index: "no" } } }),
302
+ );
303
+ await expect(readHubSite()).resolves.toEqual({ identity: HUB_IDENTITY, seo: null, navigation: null });
304
+ expect(mockGet).toHaveBeenCalledTimes(1);
305
+ expect(heldHubSite()).toEqual({ identity: HUB_IDENTITY, seo: null, navigation: null });
306
+ });
307
+
308
+ it("a 401 resolves no rows from inside the cached callback, so a later request makes no second call; one warn", async () => {
309
+ keylessConnect();
310
+ mockGet.mockResolvedValue({ ok: false, status: 401, body: {} });
311
+
312
+ await expect(readHubSite()).resolves.toEqual({ identity: null, seo: null, navigation: null });
313
+ expect(mockCacheStore.size).toBe(1);
314
+ expect([...mockCacheStore.values()]).toEqual([{ identity: null, seo: null, navigation: null }]);
315
+
316
+ await expect(readHubSite()).resolves.toEqual({ identity: null, seo: null, navigation: null });
317
+ expect(mockGet).toHaveBeenCalledTimes(1);
318
+ expect(refusalWarns()).toHaveLength(1);
319
+ expect(String(refusalWarns()[0]![0])).toContain("_site.mdx");
320
+ });
321
+
322
+ it("the contact email falls back to the file after a refused read", async () => {
323
+ keylessConnect();
324
+ mockGet.mockResolvedValue({ ok: false, status: 401, body: {} });
325
+ await readHubSite();
326
+ expect(readSiteContactEmail()).toBe("file@file.example");
327
+ });
328
+
329
+ it.each([
330
+ ["a 500", 0, () => mockGet.mockResolvedValue({ ok: false, status: 500, body: {} })],
331
+ ["a throw", 0, () => mockGet.mockRejectedValue(new TypeError("fetch failed"))],
332
+ ["a timeout", HUB_SITE_TIMEOUT_MS, () => mockGet.mockReturnValue(new Promise(() => undefined))],
333
+ ])("%s writes nothing, answers undefined cold, and makes no further call for 15 s", async (_label, failsAfterMs, fail) => {
334
+ jest.useFakeTimers();
335
+ keylessConnect();
336
+ fail();
337
+
338
+ // The 15 s run from when the read failed.
339
+ const first = readHubSite();
340
+ await jest.advanceTimersByTimeAsync(failsAfterMs);
341
+ await expect(first).resolves.toBeUndefined();
342
+ expect(mockCacheStore.size).toBe(0);
343
+ expect(mockGet).toHaveBeenCalledTimes(1);
344
+ expect(refusalWarns()).toHaveLength(0);
345
+
346
+ await expect(readHubSite()).resolves.toBeUndefined();
347
+ jest.advanceTimersByTime(HUB_SITE_RETRY_SECONDS * 1_000 - 1);
348
+ await expect(readHubSite()).resolves.toBeUndefined();
349
+ expect(mockGet).toHaveBeenCalledTimes(1);
350
+
351
+ jest.advanceTimersByTime(1);
352
+ mockGet.mockReset().mockResolvedValue(ok({ identity: HUB_IDENTITY, seo: HUB_SEO }));
353
+ await expect(readHubSite()).resolves.toEqual({ identity: HUB_IDENTITY, seo: HUB_SEO, navigation: null });
354
+ expect(mockGet).toHaveBeenCalledTimes(1);
355
+ });
356
+
357
+ it.each([
358
+ ["a 500", () => mockGet.mockResolvedValue({ ok: false, status: 500, body: {} })],
359
+ ["a throw", () => mockGet.mockRejectedValue(new TypeError("fetch failed"))],
360
+ ])("%s after a good read answers the last good value, and holds it without a call", async (_label, fail) => {
361
+ keylessConnect();
362
+ mockGet.mockResolvedValue(ok({ identity: HUB_IDENTITY, seo: HUB_SEO }));
363
+ await readHubSite();
364
+ mockCacheStore.clear(); // a purged tag: the next read misses
365
+ mockGet.mockReset();
366
+ fail();
367
+
368
+ await expect(readHubSite()).resolves.toEqual({ identity: HUB_IDENTITY, seo: HUB_SEO, navigation: null });
369
+ await expect(readHubSite()).resolves.toEqual({ identity: HUB_IDENTITY, seo: HUB_SEO, navigation: null });
370
+ expect(mockGet).toHaveBeenCalledTimes(1);
371
+ expect(mockCacheStore.size).toBe(0);
372
+ });
373
+
374
+ it("is HUB_SITE_RETRY_SECONDS 15", () => {
375
+ expect(HUB_SITE_RETRY_SECONDS).toBe(15);
376
+ });
377
+
378
+ it("keys its entry `keyless:` plus a digest of the API address, one entry per address", async () => {
379
+ keylessConnect("https://api.one.example");
380
+ mockGet.mockResolvedValue(ok({ identity: HUB_IDENTITY, seo: null }));
381
+ await readHubSite();
382
+ keylessConnect("https://api.two.example");
383
+ await readHubSite();
384
+ const [a, b] = mockUnstableCache.mock.calls.map((call) => call[1]);
385
+ expect(a![2]).toBe(
386
+ `keyless:${createHash("sha256").update("https://api.one.example", "utf8").digest("hex").slice(0, 16)}`,
387
+ );
388
+ expect(b![2]).toMatch(/^keyless:[0-9a-f]{16}$/);
389
+ expect(a![2]).not.toBe(b![2]);
390
+ expect(mockGet).toHaveBeenCalledTimes(2);
391
+ });
392
+
393
+ it("a key that would be dropped on the wire reads under the keyless part, never the key's digest", async () => {
394
+ keylessConnect();
395
+ process.env.AILK_SPOKE_KEY = "has a space";
396
+ mockGet.mockResolvedValue({ ok: false, status: 401, body: {} });
397
+ await expect(readHubSite()).resolves.toEqual({ identity: null, seo: null, navigation: null });
398
+ const [, keyParts] = mockUnstableCache.mock.calls[0]!;
399
+ expect(keyParts[2]).toMatch(/^keyless:/);
400
+ expect(keyParts[2]).not.toContain(
401
+ createHash("sha256").update("has a space", "utf8").digest("hex").slice(0, 16),
402
+ );
403
+ });
404
+
405
+ it("a keyless entry is never read by a keyed call at the same address", async () => {
406
+ keylessConnect("https://hub.example");
407
+ mockGet.mockResolvedValue({ ok: false, status: 401, body: {} });
408
+ await readHubSite();
409
+ hubConnect();
410
+ mockGet.mockReset().mockResolvedValue(ok({ identity: HUB_IDENTITY, seo: HUB_SEO }));
411
+ await expect(readHubSite()).resolves.toEqual({ identity: HUB_IDENTITY, seo: HUB_SEO, navigation: null });
412
+ expect(mockGet).toHaveBeenCalledTimes(1);
413
+ });
414
+ });
415
+
207
416
  describe("the contact email from the held hub value (#11456 D4, AC-2)", () => {
208
417
  it("answers the file value on a cold instance, then the hub email after one resolved read", async () => {
209
418
  hubConnect();
@@ -240,7 +449,182 @@ describe("the contact email from the held hub value (#11456 D4, AC-2)", () => {
240
449
  mockGet.mockResolvedValue(ok({ identity: HUB_IDENTITY, seo: null }));
241
450
  await readHubSite();
242
451
  delete process.env.AILK_SPOKE_KEY;
452
+ delete process.env.AILK_API_URL;
243
453
  expect(heldHubSite()).toBeUndefined();
244
454
  expect(readSiteContactEmail()).toBe("file@file.example");
245
455
  });
246
456
  });
457
+
458
+ // #12401 D1, AC-1 — the workspace's navigation record rides the same read.
459
+ describe("readHubSite — the navigation record (#12401 D1, AC-1)", () => {
460
+ const RECORD = {
461
+ version: "1",
462
+ header: {
463
+ items: [
464
+ { kind: "link", label: { text: "Plans" }, target: { kind: "path", path: "/plans" } },
465
+ {
466
+ kind: "menu",
467
+ label: { text: "Learn" },
468
+ groups: [{ links: [{ kind: "link", label: { text: "Guide" }, target: { kind: "page", address: { type: "AboutPage", slug: "about" } } }] }],
469
+ },
470
+ ],
471
+ button: { kind: "link", label: { text: "Book" }, target: { kind: "path", path: "/contact" } },
472
+ },
473
+ footer: {
474
+ columns: [{ heading: { text: "Company" }, links: [{ kind: "link", label: { text: "About" }, target: { kind: "path", path: "/about" } }] }],
475
+ bottomBar: { links: [{ kind: "link", label: { text: "Privacy" }, target: { kind: "path", path: "/privacy" } }] },
476
+ closing: {
477
+ location: "Paris",
478
+ buttons: [{ kind: "link", label: { text: "Call" }, target: { kind: "page", address: { type: "AboutPage", slug: "about" } } }],
479
+ },
480
+ },
481
+ };
482
+ const NAV = { version: 3, record: RECORD };
483
+ const withTarget = (target: unknown) => ({
484
+ ...RECORD,
485
+ header: { ...RECORD.header, items: [{ kind: "link", label: { text: "Secret label" }, target }] },
486
+ });
487
+ const navWarns = () =>
488
+ jest.mocked(console.warn).mock.calls.filter((call) => String(call[0]).includes("navigation unreadable"));
489
+
490
+ it.each([
491
+ ["a spoke key", hubConnect],
492
+ ["a safe API address and no key", () => keylessConnect()],
493
+ ])("with %s, a valid navigation resolves its version and record beside identity and seo", async (_label, connect) => {
494
+ connect();
495
+ mockGet.mockResolvedValue(ok({ identity: HUB_IDENTITY, seo: HUB_SEO, navigation: NAV }));
496
+ await expect(readHubSite()).resolves.toEqual({ identity: HUB_IDENTITY, seo: HUB_SEO, navigation: NAV });
497
+ expect(navWarns()).toHaveLength(0);
498
+ });
499
+
500
+ it.each([
501
+ ["no navigation key (a hub older than #12400)", { identity: HUB_IDENTITY, seo: HUB_SEO }],
502
+ ["navigation: null", { identity: HUB_IDENTITY, seo: HUB_SEO, navigation: null }],
503
+ ])("%s resolves navigation null with identity and seo as given, and no warning", async (_label, body) => {
504
+ hubConnect();
505
+ mockGet.mockResolvedValue(ok(body));
506
+ await expect(readHubSite()).resolves.toEqual({ identity: HUB_IDENTITY, seo: HUB_SEO, navigation: null });
507
+ expect(navWarns()).toHaveLength(0);
508
+ });
509
+
510
+ it.each([
511
+ ["a record the schema refuses", { version: 7, record: withTarget({ kind: "url", href: "javascript:alert(1)" }) }, "header.items.0.target.href"],
512
+ ["a version of 0", { version: 0, record: RECORD }, "version"],
513
+ ["a version that is not an integer", { version: 1.5, record: RECORD }, "version"],
514
+ ])("%s resolves navigation null with identity and seo kept, and one warning with no value in it", async (_label, navigation, field) => {
515
+ hubConnect();
516
+ mockGet.mockResolvedValue(ok({ identity: HUB_IDENTITY, seo: HUB_SEO, navigation }));
517
+ await expect(readHubSite()).resolves.toEqual({ identity: HUB_IDENTITY, seo: HUB_SEO, navigation: null });
518
+ expect(navWarns()).toHaveLength(1);
519
+ const line = String(navWarns()[0]![0]);
520
+ expect(line).toBe(`[hub-site-config] navigation unreadable at ${field}; rendering the site's own navigation`);
521
+ expect(line).not.toContain("Secret label");
522
+ expect(line).not.toContain("javascript");
523
+ expect(line).not.toContain(String(navigation.version));
524
+ });
525
+
526
+ it.each([
527
+ ["a url target javascript:alert(1)", { kind: "url", href: "javascript:alert(1)" }, "header.items.0.target.href"],
528
+ ["a url target JaVaScRiPt:alert(1)", { kind: "url", href: "JaVaScRiPt:alert(1)" }, "header.items.0.target.href"],
529
+ ["a url target data:text/html,x", { kind: "url", href: "data:text/html,x" }, "header.items.0.target.href"],
530
+ ["a path target //evil.example", { kind: "path", path: "//evil.example" }, "header.items.0.target.path"],
531
+ ["a path target /\\evil.example", { kind: "path", path: "/\\evil.example" }, "header.items.0.target.path"],
532
+ ])("%s is refused: navigation null, identity kept, a warning naming the field's path", async (_label, target, field) => {
533
+ hubConnect();
534
+ mockGet.mockResolvedValue(ok({ identity: HUB_IDENTITY, seo: null, navigation: { version: 2, record: withTarget(target) } }));
535
+ await expect(readHubSite()).resolves.toEqual({ identity: HUB_IDENTITY, seo: null, navigation: null });
536
+ expect(navWarns().map((call) => String(call[0]))).toEqual([
537
+ `[hub-site-config] navigation unreadable at ${field}; rendering the site's own navigation`,
538
+ ]);
539
+ });
540
+
541
+ it("a byLocale key the schema refuses is masked in the warning's path", async () => {
542
+ hubConnect();
543
+ const record = {
544
+ ...RECORD,
545
+ header: { items: [{ kind: "link", label: { text: "Plans", byLocale: { secretvalue: "x" } }, target: { kind: "path", path: "/plans" } }] },
546
+ };
547
+ mockGet.mockResolvedValue(ok({ identity: HUB_IDENTITY, seo: null, navigation: { version: 2, record } }));
548
+ await expect(readHubSite()).resolves.toMatchObject({ identity: HUB_IDENTITY, navigation: null });
549
+ const lines = navWarns().map((call) => String(call[0]));
550
+ expect(lines).toHaveLength(1);
551
+ expect(lines[0]).toContain("header.items.0.label.byLocale.*");
552
+ expect(lines[0]).not.toContain("secretvalue");
553
+ });
554
+
555
+ it("warns once per distinct refused path, and a different later refusal still warns", async () => {
556
+ hubConnect();
557
+ const bad = (target: unknown) =>
558
+ mockGet.mockResolvedValue(ok({ identity: HUB_IDENTITY, seo: null, navigation: { version: 2, record: withTarget(target) } }));
559
+ bad({ kind: "url", href: "javascript:alert(1)" });
560
+ await readHubSite();
561
+ mockCacheStore.clear();
562
+ bad({ kind: "url", href: "data:text/html,x" });
563
+ await readHubSite();
564
+ expect(navWarns()).toHaveLength(1);
565
+ mockCacheStore.clear();
566
+ bad({ kind: "path", path: "//evil.example" });
567
+ await readHubSite();
568
+ expect(navWarns()).toHaveLength(2);
569
+ });
570
+
571
+ it("the hub's answer for a record whose closing footer's only button pointed at a draft page — the strip removed the closing footer — resolves with no closing", async () => {
572
+ hubConnect();
573
+ // #12400 D5's `liveOnly`: a closing footer left with no button is removed.
574
+ const { closing: _stripped, ...footer } = RECORD.footer;
575
+ const stripped = { ...RECORD, footer };
576
+ mockGet.mockResolvedValue(ok({ identity: HUB_IDENTITY, seo: null, navigation: { version: 4, record: stripped } }));
577
+ const site = await readHubSite();
578
+ expect(site?.navigation).toEqual({ version: 4, record: stripped });
579
+ expect(site?.navigation?.record.footer).not.toHaveProperty("closing");
580
+ });
581
+
582
+ it("after a good read, a rejected siteConfig.get resolves the earlier record", async () => {
583
+ hubConnect();
584
+ mockGet.mockResolvedValue(ok({ identity: HUB_IDENTITY, seo: null, navigation: NAV }));
585
+ await readHubSite();
586
+ mockCacheStore.clear(); // a purged tag: the next read misses
587
+ mockGet.mockReset().mockRejectedValue(new TypeError("fetch failed"));
588
+ await expect(readHubSite()).resolves.toEqual({ identity: HUB_IDENTITY, seo: null, navigation: NAV });
589
+ });
590
+
591
+ it("after the purge, a new body's record is what resolves", async () => {
592
+ hubConnect();
593
+ mockGet.mockResolvedValue(ok({ identity: HUB_IDENTITY, seo: null, navigation: NAV }));
594
+ await readHubSite();
595
+ const renamed = {
596
+ version: 4,
597
+ record: { ...RECORD, header: { items: [{ kind: "link", label: { text: "Prices" }, target: { kind: "path", path: "/plans" } }] } },
598
+ };
599
+ mockGet.mockReset().mockResolvedValue(ok({ identity: HUB_IDENTITY, seo: null, navigation: renamed }));
600
+ // The entry stands until the tag is purged.
601
+ await expect(readHubSite()).resolves.toMatchObject({ navigation: NAV });
602
+ mockCacheStore.clear();
603
+ await expect(readHubSite()).resolves.toMatchObject({ navigation: renamed });
604
+ });
605
+
606
+ it("with neither a key nor an address, siteConfig.get is never called", async () => {
607
+ mockGet.mockResolvedValue(ok({ identity: HUB_IDENTITY, seo: null, navigation: NAV }));
608
+ await expect(readHubSite()).resolves.toBeUndefined();
609
+ expect(mockGet).not.toHaveBeenCalled();
610
+ });
611
+
612
+ it("a keyless 401 resolves navigation null beside the other rows", async () => {
613
+ keylessConnect();
614
+ mockGet.mockResolvedValue({ ok: false, status: 401, body: {} });
615
+ await expect(readHubSite()).resolves.toEqual({ identity: null, seo: null, navigation: null });
616
+ });
617
+
618
+ it("an entry of the old shape, under the key without the nav1 part, is never served", async () => {
619
+ hubConnect();
620
+ const oldKey = [
621
+ "ailk-hub-site-config",
622
+ "default",
623
+ createHash("sha256").update(KEY, "utf8").digest("hex").slice(0, 16),
624
+ ].join("|");
625
+ mockCacheStore.set(oldKey, { identity: HUB_IDENTITY, seo: null });
626
+ mockGet.mockResolvedValue(ok({ identity: HUB_IDENTITY, seo: null, navigation: NAV }));
627
+ await expect(readHubSite()).resolves.toEqual({ identity: HUB_IDENTITY, seo: null, navigation: NAV });
628
+ expect(mockGet).toHaveBeenCalledTimes(1);
629
+ });
630
+ });