Files
clean-architecture-frontend…/tests/unit/feature-shape-growth.test.ts
DongHyeonka 287dc6fa9b build(perf): 초기 JS 예산을 202 KiB로 올린다
문제: 1105 PR B의 수정이 템플릿 초기 JS를 1,184 B 늘렸다(202,927 →
204,111 B, 초기 파일 26 → 27개, `0fee3fe`). 예산 204,800 B까지 남은 여유가
689 B로, 기능 하나가 더하는 768 B보다 작다. FE-GATE-020의 생성 기능
드릴이 기능 둘을 더하면 205,463 B가 되어 check:bundle이 "over by 663"으로
실패한다. PR A 끝(`a793b83`)에서는 통과했다.

늘어난 곳(desktop, 2026-11-06, 커밋마다 `--initial-baseline`으로 잼):
- 1105/03-01 `ab0d4a1` +327 B: 조립 때 `refusedBeforeWrite` code 검사
- 1105/08-03 `7ce9bf1` +293 B, 초기 파일 +1: `navigation-policy` 공유 청크
- 1105/02-01 `f738348` +251 B: scope closer 진단
- 계약·앱 코어 브랜치를 합칠 때 +148 B
- 나머지 +165 B: realtime +72 B, 05-01 +46 B, 테스트 +23 B, 03-02 +15 B와
  합칠 때의 압축 차

고른 방법: `config/performance/budgets.json`의 `initialJsGzipBytes`를
204,800 → 206,848 B(202 KiB)로 올린다(메인 세션 결정, 2026-11-06). 늘어난
바이트는 비대가 아니라 검사와 진단 코드다. 이유와 재검토 조건을
`docs/operations/performance-evidence.md`에, 계약 행을 CHANGELOG 미출시 절에
적는다. 초기 파일 상한을 26 → 32로 올린 `2fef27d`와 같은 방식이다.
버린 대안: 줄이기. 08-03 청크를 기존 청크 그룹에 넣으면 약 240 B가 돌아올
것으로 본다(추정, 재지 않음). 663 B에는 모자라서 03-01·02-01의 런타임 검사를
빌드 시점으로 옮기는 설계 변경이 더 필요하다. 리뷰 라운드 중간의 설계
변경은 다음 라운드의 숫자를 흐린다.

위험·한계:
- 올린 뒤 여유는 템플릿 그대로 2,737 B(768 B 기능 셋), 드릴이 기능 둘을
  더한 뒤 1,385 B다. 기록 대조 허용치 256 B(1105/15-02)는 두 여유보다 작다.
- 재검토 조건: 예산을 다시 넘을 때, 또는 1103/13-03(zod 갱신, 약 7 KB)을
  정할 때. 그때 03-01·02-01의 런타임 검사를 빌드 시점으로 옮기는 것부터
  본다. 이 상향은 13-03을 정하지 않는다.
- 기준선 기록(`initialJavascript`)은 앞 커밋 `0fee3fe`이 측정 스크립트
  `--initial-baseline --write`로 다시 썼다. 이 커밋은 기록을 바꾸지 않는다.

검증: desktop batch에서 확인.
corepack pnpm ci:gate FE-GATE-020 (CI env)
node scripts/run-vitest.ts run tests/unit/performance-budgets.test.ts tests/unit/check-bundle-output.test.ts tests/unit/feature-shape-growth.test.ts

Refs: 1105/15-02
2026-11-06 02:59:03 +09:00

239 lines
9.9 KiB
TypeScript

import { readFile } from "node:fs/promises";
import { describe, expect, it } from "vitest";
import {
FEATURE_SHAPE_BASELINE_PATH,
FEATURE_SHAPE_CEILINGS_PATH,
initialJavascriptRecordDrift,
measureInitialJavascriptCosts,
renameReferenceFeature,
type InitialJavascriptCosts,
} from "../../scripts/scale/measure-feature-shape-growth.ts";
import { APP_MAX_CONTEXTS } from "../../src/apps/template-app/app-module.ts";
import { CONTEXT_MAX_FEATURES } from "../../src/contexts/reference-context/context-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 the real reference feature.
*/
const context = { id: "shape-context-0", routeRoot: "shape-0" } as const;
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("renames every feature-owned identifier of a reference copy", () => {
const source = [
'featureId: "reference-feature"',
'path: "/examples/reference-resources/:id"',
"REFERENCE_RESOURCE_LIST",
"ReferenceResourcePage",
'contextId: "reference-context"',
'topic: "qinv.01k10f7m3w9p6r2c8v5n4x"',
"NoRequest",
].join("\n");
const renamed = renameReferenceFeature(source, "s007", context);
expect(renamed).not.toMatch(/reference/iu);
expect(renamed).toContain('featureId: "s007-feature"');
expect(renamed).toContain('path: "/shape-0/s007-resources/:id"');
expect(renamed).toContain("S007_RESOURCE_LIST");
expect(renamed).toContain("S007ResourcePage");
expect(renamed).toContain('contextId: "shape-context-0"');
expect(renamed).toContain('topic: "qinv.shape.s007"');
expect(renamed).toContain("S007NoRequest");
});
// 1101/07-04 made assembly refuse a TEMPLATE_FIXTURE contribution outside
// the reference feature, so every copy must pin an EXTERNAL_PACKAGE the way
// a product feature does.
it("turns the copied fixture source into an external package", () => {
const source = [
" source: Object.freeze({",
' kind: "TEMPLATE_FIXTURE" as const,',
' fixtureId: "REFERENCE_FEATURE_V1" as const,',
" revision: 1 as const,",
" }),",
" contractPackages: Object.freeze([]),",
].join("\n");
const renamed = renameReferenceFeature(source, "s007", context);
expect(renamed).not.toContain("TEMPLATE_FIXTURE");
expect(renamed).toContain('kind: "EXTERNAL_PACKAGE" as const');
expect(renamed.match(/packageId: "@feature-shape\/s007"/gu)).toHaveLength(2);
expect(renamed).not.toContain("contractPackages: Object.freeze([])");
});
it("keeps platform-owned names that only contain the word", () => {
const source =
"REFERENCE_FEATURE_V1 supportReference";
expect(renameReferenceFeature(source, "s001", context)).toBe(source);
});
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<Record<string, unknown>>;
source: unknown;
runs: readonly Run[];
}>;
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 reference 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 rest of the first copy is the context. Each has its own bound and
* the failure names which one grew.
*
* 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,
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"]);
});
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<Record<string, string>>[];
}>;
expect(rationale.baseline).toBe(FEATURE_SHAPE_BASELINE_PATH);
expect(rationale.ceilings).toEqual({
contextMaxFeatures: CONTEXT_MAX_FEATURES,
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);
}
});
});