| 1 | import assert from "node:assert/strict"; |
| 2 | import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from "node:fs"; |
| 3 | import { tmpdir } from "node:os"; |
| 4 | import { join, dirname } from "node:path"; |
| 5 | import ts from "typescript"; |
| 6 | import { checkAppLayers, moduleEdges } from "./check-app-layers.mjs"; |
| 7 | |
| 8 | const fixture = mkdtempSync(join(tmpdir(), "reasonix-app-layers-")); |
| 9 | const options = { moduleResolution: ts.ModuleResolutionKind.Bundler, baseUrl: fixture, paths: { "@/*": ["*"] } }; |
| 10 | const write = (name, source) => { |
| 11 | const file = join(fixture, name); |
| 12 | mkdirSync(dirname(file), { recursive: true }); |
| 13 | writeFileSync(file, source); |
| 14 | }; |
| 15 | try { |
| 16 | const parsed = moduleEdges(` |
| 17 | // import React from 'react'; |
| 18 | import type { ReactNode } from 'react'; |
| 19 | import { type Config } from './types'; |
| 20 | export { type Target } from './types'; |
| 21 | export * from './runtime'; |
| 22 | const later = () => import('./lazy'); |
| 23 | `, "fixture.ts"); |
| 24 | assert.deepEqual(parsed.edges.map((edge) => [edge.specifier, edge.typeOnly]), [ |
| 25 | ["react", true], ["./types", true], ["./types", true], ["./runtime", false], ["./lazy", false], |
| 26 | ]); |
| 27 | write("app-domain/owner.ts", "export { run } from '@/lib/middle';"); |
| 28 | write("lib/middle.ts", "export const run = () => import('./leaf');"); |
| 29 | write("lib/leaf.ts", "import React from 'react'; export const value = React;"); |
| 30 | assert.ok(checkAppLayers(fixture, options).some((failure) => failure.includes("domain reaches presentation")), |
| 31 | "alias, re-export and lazy edges cannot conceal a transitive React dependency"); |
| 32 | write("lib/leaf.ts", "export const value = document.title;"); |
| 33 | assert.ok(checkAppLayers(fixture, options).some((failure) => failure.includes("domain reaches DOM"))); |
| 34 | write("lib/leaf.ts", "export interface Size { window: number }; export const value = 1;"); |
| 35 | assert.deepEqual(checkAppLayers(fixture, options), [], "DTO field names are not browser runtime references"); |
| 36 | write("lib/leaf.ts", "import type { ReactNode } from 'react'; export const value = 1;"); |
| 37 | assert.deepEqual(checkAppLayers(fixture, options), []); |
| 38 | write("lib/leaf.ts", "export const load = (name: string) => import(name);"); |
| 39 | assert.ok(checkAppLayers(fixture, options).some((failure) => failure.includes("unresolvable runtime dependency"))); |
| 40 | write("lib/leaf.ts", "export const value = 1;"); |
| 41 | write("app-shell/Region.tsx", "export { run } from './wrapper';"); |
| 42 | write("app-shell/wrapper.ts", "export { app as run } from '@/lib/bridge';"); |
| 43 | write("lib/bridge.ts", "export const app = {};"); |
| 44 | assert.ok(checkAppLayers(fixture, options).some((failure) => failure.includes("presentation reaches bridge"))); |
| 45 | write("app-shell/wrapper.ts", "export const run = 1;"); |
| 46 | write("lib/useCommittedSlot.ts", "export * from '../app-runtime/adapter';"); |
| 47 | write("app-runtime/adapter.ts", "export const value = 1;"); |
| 48 | assert.ok(checkAppLayers(fixture, options).some((failure) => failure.includes("shared primitive reaches App"))); |
| 49 | console.log("PASS AST layer checks resolve runtime edges and reject transitive boundary violations"); |
| 50 | } finally { |
| 51 | rmSync(fixture, { recursive: true, force: true }); |
| 52 | } |
| 53 |