// Isolated audit regression: session identity, verified repair selection and
// live modal refresh. Provider/session data is fixture-only; no production DB.
import assert from "node:assert/strict";
import { createServer } from "node:http";
import { readFile, mkdir } from "node:fs/promises";
import { resolve, extname } from "node:path";
import { launchChrome } from "./browser.mjs";
const root=resolve(import.meta.dirname,"..");
const out=resolve(process.argv[2]??"captures-session-audit");await mkdir(out,{recursive:true});
const html=`
${["design/tokens/tokens","base","shell","lists","run-detail","chat","dialogs"].map(n=>``).join("")}
`;
const server=createServer(async(req,res)=>{try{
const pathname=new URL(req.url,"http://fixture").pathname;
if(pathname==="/"){res.setHeader('Content-Type','text/html');res.end(html);return;}
if(pathname.startsWith('/artifacts/')){res.setHeader('Content-Type','text/html');res.setHeader('Content-Security-Policy',"sandbox; default-src 'none'");res.end('Recorded session ses_exact
');return;}
const file=resolve(root,`.${pathname}`);if(!file.startsWith(root+'/'))throw Error('outside fixture');
res.setHeader('Content-Type',extname(file)==='.css'?'text/css':'text/javascript');res.end(await readFile(file));
}catch{res.writeHead(404);res.end();}});
await new Promise(r=>server.listen(0,'127.0.0.1',r));
let browser;
try{
browser=await launchChrome({width:390,height:844});
const page=await browser.openPage(`http://127.0.0.1:${server.address().port}/`);
await page.waitForSelector('.audit-open');await page.click('.audit-open');await page.click('[data-audit-tab=session]');
assert.match(await page.evaluate("document.querySelector('.audit-body').textContent"),/ses_exact is recorded/);
assert.doesNotMatch(await page.evaluate("document.querySelector('.audit-body').textContent"),/Session not recorded|exported by model stages/);
await page.screenshot(resolve(out,'01-recorded-identity-export-failed.png'));
await page.evaluate('parkForExport()');
assert.match(await page.evaluate("document.querySelector('.audit-body').textContent"),/pending recovery/);
assert.equal(await page.evaluate("document.querySelector('.session-export-retry').disabled"),false);
assert.equal(await page.evaluate("document.querySelectorAll('.decision-card').length"),0);
await page.screenshot(resolve(out,'03-export-wait-is-not-a-human-decision.png'));
await page.click('.session-export-retry');await page.waitForSelector('.audit-session-frame');
assert.deepEqual(await page.evaluate('repairRequests'),[{id:19,sequence:1}]);
assert.match(await page.evaluate("document.querySelector('.audit-session-frame').src"),/session-repair-1-fixed.html$/);
assert.match(await page.evaluate("document.querySelector('.audit-body').textContent"),/Recorded session: ses_exact/);
assert.doesNotMatch(await page.evaluate("document.querySelector('.audit-body').textContent"),/native readback failed/);
assert.equal(await page.evaluate("document.querySelector('.audit-session-frame').getAttribute('sandbox')"),'');
const same=await page.evaluate("window.savedFrame=document.querySelector('.audit-session-frame');true");
await page.evaluate('recordRepair()');assert.equal(await page.evaluate("savedFrame===document.querySelector('.audit-session-frame')"),same);
await page.screenshot(resolve(out,'02-complete-repair-refreshes-open-audit.png'));
console.log('PASS: recorded identity remains visible; a verified repair refreshes the open Session tab without reopening or resetting an unchanged iframe.');
}finally{await browser?.close();await new Promise(r=>server.close(r));}