Files
clean-architecture-frontend…/tests/unit/two-feature-install-typecheck.test.ts
T
DongHyeonkaandClaude Opus 5.5 30cf71b249 fix(features): keep the installed composers typed with two features (09-01)
flatMap over the installed-module union, the route-registry cast and the
non-distributive locale types all failed once a second feature was installed.
A unit test installs a differently shaped second feature into a copy of src
and runs the app typecheck.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 22:10:07 +09:00

107 lines
3.9 KiB
TypeScript

import { spawnSync } from "node:child_process";
import { cp, mkdir, mkdtemp, readFile, rm, symlink, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { afterAll, describe, expect, it } from "vitest";
/**
* 09-01. The central composers (`src/features/installed-*`) are typed from the
* generated feature index, so the repository's own typecheck only ever sees
* one installed feature. This installs a second feature whose shape differs
* from reference-feature on every composed axis (feature id, adapter needs,
* no routes, no runtimes, a different locale set) into a copy of `src`,
* regenerates the index with the production generator and runs the app
* typecheck over the copy.
*/
const SECOND_FEATURE_MODULE = `import { defineFeatureAdapterContribution } from "../feature-adapter-contribution.ts";
import { defineFeatureModule } from "../feature-module.ts";
type SecondFeatureInput = Readonly<{ ping(): string }>;
declare module "../../application/ports/in/application-api.ts" {
interface ApplicationFeatureInputs {
"second-feature": SecondFeatureInput;
}
}
export const FEATURE_MODULE_ID = "second-feature";
export const FEATURE_CONTEXT_ID = "reference-context";
export const FEATURE_MODULE = defineFeatureModule({
id: FEATURE_MODULE_ID,
contracts: {
featureId: FEATURE_MODULE_ID,
routes: {},
routeRuntimeContracts: {},
apiOperations: {},
invalidation: { topics: [], namespaces: [], edges: [] },
topicVersions: [],
schemas: {},
runtimeSchemas: {},
mappers: {},
},
routes: {},
adapters: [
defineFeatureAdapterContribution({
featureId: FEATURE_MODULE_ID,
needs: ["telemetry"] as const,
createInput: () =>
Object.freeze({
featureId: FEATURE_MODULE_ID,
input: Object.freeze({ ping: () => "pong" }),
}),
}),
],
runtimes: [],
messages: { "en-US": { "secondFeature.title": "Second feature" } },
contractContributions: [],
} as const);
`;
const SECOND_FEATURE_ROUTES = `import type { RouteObject } from "react-router-dom";
export const FEATURE_CONTEXT_ROUTES: readonly RouteObject[] = Object.freeze([]);
`;
const roots: string[] = [];
afterAll(async () => {
await Promise.all(roots.map((root) => rm(root, { recursive: true, force: true })));
});
describe("installing a second feature (09-01)", () => {
it("keeps the app typecheck green with two differently shaped features", async () => {
const root = await mkdtemp(path.join(tmpdir(), "two-feature-install-"));
roots.push(root);
await cp(path.resolve("src"), path.join(root, "src"), { recursive: true });
for (const config of ["tsconfig.base.json", "tsconfig.app.json"]) {
await cp(path.resolve(config), path.join(root, config));
}
await symlink(path.resolve("node_modules"), path.join(root, "node_modules"), "dir");
const featureRoot = path.join(root, "src/features/second-feature");
await mkdir(featureRoot, { recursive: true });
await writeFile(path.join(featureRoot, "feature-module.ts"), SECOND_FEATURE_MODULE);
await writeFile(path.join(featureRoot, "context-routes.ts"), SECOND_FEATURE_ROUTES);
const generated = spawnSync(
process.execPath,
[path.resolve("scripts/generate-product-feature-index.ts"), `--root=${root}`],
{ encoding: "utf8" },
);
expect(generated.stderr).toBe("");
expect(generated.status).toBe(0);
const index = await readFile(path.join(root, "src/.generated/product-feature-index.ts"), "utf8");
expect(index).toContain("../features/second-feature/feature-module.ts");
expect(index).toContain("../features/reference-feature/feature-module.ts");
const typecheck = spawnSync(
path.resolve("node_modules/.bin/tsc"),
["--project", path.join(root, "tsconfig.app.json")],
{ encoding: "utf8" },
);
expect(typecheck.stdout + typecheck.stderr).toBe("");
expect(typecheck.status).toBe(0);
}, 60_000);
});