Files
clean-architecture-frontend…/tests/architecture/app-build-input.test.ts
T

94 lines
4.1 KiB
TypeScript

import { spawnSync } from "node:child_process";
import { mkdtemp, readdir, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { afterAll, describe, expect, it } from "vitest";
import { inspectAppBuildInput } from "../../scripts/check-app-build-input.ts";
import { renderContextIndex, renderFeatureIndex } from "../../scripts/generate-product-feature-index.ts";
import { APP_A, writeFilesystemTopology } from "../scale/filesystem-topology.ts";
/**
* Deployable-app isolation regression gate: the build input of one app must
* never contain another app's features, whether through the generated index
* or through the production bundle.
*/
const GENERATOR = path.resolve("scripts/generate-product-feature-index.ts");
const roots: string[] = [];
afterAll(async () => {
await Promise.all(roots.map((root) => rm(root, { recursive: true, force: true })));
});
async function multiAppRepo() {
const root = await mkdtemp(path.join(tmpdir(), "app-build-input-"));
roots.push(root);
await writeFilesystemTopology(root, { globalFeatures: 1_100 });
const run = spawnSync(process.execPath, [GENERATOR, `--root=${root}`, `--app=${APP_A.id}`], {
encoding: "utf8",
});
if (run.status !== 0) throw new Error(run.stderr);
return root;
}
async function inventory(root: string, modules: readonly string[]) {
const file = path.join(root, "inventory.json");
await writeFile(file, JSON.stringify({ schemaVersion: 1, chunks: [{ fileName: "a.js", modules }] }));
return file;
}
describe("app build input isolation gate", () => {
it("accepts the production generator's output and an app-local bundle", async () => {
const root = await multiAppRepo();
const bundle = await inventory(root, [
"src/features/app-a-ctx0-f0/feature-module.ts",
"src/features/app-a-ctx1-f49/presentation/page.ts",
"src/platform/traffic/request-budget.ts",
]);
const report = await inspectAppBuildInput(root, { bundleInventory: bundle });
expect(report.violations).toEqual([]);
expect(report.appId).toBe(APP_A.id);
expect(report.generatedFeatureImports).toHaveLength(APP_A.contexts * APP_A.featuresPerContext);
expect(report.bundle.checked).toBe(true);
});
it("rejects a generator regression that imports every discovered feature", async () => {
const root = await multiAppRepo();
const everyFeature = (await readdir(path.join(root, "src/features"))).map((directory) => ({
id: directory,
importPath: `../features/${directory}/feature-module.ts`,
directory,
contextId: directory.replace(/-f\d+$/u, ""),
contextRoutePath: "x/*",
contextMaxFeatures: 200,
}));
await writeFile(path.join(root, "src/.generated/product-feature-index.ts"), renderFeatureIndex(everyFeature));
await writeFile(path.join(root, "src/.generated/product-context-index.ts"), renderContextIndex(everyFeature));
const report = await inspectAppBuildInput(root, { bundleInventory: null });
expect(report.violations.length).toBeGreaterThan(0);
expect(report.violations.join("\n")).toMatch(/imports feature "other-0-ctx0-f0" not owned by app app-a/);
expect(report.violations.join("\n")).toMatch(/declares context "other-0-ctx0" not owned/);
});
it("rejects a production bundle that contains another app's feature", async () => {
const root = await multiAppRepo();
const bundle = await inventory(root, [
"src/features/app-a-ctx0-f0/feature-module.ts",
"src/features/other-0-ctx3-f7/feature-module.ts",
]);
const report = await inspectAppBuildInput(root, { bundleInventory: bundle });
expect(report.violations).toEqual([
'production bundle contains feature "other-0-ctx3-f7" not owned by app app-a',
]);
});
it("holds for this repository's generated indexes", async () => {
// The bundle side is gated after a fresh build (check:app-build-input:bundle).
const report = await inspectAppBuildInput(path.resolve("."), { bundleInventory: null });
expect(report.violations).toEqual([]);
expect(report.generatedFeatureImports).toEqual(report.ownedFeatureDirectories);
});
});