Files
pipelines/web/test/session-audit-browser.mjs
T

60 lines
5.5 KiB
JavaScript

// 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=`<!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
${["design/tokens/tokens","base","shell","lists","run-detail","chat","dialogs"].map(n=>`<link rel="stylesheet" href="/${n}.css">`).join("")}
<div class="app-root"><main></main></div><script type="module">
import {createModalController} from '/src/modal.js';
import {createChatView} from '/src/views/chat.js';
import {createDialogsView} from '/src/views/dialogs.js';
import {createRunDetailView} from '/src/views/runDetail.js';
const step={sequence:1,stage:'Survey',status:'completed',outcome:'success',model:'fixture',session_id:'ses_exact',session_export:{session_id:'ses_exact',status:'failed',diagnostic:'native readback failed'},artifacts:[]};
const run={id:19,pipeline_id:25,version:6,status:'succeeded',name:'Audit fixture',graph:{stages:[{key:'Survey',type:'model',edges:{success:'#success'}}]},steps:[step]};
const state={url:'ws://'+location.host+'/connect',runDetail:run,runStageSections:new Set(),runStagePane:{stageKey:'Survey',visitIndex:0}};
window.repairRequests=[];
const rt={state,modal:createModalController(),views:{},repo:{repairSessionExport:async(id,sequence)=>{repairRequests.push({id,sequence});window.recordRepair();return state.runDetail;}},renderContent:()=>{}};
rt.views.chat=createChatView(rt);rt.views.dialogs=createDialogsView(rt);rt.views.runDetail=createRunDetailView(rt);
document.querySelector('main').append(rt.views.runDetail.renderStagePane(run));rt.modal.reconcile();
window.recordRepair=()=>{const updated={...step,session_export_repairs:[{session_id:'ses_exact',status:'complete',messages:14,html:'session-repair-1-fixed.html',html_bytes:100}],artifacts:[{name:'session-repair-1-fixed.html',url:'/artifacts/19/1/session-repair-1-fixed.html',content_type:'text/html',size_bytes:100}]};state.runDetail={...run,steps:[updated]};rt.views.dialogs.refreshStageAudit();};
window.parkForExport=()=>{state.runDetail={...run,status:'waiting',export_pending_sequence:1,steps:[{...step,session_export_recovery:{status:'pending'}}]};rt.views.dialogs.refreshStageAudit();};
</script>`;
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('<h1>Recorded session ses_exact</h1>');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));}