Files
clean-architecture-frontend…/tests/unit/product-feature-selection-build.test.ts
T
DongHyeonka c9aa73fbfd refactor(scale): vite 빌드 입력 목록을 한 모듈에 둔다
문제: 초기 JS 측정(scripts/scale/measure-feature-shape-growth.ts)과
VITE_PRODUCT_FEATURES=none 빌드 테스트
(tests/unit/product-feature-selection-build.test.ts)가 임시 폴더에 복사할
빌드 입력 8개를 따로 적는다. vite.config.ts가 새 로컬 파일을 import하면
두 목록이 함께 낡고, 한쪽만 고치면 원인을 말하지 않는 "Could not resolve"로
실패한다(7f9a515, 799ab7f가 두 곳을 같이 고쳤다).

고른 방법: scripts/lib/vite-build-inputs.ts에 VITE_BUILD_INPUTS 하나를 두고
두 파일이 import한다. 측정 스크립트가 테스트 파일을 import하지 않는다.
새 tests/unit/vite-build-inputs.test.ts가 (1) 두 소비자에 자기 목록이
없고 이 모듈을 쓰는지, (2) vite.config.ts가 상대 경로로 닿는 모든 파일이
목록 안에 있는지 본다. (2)는 새 로컬 import를 목록에 넣지 않으면 빌드
전에 그 파일 이름으로 실패한다.
버린 대안: 목록을 vite.config.ts에서 export — 설정 파일을 빌드 도구 밖에서
import하면 플러그인 초기화가 따라온다.

위험·한계: import 그래프는 정규식으로 따라간다. 동적 import나 alias로
읽는 파일은 보지 못한다(지금은 없다). scripts/lib/removal-fixture.ts의
복사 목록은 저장소 전체 사본용이라 목적이 달라 합치지 않았다. 측정
스크립트는 참조 기능과 함께 지워지므로(test:sample-removal), (1)은 있는
소비자만 본다. 템플릿에서 그 파일이 없어지면
tests/unit/feature-shape-growth.test.ts가 import에서 실패한다.

검증: desktop batch에서 확인.
node scripts/run-vitest.ts run tests/unit/vite-build-inputs.test.ts
tests/unit/product-feature-selection-build.test.ts
tests/unit/feature-shape-growth.test.ts
corepack pnpm ci:gate FE-GATE-020 (참조 기능을 지운 사본의 test:all)

Refs: 1105/15-03
2026-11-06 01:23:48 +09:00

94 lines
4.3 KiB
TypeScript

import { spawnSync } from "node:child_process";
import { cp, mkdir, mkdtemp, readFile, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { pathToFileURL } from "node:url";
import { afterAll, describe, expect, it } from "vitest";
import { linkFixtureNodeModules } from "../../scripts/lib/fixture-node-modules.ts";
import { filesBelow } from "../../scripts/lib/removal-fixture.ts";
import { VITE_BUILD_INPUTS } from "../../scripts/lib/vite-build-inputs.ts";
import { ROUTE_FEATURE_OWNER } from "../../src/features/installed-feature-contracts.ts";
import { COMPILED_PRODUCT_FEATURE_IDS } from "../../src/features/installed-product-manifest.ts";
/**
* 09-02 (D-9). `VITE_PRODUCT_FEATURES=none` has to leave every feature out of
* every consumer, not only the browser bundle: the generated context index
* (router), the installed contract contributions (Node contract-set
* generation) and the built assets. This runs the production generator and a
* Vite build over a copy of the build inputs (scripts/lib/vite-build-inputs.ts,
* 1105/15-03). Written against whatever features the tree declares, so it
* holds after the sample's removal too.
*/
// Feature ids stay: the generated index still declares them so the platform
// overview reports NOT_INSTALLED. Their modules and route ids go.
const FEATURE_ROUTE_IDS = Object.keys(ROUTE_FEATURE_OWNER);
const FEATURE_MODULE = /^src\/(?:features\/[^/]+|contexts\/[^/]+)\//u;
const roots: string[] = [];
afterAll(async () => {
await Promise.all(roots.map((root) => rm(root, { recursive: true, force: true })));
});
describe("VITE_PRODUCT_FEATURES=none build (09-02)", () => {
it("leaves every feature out of the context index, the contract set and dist", async () => {
const root = await mkdtemp(path.join(tmpdir(), "product-selection-build-"));
roots.push(root);
for (const input of VITE_BUILD_INPUTS) {
await mkdir(path.dirname(path.join(root, input)), { recursive: true });
await cp(path.resolve(input), path.join(root, input), { recursive: true });
}
await linkFixtureNodeModules(root);
const env = { ...process.env, VITE_PRODUCT_FEATURES: "none" };
const generated = spawnSync(
process.execPath,
[path.resolve("scripts/generate-product-feature-index.ts"), `--root=${root}`],
{ encoding: "utf8", env },
);
expect(generated.stderr).toBe("");
expect(generated.status).toBe(0);
const contextIndex = await readFile(
path.join(root, "src/.generated/product-context-index.ts"),
"utf8",
);
expect(contextIndex).not.toContain("../features/");
const contributions = (await import(
pathToFileURL(path.join(root, "src/features/loaded-feature-runtimes.ts")).href
)) as typeof import("../../src/features/loaded-feature-runtimes.ts");
expect(contributions.INSTALLED_CONTRACT_CONTRIBUTIONS).toEqual([]);
const manifest = (await import(
pathToFileURL(path.join(root, "src/features/installed-product-manifest.ts")).href
)) as typeof import("../../src/features/installed-product-manifest.ts");
expect(manifest.INSTALLED_PRODUCT_FEATURE_IDS).toEqual([]);
// Still declared, so the platform overview reports them as NOT_INSTALLED.
expect(manifest.COMPILED_PRODUCT_FEATURE_IDS).toEqual(COMPILED_PRODUCT_FEATURE_IDS);
const built = spawnSync(
path.resolve("node_modules/.bin/vite"),
["build", "--logLevel", "error"],
{ cwd: root, encoding: "utf8", env },
);
expect(built.stderr).toBe("");
expect(built.status).toBe(0);
const residue: string[] = [];
for (const file of await filesBelow(path.join(root, "dist"))) {
if (!/\.(?:js|css|html)$/u.test(file)) continue;
const content = await readFile(file, "utf8");
for (const routeId of FEATURE_ROUTE_IDS) {
if (content.includes(routeId)) residue.push(`${path.relative(root, file)}: ${routeId}`);
}
}
expect(residue).toEqual([]);
const inventory = JSON.parse(
await readFile(path.join(root, "dist/.vite/module-inventory.json"), "utf8"),
) as { chunks: { modules: string[] }[] };
const modules = inventory.chunks.flatMap((chunk) => chunk.modules);
expect(modules).toContain("src/features/installed-product-manifest.ts");
expect(modules.filter((module) => FEATURE_MODULE.test(module))).toEqual([]);
}, 120_000);
});