Files
clean-architecture-frontend…/scripts/test-sample-removal.ts
T
DongHyeonka 7411d4a404 test(scaffold): 생성 드릴이 --with-create 쓰기 slice도 만들고 게이트를 돈다
문제
--with-create(NEVER)와 --with-create --keyed(KEYED)가 만드는 코드는 단위
테스트가 텍스트로만 본다. FE-GATE-020의 두 드릴(test:feature-scaffold,
test:sample-removal)은 기본 기능 하나만 만들어 게이트를 돈다. 그래서 쓰기
slice의 타입·lint·테스트·빌드가 머지 게이트에서 깨져도 모른다(16 5.3절의
"생성 결과를 매번 게이트로 돌린다"는 전제).

고른 방법
- scripts/test-feature-scaffold.ts: 기본 기능(scaffold-feature) 옆에
  scaffold-write-feature를 --with-create --keyed로 하나 더 만들고, 둘 다
  만들어졌을 때 같은 게이트를 한 번 돈다. 바꾼 것은 WRITE_FEATURE_ID 상수,
  create-feature-write 검사 한 개, 게이트 조건(created && createdWrite), 머리
  주석이다.
- scripts/test-sample-removal.ts: 참조 기능을 지운 복사본의 create:feature
  인자에 --with-create 한 개를 더했다. 쓰기 slice도 샘플에 기대지 않는다는
  증거다.
- 세 변형(기본, NEVER, KEYED)이 모두 드릴의 게이트를 지난다.
- reference/create-feature.md의 드릴 설명을 맞췄다.

버린 대안
- 드릴 기능 하나를 --with-create로 바꾸기: 기본 변형이 게이트에서 빠진다.

위험·한계
- test:feature-scaffold는 기능 하나만큼(create:feature 한 번과 그 기능의
  테스트) 길어진다. 게이트는 한 번만 돈다.
- 다른 작업(게이트 담당)이 같은 두 파일의 명령 목록을 고칠 수 있다. 내 수정은
  위 줄로 좁혔다.

검증
- flock 아래 CHOKIDAR_USEPOLLING=1 corepack pnpm test:feature-scaffold:
  rc=0, "feature scaffold: PASS (17 checks)" (생성 2 + 게이트 15), 162초.
- 같은 조건 corepack pnpm test:sample-removal: rc=0, "reference-feature
  removal: PASS (23 checks, no fixture IDs)". 참조 기능을 지운 복사본에서
  --with-create 기능을 만든 뒤 scaffold-* 게이트 9개가 모두 통과했다. 첫
  실행은 tests/unit/create-feature.test.ts에 샘플 ID 문자열이 있어
  fixture-id-residue로 실패했고, 그 테스트를 앞 커밋에서 고친 뒤 통과했다.

Refs: docs/reviews/2026-11-03-maturity-review/09-app-core.md 09-06
Refs: docs/reviews/2026-11-03-maturity-review/16-structure-patterns.md 16-01
2026-11-04 23:47:24 +09:00

489 lines
18 KiB
TypeScript

