Files
clean-architecture-frontend…/tests/unit/bundle-artifact-contract.test.ts
DongHyeonka 81447354cd fix(bundle): check:bundle이 Service Worker와 manifest 밖 JS를 잰다
check:bundle은 Vite manifest의 chunk만 잰다. Service Worker를 켠 제품의
worker와 public·Web Worker 출력은 크기가 얼마든 FE-GATE-012를 통과한다.

빌드가 만든 Service Worker는 새 key `bundle.serviceWorkerGzipBytes`로,
assets/에 빌드가 만든 hash 이름 파일은 파일마다 lazy 예산으로 잰다.
public/ 사본은 이름이 hash처럼 보이거나 service-worker.js여도 빌드
출력이 아니므로, 그 밖의 예산 없는 JS와 함께 경로를 대고 실패한다.
이름만 보는 안은 HTML이 바로 읽는 public JS 221,678 B가 통과해 버렸다.
새 key가 없으면 파일과 key를 대고 실패한다.

예산 값은 자산 목록을 상한까지(채운 이름은 줄기 16자 무작위) 채우고
build·release id를 128자로 준 worker(Mmax)를 1 KiB 단위로 올림한
값이다(review 판정 2026-11-23, 소유자 결정 아님). 100 B 단위 올림은
기능을 더한 제품이 코드 변경 없이 막히고, 긴 이름에 맞춰 올리거나
id를 512자로 재는 안은 worker 코드 증가를 더 숨긴다. 목록과 코드를
나눠 재는 안은 빌드 구조를 바꿔서 버렸다.

위험: 자산 목록이 상한에 가까우면 이름이 길고 다양하거나 id가 길 때
예산을 넘을 수 있다. 기본 id로도 넘는 조합을 쟀다. 자산이 적은
빌드에서는 목록 몫만큼 worker 코드 증가를 못 잡는다. 기본 선택이
null이라 템플릿 CI는 worker 값을 재지 않는다.

검증: 고치기 전 코드에 이 커밋의 시험을 얹으면 세 파일 47개 가운데
25개가 실패하고 이 트리에서 통과한다. 보호 규칙 변이 15개가 실패한다.
노트북 check:types, lint, check:architecture, test:unit, test:contract,
test:runtime-schema, verify:documentation 통과. desktop에서 worker
입력이 같은 트리의 Mmax 23404 B, 자산 길이 다섯 자리 23442 B, handler
둘 23424 B가 예산 안이고 zod를 넣은 worker는 실패했다. merge 단계와
FE-GATE-029·030은 커밋 때 돌리지 않았다.

Refs: REV-FE-0010
2026-11-23 06:00:49 +09:00

142 lines
5.1 KiB
TypeScript

