import { readdir, readFile } from "node:fs/promises"; import path from "node:path"; /** * 1103/12-04. The architecture lint fixtures are linted under the production * configuration of the layer they stand for. * * They used to be linted where they live, under tests/, by an ESLint entry of * their own that banned every layer's imports and globals at once, and a * fixture counted as rejected whenever ESLint exited non-zero. Deleting the * production rule for a layer therefore still left "9 forbidden rejected": * presentation code could call `fetch` and the fixtures said it could not. * * Each fixture is now linted as if it were the file at `lintAs`, so exactly * the production entries for that path apply, and a forbidden fixture passes * only when the rule named in `rejectedBy` reports it. */ export type ArchitectureLintRule = "no-restricted-imports" | "no-restricted-globals"; export type ArchitectureLintFixture = Readonly<{ /** Relative to tests/fixtures/architecture. */ file: string; /** The production path whose ESLint configuration the fixture is linted under. */ lintAs: string; /** For a forbidden fixture: the production rule that must reject it. */ rejectedBy?: ArchitectureLintRule; }>; export const ARCHITECTURE_LINT_FIXTURE_ROOT = "tests/fixtures/architecture"; const LAYER_RULES: ReadonlySet = new Set([ "no-restricted-imports", "no-restricted-globals", ]); const at = (layer: string, file: string) => `${layer}/__architecture_fixture__/${path.posix.basename(file)}`; export const ARCHITECTURE_LINT_FIXTURES: readonly ArchitectureLintFixture[] = Object.freeze([ ...[ "allowed/bootstrap-composes-adapter.ts", ].map((file) => ({ file, lintAs: at("src/bootstrap", file) })), ...[ "allowed/presentation-facade.ts", "allowed/presentation-facade.tsx", ].map((file) => ({ file, lintAs: at("src/presentation", file) })), ...[ "forbidden/application-imports-adapter.ts", "forbidden/application-imports-tanstack.ts", ].map((file) => ({ file, lintAs: at("src/application", file), rejectedBy: "no-restricted-imports" as const, })), // Domain code lives inside features; src/features/*/domain is the entry // that applies to it. { file: "forbidden/domain-imports-react.ts", lintAs: "src/features/__architecture_fixture__/domain/domain-imports-react.ts", rejectedBy: "no-restricted-imports", }, { file: "forbidden/presentation-direct-fetch.tsx", lintAs: at("src/presentation", "presentation-direct-fetch.tsx"), rejectedBy: "no-restricted-globals", }, ...[ "forbidden/presentation-imports-adapter.ts", "forbidden/presentation-imports-adapter.tsx", "forbidden/presentation-imports-output-port.tsx", "forbidden/presentation-imports-tanstack.tsx", ].map((file) => ({ file, lintAs: at("src/presentation", file), rejectedBy: "no-restricted-imports" as const, })), { file: "forbidden/template-imports-application.tsx", lintAs: at("src/presentation/templates", "template-imports-application.tsx"), rejectedBy: "no-restricted-imports", }, ]); type LintMessage = Readonly<{ ruleId: string | null; message: string; fatal?: boolean; line?: number; }>; export type ArchitectureLinter = Readonly<{ lintText( code: string, options: Readonly<{ filePath: string; warnIgnored: boolean }>, ): Promise[]>; }>; export type ArchitectureLintFixtureResult = Readonly<{ allowed: number; forbidden: number; failures: readonly string[]; }>; const SOURCE_FILE = /\.(?:ts|tsx)$/u; async function filesBelow(root: string, directory: string): Promise { const entries = await readdir(path.join(root, directory), { withFileTypes: true }); const files = await Promise.all(entries.map((entry) => { const relative = path.posix.join(directory, entry.name); if (entry.isDirectory()) return filesBelow(root, relative); return SOURCE_FILE.test(entry.name) ? [relative] : []; })); return files.flat(); } export async function checkArchitectureLintFixtures(input: Readonly<{ root: string; linter: ArchitectureLinter; fixtures?: readonly ArchitectureLintFixture[]; }>): Promise { const fixtures = input.fixtures ?? ARCHITECTURE_LINT_FIXTURES; const failures: string[] = []; const onDisk = new Set([ ...await filesBelow(input.root, path.posix.join(ARCHITECTURE_LINT_FIXTURE_ROOT, "allowed")), ...await filesBelow(input.root, path.posix.join(ARCHITECTURE_LINT_FIXTURE_ROOT, "forbidden")), ].map((file) => path.posix.relative(ARCHITECTURE_LINT_FIXTURE_ROOT, file))); const listed = new Set(fixtures.map(({ file }) => file)); for (const file of [...onDisk].filter((candidate) => !listed.has(candidate)).sort()) { failures.push(`${file}: not listed in ARCHITECTURE_LINT_FIXTURES, so nothing says what must reject it`); } for (const fixture of fixtures) { const forbidden = fixture.file.startsWith("forbidden/"); if (forbidden !== (fixture.rejectedBy !== undefined)) { failures.push(`${fixture.file}: a forbidden fixture names its rule and an allowed one names none`); continue; } if (!onDisk.has(fixture.file)) { failures.push(`${fixture.file}: listed fixture does not exist`); continue; } const code = await readFile( path.join(input.root, ARCHITECTURE_LINT_FIXTURE_ROOT, fixture.file), "utf8", ); const [result] = await input.linter.lintText(code, { filePath: path.join(input.root, fixture.lintAs), warnIgnored: true, }); const messages = result?.messages ?? []; const unlinted = messages.filter((message) => message.fatal || message.ruleId === null); if (unlinted.length > 0) { failures.push( `${fixture.file} (as ${fixture.lintAs}) was not linted: ${unlinted.map(({ message }) => message).join("; ")}`, ); continue; } const layerMessages = messages.filter(({ ruleId }) => ruleId !== null && LAYER_RULES.has(ruleId)); if (fixture.rejectedBy !== undefined) { if (!layerMessages.some(({ ruleId }) => ruleId === fixture.rejectedBy)) { failures.push( `${fixture.file} (as ${fixture.lintAs}) was accepted: the production ${fixture.rejectedBy} rule for that path no longer rejects it`, ); } } else if (layerMessages.length > 0) { failures.push( `${fixture.file} (as ${fixture.lintAs}) was rejected: ${layerMessages .map(({ ruleId, line, message }) => `${ruleId} at line ${line ?? "?"}: ${message}`) .join("; ")}`, ); } } return Object.freeze({ allowed: fixtures.filter(({ rejectedBy }) => rejectedBy === undefined).length, forbidden: fixtures.filter(({ rejectedBy }) => rejectedBy !== undefined).length, failures: Object.freeze(failures), }); }