241 lines
11 KiB
JavaScript
241 lines
11 KiB
JavaScript
// tests/validators/next-session-prompt-validator.test.mjs
|
|
// Unit + CLI integration tests for lib/validators/next-session-prompt-validator.mjs.
|
|
// Covers Bug 3 contract: producer-mismatch detection + state-anchored staleness +
|
|
// 24h soft-warning + missing-frontmatter downgrade.
|
|
|
|
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
import { execFileSync, spawnSync } from 'node:child_process';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { childEnv } from '../helpers/child-env.mjs';
|
|
|
|
import {
|
|
validateNextSessionPromptContent,
|
|
validateNextSessionPromptObject,
|
|
validateNextSessionPromptConsistency,
|
|
} from '../../lib/validators/next-session-prompt-validator.mjs';
|
|
|
|
function frontmatter(producedBy, producedAt, extra = '') {
|
|
return `---\nproduced_by: ${producedBy}\nproduced_at: ${producedAt}\n${extra}---\n\n# A1 — example\n\nbody\n`;
|
|
}
|
|
|
|
test('validateNextSessionPromptContent — both consistent producers (valid)', () => {
|
|
const text = frontmatter('trekexecute', '2026-05-04T16:00:00.000Z');
|
|
const r = validateNextSessionPromptContent(text);
|
|
assert.equal(r.valid, true, JSON.stringify(r.errors));
|
|
assert.equal(r.parsed.produced_by, 'trekexecute');
|
|
});
|
|
|
|
test('validateNextSessionPromptObject — missing produced_by is invalid', () => {
|
|
const r = validateNextSessionPromptObject({ produced_at: '2026-05-04T16:00:00Z' });
|
|
assert.equal(r.valid, false);
|
|
assert.ok(r.errors.find(e => e.code === 'NEXT_SESSION_PROMPT_MISSING_FIELD' && /produced_by/.test(e.message)));
|
|
});
|
|
|
|
test('validateNextSessionPromptObject — missing produced_at is invalid', () => {
|
|
const r = validateNextSessionPromptObject({ produced_by: 'trekexecute' });
|
|
assert.equal(r.valid, false);
|
|
assert.ok(r.errors.find(e => e.code === 'NEXT_SESSION_PROMPT_MISSING_FIELD' && /produced_at/.test(e.message)));
|
|
});
|
|
|
|
test('validateNextSessionPromptObject — invalid produced_at timestamp rejected', () => {
|
|
const r = validateNextSessionPromptObject({ produced_by: 'x', produced_at: 'not-a-date' });
|
|
assert.equal(r.valid, false);
|
|
assert.ok(r.errors.find(e => e.code === 'NEXT_SESSION_PROMPT_INVALID_TIMESTAMP'));
|
|
});
|
|
|
|
test('validateNextSessionPromptContent — no frontmatter downgrades to warning (valid)', () => {
|
|
const r = validateNextSessionPromptContent('# Plain markdown, no frontmatter\n\ntext\n');
|
|
assert.equal(r.valid, true);
|
|
assert.ok(r.warnings.find(w => w.code === 'NEXT_SESSION_PROMPT_NO_FRONTMATTER'));
|
|
});
|
|
|
|
test('validateNextSessionPromptConsistency — producer mismatch with both fresh fails', () => {
|
|
const a = { path: '/a', parsed: { produced_by: 'trekexecute', produced_at: '2026-05-04T16:00:00.000Z' } };
|
|
const b = { path: '/b', parsed: { produced_by: 'graceful-handoff', produced_at: '2026-05-04T16:05:00.000Z' } };
|
|
const state = { updated_at: '2026-05-04T15:00:00.000Z' };
|
|
const r = validateNextSessionPromptConsistency(a, b, { state, now: Date.parse('2026-05-04T16:30:00.000Z') });
|
|
assert.equal(r.valid, false);
|
|
assert.ok(r.errors.find(e => e.code === 'NEXT_SESSION_PROMPT_PRODUCER_MISMATCH'));
|
|
});
|
|
|
|
test('validateNextSessionPromptConsistency — state-anchored stale candidate ignored', () => {
|
|
const a = { path: '/a', parsed: { produced_by: 'graceful-handoff', produced_at: '2026-05-03T10:00:00.000Z' } };
|
|
const b = { path: '/b', parsed: { produced_by: 'trekexecute', produced_at: '2026-05-04T16:05:00.000Z' } };
|
|
const state = { updated_at: '2026-05-04T16:00:00.000Z' };
|
|
const r = validateNextSessionPromptConsistency(a, b, { state, now: Date.parse('2026-05-04T16:30:00.000Z') });
|
|
assert.equal(r.valid, true, JSON.stringify(r.errors));
|
|
assert.ok(r.warnings.find(w => w.code === 'NEXT_SESSION_PROMPT_STALE_IGNORED'));
|
|
});
|
|
|
|
test('validateNextSessionPromptConsistency — 24h wall-clock drift emits soft warning', () => {
|
|
const a = { path: '/a', parsed: { produced_by: 'trekexecute', produced_at: '2026-05-01T16:00:00.000Z' } };
|
|
const b = { path: '/b', parsed: { produced_by: 'trekexecute', produced_at: '2026-05-01T16:00:00.000Z' } };
|
|
const r = validateNextSessionPromptConsistency(a, b, { now: Date.parse('2026-05-04T16:30:00.000Z') });
|
|
assert.equal(r.valid, true);
|
|
assert.ok(r.warnings.find(w => w.code === 'NEXT_SESSION_PROMPT_WALL_CLOCK_DRIFT'));
|
|
});
|
|
|
|
test('validateNextSessionPromptConsistency — same producer, both fresh, no errors', () => {
|
|
const a = { path: '/a', parsed: { produced_by: 'trekexecute', produced_at: '2026-05-04T16:00:00.000Z' } };
|
|
const b = { path: '/b', parsed: { produced_by: 'trekexecute', produced_at: '2026-05-04T16:01:00.000Z' } };
|
|
const r = validateNextSessionPromptConsistency(a, b, { now: Date.parse('2026-05-04T16:30:00.000Z') });
|
|
assert.equal(r.valid, true);
|
|
assert.deepEqual(r.errors, []);
|
|
// No 24h warning: produced_at is well within 24h of `now`.
|
|
assert.deepEqual(r.warnings.filter(w => w.code === 'NEXT_SESSION_PROMPT_WALL_CLOCK_DRIFT'), []);
|
|
});
|
|
|
|
test('CLI shim — single-file mode returns JSON for valid file', () => {
|
|
const dir = mkdtempSync(join(tmpdir(), 'nspv-cli-'));
|
|
try {
|
|
const file = join(dir, 'NEXT-SESSION-PROMPT.local.md');
|
|
writeFileSync(file, frontmatter('trekexecute', '2026-05-04T16:00:00.000Z'));
|
|
const out = execFileSync(process.execPath, [
|
|
'lib/validators/next-session-prompt-validator.mjs',
|
|
'--json',
|
|
file,
|
|
], { encoding: 'utf-8' });
|
|
const parsed = JSON.parse(out);
|
|
assert.equal(parsed.valid, true);
|
|
} finally {
|
|
rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test('CLI shim — consistency mode flags producer mismatch', () => {
|
|
const dir = mkdtempSync(join(tmpdir(), 'nspv-cli-'));
|
|
try {
|
|
const a = join(dir, 'a.md');
|
|
const b = join(dir, 'b.md');
|
|
writeFileSync(a, frontmatter('trekexecute', '2026-05-04T16:00:00.000Z'));
|
|
writeFileSync(b, frontmatter('graceful-handoff', '2026-05-04T16:01:00.000Z'));
|
|
let exitCode = 0;
|
|
let out = '';
|
|
try {
|
|
out = execFileSync(process.execPath, [
|
|
'lib/validators/next-session-prompt-validator.mjs',
|
|
'--json',
|
|
'--consistency',
|
|
a,
|
|
b,
|
|
], { encoding: 'utf-8' });
|
|
} catch (e) {
|
|
exitCode = e.status;
|
|
out = e.stdout ? e.stdout.toString() : '';
|
|
}
|
|
assert.notEqual(exitCode, 0);
|
|
const parsed = JSON.parse(out);
|
|
assert.equal(parsed.valid, false);
|
|
assert.ok(parsed.errors.find(e => e.code === 'NEXT_SESSION_PROMPT_PRODUCER_MISMATCH'));
|
|
} finally {
|
|
rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
// --- #102: the consistency call reads the state file -----------------------
|
|
// The CLI counted the --state-file value as a third positional and exited 2,
|
|
// so the state-anchored staleness check never ran from the command line; and
|
|
// an invalid B still gave valid: true. These tests use spawnSync so they reach
|
|
// an assertion on exit 2 too.
|
|
|
|
const CLI = fileURLToPath(new URL('../../lib/validators/next-session-prompt-validator.mjs', import.meta.url));
|
|
|
|
function runCli(args) {
|
|
return spawnSync(process.execPath, [CLI, ...args], { encoding: 'utf-8' });
|
|
}
|
|
|
|
/** A stale master (older than the state's updated_at) and a fresh sibling by another producer. */
|
|
function stalePair(dir) {
|
|
const a = join(dir, 'a.md');
|
|
const b = join(dir, 'b.md');
|
|
const s = join(dir, '.session-state.local.json');
|
|
writeFileSync(a, frontmatter('graceful-handoff', '2026-05-03T10:00:00.000Z'));
|
|
writeFileSync(b, frontmatter('trekexecute', '2026-05-04T16:05:00.000Z'));
|
|
writeFileSync(s, JSON.stringify({ schema_version: 1, updated_at: '2026-05-04T16:00:00.000Z' }));
|
|
return { a, b, s };
|
|
}
|
|
|
|
test('[#102] CLI --json --consistency a b --state-file s: the state-anchored case exits 0 with a STALE_IGNORED warning', () => {
|
|
const dir = mkdtempSync(join(tmpdir(), 'nspv-cli-'));
|
|
try {
|
|
const { a, b, s } = stalePair(dir);
|
|
const r = runCli(['--json', '--consistency', a, b, '--state-file', s]);
|
|
assert.equal(r.status, 0, `exit ${r.status}: ${r.stdout}${r.stderr}`);
|
|
const parsed = JSON.parse(r.stdout);
|
|
assert.equal(parsed.valid, true, JSON.stringify(parsed.errors));
|
|
assert.ok(parsed.warnings.find((w) => w.code === 'NEXT_SESSION_PROMPT_STALE_IGNORED'),
|
|
'the stale candidate was not ignored');
|
|
assert.equal(parsed.errors.find((e) => e.code === 'NEXT_SESSION_PROMPT_PRODUCER_MISMATCH'), undefined);
|
|
} finally {
|
|
rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test('control: the same pair without --state-file is a producer mismatch (exit 1)', () => {
|
|
const dir = mkdtempSync(join(tmpdir(), 'nspv-cli-'));
|
|
try {
|
|
const { a, b } = stalePair(dir);
|
|
const r = runCli(['--json', '--consistency', a, b]);
|
|
assert.equal(r.status, 1, `exit ${r.status}: ${r.stdout}${r.stderr}`);
|
|
const parsed = JSON.parse(r.stdout);
|
|
assert.equal(parsed.valid, false);
|
|
assert.ok(parsed.errors.find((e) => e.code === 'NEXT_SESSION_PROMPT_PRODUCER_MISMATCH'));
|
|
} finally {
|
|
rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test('[#102] sibling: rb.valid counts: an invalid B makes the consistency result invalid (exit 1)', () => {
|
|
const dir = mkdtempSync(join(tmpdir(), 'nspv-cli-'));
|
|
try {
|
|
const a = join(dir, 'a.md');
|
|
const b = join(dir, 'b.md');
|
|
writeFileSync(a, frontmatter('trekexecute', '2026-05-04T16:00:00.000Z'));
|
|
writeFileSync(b, '---\nproduced_by: trekexecute\n---\n\n# B, no produced_at\n');
|
|
const r = runCli(['--json', '--consistency', a, b]);
|
|
assert.equal(r.status, 1, `an invalid B still passed: exit ${r.status}: ${r.stdout}${r.stderr}`);
|
|
const parsed = JSON.parse(r.stdout);
|
|
assert.equal(parsed.valid, false);
|
|
assert.ok(parsed.errors.find((e) => e.code === 'NEXT_SESSION_PROMPT_MISSING_FIELD' && /produced_at/.test(e.message)));
|
|
} finally {
|
|
rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
// --- E16a (fix round 4.5b): --state-file without a value is refused ----------
|
|
// The CLI refuses --state-file when no value follows it, or when the next word
|
|
// is another option, with exit 2 and "needs a value". Without the refusal the
|
|
// consistency call runs on the same pair and exits 0 or 1, so each test runs
|
|
// that call and asserts the exit code and the message.
|
|
|
|
function runCliClean(args) {
|
|
return spawnSync(process.execPath, [CLI, ...args], { encoding: 'utf-8', env: childEnv() });
|
|
}
|
|
|
|
test('[E16a] --state-file as the last argument exits 2 with needs a value', () => {
|
|
const dir = mkdtempSync(join(tmpdir(), 'nspv-cli-'));
|
|
try {
|
|
const { a, b } = stalePair(dir);
|
|
const r = runCliClean(['--json', '--consistency', a, b, '--state-file']);
|
|
assert.equal(r.status, 2, `a --state-file with no value was not refused: exit ${r.status}: ${r.stdout}${r.stderr}`);
|
|
assert.match(r.stderr, /--state-file needs a value/, `the refusal does not say the flag needs a value: ${r.stderr}`);
|
|
} finally {
|
|
rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test('[E16a] --state-file followed by another option exits 2', () => {
|
|
const dir = mkdtempSync(join(tmpdir(), 'nspv-cli-'));
|
|
try {
|
|
const { a, b } = stalePair(dir);
|
|
const r = runCliClean(['--state-file', '--json', '--consistency', a, b]);
|
|
assert.equal(r.status, 2, `--state-file took the option after it as its value: exit ${r.status}: ${r.stdout}${r.stderr}`);
|
|
assert.match(r.stderr, /--state-file needs a value/, `the refusal does not say the flag needs a value: ${r.stderr}`);
|
|
} finally {
|
|
rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|