|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// #4776 — the vocabulary GATE. The rule next door is a pure decision procedure |
| 4 | +// that ships to any caller with source in hand; this file is the one that makes |
| 5 | +// it bite in THIS repo, by running it over every `.ts` under `packages/`. |
| 6 | +// |
| 7 | +// Why the enforcement lives in a test rather than in another `scripts/check-*`: |
| 8 | +// the shape it looks for is a lint-layer vocabulary, and `@objectstack/lint` is |
| 9 | +// where the vocabulary lives. A rule on the package's public export surface |
| 10 | +// reads — to a human and to an AI author alike — as a check the platform |
| 11 | +// performs (Prime Directive #10, and the closure `authoring-rule-wiring.test.ts` |
| 12 | +// draws around it). This is that check. `pnpm check:startup-registry-verdict` |
| 13 | +// enforces the SERVICE-registry half of the same family and is untouched; see |
| 14 | +// the rule module for the measured division of labour between the two. |
| 15 | +// |
| 16 | +// Two false greens this is built to refuse: |
| 17 | +// |
| 18 | +// 1. **A corpus that was never read.** An unreadable directory would silently |
| 19 | +// shrink the sweep while the file count stayed comfortably non-zero, and the |
| 20 | +// test would report a clean audit over source it never opened — the exact |
| 21 | +// shape the rule itself is about, turned on the rule (#4930). So the root is |
| 22 | +// resolved up front and the walk carries no `catch`. |
| 23 | +// 2. **A rule that matches nothing.** A ratchet that has only ever been green |
| 24 | +// cannot be told apart from a dead one (#4690), and this one has been green |
| 25 | +// from its first commit. `the sweep can still fire` therefore pushes a |
| 26 | +// known-bad source through the SAME sweep function the corpus goes through. |
| 27 | +import { readdirSync, readFileSync, statSync } from 'node:fs'; |
| 28 | +import { dirname, join, relative } from 'node:path'; |
| 29 | +import { fileURLToPath } from 'node:url'; |
| 30 | +import { describe, expect, it } from 'vitest'; |
| 31 | + |
| 32 | +import { |
| 33 | + findStartupRegistryVerdicts, |
| 34 | + type StartupRegistryVerdictFinding, |
| 35 | +} from './lint-startup-registry-verdict.js'; |
| 36 | + |
| 37 | +const srcDir = dirname(fileURLToPath(import.meta.url)); |
| 38 | +const repoRoot = join(srcDir, '..', '..', '..'); |
| 39 | +const packagesDir = join(repoRoot, 'packages'); |
| 40 | + |
| 41 | +/** |
| 42 | + * Reviewed exceptions. **Shrink-only**, hand-edited: an entry must name WHY the |
| 43 | + * site is still here and WHAT closes it. There is deliberately no generator — a |
| 44 | + * `--update` flag lets a new violation be admitted by "just run the update |
| 45 | + * command", which is how a ratchet stops meaning anything. |
| 46 | + * |
| 47 | + * Empty on purpose: the three instances this vocabulary was written from |
| 48 | + * (#4769 / #4771 / #4772) were all fixed before it landed. The non-vacuity proof |
| 49 | + * is the `the sweep can still fire` case below, not the emptiness of this list. |
| 50 | + */ |
| 51 | +const LEDGER: Readonly<Record<string, string>> = {}; |
| 52 | + |
| 53 | +const SKIP_DIRS = new Set(['node_modules', 'dist', 'build', '.git', '.turbo', 'coverage', '.cache', '.next']); |
| 54 | + |
| 55 | +/** |
| 56 | + * Every auditable `.ts` under `dir`. |
| 57 | + * |
| 58 | + * No `catch`: an error during the walk means the corpus was only partly read, |
| 59 | + * which must not be reported as a clean audit. |
| 60 | + */ |
| 61 | +function collectSourceFiles(dir: string, out: string[] = []): string[] { |
| 62 | + for (const entry of readdirSync(dir)) { |
| 63 | + if (SKIP_DIRS.has(entry)) continue; |
| 64 | + const full = join(dir, entry); |
| 65 | + if (statSync(full).isDirectory()) collectSourceFiles(full, out); |
| 66 | + else if ( |
| 67 | + entry.endsWith('.ts') && |
| 68 | + !entry.endsWith('.d.ts') && |
| 69 | + !entry.includes('.test.') && |
| 70 | + !entry.includes('.spec.') && |
| 71 | + !entry.includes('.conformance.') |
| 72 | + ) { |
| 73 | + out.push(full); |
| 74 | + } |
| 75 | + } |
| 76 | + return out; |
| 77 | +} |
| 78 | + |
| 79 | +/** The sweep, as one function, so the corpus and the non-vacuity case share it. */ |
| 80 | +function sweep(sources: Array<{ file: string; source: string }>): StartupRegistryVerdictFinding[] { |
| 81 | + return sources.flatMap(({ file, source }) => findStartupRegistryVerdicts(source, { file })); |
| 82 | +} |
| 83 | + |
| 84 | +describe('startup open-vocabulary verdicts across packages/ (#4776)', () => { |
| 85 | + const stat = statSync(packagesDir); |
| 86 | + expect(stat.isDirectory(), `${packagesDir} must be a directory — the sweep's verdict is drawn from reading it`).toBe( |
| 87 | + true, |
| 88 | + ); |
| 89 | + const files = collectSourceFiles(packagesDir); |
| 90 | + |
| 91 | + it('reads a non-empty corpus', () => { |
| 92 | + // A zero-file sweep returns zero findings and would otherwise print as a |
| 93 | + // clean audit over nothing at all. |
| 94 | + expect(files.length).toBeGreaterThan(500); |
| 95 | + }); |
| 96 | + |
| 97 | + it('no package records a verdict the boot can still contradict', () => { |
| 98 | + const findings = sweep( |
| 99 | + files.map((file) => ({ file: relative(repoRoot, file), source: readFileSync(file, 'utf8') })), |
| 100 | + ); |
| 101 | + const unledgered = findings.filter((f) => !(`${f.path}::${f.rule}` in LEDGER)); |
| 102 | + |
| 103 | + expect( |
| 104 | + unledgered.map((f) => `[${f.rule}] ${f.path} — ${f.where}: ${f.message}`), |
| 105 | + `${unledgered.length} startup open-vocabulary verdict(s).\n` + |
| 106 | + `Each one draws a conclusion from a registry a plugin can still fill during this same boot, and ` + |
| 107 | + `records it where nothing retracts it. Fix it (the finding's hint carries the three shapes the ` + |
| 108 | + `#4769/#4771/#4772 fixes took), or add an entry to LEDGER in this file WITH the reason it is ` + |
| 109 | + `still here and what closes it.`, |
| 110 | + ).toEqual([]); |
| 111 | + }); |
| 112 | + |
| 113 | + it('no ledger entry is stale', () => { |
| 114 | + // A ledger that outlives its site is a standing permission nobody reviewed. |
| 115 | + const findings = sweep( |
| 116 | + files.map((file) => ({ file: relative(repoRoot, file), source: readFileSync(file, 'utf8') })), |
| 117 | + ); |
| 118 | + const live = new Set(findings.map((f) => `${f.path}::${f.rule}`)); |
| 119 | + const stale = Object.keys(LEDGER).filter((key) => !live.has(key)); |
| 120 | + expect(stale, `stale LEDGER entr(ies) — the site is fixed, delete the line: ${stale.join(', ')}`).toEqual([]); |
| 121 | + }); |
| 122 | + |
| 123 | + it('the sweep can still fire (#4690 — a green ratchet must be told apart from a dead one)', () => { |
| 124 | + // #4771, reconstructed: the flow node-type verdict drawn in start(), 0.8s |
| 125 | + // before the executor that answers it was registered. Pushed through the |
| 126 | + // SAME sweep the corpus goes through, so a change that broke matching would |
| 127 | + // fail here instead of quietly turning the corpus green. |
| 128 | + const reconstructed = ` |
| 129 | + export class AutomationServicePlugin { |
| 130 | + name = 'com.objectstack.service-automation'; |
| 131 | + async init() {} |
| 132 | + async start(ctx) { |
| 133 | + const known = this.engine.getRegisteredNodeTypes(); |
| 134 | + for (const flow of this.flows) { |
| 135 | + if (!known.includes(flow.type)) { |
| 136 | + ctx.logger.warn(\`Flow '\${flow.name}' will fail at execution time.\`); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + `; |
| 142 | + const findings = sweep([{ file: 'packages/services/service-automation/src/plugin.ts', source: reconstructed }]); |
| 143 | + expect(findings.map((f) => f.rule)).toEqual([ |
| 144 | + 'startup-open-vocabulary-verdict', |
| 145 | + 'startup-verdict-assertive-wording', |
| 146 | + ]); |
| 147 | + }); |
| 148 | +}); |
0 commit comments