143 lines
5.0 KiB
TypeScript
143 lines
5.0 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import { readdirSync, readFileSync, statSync } from "node:fs";
|
|
import { dirname, join, relative, resolve } from "node:path";
|
|
|
|
const SRC_ROOT = join(import.meta.dir, "..");
|
|
|
|
function collectTsFiles(dir: string, includeTests = false): string[] {
|
|
const results: string[] = [];
|
|
for (const entry of readdirSync(dir)) {
|
|
const full = join(dir, entry);
|
|
const stat = statSync(full);
|
|
if (stat.isDirectory()) {
|
|
results.push(...collectTsFiles(full, includeTests));
|
|
} else if (full.endsWith(".ts") && (includeTests || !full.endsWith(".test.ts"))) {
|
|
results.push(full);
|
|
}
|
|
}
|
|
return results;
|
|
}
|
|
|
|
function extractImportSpecifiers(source: string): string[] {
|
|
const specifiers: string[] = [];
|
|
const staticImportRe = /(?:import|export)\s+(?:type\s+)?(?:[\s\S]*?\s+from\s+)?["']([^"']+)["']/g;
|
|
const dynamicImportRe = /import\(\s*["']([^"']+)["']\s*\)/g;
|
|
|
|
for (const match of source.matchAll(staticImportRe)) {
|
|
if (match[1]) specifiers.push(match[1]);
|
|
}
|
|
for (const match of source.matchAll(dynamicImportRe)) {
|
|
if (match[1]) specifiers.push(match[1]);
|
|
}
|
|
return specifiers;
|
|
}
|
|
|
|
function resolveImportRelativeToSrc(fromFile: string, specifier: string): string | null {
|
|
if (!specifier.startsWith(".")) return null;
|
|
const abs = resolve(dirname(fromFile), specifier).replace(/\.[jt]s$/, "");
|
|
return relative(SRC_ROOT, abs);
|
|
}
|
|
|
|
describe("Architecture Layering Boundaries", () => {
|
|
test("workflow imports use the module directories after the platform migration", () => {
|
|
const legacy = /domains\/platform\/(?:analysis|workflow-(?:alert-store|branch|nodes|script|state|watch))$/;
|
|
const violations: string[] = [];
|
|
for (const file of collectTsFiles(SRC_ROOT, true)) {
|
|
for (const specifier of extractImportSpecifiers(readFileSync(file, "utf8"))) {
|
|
const target = resolveImportRelativeToSrc(file, specifier);
|
|
if (target && legacy.test(target)) violations.push(`${relative(SRC_ROOT, file)} -> ${target}`);
|
|
}
|
|
}
|
|
expect(violations).toEqual([]);
|
|
});
|
|
test("Entrypoints (src/cli, src/http, server.ts, cli.ts, aipro-cli.ts) must never import domains/*/api directly", () => {
|
|
const entrypointFiles = [
|
|
...collectTsFiles(join(SRC_ROOT, "cli")),
|
|
...collectTsFiles(join(SRC_ROOT, "http")),
|
|
join(SRC_ROOT, "server.ts"),
|
|
join(SRC_ROOT, "cli.ts"),
|
|
join(SRC_ROOT, "aipro-cli.ts"),
|
|
];
|
|
|
|
const violations: string[] = [];
|
|
for (const file of entrypointFiles) {
|
|
const content = readFileSync(file, "utf8");
|
|
const specifiers = extractImportSpecifiers(content);
|
|
for (const spec of specifiers) {
|
|
const targetRel = resolveImportRelativeToSrc(file, spec);
|
|
if (targetRel && /^domains\/[^/]+\/api$/.test(targetRel)) {
|
|
violations.push(`${relative(SRC_ROOT, file)} -> ${targetRel}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
expect(violations).toEqual([]);
|
|
});
|
|
|
|
test("src/domains/ must remain pure SDK and never import from cli, http, or server entrypoints", () => {
|
|
const domainFiles = collectTsFiles(join(SRC_ROOT, "domains"));
|
|
const violations: string[] = [];
|
|
|
|
for (const file of domainFiles) {
|
|
const content = readFileSync(file, "utf8");
|
|
const specifiers = extractImportSpecifiers(content);
|
|
for (const spec of specifiers) {
|
|
const targetRel = resolveImportRelativeToSrc(file, spec);
|
|
if (
|
|
targetRel &&
|
|
(targetRel.startsWith("cli/") ||
|
|
targetRel.startsWith("http/") ||
|
|
targetRel === "server" ||
|
|
targetRel === "cli" ||
|
|
targetRel === "aipro-cli")
|
|
) {
|
|
violations.push(`${relative(SRC_ROOT, file)} -> ${targetRel}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
expect(violations).toEqual([]);
|
|
});
|
|
|
|
test("src/core/ must never depend upward on domains, cli, http, or server entrypoints", () => {
|
|
const coreFiles = collectTsFiles(join(SRC_ROOT, "core"));
|
|
const violations: string[] = [];
|
|
|
|
for (const file of coreFiles) {
|
|
const content = readFileSync(file, "utf8");
|
|
const specifiers = extractImportSpecifiers(content);
|
|
for (const spec of specifiers) {
|
|
const targetRel = resolveImportRelativeToSrc(file, spec);
|
|
if (
|
|
targetRel &&
|
|
(targetRel.startsWith("domains/") ||
|
|
targetRel.startsWith("cli/") ||
|
|
targetRel.startsWith("http/") ||
|
|
targetRel === "server" ||
|
|
targetRel === "cli" ||
|
|
targetRel === "aipro-cli")
|
|
) {
|
|
violations.push(`${relative(SRC_ROOT, file)} -> ${targetRel}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
expect(violations).toEqual([]);
|
|
});
|
|
|
|
test("src/core/ must only contain whitelisted infrastructure modules", () => {
|
|
const allowedTopLevel = new Set([
|
|
"README.core.md",
|
|
"auth",
|
|
"cache",
|
|
"config",
|
|
"db",
|
|
"errors.ts",
|
|
"http",
|
|
]);
|
|
const entries = readdirSync(join(SRC_ROOT, "core"));
|
|
const unexpected = entries.filter((entry) => !allowedTopLevel.has(entry));
|
|
expect(unexpected).toEqual([]);
|
|
});
|
|
});
|