PlotDirector/PlotLine.Tests/Browser/scenario-themes.cjs

76 lines
6.2 KiB
JavaScript

// Real Razor views in the isolated browser host; no author data or database access.
const {chromium}=require('playwright');
const assert=require('node:assert/strict');
const fs=require('node:fs'),os=require('node:os'),path=require('node:path');
const base=process.env.PD_BROWSER_FIXTURE_URL||'http://127.0.0.1:5238';
const output=process.env.PD_SCENARIO_SCREENSHOTS||'/tmp/pd-scenario-visual';
fs.mkdirSync(output,{recursive:true});
(async()=>{
const profile=fs.mkdtempSync(path.join(os.tmpdir(),'pd-scenario-pwa-'));
const context=await chromium.launchPersistentContext(profile,{headless:false,viewport:{width:1440,height:1000}});
const cdp=await context.browser().newBrowserCDPSession();let installed=false;
const manifestId=base+'/Projects';
try {
const page=context.pages()[0];await page.goto(base+'/scenario-fixture/Timeline');
const themes=await page.evaluate(()=>window.PlotDirectorApplicationThemes);assert.equal(themes.length,7);
async function theme(p,key){
const choice=p.locator(`[data-app-theme-choice="${key}"]`);
if(!await choice.isVisible())await p.locator('#accountMenuButton').click();
await choice.click();await p.waitForFunction(t=>document.documentElement.dataset.appTheme===t,key);
await p.waitForFunction(()=>!document.querySelector('[data-app-theme-choice]').disabled);
await p.keyboard.press('Escape');await p.waitForTimeout(200);
}
async function check(p,mode){for(const view of ['Timeline','Compare','Create','Index']){
await p.goto(base+'/scenario-fixture/'+view);
for(const key of themes){await theme(p,key);await p.setViewportSize({width:1440,height:1000});
assert(await p.locator('.scenario-workspace').count());
if(view==='Timeline'){
assert.equal(await p.locator('.scenario-scene-card').count(),15);
assert(await p.locator('.scenario-scroll').evaluate(e=>e.scrollWidth>e.clientWidth));
await p.locator('.scenario-scroll').focus();await p.keyboard.press('ArrowRight');await p.locator('.scenario-scroll').evaluate(e=>e.scrollLeft=0);await p.locator('h1').click();
await p.locator('.scenario-chapter-form select').first().selectOption('3');
assert.equal(await p.locator('.scenario-chapter-form select').first().inputValue(),'3');
await p.locator('h1').click();await p.locator('.scenario-scroll').evaluate(e=>e.scrollLeft=0);
assert.equal(await p.locator('form[action="/Scenarios/MoveScene"]').count(),43);
}
if(view==='Compare'){assert(await p.getByText('Moved · Scenario',{exact:true}).isVisible());assert.equal(await p.locator('.scenario-position').count(),2);}
const contrasts=await p.evaluate(()=>{
const canvas=document.createElement('canvas'),ctx=canvas.getContext('2d');
const luminance=color=>{ctx.fillStyle=color;ctx.fillRect(0,0,1,1);return [...ctx.getImageData(0,0,1,1).data].slice(0,3).map(n=>n/255).map(n=>n<=.04045?n/12.92:((n+.055)/1.055)**2.4).reduce((sum,n,i)=>sum+n*[.2126,.7152,.0722][i],0);};
const results=[];
for(const selector of ['.scenario-banner h1','.scenario-banner p','.scenario-scene-card > p','.scenario-mini-data','.scenario-warning-badge','.attention-list strong','.scenario-position','.scenario-position-alternative','.scenario-chapter-form select','.scenario-scene-card .btn']){
const el=document.querySelector(selector);if(!el)continue;
let bgEl=el,bg;while(bgEl){bg=getComputedStyle(bgEl).backgroundColor;if(bg!=='rgba(0, 0, 0, 0)'&&bg!=='transparent')break;bgEl=bgEl.parentElement;}
const a=luminance(getComputedStyle(el).color),b=luminance(bg);results.push([selector,(Math.max(a,b)+.05)/(Math.min(a,b)+.05)]);
}return results;
});
for(const [selector,ratio]of contrasts)assert(ratio>=4.5,`${mode}/${view}/${key}/${selector}: contrast ${ratio}`);
await p.screenshot({path:path.join(output,`${mode}-${view}-${key}.png`),fullPage:true});
await p.setViewportSize({width:650,height:900});
if(view==='Timeline')await p.locator('.scenario-scroll').evaluate(e=>e.scrollLeft=0);
assert(await p.evaluate(()=>document.documentElement.scrollWidth<=innerWidth+1),`${mode}/${view}/${key}: page overflow`);
await p.screenshot({path:path.join(output,`${mode}-${view}-${key}-narrow.png`),fullPage:true});
}
await p.goto(base+'/scenario-fixture/'+view+'?empty=true');
for(const key of themes){await theme(p,key);if(view==='Compare')assert(await p.getByText('No scene movements yet.').isVisible());if(view==='Timeline')assert(await p.getByText('No scenes in this scenario.').isVisible());}
}}
// Reconstruct the legacy CSS presentation on the same synthetic Razor content.
if(process.env.PD_SCENARIO_BEFORE==='true'){
await context.route('**/css/site.css*',r=>r.fulfill({contentType:'text/css',body:fs.readFileSync('/tmp/pd-scenario-before-site.css','utf8')}));
await context.route('**/css/application-shell.css*',r=>r.fulfill({contentType:'text/css',body:fs.readFileSync('/tmp/pd-scenario-before-shell.css','utf8')}));
for(const view of ['Timeline','Compare'])for(const key of ['modern-light','modern-dark']){
await page.goto(base+'/scenario-fixture/'+view);await theme(page,key);
await page.screenshot({path:path.join(output,`before-css-${view}-${key}.png`),fullPage:true});
}
await context.unroute('**/css/site.css*');await context.unroute('**/css/application-shell.css*');
}
await check(page,'chromium');
await page.goto(base+'/scenario-fixture/Timeline');
await cdp.send('PWA.install',{manifestId,installUrlOrBundleUrl:base+'/scenario-fixture/Timeline'});installed=true;
await cdp.send('PWA.changeAppUserSettings',{manifestId,displayMode:'standalone'});
const opened=context.waitForEvent('page');await cdp.send('PWA.launch',{manifestId,url:base+'/scenario-fixture/Timeline'});const app=await opened;await app.waitForLoadState();
assert(await app.evaluate(()=>matchMedia('(display-mode: standalone)').matches));await check(app,'pwa');
console.log('PASS Scenario Razor views: '+themes.join(', ')+'; Chromium and installed PWA; board, Compare, Create, Index; 1440/650 widths; live token switching; populated/empty/warning states; chapter selectors and movement form contracts. Screenshots: '+output);
}finally{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;});