import { readFile } from "node:fs/promises";
import { describe, expect, it } from "vitest";
import {
bundleOutputInventoryArtifactSchema,
bundlePerformanceArtifactSchema,
} from "../../scripts/contracts/release-artifacts.ts";
const sha256 = "a".repeat(64);
const inventory = {
schemaVersion: 1,
generatedAt: "2026-08-02T00:00:00.000Z",
context: {
nodeVersion: "v24.14.0",
packageManager: "pnpm@11.17.0",
runnerImage: "fixture@sha256:abc",
},
outputs: [
{ path: "dist/assets/entry.js", bytes: 100, gzipBytes: 50, sha256 },
{ path: "dist/assets/lazy.js", bytes: 80, gzipBytes: 40, sha256 },
{ path: "dist/index.html", bytes: 20, gzipBytes: 15, sha256 },
],
} as const;
const completed = {
...inventory,
measurements: {
initialJsGzipBytes: 50,
initialJsFiles: 1,
lazyChunks: [{ path: "assets/lazy.js", gzipBytes: 40 }],
serviceWorkerGzipBytes: null,
},
classification: {
initialFiles: ["assets/entry.js"],
lazyFiles: ["assets/lazy.js"],
missingImports: [],
publicFiles: [],
},
missingOutputs: [],
unbudgetedOutputs: [],
thresholds: {
initialJsGzipBytes: 60,
initialJsFiles: 1,
lazyChunkGzipBytes: 45,
serviceWorkerGzipBytes: 30,
},
results: {
initialPassed: true,
initialFilesPassed: true,
lazyResults: [
{ path: "assets/lazy.js", gzipBytes: 40, threshold: 45, passed: true },
],
serviceWorkerPassed: true,
passed: true,
},
fixtures: [
{ name: "initial-js-over-budget", passed: true },
{ name: "lazy-chunk-over-budget", passed: true },
{ name: "initial-files-over-budget", passed: true },
{ name: "service-worker-over-budget", passed: true },
],
passed: true,
} as const;
describe("bundle artifact contracts", () => {
it("separates the raw output inventory from final budget evidence", () => {
expect(bundleOutputInventoryArtifactSchema.parse(inventory)).toEqual(inventory);
expect(() => bundleOutputInventoryArtifactSchema.parse(completed)).toThrow();
expect(bundlePerformanceArtifactSchema.parse(completed)).toEqual(completed);
expect(() => bundlePerformanceArtifactSchema.parse(inventory)).toThrow();
});
it.each([
["initial total", { measurements: { ...completed.measurements, initialJsGzipBytes: 49 } }],
["lazy classification", { classification: { ...completed.classification, lazyFiles: [] } }],
["missing output", { missingOutputs: ["assets/missing.js"], passed: true }],
["threshold result", { results: { ...completed.results, initialPassed: false } }],
["initial file count", { measurements: { ...completed.measurements, initialJsFiles: 2 } }],
["initial file result", { results: { ...completed.results, initialFilesPassed: false } }],
["lazy result", { results: { ...completed.results, lazyResults: [] } }],
["fixture identity", { fixtures: [{ name: "other", passed: true }, completed.fixtures[1], completed.fixtures[2], completed.fixtures[3]] }],
["overall result", { passed: false }],
] as const)("rejects inconsistent %s evidence", (_name, mutation) => {
expect(() =>
bundlePerformanceArtifactSchema.parse({ ...completed, ...mutation })
).toThrow();
});
// REV-FE-0010.
it.each([
[
"a Service Worker within budget left unmeasured",
{ outputs: [...inventory.outputs, { path: "dist/service-worker.js", bytes: 50, gzipBytes: 29, sha256 }] },
],
[
"a Service Worker over budget judged as passed",
{
outputs: [...inventory.outputs, { path: "dist/service-worker.js", bytes: 50, gzipBytes: 31, sha256 }],
measurements: { ...completed.measurements, serviceWorkerGzipBytes: 31 },
},
],
// Only serviceWorkerPassed is wrong: the totals agree that the worker is over.
[
"a Service Worker result against its threshold",
{
outputs: [...inventory.outputs, { path: "dist/service-worker.js", bytes: 50, gzipBytes: 31, sha256 }],
measurements: { ...completed.measurements, serviceWorkerGzipBytes: 31 },
results: { ...completed.results, passed: false },
passed: false,
},
],
[
"copies of public/ out of order",
{ classification: { ...completed.classification, publicFiles: ["z.js", "a.js"] } },
],
[
"JavaScript without a budget left unnamed",
{ outputs: [...inventory.outputs, { path: "dist/stray.js", bytes: 50, gzipBytes: 1, sha256 }] },
],
[
"JavaScript without a budget judged as passed",
{
outputs: [...inventory.outputs, { path: "dist/stray.js", bytes: 50, gzipBytes: 1, sha256 }],
unbudgetedOutputs: ["stray.js"],
},
],
] as const)("rejects %s", (_name, mutation) => {
expect(() =>
bundlePerformanceArtifactSchema.parse({ ...completed, ...mutation })
).toThrow();
});
it("publishes final evidence only through the validated atomic writer", async () => {
const source = await readFile("scripts/check-bundle.ts", "utf8");
expect(source).toContain("bundleOutputInventoryArtifactSchema.parse");
expect(source).toContain("writeValidatedJsonArtifact");
expect(source).not.toMatch(/\bwriteFile\s*\(/u);
});
});