const test = require("node:test"); const assert = require("node:assert"); const { readdirSync, readFileSync } = require("node:fs"); const { dirname, extname, join, relative, resolve, sep } = require("node:path"); const gateRoot = resolve(__dirname, ".."); const domainRoots = ["disambiguation", "memory"]; function productionFiles(root) { return readdirSync(root, { withFileTypes: true }).flatMap((entry) => { const path = join(root, entry.name); if (entry.isDirectory()) { return entry.name === "__tests__" ? [] : productionFiles(path); } return [".js", ".mjs"].includes(extname(entry.name)) ? [path] : []; }); } function relativeImports(path) { const source = readFileSync(path, "utf8"); return [...source.matchAll(/(?:from\s+|import\s*(?:\(\s*)?|require\s*\(\s*)["'](\.[^"']+)["']/g)] .map((match) => match[1]); } test("gate domain modules receive shared behavior as capabilities", () => { for (const domain of domainRoots) { const root = join(gateRoot, domain); for (const path of productionFiles(root)) { const imports = relativeImports(path).map((specifier) => relative(gateRoot, resolve(dirname(path), specifier)).split(sep).join("/")); const violations = imports.filter((target) => target !== domain && !target.startsWith(`${domain}/`)); assert.deepEqual( violations, [], `${relative(gateRoot, path)} imports gate implementation directly: ${violations.join(", ")}`, ); } } });