import { readFile } from "node:fs/promises"; import { describe, expect, it } from "vitest"; import { FEATURE_SHAPE_BASELINE_PATH, FEATURE_SHAPE_CEILINGS_PATH, FEATURES_PER_CONTEXT, initialJavascriptRecordDrift, measureInitialJavascriptCosts, planShapeFeatures, SHAPE_FEATURE_COMMAND, shapeFeature, type InitialJavascriptCosts, } from "../../scripts/scale/measure-feature-shape-growth.ts"; import { APP_MAX_CONTEXTS } from "../../src/apps/template-app/app-module.ts"; /** * 11-04 (D-18). The scale evidence used to come from stub features that only * differ by name, so the cost of one app growing towards its declared ceiling * (20 contexts x 200 features) was never measured. The committed baseline is a * measurement with copies of a real feature: since 2026-11-07 the one * `create:feature` writes, before that the template's reference sample. */ type Run = Readonly<{ addedFeatures: number; routes: number; tsc: Readonly<{ passed: boolean; instantiations: number; wallMs: number }>; build: Readonly<{ wallMs: number; initialJavascriptGzipBytes: number; lazyJavascriptFiles: number; }>; loadBefore: readonly number[]; }>; describe("feature-shape growth measurement (11-04)", () => { it("plans each copy with create:feature, 200 to a context", () => { const copies = FEATURES_PER_CONTEXT + 1; const files = planShapeFeatures(copies); const first = shapeFeature(0); const last = shapeFeature(copies - 1); expect([...files.keys()].filter((file) => file.endsWith("/context-module.ts"))).toEqual([ `src/contexts/${first.context.id}/context-module.ts`, `src/contexts/${last.context.id}/context-module.ts`, ]); expect(last.context.id).not.toBe(first.context.id); for (const index of [0, FEATURES_PER_CONTEXT - 1, FEATURES_PER_CONTEXT]) { const { id, context } = shapeFeature(index); const module = files.get(`src/features/${id}/feature-module.ts`); expect(module, id).toContain(`FEATURE_CONTEXT_ID = "${context.id}"`); // The write slice: its detail and create pages are planned too. expect([...files.keys()].filter((file) => file.startsWith(`src/features/${id}/`))).toEqual( expect.arrayContaining([ expect.stringMatching(/-detail-page\.tsx$/u), expect.stringMatching(/-create-page\.tsx$/u), ]), ); } // Only what a build reads: no tests, no accessibility records. expect([...files.keys()].every((file) => file.startsWith("src/"))).toBe(true); }); // 1101/07-04. Assembly refuses the template fixture source in a product // feature, so every copy pins an external package of its own, and has a // topic of its own. it("pins a package and a topic of its own for every copy", () => { const files = planShapeFeatures(3); const contracts = [0, 1, 2].map((index) => { const { id } = shapeFeature(index); const contract = [...files].find(([file]) => file.startsWith(`src/features/${id}/contracts/`) && file.endsWith("-feature-contract.ts"), )?.[1]; expect(contract, id).toContain(`packageId: "@feature-shape/${id}"`); return contract ?? ""; }); const topics = contracts.map((contract) => /"(qinv\.[0-9a-z]{26})"/u.exec(contract)?.[1]); expect(new Set(topics).size).toBe(3); expect(contracts.join("\n")).not.toContain("PLACEHOLDER_CONTRACT_PACKAGE"); }); it("commits a baseline measured at 25 and 100 real-shape features, twice each", async () => { const baseline = JSON.parse( await readFile(FEATURE_SHAPE_BASELINE_PATH, "utf8"), ) as Readonly<{ environment: Readonly>; source: unknown; sourceFeature: unknown; runs: readonly Run[]; }>; // 2026-11-07. Measured with the generated feature, not the deleted sample. expect(baseline.sourceFeature).toBe(SHAPE_FEATURE_COMMAND); expect(baseline.environment).toMatchObject({ cpuModel: expect.any(String), logicalCpus: expect.any(Number), }); // 11-05 (2026-09-29). Without the commit it measured, nobody can tell // whether the baseline still describes the code or compare a new run. expect(baseline.source).toEqual({ commit: expect.stringMatching(/^[0-9a-f]{40}$/u), modifiedPaths: expect.any(Array), }); const bySize = (size: number) => baseline.runs.filter((run) => run.addedFeatures === size); const [origin] = bySize(0); expect(origin?.tsc.passed).toBe(true); for (const size of [25, 100]) { const runs = bySize(size); // A failed typecheck is the result and is not repeated. expect(runs.length).toBeGreaterThanOrEqual(runs[0]?.tsc.passed ? 2 : 1); for (const run of runs) { // Every copy contributes the generated feature's own routes. expect((run.routes - origin!.routes) % size).toBe(0); expect(run.routes).toBeGreaterThan(origin!.routes); expect(run.build.lazyJavascriptFiles).toBeGreaterThan( origin!.build.lazyJavascriptFiles, ); expect(run.loadBefore).toHaveLength(3); } // Compiler counts are deterministic; only times vary with the host. expect(new Set(runs.map((run) => run.tsc.instantiations)).size).toBe(1); } }); /** * 15-01. The initial budget (config/performance/budgets.json) is a fixed * 202 KiB for the whole app, so what one installed feature adds to the * initial chunk decides how many features fit. Before 15-01 every feature's * contracts, schemas, mappers, HTTP gateway and API loaded at boot: +3,922 B * per copy, and the first added feature exceeded the budget. Only what the * shell needs before a feature route is entered may load at boot (route * table, navigation and page messages, invalidation identities); the rest * loads with the feature's routes. The bound also fails if the * feature-declarations chunk group in vite.config.ts stops matching. * * 1103/15-04. The first copy also adds a context (shape-context-0), so one * number mixed a context's cost with a feature's and had 40 B left under * the bound. A second copy in the same context is one more feature alone; * the second copy in a context of its own is one more context. Each has its * own bound and the failure names which one grew. 2026-11-07: with no * feature in the template, the first copy also brings the code all features * share into the initial chunks (`firstFeatureGzipBytes`); it is paid once * and recorded, not bounded here: the initial budget (check:bundle) bounds * the total. * * 1103/15-02. The record in config/scale/feature-shape-ceilings.json is the * one place the documents take the template's baseline from. Written by * hand, it went stale twice; it now has to match this build. */ it("bounds what one more feature and one more context add, and matches the record (15-01)", async () => { const measured = await measureInitialJavascriptCosts(); const { initialJavascript: record } = JSON.parse( await readFile(FEATURE_SHAPE_CEILINGS_PATH, "utf8"), ) as { initialJavascript: InitialJavascriptCosts }; const observed = `measured ${JSON.stringify(measured)}`; expect(measured.perFeatureGzipBytes, `one more feature in an existing context: ${observed}`) .toBeLessThanOrEqual(1_024); expect(measured.perContextGzipBytes, `one more context: ${observed}`) .toBeLessThanOrEqual(512); expect( initialJavascriptRecordDrift(measured, record), `${FEATURE_SHAPE_CEILINGS_PATH} initialJavascript is stale (${observed}); ` + "rewrite it with node scripts/scale/measure-feature-shape-growth.ts --initial-baseline --write", ).toEqual([]); }, 180_000); /** * 1105/15-02. The total used to match within 1% (2,029 B at 202,927 B), * more than the room left under the budget (1,873 B) and more than one * feature (768 B), so a template change that took the room could leave the * record. Now a few hundred bytes are enough to fail: above the build id * spread (tens of bytes), below one feature. */ it("reports a record a few hundred bytes off the build, or a different file count", () => { const measured = { gzipBytes: 200_000, files: 25, firstFeatureGzipBytes: 5_000, perFeatureGzipBytes: 700, perContextGzipBytes: 250, }; expect(initialJavascriptRecordDrift(measured, measured)).toEqual([]); // The build id alone moves the total by tens of bytes. expect( initialJavascriptRecordDrift(measured, { ...measured, gzipBytes: 200_048 }), ).toEqual([]); expect( initialJavascriptRecordDrift(measured, { ...measured, gzipBytes: 199_800 }), ).toEqual([]); // A template change that grows the initial JavaScript by 300 B. expect( initialJavascriptRecordDrift({ ...measured, gzipBytes: 200_300 }, measured), ).toEqual(["gzipBytes: record 200000, build 200300"]); expect( initialJavascriptRecordDrift(measured, { ...measured, gzipBytes: 201_500 }), ).toEqual(["gzipBytes: record 201500, build 200000"]); expect( initialJavascriptRecordDrift(measured, { ...measured, gzipBytes: 204_000 }), ).toEqual(["gzipBytes: record 204000, build 200000"]); expect(initialJavascriptRecordDrift(measured, { ...measured, files: 26 })).toEqual([ "files: record 26, build 25", ]); expect( initialJavascriptRecordDrift(measured, { ...measured, perFeatureGzipBytes: 800 }), ).toEqual(["perFeatureGzipBytes: record 800, build 700"]); // A cost of a few bytes keeps a floor: gzip moves it by a byte or two. const tiny = { ...measured, perContextGzipBytes: 4 }; expect(initialJavascriptRecordDrift(tiny, { ...tiny, perContextGzipBytes: 6 })).toEqual([]); expect(initialJavascriptRecordDrift(tiny, { ...tiny, perContextGzipBytes: 30 })).toEqual([ "perContextGzipBytes: record 30, build 4", ]); }); it("states in the committed rationale what the ceilings rest on", async () => { // The rationale lives next to the baseline, not in a review document: the // history documents may be deleted after copying (README.md). const rationale = JSON.parse( await readFile(FEATURE_SHAPE_CEILINGS_PATH, "utf8"), ) as Readonly<{ baseline: string; ceilings: Readonly<{ contextMaxFeatures: number; appMaxContexts: number }>; axes: readonly Readonly>[]; }>; expect(rationale.baseline).toBe(FEATURE_SHAPE_BASELINE_PATH); expect(rationale.ceilings).toEqual({ contextMaxFeatures: FEATURES_PER_CONTEXT, appMaxContexts: APP_MAX_CONTEXTS, }); expect(rationale.axes.map((row) => row.axis)).toEqual( expect.arrayContaining([ "Typecheck of one app", "Production build", "Initial JavaScript", "Lazy JavaScript", "Routes", ]), ); for (const row of rationale.axes) { for (const column of ["axis", "verified", "condition", "notVerified"]) expect(row[column], `${row.axis}.${column}`).toMatch(/\S/u); } }); });