106 lines
3.8 KiB
TypeScript
106 lines
3.8 KiB
TypeScript
/// <reference types="node" />
|
|
import { readFileSync, readdirSync } from "node:fs";
|
|
import { dirname, join, resolve } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
const PROJECT_ROOT = resolve(fileURLToPath(new URL(".", import.meta.url)), "../..");
|
|
const SRC_DIR = join(PROJECT_ROOT, "src");
|
|
const LIGHTCLOCK_MODULE = resolve(SRC_DIR, "lib/lightclock.ts");
|
|
const LIGHTCLOCK_UI_MODULE = resolve(SRC_DIR, "miniapps/lightclock/LightClockScreen.tsx");
|
|
const ALLOCATOR_ROOT_MODULES = [
|
|
resolve(SRC_DIR, "lib/fairshare.ts"),
|
|
resolve(SRC_DIR, "lib/ceeiSpike.ts"),
|
|
];
|
|
|
|
const SOURCE_FILE_PATTERN = /\.(ts|tsx)$/;
|
|
const TEST_FILE_PATTERN = /\.(test|spec)\.[jt]sx?$/;
|
|
const IMPORT_FROM_PATTERN = /from\s+["'](\.[^"']*)["']/g;
|
|
|
|
function listSourceFiles(dir: string): string[] {
|
|
const out: string[] = [];
|
|
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
const full = join(dir, entry.name);
|
|
if (entry.isDirectory()) {
|
|
out.push(...listSourceFiles(full));
|
|
} else if (SOURCE_FILE_PATTERN.test(entry.name) && !TEST_FILE_PATTERN.test(entry.name)) {
|
|
out.push(full);
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function resolveImportTarget(fromFile: string, specifier: string): string | null {
|
|
const base = resolve(dirname(fromFile), specifier);
|
|
for (const candidate of [base, `${base}.ts`, `${base}.tsx`, `${base}/index.ts`, `${base}/index.tsx`]) {
|
|
try {
|
|
readFileSync(candidate);
|
|
return candidate;
|
|
} catch {
|
|
continue;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function buildImportGraph(files: string[]): Map<string, string[]> {
|
|
const graph = new Map<string, string[]>();
|
|
for (const file of files) {
|
|
const source = readFileSync(file, "utf8");
|
|
const targets: string[] = [];
|
|
for (const match of source.matchAll(IMPORT_FROM_PATTERN)) {
|
|
const target = resolveImportTarget(file, match[1]);
|
|
if (target) targets.push(target);
|
|
}
|
|
graph.set(file, targets);
|
|
}
|
|
return graph;
|
|
}
|
|
|
|
function reachableFrom(graph: Map<string, string[]>, root: string): Set<string> {
|
|
const visited = new Set<string>();
|
|
const stack = [root];
|
|
while (stack.length > 0) {
|
|
const current = stack.pop();
|
|
if (!current || visited.has(current)) continue;
|
|
visited.add(current);
|
|
for (const next of graph.get(current) ?? []) {
|
|
if (!visited.has(next)) stack.push(next);
|
|
}
|
|
}
|
|
return visited;
|
|
}
|
|
|
|
describe("Light Clock / allocator import-graph isolation (plan todo 44, extended by todo 46)", () => {
|
|
const allSourceFiles = listSourceFiles(SRC_DIR);
|
|
const importGraph = buildImportGraph(allSourceFiles);
|
|
|
|
it("no allocator module (Fair Share's fairshare.ts/ceeiSpike.ts, or anything they import) reaches the Light Clock engine", () => {
|
|
for (const allocatorRoot of ALLOCATOR_ROOT_MODULES) {
|
|
const reachable = reachableFrom(importGraph, allocatorRoot);
|
|
expect(reachable.has(LIGHTCLOCK_MODULE)).toBe(false);
|
|
}
|
|
});
|
|
|
|
it("the Light Clock engine does not import any allocator module, or anything that imports one", () => {
|
|
const reachableFromLightclock = reachableFrom(importGraph, LIGHTCLOCK_MODULE);
|
|
for (const allocatorRoot of ALLOCATOR_ROOT_MODULES) {
|
|
expect(reachableFromLightclock.has(allocatorRoot)).toBe(false);
|
|
}
|
|
});
|
|
|
|
it("no allocator module reaches the Light Clock wellness UI module (plan todo 46)", () => {
|
|
for (const allocatorRoot of ALLOCATOR_ROOT_MODULES) {
|
|
const reachable = reachableFrom(importGraph, allocatorRoot);
|
|
expect(reachable.has(LIGHTCLOCK_UI_MODULE)).toBe(false);
|
|
}
|
|
});
|
|
|
|
it("the Light Clock wellness UI module does not import any allocator module, or anything that imports one", () => {
|
|
const reachableFromUi = reachableFrom(importGraph, LIGHTCLOCK_UI_MODULE);
|
|
for (const allocatorRoot of ALLOCATOR_ROOT_MODULES) {
|
|
expect(reachableFromUi.has(allocatorRoot)).toBe(false);
|
|
}
|
|
});
|
|
});
|