PlotDirector/PlotLine.Tests/Browser/writer3-investigation-diagnostics.cjs

38 lines
5.6 KiB
JavaScript

// Actual rendered MVC experience; uses exported engine witnesses, never client-side scoring.
const {chromium}=require('playwright'),fs=require('fs'),assert=require('node:assert/strict');
const base=process.env.PD_INV_BASE||'http://127.0.0.1:5238';
const identity=process.env.PD_INV_IDENTITY?JSON.parse(fs.readFileSync(process.env.PD_INV_IDENTITY)):null;
const w=identity||{UserID:4,book:12,other:13,project:7};
const report=JSON.parse(fs.readFileSync(process.env.PD_INV_TRACE||'/tmp/plotdirector-3d1a-after-traces.json'));
const wanted=['motivation','change','connections','organisation','next-event','uncertain','all-no','correlated','all-yes','competing'];
(async()=>{const browser=await chromium.launch();try{for(const pwa of[false,true]){
const c=await browser.newContext({viewport:{width:pwa?390:1440,height:pwa?844:1000}});await c.addInitScript(({u,pwa})=>{localStorage.setItem('plotdirector.desktop.v1.'+u,JSON.stringify({dismissedAt:Date.now()}));if(pwa)Object.defineProperty(navigator,'standalone',{value:true});},{u:w.UserID,pwa});
const p=await c.newPage(),errors=[],external=[];p.on('pageerror',e=>errors.push(e.message));p.on('request',r=>{if(/openai|anthropic|inference|generativelanguage/i.test(r.url()))external.push(r.url());});
if(identity){await p.goto(base+'/Account/Login');await p.locator('[name=Email]').fill(w.Email);await p.locator('[name=Password]').fill(w.Password);await Promise.all([p.waitForURL('**/Projects'),p.locator('button[type=submit]').click()]);}
for(const target of wanted){
const witness=report.paths.find(x=>x.concern==='story'&&x.target===target);assert(witness);
await p.goto(`${base}/Writer/Explore/Investigate?bookId=${w.book}`);await p.locator('[name=concern][value=story]').check();await p.getByRole('button',{name:'Begin',exact:true}).click();const url=p.url(),id=new URL(url).searchParams.get('id');
for(let i=0;i<witness.answers.length;i++){
const a=witness.answers[i],form=p.locator('[data-investigation-answer]');assert.equal(await form.locator('[name=question]').inputValue(),a.Question,target+' selected question');
await form.locator(`[name=option][value="${a.Option}"]`).check();
if(i===1){await form.locator('[name=explanation]').fill('Disposable diagnostic acceptance answer');await form.locator('button[value=save]').click();await p.goto(`${base}/Writer/Explore/Investigate?bookId=${w.book}`);await p.locator(`aside a[href*="${id}"]`).click();}
else await form.locator('button[value=answer]').click();
}
assert.equal(await p.locator('[data-investigation-answer]').count(),0,target+' terminal');const kind=await p.locator('#investigation-question').getAttribute('data-conclusion');
if(witness.final.Recommendations.length){assert.equal(kind,'supported');const form=p.locator('form[action$="/Open"]').first();const data=await form.evaluate(f=>Object.fromEntries(new FormData(f)));data.capability=await form.locator('[name=capability]').getAttribute('value');const r=await p.request.post(base+'/Writer/Explore/Investigate/Open',{form:data,maxRedirects:0});assert.equal(r.status(),302);const dest=r.headers().location;assert(!/solver|writing-problems/.test(dest));assert(dest.includes('projectId='+w.project)&&dest.includes('bookId='+w.book));await p.reload();}
else {assert.equal(await p.locator('form[action$="/Open"]').count(),0);assert.notEqual(kind,'supported');}
if(target==='uncertain')assert.equal(kind,'insufficient');if(target==='all-no')assert.equal(kind,'conflicting');if(target==='correlated')assert.equal(kind,'tentative');
assert(!(await p.locator('body').textContent()).includes("There isn't enough independent, consistent evidence"));assert.equal(await p.locator('iframe').count(),0);assert(await p.evaluate(()=>document.documentElement.scrollWidth<=innerWidth+2));
assert.equal((await p.request.get(`${base}/Writer/Explore/Investigate?bookId=${w.other||w.book+1}&id=${id}`)).status(),404);
const anon=await browser.newContext();const r=await anon.request.get(url,{maxRedirects:0});if(identity)assert.equal(r.status(),302);await anon.close();
if(!identity){const other=await browser.newContext();await other.addCookies([{name:'fixture-user',value:'5',url:base}]);assert.equal((await other.request.get(url)).status(),404);await other.close();}
if(process.env.PD_INV_SCREENSHOTS&&!pwa&&['motivation','uncertain','all-no','correlated','competing'].includes(target)){fs.mkdirSync(process.env.PD_INV_SCREENSHOTS,{recursive:true});await p.screenshot({path:process.env.PD_INV_SCREENSHOTS+'/'+target+'.png'});}
if(target==='motivation'){
await p.goto(url+'&question=motivation-4');await p.locator('[name=option][value=yes]').check();await p.locator('button[value=answer]').click();assert(await p.locator('[data-investigation-answer]').count()>0);await p.reload();assert.equal(await p.locator('[name=question]').inputValue(),'motivation-clarify');
if(!identity){await c.addCookies([{name:'solver-reviewer',value:'true',url:base}]);const d=await(await p.request.get(`${base}/admin/writer-investigations/diagnostics?bookId=${w.book}&id=${id}`)).json();assert(d.trace.conclusionReasons.conflictingEvidence);assert(d.trace.questions.some(q=>q.reason==='already answered'));assert(d.trace.blockers.length===5);const sim=await p.request.get(`${base}/admin/writer-investigations/simulator?bookId=${w.book}&id=${id}`);assert(!(await sim.text()).includes('matches: False'));await c.clearCookies({name:'solver-reviewer'});}
}
console.log(`PASS ${identity?'Development':'isolated'} ${pwa?'simulated PWA':'browser'} ${target}: ${kind}, save/resume, private scope, original tool context`);
}
assert.equal(external.length,0);assert.deepEqual(errors,[]);await c.close();
} }finally{await browser.close();}})().catch(e=>{console.error(e.stack);process.exitCode=1;});