import test from "node:test"; import assert from "node:assert/strict"; import { effectiveSessionExport, sessionExportArtifact, sessionCaptureState, stageArtifacts, systemArtifacts } from "../src/artifacts.js"; const file = (name) => ({name,size_bytes:100,content_type:"text/html",url:`/artifacts/19/1/${name}`}); const complete = (html, id="ses_stage") => ({session_id:id,backend:"v2",status:"complete",messages:14,html,html_bytes:100}); test("a missing export never hides a recorded session identity", () => { const state=sessionCaptureState({session_id:"ses_stage",model:"fixture",status:"completed",session_export:{session_id:"ses_stage",status:"failed",diagnostic:"readback failed"}}); assert.equal(state.sessionId,"ses_stage");assert.equal(state.artifact,null); assert.match(state.message,/ses_stage is recorded/);assert.match(state.message,/failed/); assert.doesNotMatch(state.message,/not recorded|exported by model stages/); }); test("the audit viewer selects a verified complete repair without changing original evidence", () => { const original={session_id:"ses_stage",status:"failed",diagnostic:"original capture failed"}; const repaired=complete("session-repair-1-good.html"); const step={session_id:"ses_stage",session_export:original,session_export_repairs:[repaired,{session_id:"ses_stage",status:"failed"}],artifacts:[file(repaired.html)]}; assert.equal(effectiveSessionExport(step),repaired); assert.equal(sessionExportArtifact(step).name,repaired.html); assert.equal(sessionCaptureState(step).artifact.url,"/artifacts/19/1/session-repair-1-good.html"); assert.equal(step.session_export,original);assert.equal(original.status,"failed"); }); test("a wrong-session repair or an unlisted file cannot masquerade as the export", () => { const original={session_id:"ses_stage",status:"failed"}; const wrong=complete("session-repair-wrong.html","ses_other"); const missing=complete("session-repair-missing.html"); const step={session_id:"ses_stage",session_export:original,session_export_repairs:[wrong,missing],artifacts:[file(wrong.html)]}; assert.equal(effectiveSessionExport({...step,session_export_repairs:[wrong]}),original); assert.equal(sessionExportArtifact(step),null); }); test("a partial repair does not replace a complete original export", () => { const original=complete("session.html"); const partial={...complete("session-repair-partial.html"),status:"partial",truncated:true}; assert.equal(effectiveSessionExport({session_id:"ses_stage",session_export:original,session_export_repairs:[partial],artifacts:[file(original.html),file(partial.html)]}),original); }); test("versioned private and historical public repairs remain audit evidence, not deliverables", () => { const repairs=[complete("session-repair-1-good.html"),complete("public/session-repair-1-old.html")]; const step={session_export_repairs:repairs,artifacts:[...repairs.map(entry=>file(entry.html)),file("report.html")]}; assert.deepEqual(stageArtifacts(step).map(entry=>entry.name),["report.html"]); assert.deepEqual(systemArtifacts(step).map(entry=>entry.name),repairs.map(entry=>entry.html)); }); test("an original export naming another session is not selected", () => { const original=complete("session.html","ses_wrong"); assert.equal(sessionExportArtifact({session_id:"ses_stage",session_export:original,artifacts:[file("session.html")]}),null); }); test("repairs must match the original backend and directory, not just its session ID", () => { const original={session_id:"ses_stage",backend:"v2",directory:"/owned",status:"failed"}; for (const override of [{backend:"pi",directory:"/owned"},{backend:"v2",directory:"/other"}]) { const repair={...complete("session-repair-other.html"),...override}; const step={session_id:"ses_stage",session_export:original,session_export_repairs:[repair],artifacts:[file(repair.html)]}; assert.equal(effectiveSessionExport(step),original); assert.equal(sessionExportArtifact(step),null); } }); test("a repair's file bytes must match its export receipt", () => { const original=complete("session.html"); const repair=complete("session-repair-mismatch.html"); const step={session_id:"ses_stage",session_export:original,session_export_repairs:[repair],artifacts:[file(original.html),{...file(repair.html),size_bytes:1}]}; assert.equal(effectiveSessionExport(step),original); assert.equal(sessionExportArtifact({...step,session_export:null}),null); }); test("pre-admission failure, starting session and export recovery have distinct states", () => { assert.match(sessionCaptureState({model:"fixture",status:"running"}).message,/being created/); assert.match(sessionCaptureState({model:"fixture",status:"failed",diagnostic:"provider unreachable"}).message,/did not create.*provider unreachable/); assert.match(sessionCaptureState({model:"fixture",status:"completed"}).message,/Runtime error/); assert.match(sessionCaptureState({model:"fixture",status:"completed",outcome:"skipped"}).message,/was skipped/); assert.match(sessionCaptureState({session_id:"ses_stage",session_export_recovery:{status:"pending"}}).message,/pending recovery/); });