|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * PIN (#6247) — `os test` REFUSES a malformed `qa/*.test.json` at LOAD time. |
| 5 | + * |
| 6 | + * The load site used to be `JSON.parse(content) as QA.TestSuite`, with the |
| 7 | + * schema author's own `// Should validate with Zod` sitting next to it. A type |
| 8 | + * assertion is not a check: the cast made every JSON document on disk a |
| 9 | + * `TestSuite` as far as the compiler was concerned, and the first thing that |
| 10 | + * noticed otherwise was the runner, several layers down and with no idea which |
| 11 | + * file it came from. The three failure shapes that produced: |
| 12 | + * |
| 13 | + * - `{}` (no `scenarios`) → `TestRunner.runSuite` iterates `undefined` and |
| 14 | + * throws a TypeError attributed to the runner, not the file; |
| 15 | + * - `{ scenarios: [] }` with a typo'd key → the suite reports SUCCESS having |
| 16 | + * executed nothing, which is the dangerous one: a broken suite that passes |
| 17 | + * is indistinguishable from a green one in CI; |
| 18 | + * - a bad `action.type` → survives the load, survives the runner, and dies in |
| 19 | + * the HTTP adapter's `default:` branch mid-run, after the preceding steps |
| 20 | + * have already written records. |
| 21 | + * |
| 22 | + * An AI-authored suite hits all three routinely, which is exactly the |
| 23 | + * declared≠enforced gap ADR-0049 names: `TestSuiteSchema` was declared, shipped, |
| 24 | + * documented — and had no `parse` site anywhere in the platform. |
| 25 | + * |
| 26 | + * So the pin is on the LOAD boundary, not on the runner: a document that |
| 27 | + * `TestSuiteSchema` rejects must never reach `runSuite`, and the refusal must |
| 28 | + * name the file and the issues. Valid suites must still load unchanged. |
| 29 | + */ |
| 30 | + |
| 31 | +import { describe, it, expect } from 'vitest'; |
| 32 | +import { mkdtempSync, writeFileSync } from 'node:fs'; |
| 33 | +import { tmpdir } from 'node:os'; |
| 34 | +import { join } from 'node:path'; |
| 35 | +import { loadTestSuite } from '../src/commands/test'; |
| 36 | + |
| 37 | +const dir = mkdtempSync(join(tmpdir(), 'os-qa-suite-')); |
| 38 | + |
| 39 | +function suiteFile(name: string, body: string): string { |
| 40 | + const file = join(dir, name); |
| 41 | + writeFileSync(file, body, 'utf-8'); |
| 42 | + return file; |
| 43 | +} |
| 44 | + |
| 45 | +const VALID_SUITE = { |
| 46 | + name: 'crm smoke', |
| 47 | + scenarios: [ |
| 48 | + { |
| 49 | + id: 'create-account', |
| 50 | + name: 'Create an account', |
| 51 | + steps: [ |
| 52 | + { |
| 53 | + name: 'create', |
| 54 | + action: { type: 'create_record', target: 'accounts', payload: { name: 'Acme' } }, |
| 55 | + assertions: [{ field: 'id', operator: 'not_null', expectedValue: null }], |
| 56 | + }, |
| 57 | + ], |
| 58 | + }, |
| 59 | + ], |
| 60 | +}; |
| 61 | + |
| 62 | +describe('os test — suite load is schema-checked (#6247)', () => { |
| 63 | + it('loads a valid suite unchanged', () => { |
| 64 | + const file = suiteFile('valid.test.json', JSON.stringify(VALID_SUITE)); |
| 65 | + const suite = loadTestSuite(file); |
| 66 | + expect(suite.name).toBe('crm smoke'); |
| 67 | + expect(suite.scenarios).toHaveLength(1); |
| 68 | + expect(suite.scenarios[0].steps[0].action.type).toBe('create_record'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('refuses a suite with no `scenarios` — the shape that TypeErrors inside the runner', () => { |
| 72 | + const file = suiteFile('no-scenarios.test.json', JSON.stringify({ name: 'empty' })); |
| 73 | + expect(() => loadTestSuite(file)).toThrow(/no-scenarios\.test\.json/); |
| 74 | + expect(() => loadTestSuite(file)).toThrow(/scenarios/); |
| 75 | + }); |
| 76 | + |
| 77 | + it('refuses a misspelled step key — the shape that SILENTLY passes', () => { |
| 78 | + // `stepz` instead of `steps`: the cast let this through and the scenario |
| 79 | + // reported PASSED having run nothing at all. |
| 80 | + const file = suiteFile( |
| 81 | + 'typo.test.json', |
| 82 | + JSON.stringify({ name: 's', scenarios: [{ id: 'a', name: 'A', stepz: [] }] }), |
| 83 | + ); |
| 84 | + expect(() => loadTestSuite(file)).toThrow(/typo\.test\.json/); |
| 85 | + }); |
| 86 | + |
| 87 | + it('refuses an unknown action type — the shape that dies mid-run after writes', () => { |
| 88 | + const bad = structuredClone(VALID_SUITE) as Record<string, any>; |
| 89 | + bad.scenarios[0].steps[0].action.type = 'summon_record'; |
| 90 | + const file = suiteFile('bad-action.test.json', JSON.stringify(bad)); |
| 91 | + expect(() => loadTestSuite(file)).toThrow(/bad-action\.test\.json/); |
| 92 | + }); |
| 93 | + |
| 94 | + it('names the offending file and the issues, and prescribes the shape', () => { |
| 95 | + const file = suiteFile('broken.test.json', JSON.stringify({ scenarios: 'not an array' })); |
| 96 | + let message = ''; |
| 97 | + try { |
| 98 | + loadTestSuite(file); |
| 99 | + } catch (e) { |
| 100 | + message = e instanceof Error ? e.message : String(e); |
| 101 | + } |
| 102 | + expect(message).toContain('broken.test.json'); |
| 103 | + expect(message).toContain('TestSuiteSchema'); |
| 104 | + // Self-prescribing: the author is told what a suite looks like, not just |
| 105 | + // that theirs is wrong. |
| 106 | + expect(message).toContain('scenarios'); |
| 107 | + }); |
| 108 | + |
| 109 | + it('refuses invalid JSON with the file named, rather than a bare SyntaxError', () => { |
| 110 | + const file = suiteFile('not-json.test.json', '{ "name": '); |
| 111 | + expect(() => loadTestSuite(file)).toThrow(/not-json\.test\.json/); |
| 112 | + }); |
| 113 | +}); |
0 commit comments