Files
faunosteo/apps/app/tests/unit/r5.census.test.ts
T

347 lines
13 KiB
TypeScript

/**
* R5.7 census (plan 2026-10-25, RUST.md §11.23): the anatomical pipeline and the
* sheet, report and archive rules run in the Rust core only; `packages/anatomy`
* and `packages/domain` are gone and nothing reaches them again. Grep facts over
* the repository, and the two disposition matrices of the deletion checked
* against the tree, so that a reintroduced package, dependency, import, script or
* a ledger target that no longer exists fails here.
*/
import { createHash } from 'node:crypto'
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs'
import { dirname, join, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vitest'
const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '../../../..')
const read = (path: string) => readFileSync(join(ROOT, path), 'utf8')
const exists = (path: string) => existsSync(join(ROOT, path))
const sha256 = (path: string) =>
createHash('sha256')
.update(readFileSync(join(ROOT, path)))
.digest('hex')
const DELETED_PACKAGES = ['packages/anatomy', 'packages/domain']
const BANNED_SPECIFIERS = [
'@faunosteo/anatomy',
'@faunosteo/domain',
'@xmldom/xmldom',
]
const BANNED_DEPENDENCIES = [...BANNED_SPECIFIERS, 'tsx']
/** Source files under `dir` (relative to the root), node_modules excluded. */
function sources(dir: string, pattern = /\.(tsx?|mjs|js)$/): string[] {
const out: string[] = []
const walk = (at: string) => {
for (const entry of readdirSync(join(ROOT, at))) {
if (entry === 'node_modules') continue
const path = `${at}/${entry}`
if (statSync(join(ROOT, path)).isDirectory()) walk(path)
else if (pattern.test(entry)) out.push(path)
}
}
walk(dir)
return out
}
/** Static, dynamic and `require` module specifiers of a source text. */
const specifiersOf = (text: string): string[] =>
[
...text.matchAll(/\bfrom\s+['"]([^'"]+)['"]/g),
...text.matchAll(/\bimport\s*\(\s*['"]([^'"]+)['"]/g),
...text.matchAll(/\brequire\s*\(\s*['"]([^'"]+)['"]/g),
].map((m) => m[1]!)
const isBanned = (spec: string) =>
BANNED_SPECIFIERS.some((b) => spec === b || spec.startsWith(`${b}/`)) ||
DELETED_PACKAGES.some((p) => spec.includes(p)) ||
/(^|\/)(anatomy|domain)\/src(\/|$)/.test(spec)
type Manifest = Record<string, Record<string, string> | undefined>
/** Every workspace manifest: the root, `apps/*`, `packages/*` (with a manifest). */
function manifests(): string[] {
const found = ['package.json']
for (const group of ['apps', 'packages']) {
for (const entry of readdirSync(join(ROOT, group))) {
const path = `${group}/${entry}/package.json`
if (exists(path)) found.push(path)
}
}
return found.sort()
}
/** `pnpm-lock.yaml` importers (name → block) and the `packages:` keys. */
function lockfile(): { importers: Map<string, string>; packages: string[] } {
const lock = read('pnpm-lock.yaml')
const start = lock.indexOf('\nimporters:\n')
const end = lock.indexOf('\npackages:\n')
const section = lock.slice(start, end)
const importers = new Map<string, string>()
for (const block of section.split(/\n(?= [^ \n][^\n]*:\n)/).slice(1)) {
importers.set(block.slice(2, block.indexOf(':\n')), block)
}
const after = lock.slice(end + '\npackages:\n'.length)
const packages = [
...after
.slice(0, after.indexOf('\nsnapshots:\n'))
.matchAll(/^ {2}'?([^\s'][^\n]*?)'?:$/gm),
].map((m) => m[1]!)
return { importers, packages }
}
/** Data rows of a committed TSV, checked against its header. */
function tsv(path: string, header: string[]): string[][] {
const [head, ...rows] = read(path).trimEnd().split('\n')
expect(head!.split('\t'), path).toEqual(header)
return rows.map((row) => {
const cells = row.split('\t')
expect(cells.length, `${path}: ${row}`).toBe(header.length)
return cells
})
}
/** The file a ledger target names: `cargo <binary>::<path>::<test>`. */
function cargoTargetFile(binary: string): string[] {
if (binary === 'faunosteo_core') return sources('crates/core/src', /\.rs$/)
if (['import_equine', 'report_compare'].includes(binary)) {
return [`crates/core/src/bin/${binary}.rs`]
}
if (binary === 'upload_sheet_assets') {
return sources('crates/core/src/bin/upload_sheet_assets', /\.rs$/)
}
return [`crates/core/tests/${binary}.rs`]
}
describe('the TypeScript pipeline and domain packages are gone', () => {
it('packages/anatomy, packages/domain and the TS upload script no longer exist', () => {
// A folder may linger with a git-ignored node_modules of a stale install.
for (const dir of DELETED_PACKAGES) {
for (const part of ['package.json', 'src', 'tests']) {
expect(exists(`${dir}/${part}`), `${dir}/${part}`).toBe(false)
}
}
expect(exists('packages/supabase/scripts/upload-sheet-assets.ts')).toBe(
false,
)
})
it('no workspace manifest depends on them, and the scripts run the Rust tools', () => {
const found = manifests()
for (const path of [
'package.json',
'apps/app/package.json',
'packages/core-wasm/package.json',
'packages/supabase/package.json',
]) {
expect(found).toContain(path)
}
for (const path of found) {
const pkg = JSON.parse(read(path)) as Manifest
const names = Object.keys({
...pkg.dependencies,
...pkg.devDependencies,
...pkg.peerDependencies,
...pkg.optionalDependencies,
})
for (const banned of BANNED_DEPENDENCIES) {
expect(names, path).not.toContain(banned)
}
for (const [name, script] of Object.entries(pkg.scripts ?? {})) {
expect(name, path).not.toMatch(/:legacy$/)
expect(script, `${path} ${name}`).not.toMatch(
/@faunosteo\/(anatomy|domain)|packages\/(anatomy|domain)|\btsx\b/,
)
}
}
const root = JSON.parse(read('package.json')) as Manifest
expect(root.scripts?.['import:equine']).toBe(
'bash scripts/import-equine.sh',
)
expect(root.scripts?.['r5:import-equine']).toBe(
'bash scripts/import-equine.sh',
)
expect(root.scripts?.['assets:upload']).toBe(
'bash scripts/upload-sheet-assets.sh',
)
expect(read('scripts/import-equine.sh')).toContain(
'--features curation --bin import-equine',
)
expect(read('scripts/upload-sheet-assets.sh')).toContain(
'--bin upload-sheet-assets',
)
})
it('the lockfile has one importer per manifest and resolves none of them', () => {
const { importers, packages } = lockfile()
const dirs = manifests().map((path) =>
path === 'package.json' ? '.' : path.slice(0, -'/package.json'.length),
)
// Non-vacuity: the parser sees exactly the workspace the manifests declare.
expect([...importers.keys()].sort()).toEqual(dirs.sort())
expect(packages.length).toBeGreaterThan(100)
expect(packages.some((key) => key.startsWith('zod@'))).toBe(true)
for (const [name, block] of importers) {
for (const banned of BANNED_DEPENDENCIES) {
expect(block, `${name}: ${banned}`).not.toMatch(
new RegExp(`^ {6}'?${banned.replace('/', '\\/')}'?:$`, 'm'),
)
}
}
for (const banned of BANNED_SPECIFIERS) {
expect(
packages.filter((key) => key.startsWith(`${banned}@`)),
banned,
).toEqual([])
}
// `tsx` left with the pipeline's CLI; it stays resolved only as the optional peer of vite,
// which pnpm marks `optional: true` (`pnpm dedupe --check` clean, RUST.md §11.23).
const lock = read('pnpm-lock.yaml')
const snapshots = lock.slice(lock.indexOf('\nsnapshots:\n'))
const tsx = snapshots.split('\n\n').filter((b) => /^ {2}tsx@/.test(b))
for (const block of tsx) expect(block).toMatch(/^ {4}optional: true$/m)
})
})
describe('no production path reaches a deleted engine', () => {
it('the application, the glue, the Supabase package and their configs import none of them', () => {
const files = [
...sources('apps/app/src'),
...sources('apps/app/vite'),
'apps/app/vite.config.ts',
'apps/app/vitest.config.ts',
'apps/app/eslint.config.js',
...sources('packages/core-wasm/src'),
...sources('packages/supabase/src'),
]
// Non-vacuity: the specifier scan finds the imports these sources do have.
expect(files.length).toBeGreaterThan(100)
const specs = files.flatMap((file) => specifiersOf(read(file)))
expect(specs).toContain('@faunosteo/core-wasm')
for (const file of files) {
expect(specifiersOf(read(file)).filter(isBanned), file).toEqual([])
}
const tsconfig = read('apps/app/tsconfig.json')
expect(tsconfig).not.toMatch(/packages\/(anatomy|domain)/)
})
it('the Dockerfile copies only manifests that exist', () => {
const dockerfile = read('Dockerfile')
expect(dockerfile).not.toMatch(/packages\/(anatomy|domain)/)
const copied = [
...dockerfile.matchAll(/^COPY (\S+\/package\.json) /gm),
].map((m) => m[1]!)
expect(copied).toContain('packages/core-wasm/package.json')
expect(copied).toContain('packages/supabase/package.json')
for (const path of copied) expect(exists(path), path).toBe(true)
})
it('the application lint rule keeps the deleted packages out', () => {
const config = read('apps/app/eslint.config.js')
for (const name of BANNED_SPECIFIERS) {
expect(config).toContain(`name: '${name}'`)
}
})
})
describe('the disposition of the deleted files and tests', () => {
it('every deleted file is absent and its counterpart exists', () => {
const rows = tsv('crates/core/tests/curation/FILES.tsv', [
'ts_file',
'disposition',
'counterpart',
'note',
])
expect(rows.length).toBeGreaterThan(80)
for (const [file, disposition, counterpart] of rows) {
expect(file, file).toMatch(/^packages\/(anatomy|domain|supabase)\//)
expect(exists(file!), file).toBe(false)
expect(['ported', 'replaced', 'deleted'], file).toContain(disposition)
if (disposition === 'deleted') continue
for (const path of counterpart!.split(', ')) {
expect(exists(path), `${file} → ${path}`).toBe(true)
}
}
})
it('every TS case of the two packages has a disposition and a live target', () => {
const rows = tsv('crates/core/tests/curation/LEDGER.tsv', [
'ts_file',
'case_title',
'contract',
'disposition',
'target',
'oracle',
'note',
])
const per = (dir: string) =>
rows.filter(([file]) => file!.startsWith(dir)).length
expect(rows.length).toBe(407)
expect(per('packages/anatomy/tests/')).toBe(144)
expect(per('packages/domain/tests/')).toBe(263)
for (const [file, title, , disposition, target, oracle] of rows) {
const id = `${file} > ${title}`
expect(exists(file!), id).toBe(false)
expect(['ported', 'replaced', 'kept', 'deleted'], id).toContain(
disposition,
)
for (const path of oracle === '-' ? [] : oracle!.split(', ')) {
expect(exists(path), `${id}: ${path}`).toBe(true)
}
if (disposition === 'deleted') continue
expect(target, id).not.toBe('-')
for (const part of target!.split(' ; ')) {
const cargo =
/^cargo ([a-z0-9_]+)::(?:[a-z0-9_]+::)*([a-z0-9_]+)$/.exec(part)
if (cargo) {
const [, binary, test] = cargo
const files = cargoTargetFile(binary!)
// A `#[test]` function of that name, not any `fn` (attributes and doc lines between),
// and never an `#[ignore]` one: `cargo test` would skip it and the row would be passive.
const testFn = new RegExp(
`#\\[test\\]\\s*(?:(?:#\\[(?!ignore)[^\\]]*\\]|///[^\\n]*)\\s*)*fn ${test}\\(`,
)
const defined = files.some(
(path) => exists(path) && testFn.test(read(path)),
)
expect(defined, `${id}: ${part}`).toBe(true)
continue
}
const vitest = /^vitest (\S+) > (.+)$/.exec(part)
if (vitest) {
expect(exists(vitest[1]!), `${id}: ${part}`).toBe(true)
continue
}
expect(part, id).toBe('import-equine --check')
}
}
})
})
describe('the historical references stay byte for byte', () => {
it('the moved report fixtures keep their sha256', () => {
expect(sha256('crates/core/tests/fixtures/report-v2.full.json')).toBe(
'e96b088282b5d20bd5ed925ea980c1faca86cf1f87eb500201834dda714b729c',
)
expect(sha256('crates/core/tests/fixtures/report-v2.minimal.json')).toBe(
'ba79698c5691dd89f8c6a3ada5a9c6ceb6ea41a85e64596e3eec7399c33cfde7',
)
})
it('the oracles, the pre-switch seed files and the N-1 readers stay', () => {
for (const path of [
'crates/core/tests/oracles/sheet.draft.json',
'crates/core/tests/oracles/migration-map.draft.json',
'crates/core/tests/oracles/r3/highlight.json',
'crates/core/tests/oracles/r5/pipeline.json',
'crates/core/tests/oracles/r5/ids.json',
'crates/core/tests/oracles/r5/fingerprints.json',
'crates/core/tests/fixtures/r5-6/pre-switch/sheet-ref.json',
'crates/core/tests/fixtures/r5-6/pre-switch/report-migration-map.json',
'crates/core/tests/fixtures/horse-slice.json',
'packages/core-wasm/tests/n-1/domain',
]) {
expect(exists(path), path).toBe(true)
}
})
})