PlotDirector/PlotLine.Tests/Browser/sql-audit-live.cjs

9 lines
3.1 KiB
JavaScript

const {chromium}=require('playwright'),fs=require('node:fs'),assert=require('node:assert/strict'),crypto=require('node:crypto');
const who=JSON.parse(fs.readFileSync(process.env.PD_SQL_IDENTITY)),scope=JSON.parse(fs.readFileSync(process.env.PD_SQL_SCOPE)),phase=process.env.PD_SQL_PHASE||'before',base='https://dev.plotdirector.com';
assert(who.Database==='PlotDirector_Development'&&/^pwa-browser-.*@example\.invalid$/.test(who.Email));
(async()=>{const browser=await chromium.launch();const results=[];try{for(const pwa of[false,true]){const context=await browser.newContext({viewport:{width:1666,height:1050}});await context.addInitScript(({user,pwa})=>{localStorage.setItem('plotdirector.desktop.v1.'+user,JSON.stringify({dismissedAt:Date.now()}));if(pwa)Object.defineProperty(navigator,'standalone',{value:true});},{user:who.UserID,pwa});const page=await context.newPage(),errors=[];page.on('pageerror',e=>errors.push(e.message));await page.goto(base+'/Account/Login');await page.locator('[name=Email]').fill(who.Email);await page.locator('[name=Password]').fill(who.Password);await Promise.all([page.waitForURL('**/Projects'),page.locator('button[type=submit]').click()]);
const paths={plot:`/PlotLineWorkspace?projectId=${scope.project}`,reader:`/Writer/Review?bookId=${scope.book}&chapterId=${scope.chapter}`,book:`/Books/Details/${scope.book}`,maps:`/Maps?projectId=${scope.project}&bookId=${scope.book}`};
if(!pwa)for(const [area,path] of Object.entries(paths))for(let repeat=0;repeat<6;repeat++){const start=performance.now(),response=await context.request.get(base+path),body=await response.body();assert.equal(response.status(),200,area);assert.equal(new URL(response.url()).pathname,new URL(base+path).pathname,area+' must not be an authentication redirect');results.push({area,repeat,ms:performance.now()-start,bytes:body.length,dbCommandCount:null,totalDbTimeMs:null,note:'Authenticated small synthetic fixture; HTTP includes network and server render; no per-request SQL interceptor available'});}
for(const [area,path] of Object.entries(paths)){const response=await page.goto(base+path);assert.equal(response.status(),200);assert.equal(new URL(response.url()).pathname,new URL(base+path).pathname);if(area==='reader'){await page.locator('[data-review-panel]').waitFor();assert(await page.locator('#reader-content [data-paragraph-id]').count()>100);const text=await page.locator('#reader-content').textContent();results.push({area,pwa,acceptedTextSha256:crypto.createHash('sha256').update(text).digest('hex')});}if(process.env.PD_SQL_SCREENSHOTS){fs.mkdirSync(process.env.PD_SQL_SCREENSHOTS,{recursive:true});await page.screenshot({path:process.env.PD_SQL_SCREENSHOTS+'/'+phase+'-'+area+'-'+(pwa?'PWA':'browser')+'.png'});}}
assert.deepEqual(errors,[]);console.log('PASS SQL audit authenticated '+(pwa?'simulated PWA':'browser')+' plot workspace, accepted Reader, book dashboard and Maps; only disposable author fixtures; all routes 200 and no page errors');await context.close();}fs.writeFileSync(process.env.PD_SQL_RESULTS,JSON.stringify(results,null,2));}finally{await browser.close();}})().catch(e=>{console.error(String(e.stack).replaceAll(who.Password,'[redacted]'));process.exitCode=1;});