PlotDirector/PlotLine.Tests/Browser/application-domain-pwa-development.cjs

15 lines
3.5 KiB
JavaScript

// Real installed Chromium PWA against Development, with only a dedicated synthetic account.
const {chromium}=require('playwright'),fs=require('node:fs'),assert=require('node:assert/strict');const file=process.argv[2];assert(file?.startsWith('/tmp/pd-dev-browser-'));const i=JSON.parse(fs.readFileSync(file));assert.equal(i.Database,'PlotDirector_Development');assert(i.Email.startsWith('pwa-browser-'));
const base='https://dev.plotdirector.com',out=process.env.DOMAIN_EVIDENCE_DIR||'/tmp/plotdirector-app-domain-pwa';fs.mkdirSync(out,{recursive:true});
(async()=>{const profile=fs.mkdtempSync('/tmp/pd-domain-pwa-');const context=await chromium.launchPersistentContext(profile,{headless:false,viewport:{width:1440,height:1000}}),cdp=await context.browser().newBrowserCDPSession(),manifestId=base+'/Projects';const checks=[];let installed=false;try{
const tab=context.pages()[0];await tab.goto(base+'/Projects');await tab.locator('#Email').fill(i.Email);await tab.locator('#Password').fill(i.Password);await Promise.all([tab.waitForNavigation(),tab.locator('form button[type=submit]').click()]);assert.equal(new URL(tab.url()).pathname,'/Projects');
await cdp.send('PWA.install',{manifestId,installUrlOrBundleUrl:base+'/Projects'});installed=true;await cdp.send('PWA.changeAppUserSettings',{manifestId,displayMode:'standalone'});
const opened=context.waitForEvent('page');await cdp.send('PWA.launch',{manifestId});const app=await opened;await app.waitForLoadState();assert.equal(new URL(app.url()).pathname,'/Projects');assert(await app.evaluate(()=>matchMedia('(display-mode: standalone)').matches));checks.push('real installed PWA launches authenticated Projects by default');
assert.equal(await app.locator('[data-app-theme-choice]').count(),7);checks.push('all authenticated themes retained');await app.screenshot({path:out+'/installed-projects.png',fullPage:true});
await context.setOffline(true);const offline=await app.evaluate(async()=>{try{await fetch('/Projects?offline-acceptance=1',{cache:'no-store'});return false;}catch{return true;}});assert(offline,'no authenticated offline cached response');await context.setOffline(false);checks.push('offline requests fail honestly; no cached authenticated response');
const logout=app.locator('form[action="/Account/Logout"]').first();await Promise.all([app.waitForNavigation(),logout.evaluate(f=>f.requestSubmit())]);assert.equal(new URL(app.url()).pathname,'/Account/Login');assert.equal(await app.locator('html').getAttribute('data-site-theme'),'public');assert.equal(await app.locator('html').getAttribute('data-theme'),'light');checks.push('installed signed-out authentication keeps public brand');
await app.locator('#Email').fill(i.Email);await app.locator('#Password').fill(i.Password);await Promise.all([app.waitForNavigation(),app.locator('form button[type=submit]').click()]);assert.equal(new URL(app.url()).pathname,'/Projects');checks.push('installed sign-in returns Projects');
fs.writeFileSync(out+'/results.json',JSON.stringify({passed:true,installedChromium:true,checks,ownerPwaMigrated:false,physicalWordTested:false},null,2));console.log(JSON.stringify({passed:true,checks:checks.length,out}));
}catch(e){fs.writeFileSync(out+'/results.json',JSON.stringify({passed:false,checks,error:String(e)},null,2));throw e;}
finally{await context.setOffline(false).catch(()=>{});if(installed)await cdp.send('PWA.uninstall',{manifestId}).catch(()=>{});await context.close();fs.rmSync(profile,{recursive:true,force:true});}})().catch(e=>{console.error(e);process.exitCode=1;});