import test from 'node:test'; import assert from 'node:assert/strict'; import { readFile } from 'node:fs/promises'; const fixturePath = new URL('./failure-scenario-002.jsonl', import.meta.url); const bytes = await readFile(fixturePath); const rows = bytes.toString('utf8').trim().split('\n').map(JSON.parse); const fresh = () => structuredClone(rows[1].input); const core = () => import('./scenario-002-evaluator.mjs'); for (const row of rows) test(`${row.case_id} ${row.case_name}`, async () => { const { evaluate } = await core(); const input = structuredClone(row.input), before = JSON.stringify(input); const result = evaluate(input); assert.equal(result.code, row.expected_control); assert.equal(result.citation_check, ['HOLD_INVALID_INPUT'].includes(result.code) ? 'NOT_CHECKED' : result.code === 'HOLD_CITATION' ? 'FAIL' : 'PASS'); assert.equal(result.answer_released, false); assert.equal(result.release_eligible, false); assert.ok(Array.isArray(result.reasons) && result.reasons.length > 0 && result.reasons.every(reason => typeof reason === 'string' && reason.trim())); assert.ok(result.age_hours === null || Number.isFinite(result.age_hours)); assert.equal(JSON.stringify(input), before); assert.deepEqual(evaluate(input), result); }); test('validator rejects malformed shapes, unknown keys, bounds and non-real UTC before logic', async () => { const { evaluate } = await core(); const invalid = [null, [], {}, { ...fresh(), extra: true }]; const change = mutate => { const input = fresh(); mutate(input); invalid.push(input); }; change(input => { input.claim.value = ' '; }); change(input => { input.claim.key = 'x'.repeat(201); }); change(input => { input.claim.extra = 'no'; }); change(input => { input.citation.extra = 'no'; }); change(input => { delete input.current_source.revision; }); change(input => { input.documents = []; }); change(input => { input.documents = Array(21).fill(input.documents[0]); }); change(input => { input.documents[0].source_kind = 'real'; }); change(input => { input.documents[0].evidence_status = 'verified'; }); change(input => { input.max_age_hours = '48'; }); change(input => { input.max_age_hours = 0; }); change(input => { input.max_age_hours = 8761; }); change(input => { input.max_age_hours = 1.5; }); change(input => { input.as_of = '2026-02-30T09:00:00Z'; }); change(input => { input.as_of = '2026-10-22T09:00:00+00:00'; }); change(input => { input.citation = null; input.documents[0].updated_at = '2026-10-22T24:00:00Z'; }); change(input => { input.documents.push({ ...input.documents[0], claim_key: 'unrelated', updated_at: 'not-a-time' }); }); for (const input of invalid) { assert.doesNotThrow(() => evaluate(input)); assert.equal(evaluate(input).code, 'HOLD_INVALID_INPUT'); assert.equal(evaluate(input).citation_check, 'NOT_CHECKED'); assert.equal(evaluate(input).age_hours, null); } }); test('citation must resolve uniquely and exactly; no wildcard expansion', async () => { const { evaluate } = await core(); const input = fresh(); input.citation.passage_id = '*'; assert.equal(evaluate(input).code, 'HOLD_CITATION'); input.citation = structuredClone(rows[1].input.citation); input.documents.push(structuredClone(input.documents[0])); assert.equal(evaluate(input).code, 'HOLD_CITATION'); }); test('source mismatch and ordered holds precede later checks', async () => { const { evaluate } = await core(); const input = fresh(); input.current_source.source_id = 'OTHER'; assert.equal(evaluate(input).code, 'HOLD_SOURCE_STATE'); input.current_source.source_id = input.citation.source_id; input.current_source.revision = 'OTHER'; input.documents[0].evidence_status = 'unverified'; assert.equal(evaluate(input).code, 'HOLD_REVISION_MISMATCH'); input.current_source.revision = input.citation.revision; input.documents[0].updated_at = '2026-10-23T09:00:00Z'; assert.equal(evaluate(input).code, 'HOLD_UNVERIFIED'); input.documents[0].evidence_status = 'fixture_verified'; input.claim.value = 'OTHER'; assert.equal(evaluate(input).code, 'HOLD_FUTURE'); input.documents[0].updated_at = '2026-10-01T09:00:00Z'; assert.equal(evaluate(input).code, 'HOLD_STALE'); }); test('exact 48 hour age boundary passes; one second later holds', async () => { const { evaluate } = await core(); const input = fresh(); input.documents[0].updated_at = '2026-10-20T09:00:00Z'; assert.equal(evaluate(input).code, 'ALLOW_IN_FIXTURE'); assert.equal(evaluate(input).age_hours, 48); input.as_of = '2026-10-22T09:00:01Z'; assert.equal(evaluate(input).code, 'HOLD_STALE'); assert.equal(evaluate(input).age_hours, 48 + 1 / 3600); }); test('stale conflicts and unrelated fresh future/unverified passages do not block', async () => { const { evaluate } = await core(); const input = fresh(); input.documents.push({ ...input.documents[0], source_id: 'STALE', claim_value: 'OTHER', updated_at: '2026-10-01T09:00:00Z' }); input.documents.push({ ...input.documents[0], source_id: 'UNRELATED', claim_key: 'other_key', claim_value: 'OTHER', evidence_status: 'unverified', updated_at: '2026-10-23T09:00:00Z' }); assert.equal(evaluate(input).code, 'ALLOW_IN_FIXTURE'); input.documents[1].updated_at = input.documents[0].updated_at; assert.equal(evaluate(input).code, 'HOLD_CONTRADICTION'); }); test('editing session input clears prior result including malformed JSON', async () => { const { createSession } = await core(); const session = createSession(); session.setInputText(JSON.stringify(fresh())); assert.equal(session.run().code, 'ALLOW_IN_FIXTURE'); session.setInputText('{'); assert.equal(session.result, null); assert.equal(session.run().code, 'HOLD_INVALID_INPUT'); });