Files
Reims-Go/frontend/scripts/test-import-cycles.mjs
T
NwflowerandClaude Opus 5.5 5df61df5b9 test(frontend): reject import cycles
The theme engine and the interactive UI SDK imported each other's types; the
SDK now owns the types of its own interface.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 22:02:53 -07:00

46 lines
1.7 KiB
JavaScript

// Rejects import cycles in src/, type imports included: two files that import
// each other are one module split in the wrong place.
import test from 'node:test'
import assert from 'node:assert/strict'
import fs from 'node:fs'
import path from 'node:path'
import ts from 'typescript'
const src = path.resolve(import.meta.dirname, '..', 'src')
const rel = (file) => path.relative(src, file).split(path.sep).join('/')
function importGraph() {
const files = fs.readdirSync(src, { recursive: true })
.filter((name) => /\.tsx?$/.test(name))
.map((name) => path.join(src, name))
const known = new Set(files)
return new Map(files.map((file) => {
const { importedFiles } = ts.preProcessFile(fs.readFileSync(file, 'utf8'), true, true)
const targets = importedFiles
.filter(({ fileName }) => fileName.startsWith('.'))
.map(({ fileName }) => path.resolve(path.dirname(file), fileName))
.map((base) => ['', '.ts', '.tsx', `${path.sep}index.ts`].map((ext) => base + ext).find((p) => known.has(p)))
.filter(Boolean)
return [file, targets]
}))
}
test('source modules do not import each other in a cycle', () => {
const graph = importGraph()
assert.ok(graph.size > 50, `expected to scan src/, found ${graph.size} files`)
const done = new Set()
const cycles = []
const visit = (file, trail) => {
const start = trail.indexOf(file)
if (start >= 0) {
cycles.push([...trail.slice(start), file].map(rel).join(' -> '))
return
}
if (done.has(file)) return
for (const next of graph.get(file)) visit(next, [...trail, file])
done.add(file)
}
for (const file of graph.keys()) visit(file, [])
assert.deepEqual(cycles, [])
})