|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `os lint --eval --json` had NO machine face for an uncaught throw. |
| 5 | + * |
| 6 | + * ## The measured before-shape |
| 7 | + * |
| 8 | + * `run()` dispatches eval mode and returns ENTIRELY ABOVE the project-lint |
| 9 | + * `try`, so nothing thrown out of `runEval` can reach that mode's catch-all |
| 10 | + * JSON exit; and `lint.ts` hand-rolls `json` as a plain `Flags.boolean` rather |
| 11 | + * than oclif's `enableJsonFlag` (zero occurrences anywhere in |
| 12 | + * `packages/cli/src`), so no framework envelope sits underneath either. Driven |
| 13 | + * on the published entry before the fix: |
| 14 | + * |
| 15 | + * os lint --eval --json --generator ./poison.mjs |
| 16 | + * exit 1 · stdout 0 BYTES · stderr " Error: poison getter" |
| 17 | + * |
| 18 | + * ⇒ a caller that asked for `--json` got oclif's human text on stderr and no |
| 19 | + * document at all to parse. |
| 20 | + * |
| 21 | + * ## What was actually broken, and what was NOT |
| 22 | + * |
| 23 | + * ⛔ Nothing new appears on the `--json` face and nothing was added to it. The |
| 24 | + * eval report exit already emits JSON and already `process.exit(1)`s when |
| 25 | + * `!report.ok`. The defect was that a whole class of failure could never REACH |
| 26 | + * that exit, because `runMetadataEval` — whose docblock says *"Never throws"* — |
| 27 | + * wrapped only `options.generate(...)` in its `try` and left the |
| 28 | + * `scoreMetadata(stack)` call outside it. A generator that THREW became a |
| 29 | + * failed case; one that RETURNED a value nobody could walk escaped. The fix |
| 30 | + * makes the existing exit reachable; it does not widen it. |
| 31 | + * |
| 32 | + * ## ⛔ The trap this file exists to keep shut |
| 33 | + * |
| 34 | + * A guard that swallowed the throw and let the poisoned case be reported as |
| 35 | + * PASSING would be worse than the crash — it turns a loud failure into a quiet |
| 36 | + * wrong answer. So `stdout parses` is never asserted alone here: every positive |
| 37 | + * requires `ok: false`, the case FAILED, the cause NAMED in `generationError`, |
| 38 | + * and a nonzero exit. `a silent swallow would be caught` pins the negative |
| 39 | + * directly, against the specific benign shape a swallow would produce |
| 40 | + * (`scoreMetadata({})` is 100 / A / `valid: true`). |
| 41 | + * |
| 42 | + * ## Why the negative controls are here |
| 43 | + * |
| 44 | + * The reachable class is narrow, and that narrowness is a MEASUREMENT: every |
| 45 | + * off-shape stack below already produced valid JSON before this change, and |
| 46 | + * must still. They are the guard against a fix that "solved" the crash by |
| 47 | + * routing ordinary bad metadata into the failure channel too — an off-shape |
| 48 | + * stack is a SCORED case with schema errors, never a `generationError`. |
| 49 | + * |
| 50 | + * ## Why no `dist/` sits on the measured path |
| 51 | + * |
| 52 | + * These run the CLI through `bin/run-dev.js`, the SOURCE entry — same CLI, run |
| 53 | + * from `src/` through tsx — so `metadata-eval.ts` is loaded from source by the |
| 54 | + * child and an ablation of it is measured without a rebuild. Its dependency |
| 55 | + * `@objectstack/spec`, which owns `normalizeStackInput`, resolves through |
| 56 | + * `exports` to `dist/`, and this change does not touch it. |
| 57 | + */ |
| 58 | + |
| 59 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 60 | +import { execFile } from 'node:child_process'; |
| 61 | +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
| 62 | +import { tmpdir } from 'node:os'; |
| 63 | +import { join, resolve } from 'node:path'; |
| 64 | +import { fileURLToPath } from 'node:url'; |
| 65 | +import { childEnv } from './helpers/serve-process.js'; |
| 66 | + |
| 67 | +const HERE = resolve(fileURLToPath(import.meta.url), '..'); |
| 68 | +const CLI = resolve(HERE, '../bin/run-dev.js'); |
| 69 | +const TSX = resolve(HERE, '../../../node_modules/.bin/tsx'); |
| 70 | + |
| 71 | +interface Run { |
| 72 | + code: number; |
| 73 | + stdout: string; |
| 74 | + stderr: string; |
| 75 | +} |
| 76 | + |
| 77 | +let dir: string; |
| 78 | + |
| 79 | +function generator(name: string, source: string): string { |
| 80 | + const file = join(dir, `${name}.mjs`); |
| 81 | + writeFileSync(file, source, 'utf8'); |
| 82 | + return file; |
| 83 | +} |
| 84 | + |
| 85 | +function runEval(generatorPath?: string): Promise<Run> { |
| 86 | + const args = [CLI, 'lint', '--eval', '--json', ...(generatorPath ? ['--generator', generatorPath] : [])]; |
| 87 | + return new Promise((resolvePromise) => { |
| 88 | + execFile( |
| 89 | + TSX, |
| 90 | + args, |
| 91 | + { cwd: dir, maxBuffer: 16 * 1024 * 1024, env: childEnv({ NO_COLOR: '1' }) }, |
| 92 | + (err, stdout, stderr) => { |
| 93 | + resolvePromise({ |
| 94 | + code: err ? (typeof (err as { code?: unknown }).code === 'number' ? (err as unknown as { code: number }).code : 1) : 0, |
| 95 | + stdout: String(stdout), |
| 96 | + stderr: String(stderr), |
| 97 | + }); |
| 98 | + }, |
| 99 | + ); |
| 100 | + }); |
| 101 | +} |
| 102 | + |
| 103 | +interface EvalCaseResult { |
| 104 | + id: string; |
| 105 | + generationError?: string; |
| 106 | + passed: boolean; |
| 107 | + score: { score: number; grade: string; valid: boolean; counts: { schemaErrors: number } }; |
| 108 | +} |
| 109 | + |
| 110 | +interface EvalReport { |
| 111 | + ok: boolean; |
| 112 | + total: number; |
| 113 | + passed: number; |
| 114 | + failed: number; |
| 115 | + meanScore: number; |
| 116 | + results: EvalCaseResult[]; |
| 117 | +} |
| 118 | + |
| 119 | +/** stdout as ONE JSON document, or a failure that quotes what was there instead. */ |
| 120 | +function payloadOf(run: Run, label: string): EvalReport { |
| 121 | + try { |
| 122 | + return JSON.parse(run.stdout) as EvalReport; |
| 123 | + } catch { |
| 124 | + throw new Error( |
| 125 | + `${label}: stdout was not one JSON document (exit ${run.code}, ${run.stdout.length} stdout bytes)\n` + |
| 126 | + `stdout: ${JSON.stringify(run.stdout)}\nstderr: ${JSON.stringify(run.stderr)}`, |
| 127 | + ); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +/** Poison on a TOP-LEVEL key — throws in `normalizeStackInput`'s `{ ...input }`. */ |
| 132 | +const TOP_LEVEL_POISON = `export default function () { |
| 133 | + return { name: 'poison', get objects() { throw new Error('poison getter'); } }; |
| 134 | +} |
| 135 | +`; |
| 136 | + |
| 137 | +/** Poison one level DOWN — survives the shallow spread, throws inside the schema parse. */ |
| 138 | +const NESTED_POISON = `export default function () { |
| 139 | + return { |
| 140 | + name: 'poison_nested', |
| 141 | + objects: [{ name: 'account', label: 'Account', get fields() { throw new Error('nested poison getter'); } }], |
| 142 | + }; |
| 143 | +} |
| 144 | +`; |
| 145 | + |
| 146 | +beforeAll(() => { |
| 147 | + dir = mkdtempSync(join(tmpdir(), 'os-lint-eval-json-')); |
| 148 | +}); |
| 149 | + |
| 150 | +afterAll(() => { |
| 151 | + rmSync(dir, { recursive: true, force: true }); |
| 152 | +}); |
| 153 | + |
| 154 | +describe('os lint --eval --json — an unscorable generated stack has a machine face', () => { |
| 155 | + it('a top-level poisoned getter: stdout is JSON, the case FAILED, the cause is named', async () => { |
| 156 | + const run = await runEval(generator('top-level-poison', TOP_LEVEL_POISON)); |
| 157 | + const payload = payloadOf(run, 'top-level poison'); |
| 158 | + |
| 159 | + // The failure is LOUD: nonzero exit, ok:false, every case failed. |
| 160 | + expect(run.code).toBe(1); |
| 161 | + expect(payload.ok).toBe(false); |
| 162 | + expect(payload.failed).toBe(payload.total); |
| 163 | + expect(payload.passed).toBe(0); |
| 164 | + |
| 165 | + // …and the cause is NAMED, on the per-case channel a throwing generator uses. |
| 166 | + expect(payload.results[0].generationError).toContain('poison getter'); |
| 167 | + expect(payload.results[0].passed).toBe(false); |
| 168 | + |
| 169 | + // Nothing leaked to the human channel on a --json run. |
| 170 | + expect(run.stderr).toBe(''); |
| 171 | + }, 120_000); |
| 172 | + |
| 173 | + it('a poisoned getter BELOW the top level is caught too — the schema parse walks there', async () => { |
| 174 | + // The SITE control. This throw never reaches `normalizeStackInput`: the |
| 175 | + // top-level spread copies `objects` by reference and the getter fires later, |
| 176 | + // inside zod. A guard around the normalizer alone would leave this red. |
| 177 | + const run = await runEval(generator('nested-poison', NESTED_POISON)); |
| 178 | + const payload = payloadOf(run, 'nested poison'); |
| 179 | + |
| 180 | + expect(run.code).toBe(1); |
| 181 | + expect(payload.ok).toBe(false); |
| 182 | + expect(payload.results[0].generationError).toContain('nested poison getter'); |
| 183 | + expect(payload.results[0].passed).toBe(false); |
| 184 | + }, 120_000); |
| 185 | + |
| 186 | + it('⛔ a silent swallow would be caught: the unscorable case is not scored as clean', async () => { |
| 187 | + const run = await runEval(generator('swallow-control', TOP_LEVEL_POISON)); |
| 188 | + const payload = payloadOf(run, 'swallow control'); |
| 189 | + const first = payload.results[0]; |
| 190 | + |
| 191 | + // A swallow that substituted the empty stack would report 100 / A / valid. |
| 192 | + expect(first.score.score).toBe(0); |
| 193 | + expect(first.score.grade).toBe('F'); |
| 194 | + expect(first.score.valid).toBe(false); |
| 195 | + expect(payload.meanScore).toBe(0); |
| 196 | + }, 120_000); |
| 197 | +}); |
| 198 | + |
| 199 | +describe('os lint --eval --json — the negative controls still answer the same', () => { |
| 200 | + it('offline mode is untouched: exit 0 and every golden case passes', async () => { |
| 201 | + const run = await runEval(); |
| 202 | + const payload = payloadOf(run, 'offline baseline'); |
| 203 | + |
| 204 | + expect(run.code).toBe(0); |
| 205 | + expect(payload.ok).toBe(true); |
| 206 | + expect(payload.failed).toBe(0); |
| 207 | + expect(payload.results.every((r) => r.generationError === undefined)).toBe(true); |
| 208 | + }, 120_000); |
| 209 | + |
| 210 | + it('a generator that THROWS is still a generation error, not a scoring one', async () => { |
| 211 | + const run = await runEval( |
| 212 | + generator('throws', `export default function () { throw new Error('model unavailable'); }\n`), |
| 213 | + ); |
| 214 | + const payload = payloadOf(run, 'throwing generator'); |
| 215 | + |
| 216 | + expect(run.code).toBe(1); |
| 217 | + expect(payload.results[0].generationError).toBe('model unavailable'); |
| 218 | + }, 120_000); |
| 219 | + |
| 220 | + it.each([ |
| 221 | + ['manifest-as-string', `export default () => ({ manifest: 'not-an-object' });\n`], |
| 222 | + ['objects-as-string', `export default () => ({ objects: 'not-an-array' });\n`], |
| 223 | + ['objects-as-number', `export default () => ({ objects: 42 });\n`], |
| 224 | + ['objects-as-null', `export default () => ({ objects: null });\n`], |
| 225 | + ['nested-wrong-types', `export default () => ({ objects: [{ name: 123, label: [], fields: 'nope' }] });\n`], |
| 226 | + ['bare-string', `export default () => 'just a string';\n`], |
| 227 | + ])('off-shape stack %s is a SCORED case with schema errors, never a generationError', async (name, source) => { |
| 228 | + const run = await runEval(generator(name, source)); |
| 229 | + const payload = payloadOf(run, name); |
| 230 | + const first = payload.results[0]; |
| 231 | + |
| 232 | + expect(run.code).toBe(1); |
| 233 | + expect(payload.ok).toBe(false); |
| 234 | + // ⭐ The line that keeps the fix honest: ordinary bad metadata must NOT be |
| 235 | + // rerouted into the failure channel — it is scored, and its schema errors |
| 236 | + // are what fail it. |
| 237 | + expect(first.generationError).toBeUndefined(); |
| 238 | + expect(first.score.counts.schemaErrors).toBeGreaterThan(0); |
| 239 | + }, 120_000); |
| 240 | + |
| 241 | + it('a generator that cannot be loaded still takes the generator-load JSON exit', async () => { |
| 242 | + const run = await runEval(join(dir, 'does-not-exist.mjs')); |
| 243 | + |
| 244 | + expect(run.code).toBe(1); |
| 245 | + const payload = JSON.parse(run.stdout) as { error?: string }; |
| 246 | + expect(payload.error).toContain('Failed to load generator'); |
| 247 | + }, 120_000); |
| 248 | +}); |
0 commit comments