droboticflow/tests/frontend/frontend-architecture.test.mjs
2026-10-21 23:48:17 +08:00

110 lines
4.3 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import { checkFrontendArchitecture } from "../../scripts/check-frontend-architecture.mjs";
const presenter = "src/features/annotation-output/DashboardPresentation.tsx";
const protocol = "src/lib/review-draft-persistence.js";
const check = (overrides) =>
checkFrontendArchitecture({
read: (file) =>
Object.hasOwn(overrides, file)
? overrides[file]
: fs.readFileSync(new URL(`../../${file}`, import.meta.url), "utf8"),
exists: (file) => Object.hasOwn(overrides, file) || fs.existsSync(new URL(`../../${file}`, import.meta.url)),
});
test("current presentation and protocol graphs satisfy their responsibility rules", () =>
assert.deepEqual(check({}), []));
test("presentation cannot import data flow, business API, alias or external transport", () => {
for (const module of [
"./useAnnotationOutputData",
"../../services/api/client",
"@/src/services/api/client",
"axios",
]) {
const issues = check({ [presenter]: `import x from '${module}';` });
assert.equal(issues.length, 1);
assert.equal(issues[0].file, presenter);
}
});
test("protocol rules trace indirect helpers and report the violating import chain", () => {
const helper = "src/lib/synthetic-helper.js";
const issues = check({
[protocol]: "import './synthetic-helper.js'",
[helper]: "export {APIError} from '../services/api/client'",
});
assert.equal(issues.length, 1);
assert.deepEqual(issues[0].chain, [protocol, helper]);
assert.equal(issues[0].line, 1);
assert.equal(
check({ [protocol]: "import './synthetic-helper.js'", [helper]: "import React from 'react'" }).length,
1,
);
});
test("dynamic, CommonJS, export and type imports cannot bypass dependency checks", () => {
for (const source of [
"import('../services/api/client')",
"require('../services/api/client')",
"export * from '../services/api/client'",
"type T = import('../services/api/client').APIError",
"import client = require('../services/api/client')",
"import(name)",
"require(name)",
])
assert.equal(check({ [protocol]: source }).length, 1, source);
});
test("comments and ordinary strings are not dependencies; local cycles terminate", () => {
const helper = "src/lib/synthetic-helper.js";
assert.deepEqual(check({ [protocol]: "// import React from 'react'\nconst message = \"require('axios')\";" }), []);
assert.deepEqual(
check({ [protocol]: "import './synthetic-helper.js'", [helper]: "export * from './review-draft-persistence.js'" }),
[],
);
});
test("missing protected modules and unresolved helpers fail closed", () => {
assert.equal(check({ [protocol]: "import './missing-protocol-helper.js'" }).length, 1);
const issues = checkFrontendArchitecture({
read: () => {
throw Error("missing");
},
exists: () => false,
});
assert.ok(issues.length > 0);
assert.ok(issues.every((issue) => issue.message.includes("unreadable")));
});
test("malformed protected source cannot silently pass the import check", () => {
const issues = check({ [protocol]: "import {" });
assert.equal(issues.length, 1);
assert.match(issues[0].message, /Cannot parse/);
});
test("ambiguous module extensions cannot hide a different bundler dependency", () => {
const issues = check({
[protocol]: "import './ambiguous-helper'",
"src/lib/ambiguous-helper.ts": "export {}",
"src/lib/ambiguous-helper.js": "import React from 'react'",
});
assert.equal(issues.length, 1);
assert.match(issues[0].message, /Ambiguous/);
});
test("parenthesized require, module.require and Vite globs cannot skip dependency rules", () => {
for (const source of [
"(require)('../services/api/client')",
"module.require('../services/api/client')",
"import.meta.glob('../services/api/*.ts')",
"import.meta.globEager('../services/api/*.ts')",
])
assert.equal(check({ [protocol]: source }).length, 1, source);
});
test("JSX helpers cannot introduce implicit React runtime imports into protocols", () => {
for (const source of [
"export const view = <div />",
"export const view = <div>text</div>",
"export const view = <>text</>",
]) {
const issues = check({ [protocol]: "import './jsx-helper.tsx'", "src/lib/jsx-helper.tsx": source });
assert.equal(issues.length, 1);
assert.match(issues[0].message, /JSX/);
}
});