import {
access,
mkdir,
readdir,
readFile,
rm,
writeFile,
} from "node:fs/promises";
import path from "node:path";
import { ROUTE_FEATURE_OWNER } from "../src/features/installed-feature-contracts.ts";
import { createFileSystemDeclarationSource } from "./generate-product-feature-index.ts";
import { isProductionModulePath } from "./lib/risk-coverage.ts";
import {
createRemovalFixtureRoot,
filesBelow,
prepareRemovalFixture,
pruneRemovalFixtureCiContract,
pruneScriptOrchestration,
regenerateRemovalFixtureWorkflow,
renderRemovalJunit,
requireRemovalFixtureEnvironment,
runRemovalFixtureCheck,
runRemovalFixturePnpm,
sealRemovalFixtureRepository,
type RemovalCheck,
} from "./lib/removal-fixture.ts";
/**
* 09-07 (D-10). Removes one installed feature, `--feature=<id>` (default: the
* reference sample), from a copy of the repository and proves the rest still
* typechecks, tests, covers and builds. Every feature loses its directory, its
* tests/features directory, its context when no other feature is left in it,
* its runtime-profile override and its central policy rows. The sample also
* owns the extra paths and scripts listed in SAMPLE_EXTRAS.
*/
const featureId =
process.argv.find((argument) => argument.startsWith("--feature="))
?.slice("--feature=".length) ?? "reference-feature";
type FeatureExtras = Readonly<{
paths: readonly string[];
ciScripts: readonly string[];
evidencePathFragments: readonly string[];
residue: RegExp;
}>;
const SAMPLE_EXTRAS: Readonly<Record<string, FeatureExtras>> = Object.freeze({
"reference-feature": Object.freeze({
// 0929/10-02. The HTTP scenario catalog (tests/mocks) and the negative
// type fixtures of the feature APIs run on the test contracts, so they are
// platform checks and stay (commonTestPaths below).
paths: [
"tests/component/product-feature-switch.test.tsx",
"tests/e2e/reference-form.spec.ts",
"tests/e2e/reference-route.spec.ts",
"tests/e2e/reference-save.spec.ts",
// 11-04: the growth measurement copies this sample feature.
"tests/unit/feature-shape-growth.test.ts",
"scripts/scale/measure-feature-shape-growth.ts",
"config/scale/feature-shape-growth.json",
"config/scale/feature-shape-ceilings.json",
],
ciScripts: [
"test:reference-feature",
],
evidencePathFragments: [
"reference-feature.xml",
],
residue: /REFERENCE_RESOURCE|reference-feature|reference-resource/i,
}),
});
function escapeRegExp(value: string): string {
return value.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&");
}
async function locateFeature(id: string) {
const source = await createFileSystemDeclarationSource(process.cwd());
for (const appId of source.appIds()) {
for (const context of source.contextsOf(appId)) {
const features = source.featuresOf(context.id);
const feature = features.find((candidate) => candidate.id === id);
if (!feature) continue;
let contextDirectory: string | null = null;
for (const entry of await readdir("src/contexts", { withFileTypes: true })) {
if (!entry.isDirectory()) continue;
const declaration = await readFile(
path.join("src/contexts", entry.name, "context-module.ts"),
"utf8",
).catch(() => "");
if (
new RegExp(`CONTEXT_MODULE_ID\\s*=\\s*["']${escapeRegExp(context.id)}["']`, "u")
.test(declaration)
) {
contextDirectory = `src/contexts/${entry.name}`;
}
}
return Object.freeze({
directory: feature.directory,
// The context goes with its last feature.
contextDirectory: features.length === 1 ? contextDirectory : null,
});
}
}
throw new Error(`No installed feature declares FEATURE_MODULE_ID "${id}"`);
}
const located = await locateFeature(featureId);
const extras: FeatureExtras = SAMPLE_EXTRAS[featureId] ?? Object.freeze({
paths: [],
ciScripts: [],
evidencePathFragments: [],
residue: new RegExp(
`${escapeRegExp(featureId)}|${escapeRegExp(located.directory)}`,
"i",
),
});
// Captured from this repository before the copy loses them.
const featureRouteIds = Object.entries(ROUTE_FEATURE_OWNER)
.filter(([, owner]) => owner === featureId)
.map(([routeId]) => routeId);
const fixtureRoot = await createRemovalFixtureRoot(`${featureId}-removal`);
const pnpmCli = requireRemovalFixtureEnvironment("npm_execpath");
const featureSource = `src/features/${located.directory}`;
const featureTests = `tests/features/${located.directory}`;
const isSample = featureId in SAMPLE_EXTRAS;
/**
* Platform tests that must survive the sample feature's removal. Asserting they
* are still present is what stops the removal fixture from "passing" by having
* quietly deleted the platform's own coverage along with the feature.
*/
const commonTestPaths = [
"tests/unit/external-contract-runtime.test.ts",
"tests/unit/http-execution-v3.test.ts",
"tests/unit/runtime-adapters.test.ts",
"tests/integration/http-execution-v3-observability.test.ts",
// 0929/10-02. HTTP scenario evidence (FE-GATE-007) and the feature API
// negative type fixtures (FE-GATE-003) run on the test contracts.
"tests/integration/http-scenario-catalog.test.ts",
"tests/mocks/scenarios/catalog.ts",
"tests/mocks/handlers/test-entities.ts",
// 16-02. Feature tests answer their own operations with it.
"tests/mocks/handlers/http-scenarios.ts",
"tests/fixtures/typecheck/fixture-feature-contract.ts",
"tests/fixtures/typecheck/invalid-feature-input.ts",
"tests/fixtures/typecheck/invalid-reference-operation.ts",
"tests/fixtures/typecheck/invalid-http-operation-input.ts",
"tests/fixtures/typecheck/invalid-http-wire-mapper.ts",
"tests/fixtures/typecheck/invalid-http-operation-id.ts",
"tests/fixtures/typecheck/invalid-http-route-id.ts",
"tests/fixtures/typecheck/invalid-feature-contribution-input.ts",
"tests/fixtures/typecheck/invalid-feature-capability-selection.ts",
];
const featureOwnedPaths = [
featureSource,
featureTests,
// The feature's bounded-context declaration when it was the last feature in
// it; the template app stays.
...(located.contextDirectory === null ? [] : [located.contextDirectory]),
...extras.paths,
];
type CoveragePolicy = {
repositoryBaseline: number;
generatedPaths: string[];
criticalModules: Array<{
path?: string;
minimum?: Record<string, number>;
}>;
highRiskPaths: string[];
waivers: Array<{ path?: string }>;
};
type EvidenceContribution = Readonly<{ owner?: string }>;
type EvidencePolicy = Record<
"scenarioCatalogs" | "sourceContracts",
unknown
>;
type GovernanceConsumer = Readonly<{ path?: string }>;
type GovernanceRegistry = Record<string, unknown> & {
consumers?: unknown;
consumerDirectories?: unknown;
};
type RemovalGovernance = { registries: GovernanceRegistry[] };
function runPnpm(script: string, extra: string[] = []): boolean {
return runRemovalFixturePnpm(fixtureRoot, pnpmCli, script, extra);
}
function check(name: string, script: string, extra: string[] = []): RemovalCheck {
return runRemovalFixtureCheck(name, fixtureRoot, pnpmCli, script, extra);
}
/** 14-08. A non-command check fails with its own findings, nothing else's. */
function outcome(name: string, findings: readonly string[], label: string): RemovalCheck {
return findings.length === 0
? { name, passed: true }
: { name, passed: false, failure: `${label}: ${findings.join(", ")}` };
}
try {
await prepareRemovalFixture(fixtureRoot);
for (const excludedFixtureTest of [
"tests/unit/ci-workflow-generation.test.ts",
"tests/unit/__snapshots__/ci-workflow-generation.test.ts.snap",
"tests/unit/removal-fixture.test.ts",
"tests/unit/http-scenario-evidence.test.ts",
// They check README.md and CHANGELOG.md, which the fixture does not copy
// (REMOVAL_FIXTURE_COPY_TARGETS); removing a feature does not change them.
"tests/unit/changelog-review-ids.test.ts",
"tests/unit/documentation-readiness.test.ts",
]) {
await rm(path.join(fixtureRoot, excludedFixtureTest), { force: true });
}
const coveragePolicyFile = path.join(
fixtureRoot,
"config/testing/risk-coverage.json",
);
const coveragePolicy = JSON.parse(
await readFile(coveragePolicyFile, "utf8"),
) as CoveragePolicy;
const generatedProductionModules = new Set(coveragePolicy.generatedPaths);
const removedProductionModuleCount = (
await filesBelow(path.join(fixtureRoot, featureSource))
)
.map((file) => path.relative(fixtureRoot, file).split(path.sep).join("/"))
.filter(
(file) =>
isProductionModulePath(file) && !generatedProductionModules.has(file),
).length;
for (const ownedPath of featureOwnedPaths) {
await rm(path.join(fixtureRoot, ownedPath), {
recursive: true,
force: true,
});
}
if (!runPnpm("generate:feature-index")) {
throw new Error("Feature topology indexes failed to regenerate after sample removal");
}
// Runtime profiles are product configuration. Once the feature is physically
// removed, its override key must disappear too or the built config still
// advertises a source capability that no longer exists. public/config.json
// is the local profile `dev` serves and has to stay equal to it (F-5).
for (const configPath of [
"config/runtime/local.json",
"config/runtime/development.json",
"config/runtime/staging.json",
"config/runtime/production.json",
"public/config.json",
]) {
const profilePath = path.join(fixtureRoot, configPath);
const runtimeConfig = JSON.parse(
await readFile(profilePath, "utf8"),
) as {
FEATURE_OVERRIDES?: Record<string, string>;
};
if (runtimeConfig.FEATURE_OVERRIDES) {
delete runtimeConfig.FEATURE_OVERRIDES[featureId];
}
await writeFile(
profilePath,
`${JSON.stringify(runtimeConfig, null, 2)}\n`,
);
}
const retainedCriticalModules = coveragePolicy.criticalModules.filter(
(modulePolicy) => !modulePolicy.path?.startsWith(`${featureSource}/`),
);
if (
isSample &&
retainedCriticalModules.length === coveragePolicy.criticalModules.length
) {
throw new Error("Reference feature coverage policy is not registered");
}
coveragePolicy.criticalModules = retainedCriticalModules;
coveragePolicy.highRiskPaths = coveragePolicy.highRiskPaths.filter(
(modulePath) => !modulePath.startsWith(`${featureSource}/`),
);
coveragePolicy.waivers = coveragePolicy.waivers.filter(
(waiver) => !waiver.path?.startsWith(`${featureSource}/`),
);
coveragePolicy.generatedPaths = coveragePolicy.generatedPaths.filter(
(modulePath) => !modulePath.startsWith(`${featureSource}/`),
);
coveragePolicy.repositoryBaseline -= removedProductionModuleCount;
if (coveragePolicy.repositoryBaseline <= 0) {
throw new Error("Reference feature removal produced an invalid coverage baseline");
}
await writeFile(
coveragePolicyFile,
`${JSON.stringify(coveragePolicy, null, 2)}\n`,
);
const evidencePolicyFile = path.join(
fixtureRoot,
"config/testing/test-evidence.json",
);
const evidencePolicy = JSON.parse(
await readFile(evidencePolicyFile, "utf8"),
) as EvidencePolicy;
for (const policyKey of ["scenarioCatalogs", "sourceContracts"] as const) {
const contributions = evidencePolicy[policyKey];
if (!Array.isArray(contributions)) {
throw new Error(`Test evidence policy is missing ${policyKey}`);
}
// A feature's own evidence goes with it. The sample has none since
// 0929/10-02: the HTTP scenario catalog is owned by the platform.
evidencePolicy[policyKey] = contributions.filter(
(candidate: unknown) =>
(candidate as EvidenceContribution).owner !== featureId,
);
}
await writeFile(
evidencePolicyFile,
`${JSON.stringify(evidencePolicy, null, 2)}\n`,
);
const governanceFile = path.join(
fixtureRoot,
"config/contracts/registry-governance.json",
);
const removalGovernance = JSON.parse(
await readFile(governanceFile, "utf8"),
) as RemovalGovernance;
removalGovernance.registries = removalGovernance.registries.map(
(registry) => ({
...registry,
...(Array.isArray(registry.consumers)
? {
consumers: registry.consumers.filter(
(candidate: unknown) => {
const consumer = candidate as GovernanceConsumer;
return !consumer.path?.includes(featureSource);
},
),
}
: {}),
...(Array.isArray(registry.consumerDirectories)
? {
consumerDirectories: registry.consumerDirectories.filter(
(directory: unknown) =>
typeof directory !== "string" ||
!directory.includes(featureSource),
),
}
: {}),
}),
);
await writeFile(
governanceFile,
`${JSON.stringify(removalGovernance, null, 2)}\n`,
);
const removedCiScripts = new Set(extras.ciScripts);
const fixturePackagePath = path.join(fixtureRoot, "package.json");
const fixturePackage = JSON.parse(await readFile(fixturePackagePath, "utf8")) as {
scripts: Record<string, string>;
};
pruneScriptOrchestration(fixturePackage.scripts, "test:all", removedCiScripts);
await writeFile(fixturePackagePath, `${JSON.stringify(fixturePackage, null, 2)}\n`);
await pruneRemovalFixtureCiContract({
root: fixtureRoot,
removedScripts: removedCiScripts,
removedEvidencePathFragments: extras.evidencePathFragments,
});
// D-13: removing the sample is a breaking registry change. The fixture
// records the new registry baseline the way a downstream project does, and
// the canonical check:registries then runs against it.
if (!runRemovalFixturePnpm(fixtureRoot, pnpmCli, "update:registry-baseline", [], {
REGISTRY_BASELINE_OWNER: "removal-fixture",
REGISTRY_BASELINE_REASON: `${featureId} removed`,
})) {
throw new Error("Registry baseline failed to update after sample removal");
}
await regenerateRemovalFixtureWorkflow(fixtureRoot);
const residue: string[] = [];
for (const root of ["src", "tests"]) {
for (const file of await filesBelow(path.join(fixtureRoot, root))) {
const relative = path.relative(fixtureRoot, file);
const content = await readFile(file, "utf8");
if (extras.residue.test(`${relative}\n${content}`)) {
residue.push(relative);
}
}
}
const missingCommonTests: string[] = [];
for (const testPath of commonTestPaths) {
await access(path.join(fixtureRoot, testPath)).catch(() => {
missingCommonTests.push(testPath);
});
}
const gateChecks = (prefix: string): RemovalCheck[] => [
check(`${prefix}typecheck`, "check:types"),
check(`${prefix}architecture`, "check:architecture"),
check(`${prefix}registries`, "check:registries"),
check(`${prefix}unit-integration`, "test:all"),
check(`${prefix}coverage`, "test:coverage"),
check(`${prefix}test-evidence-source`, "check:test-evidence:source"),
check(`${prefix}ci-contract`, "check:ci"),
check(`${prefix}home-smoke`, "exec", [
"vitest",
"run",
"tests/component/router.test.tsx",
"--reporter=default",
]),
check(`${prefix}build`, "build"),
];
const checks: RemovalCheck[] = [
outcome("common-test-evidence", missingCommonTests, "platform tests missing"),
...gateChecks(""),
];
const builtResidue: string[] = [];
const buildSucceeded =
checks.find(({ name }) => name === "build")?.passed === true;
if (buildSucceeded) {
for (const file of await filesBelow(path.join(fixtureRoot, "dist"))) {
if (!/\.(?:js|css|html|json)$/.test(file)) continue;
const content = await readFile(file, "utf8");
if (extras.residue.test(content)) {
builtResidue.push(path.relative(fixtureRoot, file));
}
}
}
const routeCatalog = await import(
`${new URL(
"../src/features/installed-feature-contracts.ts",
`file://${fixtureRoot}/scripts/`,
).href}?removed=${Date.now()}`
) as { ROUTE_REGISTRY: Readonly<Record<string, unknown>> };
const routeIds = new Set(Object.keys(routeCatalog.ROUTE_REGISTRY));
checks.push(
outcome(
"route-absent",
featureRouteIds.filter((routeId) => routeIds.has(routeId)),
"routes still registered",
),
outcome("fixture-id-residue", residue, "residue"),
outcome("built-fixture-id-residue", builtResidue, "built residue"),
);
// U7 (decision 4). The same copy, now without the sample, gets one feature
// from create:feature and runs the same gates again: the command and the
// feature it writes need nothing from the sample.
if (isSample) {
// 1103/09-06. With the write slice (NEVER), which needs nothing from
// the sample either.
const created = check("scaffold-create-feature", "create:feature", [
"scaffold-feature",
"--with-create",
]);
checks.push(created);
if (created.passed) {
sealRemovalFixtureRepository(fixtureRoot);
checks.push(...gateChecks("scaffold-"));
}
}
const failed = checks.filter((entry) => !entry.passed);
await mkdir("artifacts/tests", { recursive: true });
await writeFile(
isSample
? "artifacts/tests/sample-removal.xml"
: `artifacts/tests/feature-removal-${featureId}.xml`,
renderRemovalJunit(`${featureId}-removal`, checks),
);
if (failed.length > 0) {
process.stderr.write(
`${featureId} removal failed:\n${failed
.map((entry) => ` ${entry.name}: ${entry.failure ?? "failed"}`)
.join("\n")}\n`,
);
process.exitCode = 1;
} else {
process.stdout.write(
`${featureId} removal: PASS (${checks.length} checks, no fixture IDs)\n`,
);
}
} finally {
await rm(fixtureRoot, { recursive: true, force: true });
}