brainy/tests/unit/boundary-no-cortex.test.ts

57 lines
3 KiB
TypeScript
Raw Normal View History

/**
* @module tests/unit/boundary-no-cortex
* @description Enforces the open-source / proprietary boundary in CI: the public
* MIT Brainy repo must never DEPEND ON the proprietary `@soulcraft/cortex`
* package to build or test. Cortex is the one product Brainy may NAME (docs say
* `npm install @soulcraft/cortex`, error messages point at its `idSpace:'u64'`
* mode), and registering it through the public plugin API is the supported path
* but nothing in `src/` or `tests/` may statically `import`/`require` it, and it
* must not appear in any dependency field. This guards against someone turning an
* optional integration into a hard coupling that silently makes green CI depend
* on a private build. See the perf-comparison boundary decision (handoff AJ/AK).
*/
import { describe, it, expect } from 'vitest'
import * as fs from 'node:fs'
import * as path from 'node:path'
import { fileURLToPath } from 'node:url'
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..')
const SELF = fileURLToPath(import.meta.url)
/** Recursively collect .ts/.js/.mjs/.cjs files under a directory. */
function sourceFiles(dir: string): string[] {
const out: string[] = []
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
if (entry.name === 'node_modules' || entry.name === 'dist' || entry.name.startsWith('.')) continue
const full = path.join(dir, entry.name)
if (entry.isDirectory()) out.push(...sourceFiles(full))
else if (/\.(ts|js|mjs|cjs)$/.test(entry.name) && full !== SELF) out.push(full)
}
return out
}
// Matches a real module specifier for cortex in an import/require/dynamic-import —
// NOT a bare string mention (docs/JSDoc/error-message/mock-name are allowed).
const CORTEX_MODULE = /(?:import\s[^'"]*from\s*|import\s*\(\s*|require\s*\(\s*)['"]@soulcraft\/cortex(?:\/[^'"]*)?['"]/
describe('open/proprietary boundary — public repo must not depend on @soulcraft/cortex', () => {
it('no src/ or tests/ file imports or requires @soulcraft/cortex', () => {
const offenders: string[] = []
for (const dir of ['src', 'tests']) {
for (const file of sourceFiles(path.join(repoRoot, dir))) {
if (CORTEX_MODULE.test(fs.readFileSync(file, 'utf8'))) {
offenders.push(path.relative(repoRoot, file))
}
}
}
expect(offenders, `These files statically depend on the proprietary package — use the public plugin API at runtime instead:\n${offenders.join('\n')}`).toEqual([])
})
it('@soulcraft/cortex is in no dependency field of package.json', () => {
const pkg = JSON.parse(fs.readFileSync(path.join(repoRoot, 'package.json'), 'utf8'))
const fields = ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies'] as const
const present = fields.filter((f) => pkg[f] && Object.prototype.hasOwnProperty.call(pkg[f], '@soulcraft/cortex'))
expect(present, `@soulcraft/cortex must not be a dependency (public CI must pass with it absent); found in: ${present.join(', ')}`).toEqual([])
})
})