Files
reverse-engineering-anything/src/domain/analysisView/analysisViewCycles.test.ts
T
morlutoandCursor Agent 249b90c5e3 refactor(javascript): break the semantic analysis import cycle (#8063)
* refactor(javascript): break the semantic analysis import cycle

Split shared semantic facts out of the analysis hub so projectors of
effects, evaluation, and identity no longer import one another in a cycle.

* refactor(javascript): share the import specifier reader

Read static imports in one module used by the boundary checker and both
cycle guards, so those rules cannot drift apart.

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
2026-10-30 06:07:49 +08:00

117 lines
3.6 KiB
TypeScript

import { readdirSync, readFileSync, statSync } from "node:fs";
import { dirname, join, relative, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { describe, expect, it } from "vitest";
import { readImportSpecifiers } from "../../../scripts/lib/import-specifiers.mjs";
const root = dirname(fileURLToPath(import.meta.url));
const relativeSpecifiers = (source: string): readonly string[] =>
readImportSpecifiers(source)
.map(({ specifier }) => specifier)
.filter((specifier) => specifier.startsWith("."));
/**
* Cycles that already existed outside the analysis-view hub. Membership is
* exact so a new edge cannot hide inside the allow-list.
*/
const allowedCycles = [
[
"StateRecordChronology.ts",
"environmentStateListQuery.ts",
"environmentStateRules.ts",
"environmentStateWriteRules.ts",
],
["environmentStateGate.ts", "environmentStateListFacts.ts"],
["environmentStateWriteAddress.ts", "environmentStateWriteEffects.ts"],
];
const productionFiles = (directory: string): string[] => {
const files: string[] = [];
for (const name of readdirSync(directory)) {
const path = join(directory, name);
if (statSync(path).isDirectory()) files.push(...productionFiles(path));
else if (name.endsWith(".ts") && !name.includes(".test.")) files.push(path);
}
return files;
};
const importGraph = (): Map<string, Set<string>> => {
const files = new Set(
productionFiles(root).map((path) => relative(root, path)),
);
const graph = new Map<string, Set<string>>();
for (const file of files) {
const source = readFileSync(join(root, file), "utf8");
const edges = new Set<string>();
for (const spec of relativeSpecifiers(source)) {
const resolved = resolve(dirname(join(root, file)), spec).replace(
/\.js$/,
".ts",
);
const target = relative(root, resolved);
if (files.has(target)) edges.add(target);
}
graph.set(file, edges);
}
return graph;
};
const stronglyConnected = (
graph: Map<string, Set<string>>,
): readonly (readonly string[])[] => {
let index = 0;
const stack: string[] = [];
const onStack = new Set<string>();
const indices = new Map<string, number>();
const low = new Map<string, number>();
const components: string[][] = [];
const visit = (node: string): void => {
indices.set(node, index);
low.set(node, index);
index += 1;
stack.push(node);
onStack.add(node);
for (const next of graph.get(node) ?? []) {
if (!indices.has(next)) {
visit(next);
low.set(node, Math.min(low.get(node) ?? index, low.get(next) ?? index));
} else if (onStack.has(next)) {
low.set(
node,
Math.min(low.get(node) ?? index, indices.get(next) ?? index),
);
}
}
if (low.get(node) === indices.get(node)) {
const component: string[] = [];
let member = "";
while (member !== node) {
member = stack.pop() ?? node;
onStack.delete(member);
component.push(member);
}
if (component.length > 1) components.push(component.sort());
}
};
for (const node of [...graph.keys()].sort()) {
if (!indices.has(node)) visit(node);
}
return components.sort(
(left, right) =>
right.length - left.length || left.join().localeCompare(right.join()),
);
};
describe("analysis view import cycles", () => {
it("keeps projectors from importing the request and result hub", () => {
const cycles = stronglyConnected(importGraph()).map((cycle) =>
cycle.join("\n"),
);
expect(cycles).toEqual(
allowedCycles.map((cycle) => [...cycle].sort().join("\n")),
);
});
});