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,288 @@
1
+ /**
2
+ * @jest-environment node
3
+ */
4
+
5
+ /**
6
+ * The navigation record's translator (#12401, spec
7
+ * docs/specs/12401-a-site-renders-its-header-spec.md D2, D3; AC-2).
8
+ *
9
+ * The translator reads the docs base and the services base at module load,
10
+ * so each setting loads it afresh (`jest.isolateModules`). The fixture is
11
+ * parsed by the real `siteNavigationSchema`, so every case is a record the
12
+ * hub could send.
13
+ */
14
+ import { siteNavigationSchema, type SiteNavigation } from "@working-theory/validation";
15
+
16
+ import type * as SiteNavigationModule from "../site-navigation";
17
+
18
+ const ENV = ["NEXT_PUBLIC_AILK_SERVICES_PATH", "NEXT_PUBLIC_AILK_DOCS_PATH", "NEXT_PUBLIC_AILK_ROUTE_PATHS"] as const;
19
+ const saved = new Map(ENV.map((k) => [k, process.env[k]]));
20
+
21
+ afterEach(() => {
22
+ jest.restoreAllMocks();
23
+ for (const [k, v] of saved) {
24
+ if (v === undefined) delete process.env[k];
25
+ else process.env[k] = v;
26
+ }
27
+ });
28
+
29
+ /** The translator, loaded with `env` set and, optionally, `pagePath` answering `pagePathAnswer`. */
30
+ function load(env: Partial<Record<(typeof ENV)[number], string>> = {}, pagePathAnswer?: string): typeof SiteNavigationModule {
31
+ for (const k of ENV) delete process.env[k];
32
+ Object.assign(process.env, env);
33
+ let mod: typeof SiteNavigationModule | undefined;
34
+ jest.isolateModules(() => {
35
+ if (pagePathAnswer === undefined) jest.dontMock("@working-theory/templates/routes");
36
+ else {
37
+ jest.doMock("@working-theory/templates/routes", () => ({
38
+ ...jest.requireActual("@working-theory/templates/routes"),
39
+ pagePath: () => pagePathAnswer,
40
+ }));
41
+ }
42
+ mod = jest.requireActual("../site-navigation");
43
+ });
44
+ return mod!;
45
+ }
46
+
47
+ const label = (text: string, fr?: string) => (fr === undefined ? { text } : { text, byLocale: { fr } });
48
+ const path = (p: string) => ({ kind: "path" as const, path: p });
49
+ const link = (text: string, target: object, extra: object = {}) => ({
50
+ kind: "link" as const,
51
+ label: label(text),
52
+ target,
53
+ ...extra,
54
+ });
55
+
56
+ /** One record carrying every shape the translator handles. */
57
+ const RECORD: SiteNavigation = siteNavigationSchema.parse({
58
+ version: "1",
59
+ header: {
60
+ items: [
61
+ { kind: "link", label: label("Pricing", "Tarifs"), target: path("/pricing-plans") },
62
+ {
63
+ kind: "menu",
64
+ label: label("Services"),
65
+ groups: [
66
+ {
67
+ links: [
68
+ link("Audit", { kind: "page", address: { type: "Service", slug: "audit" } }, {
69
+ description: label("Find the gaps", "Trouver les écarts"),
70
+ }),
71
+ link("Start here", path("/docs/start")),
72
+ ],
73
+ },
74
+ ],
75
+ },
76
+ {
77
+ kind: "menu",
78
+ label: label("Resources"),
79
+ groups: [
80
+ { heading: label("Read", "Lire"), links: [link("Same", { kind: "url", href: "https://example.com/a" })] },
81
+ { links: [link("Same", path("/services/x"))] },
82
+ ],
83
+ },
84
+ ],
85
+ button: link("Book a call", path("/contact")),
86
+ signIn: "hidden",
87
+ },
88
+ footer: {
89
+ columns: [
90
+ { heading: label("Company", "Société"), links: [link("About", path("/about"))] },
91
+ { heading: label("Help"), links: [link("FAQ", path("/faq")), link("Status", { kind: "url", href: "https://status.example" })] },
92
+ ],
93
+ bottomBar: { links: [link("Privacy", path("/privacy")), link("Terms", path("/terms"))] },
94
+ closing: { location: "Paris", buttons: [link("Call us", path("/meet"))] },
95
+ },
96
+ });
97
+
98
+ describe("navigationChrome (#12401 D2, AC-2)", () => {
99
+ it("translates the whole record for locale fr, in the record's order", () => {
100
+ const { navigationChrome } = load();
101
+ expect(navigationChrome(RECORD, "fr")).toEqual({
102
+ navItems: [
103
+ { labelKey: "rec.h0", path: "/pricing-plans" },
104
+ {
105
+ labelKey: "rec.h1",
106
+ flyout: {
107
+ features: [
108
+ { labelKey: "rec.h1.g0.l0", path: "/services/audit", descKey: "rec.h1.g0.l0.d" },
109
+ { labelKey: "rec.h1.g0.l1", path: "/docs/start" },
110
+ ],
111
+ presentation: "grid",
112
+ width: "panel",
113
+ },
114
+ },
115
+ {
116
+ labelKey: "rec.h2",
117
+ flyout: {
118
+ features: [
119
+ { labelKey: "rec.h2.g0.l0", path: "https://example.com/a" },
120
+ { labelKey: "rec.h2.g1.l0", path: "/services/x" },
121
+ ],
122
+ groups: [
123
+ { headingKey: "rec.h2.g0", items: [{ labelKey: "rec.h2.g0.l0", path: "https://example.com/a" }] },
124
+ { headingKey: "rec.h2.g1", items: [{ labelKey: "rec.h2.g1.l0", path: "/services/x" }] },
125
+ ],
126
+ width: "panel",
127
+ },
128
+ },
129
+ ],
130
+ columns: [
131
+ {
132
+ heading: "Société",
133
+ headingKey: "rec.f0",
134
+ items: [{ labelKey: "rec.f0.l0", path: "/about", label: "About" }],
135
+ },
136
+ {
137
+ heading: "Help",
138
+ headingKey: "rec.f1",
139
+ items: [
140
+ { labelKey: "rec.f1.l0", path: "/faq", label: "FAQ" },
141
+ { labelKey: "rec.f1.l1", path: "https://status.example", label: "Status" },
142
+ ],
143
+ },
144
+ ],
145
+ labels: {
146
+ "rec.h0": "Tarifs",
147
+ "rec.h1": "Services",
148
+ "rec.h1.g0.l0": "Audit",
149
+ "rec.h1.g0.l0.d": "Trouver les écarts",
150
+ "rec.h1.g0.l1": "Start here",
151
+ "rec.h2": "Resources",
152
+ "rec.h2.g0": "Lire",
153
+ "rec.h2.g0.l0": "Same",
154
+ "rec.h2.g1": "",
155
+ "rec.h2.g1.l0": "Same",
156
+ "rec.f0": "Société",
157
+ "rec.f0.l0": "About",
158
+ "rec.f1": "Help",
159
+ "rec.f1.l0": "FAQ",
160
+ "rec.f1.l1": "Status",
161
+ },
162
+ button: { label: "Book a call", href: "/contact" },
163
+ signIn: "hidden",
164
+ legalLinks: [
165
+ { label: "Privacy", href: "/privacy" },
166
+ { label: "Terms", href: "/terms" },
167
+ ],
168
+ closing: { locality: "Paris", actions: [{ label: "Call us", href: "/meet" }] },
169
+ });
170
+ });
171
+
172
+ it("answers the default text for a locale the record has none for, and matches a locale case-insensitively", () => {
173
+ const { navigationChrome } = load();
174
+ expect(navigationChrome(RECORD, "de").labels["rec.h0"]).toBe("Pricing");
175
+ const record = siteNavigationSchema.parse({
176
+ ...RECORD,
177
+ header: { items: [{ kind: "link", label: { text: "Plans", byLocale: { "pt-BR": "Planos" } }, target: path("/p") }] },
178
+ });
179
+ expect(navigationChrome(record, "pt-br").labels["rec.h0"]).toBe("Planos");
180
+ });
181
+
182
+ it("an ungrouped menu with no description is a list at the trigger's width", () => {
183
+ const { navigationChrome } = load();
184
+ const record = siteNavigationSchema.parse({
185
+ version: "1",
186
+ header: { items: [{ kind: "menu", label: label("More"), groups: [{ links: [link("A", path("/a"))] }] }] },
187
+ footer: { columns: [] },
188
+ });
189
+ expect(navigationChrome(record, "en").navItems[0]!.flyout).toEqual({
190
+ features: [{ labelKey: "rec.h0.g0.l0", path: "/a" }],
191
+ presentation: "list",
192
+ width: "trigger",
193
+ });
194
+ });
195
+
196
+ it("every key contains a dot, and two links with the same label get two keys", () => {
197
+ const { navigationChrome } = load();
198
+ const { labels, navItems } = navigationChrome(RECORD, "en");
199
+ expect(Object.keys(labels).every((key) => key.includes("."))).toBe(true);
200
+ const same = navItems[2]!.flyout!.features.map((feature) => feature.labelKey);
201
+ expect(same).toEqual(["rec.h2.g0.l0", "rec.h2.g1.l0"]);
202
+ expect(labels[same[0]!]).toBe("Same");
203
+ expect(labels[same[1]!]).toBe("Same");
204
+ });
205
+
206
+ it("a record with no button, no Sign in, no bottom bar and no closing footer gives none of them", () => {
207
+ const { navigationChrome } = load();
208
+ const record = siteNavigationSchema.parse({ version: "1", header: { items: [] }, footer: { columns: [] } });
209
+ expect(navigationChrome(record, "en")).toEqual({
210
+ navItems: [],
211
+ columns: [],
212
+ labels: {},
213
+ legalLinks: [],
214
+ });
215
+ });
216
+ });
217
+
218
+ describe("navigationChrome — a target's address (#12401 D2, AC-2)", () => {
219
+ const recordOf = (target: object) =>
220
+ siteNavigationSchema.parse({
221
+ version: "1",
222
+ header: { items: [{ kind: "link", label: label("X"), target }] },
223
+ footer: { columns: [] },
224
+ });
225
+ const addressOf = (mod: typeof SiteNavigationModule, target: object) =>
226
+ mod.navigationChrome(recordOf(target), "en").navItems[0]!.path;
227
+
228
+ it("a Service page goes to the services base", () => {
229
+ const mod = load({ NEXT_PUBLIC_AILK_SERVICES_PATH: "/capabilities" });
230
+ expect(addressOf(mod, { kind: "page", address: { type: "Service", slug: "audit" } })).toBe("/capabilities/audit");
231
+ });
232
+
233
+ it("a page at /docs goes to the docs path", () => {
234
+ const mod = load({ NEXT_PUBLIC_AILK_DOCS_PATH: "/help" }, "/docs/getting-started");
235
+ expect(addressOf(mod, { kind: "page", address: { type: "AboutPage", slug: "about" } })).toBe("/help/getting-started");
236
+ });
237
+
238
+ it("a page of a renamed family goes to its served segment", () => {
239
+ const mod = load({ NEXT_PUBLIC_AILK_ROUTE_PATHS: '{"portfolio":"writing-samples"}' });
240
+ expect(addressOf(mod, { kind: "page", address: { type: "PortfolioItem", slug: "one" } })).toBe("/writing-samples/one");
241
+ });
242
+
243
+ it.each(["/docs/start", "/services/x", "/portfolio/one"])("a path target %s comes back as stored, under every setting", (stored) => {
244
+ const mod = load({
245
+ NEXT_PUBLIC_AILK_SERVICES_PATH: "/capabilities",
246
+ NEXT_PUBLIC_AILK_DOCS_PATH: "/help",
247
+ NEXT_PUBLIC_AILK_ROUTE_PATHS: '{"portfolio":"writing-samples"}',
248
+ });
249
+ expect(addressOf(mod, path(stored))).toBe(stored);
250
+ });
251
+
252
+ it("a url target comes back as stored, with no locale prefix added to any address", () => {
253
+ const mod = load();
254
+ expect(addressOf(mod, { kind: "url", href: "https://example.com/x?y=1" })).toBe("https://example.com/x?y=1");
255
+ expect(mod.navigationChrome(RECORD, "fr").navItems[0]!.path).toBe("/pricing-plans");
256
+ });
257
+ });
258
+
259
+ describe("every emitted address is checked (#12401 D2, D3, AC-2)", () => {
260
+ const pageRecord = siteNavigationSchema.parse({
261
+ version: "1",
262
+ header: { items: [] },
263
+ footer: {
264
+ columns: [],
265
+ bottomBar: { links: [link("Legal", { kind: "page", address: { type: "AboutPage", slug: "about" } })] },
266
+ },
267
+ });
268
+
269
+ it.each(["//x", "/\\x", "/a b", "/a\\b", "https://user:pw@host.example", "javascript:alert(1)", "relative"])(
270
+ "with pagePath answering %s, navigationChrome throws and safeNavigationChrome answers null with one warning",
271
+ (answer) => {
272
+ const warn = jest.spyOn(console, "warn").mockImplementation(() => undefined);
273
+ const mod = load({}, answer);
274
+ expect(() => mod.navigationChrome(pageRecord, "en")).toThrow();
275
+ expect(mod.safeNavigationChrome(pageRecord, "en")).toBeNull();
276
+ expect(warn).toHaveBeenCalledTimes(1);
277
+ expect(String(warn.mock.calls[0]![0])).toBe("[site-navigation] record not rendered (TypeError)");
278
+ expect(String(warn.mock.calls[0]![0])).not.toContain(answer);
279
+ },
280
+ );
281
+
282
+ it("safeNavigationChrome answers the translation for a record it can render, with no warning", () => {
283
+ const warn = jest.spyOn(console, "warn").mockImplementation(() => undefined);
284
+ const mod = load();
285
+ expect(mod.safeNavigationChrome(RECORD, "en")).toEqual(mod.navigationChrome(RECORD, "en"));
286
+ expect(warn).not.toHaveBeenCalled();
287
+ });
288
+ });
@@ -0,0 +1,115 @@
1
+ /**
2
+ * grid-read.ts — the one read of an Offerings grid for a page (#12370, spec
3
+ * docs/specs/12370-a-comparison-table-reads-an-offerings-grid-spec.md D3),
4
+ * shared by its two callers: a `<Grid … />` block line (`in-page-blocks.ts`)
5
+ * and a `comparison-table` section's `grid` setting (`pricing-page.ts`). It
6
+ * imports neither, so the two can both import it.
7
+ *
8
+ * The public read, `GET /v1/content/grids/:kind/:slug`, goes through
9
+ * api-client `content.grid`, shared per instance for 15 s (`sharedHubRead`),
10
+ * is parsed by `GridAnswerSchema` and worded by
11
+ * `gridSection` with the page locale's `grids` messages — no English default
12
+ * reaches a page (#12222 D18). A read that fails, an answer that does not
13
+ * parse and an empty grid all answer `null`: the caller draws no table. The
14
+ * grid's data is never written anywhere: every render reads the published
15
+ * facts and Stripe's features afresh.
16
+ *
17
+ * The read itself carries no deadline. A block line is bounded by
18
+ * page-renderer's own 3,000 ms backstop; a section is bounded here, by
19
+ * `withinGridDeadline`, at the same 3,000 ms.
20
+ */
21
+ import { content } from "@working-theory/api-client";
22
+ import {
23
+ GridAnswerSchema,
24
+ gridSection,
25
+ writeBlockLine,
26
+ type ComparisonGrid,
27
+ type GridAnswer,
28
+ type GridBlock,
29
+ type GridLabels,
30
+ type PageSection,
31
+ } from "@working-theory/validation";
32
+ import { getTranslations } from "next-intl/server";
33
+
34
+ import { sharedHubRead } from "./hub-read";
35
+
36
+ /** A grid as either caller names it: a block's fields, or a section's (the same, less `price` and `button`). */
37
+ export type GridSetting = GridBlock | ComparisonGrid;
38
+
39
+ /** A `comparison-table` section's data. */
40
+ export type GridTableData = Extract<PageSection, { type: "comparison-table" }>["data"];
41
+
42
+ /** How long a section waits on its grid before drawing no table: a block line's backstop (page-renderer). */
43
+ export const GRID_READ_DEADLINE_MS = 3_000;
44
+
45
+ /** `read`'s answer, or `null` once GRID_READ_DEADLINE_MS has passed or `read` rejects. */
46
+ export function withinGridDeadline<T>(read: Promise<T | null>): Promise<T | null> {
47
+ let timer: ReturnType<typeof setTimeout> | undefined;
48
+ const late = new Promise<null>((resolve) => {
49
+ timer = setTimeout(() => resolve(null), GRID_READ_DEADLINE_MS);
50
+ });
51
+ return Promise.race([read.catch(() => null), late]).finally(() => clearTimeout(timer));
52
+ }
53
+
54
+ /** The grid read, or `null` for any failed read or an answer that does not parse. */
55
+ export function readGrid(grid: GridSetting, locale: string): Promise<GridAnswer | null> {
56
+ const key = `grid:${locale}:${writeBlockLine({ tag: "Grid", block: grid as GridBlock })}`;
57
+ return sharedHubRead(key, async () => {
58
+ try {
59
+ const res = await content.grid({
60
+ kind: grid.kind,
61
+ slug: grid.kind === "capabilities" ? grid.products.join(" ") : grid.category,
62
+ show: grid.show,
63
+ ...(grid.kind === "capabilities" && grid.levels !== undefined ? { levels: grid.levels.join(" ") } : {}),
64
+ ...(grid.kind === "alternatives" && grid.ours !== undefined ? { ours: grid.ours } : {}),
65
+ ...(grid.kind === "alternatives" && grid.columns !== undefined ? { columns: grid.columns.join(" ") } : {}),
66
+ locale,
67
+ });
68
+ if (!res.ok) return null;
69
+ const parsed = GridAnswerSchema.safeParse(res.body);
70
+ return parsed.success ? parsed.data : null;
71
+ } catch {
72
+ return null;
73
+ }
74
+ });
75
+ }
76
+
77
+ /** The page locale's `grids` words (#12222 D18): every fixed word a grid shows. */
78
+ export async function gridLabels(locale: string): Promise<GridLabels> {
79
+ const t = await getTranslations({ locale, namespace: "grids" });
80
+ const dates = new Intl.DateTimeFormat(locale, { dateStyle: "medium", timeZone: "UTC" });
81
+ return {
82
+ fromLevel: (level) => t("fromLevel", { level }),
83
+ addon: t("addon"),
84
+ ourAssessment: t("ourAssessment"),
85
+ checked: (date) => t("checked", { date }),
86
+ factsAsOf: (date) => t("factsAsOf", { date }),
87
+ countOf: (yes, of) => t("countOf", { yes, of }),
88
+ capabilities: t("capabilities"),
89
+ labelHeader: t("labelHeader"),
90
+ planPicker: t("planPicker"),
91
+ formatDate: (isoDate) => dates.format(new Date(`${isoDate}T00:00:00Z`)),
92
+ };
93
+ }
94
+
95
+ /**
96
+ * A grid as `comparison-table` data, with the plan slugs of its own columns,
97
+ * or `null` for a failed read or a grid with no row. A block's `price`,
98
+ * `button` and `source` shape the data; a section's grid carries no `price`
99
+ * or `button` (its `planBar` is the one header control).
100
+ */
101
+ export async function gridTable(
102
+ grid: GridSetting,
103
+ locale: string,
104
+ ): Promise<{ data: GridTableData; plans: string[] } | null> {
105
+ const answer = await readGrid(grid, locale);
106
+ if (answer === null) return null;
107
+ const options =
108
+ grid.kind === "capabilities"
109
+ ? { price: "price" in grid ? grid.price : undefined, button: "button" in grid ? grid.button : undefined }
110
+ : { source: grid.source };
111
+ const data = gridSection(answer, await gridLabels(locale), options);
112
+ if (data === null) return null;
113
+ const plans = answer.columns.flatMap((column) => (column.ours && column.plan !== undefined ? [column.plan] : []));
114
+ return { data: data as GridTableData, plans };
115
+ }
@@ -0,0 +1,35 @@
1
+ /**
2
+ * hub-read.ts — a hub read shared across requests (#8731).
3
+ *
4
+ * A visitor view carries no forwarding headers, so on a spoke every view
5
+ * would otherwise count against the spoke server's one per-IP limit at the
6
+ * hub. An Admin edit shows within HUB_READ_TTL_MS.
7
+ *
8
+ * A leaf (#12385): it imports no other apps/web module, so `grid-read.ts` and
9
+ * `in-page-magnet.ts` can both import it without closing a cycle through
10
+ * `site-pages.config.tsx`. `in-page-magnet.ts` re-exports both names.
11
+ */
12
+
13
+ /**
14
+ * How long a hub read is shared across requests (#8731). Short, so an Admin
15
+ * edit to the magnet shows within seconds; long enough that a burst of views
16
+ * makes one read of each, not one per view.
17
+ */
18
+ export const HUB_READ_TTL_MS = 15_000;
19
+
20
+ const hubReads = new Map<string, { at: number; read: Promise<unknown> }>();
21
+
22
+ /**
23
+ * `read()`'s result, shared per instance for HUB_READ_TTL_MS. Concurrent calls
24
+ * share one in-flight read. A read that settles to `null` (any failed read,
25
+ * see the readers in `in-page-magnet.ts`) is shared the same way, so a burst
26
+ * against an unreachable hub is not a burst of retries.
27
+ */
28
+ export function sharedHubRead<T>(key: string, read: () => Promise<T>): Promise<T> {
29
+ const now = Date.now();
30
+ const hit = hubReads.get(key);
31
+ if (hit && now - hit.at < HUB_READ_TTL_MS) return hit.read as Promise<T>;
32
+ const fresh = read();
33
+ hubReads.set(key, { at: now, read: fresh });
34
+ return fresh;
35
+ }