From 7b72ca6667ab8e05437c0d68d0e2895fb03a1ef9 Mon Sep 17 00:00:00 2001 From: pxmps Date: Mon, 10 Aug 2026 09:19:02 +0000 Subject: [PATCH] test(scripts): add invalid-JSON smoke test for append-run-log Invalid JSON exits 1 with a Usage/valid-JSON message; a valid minimal entry appends without throwing. Wired into the validate gates so CI keeps the behavior locked in. --- scripts/append-run-log.test.mjs | 42 +++++++++++++++++++++++++++++++++ scripts/ci-validate-gates.sh | 3 +++ 2 files changed, 45 insertions(+) create mode 100644 scripts/append-run-log.test.mjs diff --git a/scripts/append-run-log.test.mjs b/scripts/append-run-log.test.mjs new file mode 100644 index 00000000..2b6da1d6 --- /dev/null +++ b/scripts/append-run-log.test.mjs @@ -0,0 +1,42 @@ +#!/usr/bin/env node +/** + * Smoke test for scripts/append-run-log.mjs. + * Run directly (node scripts/append-run-log.test.mjs) or via the validate gates. + */ +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { execFile } from 'node:child_process'; +import { promisify } from 'node:util'; +import { mkdtemp, writeFile, rm, readFile } from 'node:fs/promises'; +import { tmpdir } from 'node:os'; +import path from 'node:path'; + +const exec = promisify(execFile); +const SCRIPT = new URL('./append-run-log.mjs', import.meta.url).pathname; + +test('invalid JSON second arg exits 1 with a Usage / valid JSON message', async () => { + await assert.rejects( + exec('node', [SCRIPT, 'not-json', 'ignored.log']), + (err) => { + assert.equal(err.code, 1); + const msg = `${err.stderr}${err.stdout}`; + assert.match(msg, /Usage|valid JSON/i); + return true; + }, + ); +}); + +test('valid minimal JSON entry does not throw on parse', async () => { + const dir = await mkdtemp(path.join(tmpdir(), 'append-run-log-')); + const logPath = path.join(dir, 'loop-run-log.md'); + await writeFile(logPath, '\n'); + try { + const entry = JSON.stringify({ run_id: 'run-1', outcome: 'ok' }); + const { stdout } = await exec('node', [SCRIPT, entry, logPath]); + assert.match(stdout, /Appended run run-1/); + const written = await readFile(logPath, 'utf8'); + assert.ok(written.includes('"run_id":"run-1"')); + } finally { + await rm(dir, { recursive: true, force: true }); + } +}); diff --git a/scripts/ci-validate-gates.sh b/scripts/ci-validate-gates.sh index 850d9d2b..45b47db9 100755 --- a/scripts/ci-validate-gates.sh +++ b/scripts/ci-validate-gates.sh @@ -35,6 +35,9 @@ npm install --no-save yaml@2 ajv@8 node scripts/validate-registry.mjs node scripts/check-loop-init-sync.mjs +echo "Smoke-testing scripts…" +node scripts/append-run-log.test.mjs + echo "Building and testing readiness-core…" ( cd tools/readiness-core