|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// ADR-0076 D2 boundary ratchet. The lean engine entry `@objectstack/objectql/core` |
| 4 | +// (src/core.ts) and its entire local import closure must NOT depend on the kernel |
| 5 | +// plugin, the kernel factory, or the metadata-management protocol — so a thin |
| 6 | +// embedder importing `@objectstack/objectql/core` never pulls |
| 7 | +// `@objectstack/metadata-protocol` (or its 268KB) into its graph. |
| 8 | +// |
| 9 | +// If this test fails, you added a forbidden import somewhere reachable from |
| 10 | +// core.ts. Keep metadata/plugin/kernel concerns out of the core closure. |
| 11 | + |
| 12 | +import { describe, it, expect } from 'vitest'; |
| 13 | +import { readFileSync } from 'node:fs'; |
| 14 | +import { dirname, resolve } from 'node:path'; |
| 15 | +import { fileURLToPath } from 'node:url'; |
| 16 | + |
| 17 | +const SRC = dirname(fileURLToPath(import.meta.url)); |
| 18 | + |
| 19 | +const FORBIDDEN_PACKAGES = ['@objectstack/metadata-protocol']; |
| 20 | +const FORBIDDEN_LOCAL = ['plugin', 'kernel-factory']; |
| 21 | + |
| 22 | +function localImports(source: string): string[] { |
| 23 | + const out: string[] = []; |
| 24 | + const re = /(?:from|import)\s*\(?\s*['"](\.\.?\/[^'"]+)['"]/g; |
| 25 | + let m: RegExpExecArray | null; |
| 26 | + while ((m = re.exec(source))) out.push(m[1]); |
| 27 | + return out; |
| 28 | +} |
| 29 | + |
| 30 | +function toTsPath(fromFile: string, spec: string): string { |
| 31 | + const base = resolve(dirname(fromFile), spec.replace(/\.js$/, '')); |
| 32 | + return base.endsWith('.ts') ? base : `${base}.ts`; |
| 33 | +} |
| 34 | + |
| 35 | +describe('ADR-0076 D2 — @objectstack/objectql/core boundary', () => { |
| 36 | + it('core.ts closure pulls neither metadata-protocol nor plugin/kernel-factory', () => { |
| 37 | + const entry = resolve(SRC, 'core.ts'); |
| 38 | + const visited = new Set<string>(); |
| 39 | + const violations: string[] = []; |
| 40 | + const stack = [entry]; |
| 41 | + |
| 42 | + while (stack.length) { |
| 43 | + const file = stack.pop()!; |
| 44 | + if (visited.has(file)) continue; |
| 45 | + visited.add(file); |
| 46 | + |
| 47 | + let src: string; |
| 48 | + try { |
| 49 | + src = readFileSync(file, 'utf8'); |
| 50 | + } catch { |
| 51 | + continue; // generated / non-existent; ignore |
| 52 | + } |
| 53 | + |
| 54 | + for (const pkg of FORBIDDEN_PACKAGES) { |
| 55 | + if (new RegExp(`['"]${pkg.replace('/', '\\/')}['"]`).test(src)) { |
| 56 | + violations.push(`${file} imports forbidden package ${pkg}`); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + for (const spec of localImports(src)) { |
| 61 | + const base = spec.replace(/\.js$/, '').split('/').pop(); |
| 62 | + if (FORBIDDEN_LOCAL.includes(base ?? '')) { |
| 63 | + violations.push(`${file} imports forbidden local module ./${base}`); |
| 64 | + } |
| 65 | + stack.push(toTsPath(file, spec)); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + expect(violations, violations.join('\n')).toEqual([]); |
| 70 | + // sanity: the engine itself IS in the closure |
| 71 | + expect([...visited].some((f) => f.endsWith('/engine.ts'))).toBe(true); |
| 72 | + }); |
| 73 | +}); |
0 commit comments