PlotDirector/PlotLine.Tests/Browser/desktop-promotion.cjs
Nick Beckley e48889ac05 Complete desktop workspaces, context menus and plot health
Commit the pending application, help and browser regression work. Pin the patched PKCS dependency for the production release.
2026-10-31 09:27:04 +00:00

84 lines
7.0 KiB
JavaScript

const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const http = require('node:http');
const { chromium } = require('playwright');
const root = path.resolve(__dirname, '../../PlotLine/wwwroot');
const server = http.createServer((req,res) => {
if (req.url.startsWith('/Timeline')) {
res.setHeader('Content-Type','text/html');
res.end(`<html data-app-theme="modern-dark"><head><script src="/js/application-display-mode.js"></script><link rel="stylesheet" href="/css/application-shell.css"><link rel="stylesheet" href="/css/desktop-promotion.css"></head><body data-desktop-user="42" data-desktop-eligible="${!req.url.includes('new')}"><button id="accountMenuButton" data-pwa-install>Desktop</button><div id="workspace" style="height:500px">Workspace</div><script src="/js/desktop-promotion.js"></script><script src="/js/application-pwa.js"></script></body></html>`);
} else {
const file = path.join(root,req.url.split('?')[0]);
if (!fs.existsSync(file) || !fs.statSync(file).isFile()) return res.writeHead(404).end();
res.setHeader('Content-Type',file.endsWith('.js')?'application/javascript':file.endsWith('.css')?'text/css':'image/png');
res.end(fs.readFileSync(file));
}
});
(async()=>{
await new Promise(r=>server.listen(0,'127.0.0.1',r));
const browser=await chromium.launch();
const base=`http://127.0.0.1:${server.address().port}`;
try {
const context=await browser.newContext({reducedMotion:'reduce',viewport:{width:1440,height:1100}});
const page=await context.newPage();
await page.goto(base+'/Timeline?new');
await page.waitForTimeout(2000);
assert.equal(await page.locator('dialog').count(),0,'new users are not interrupted');
await page.goto(base+'/Timeline');
await page.locator('#accountMenuButton').focus();
await page.locator('dialog').waitFor();
await page.waitForFunction(()=>[...document.querySelectorAll('dialog img')].every(i=>i.complete&&i.naturalWidth===1710));
assert.equal(await page.inputValue('dialog input'), '68');
const geometry=await page.locator('dialog img').evaluateAll(imgs=>imgs.map(i=>{const r=i.getBoundingClientRect();return [r.x,r.y,r.width,r.height]}));
assert.deepEqual(geometry[0],geometry[1]);
await page.locator('dialog input').focus(); await page.keyboard.press('ArrowRight');
assert.equal(await page.inputValue('dialog input'),'69');
// The keyboard slider sits below the image at shorter heights. Pointer focus must not reveal it by scrolling.
await page.setViewportSize({width:1440,height:900});
await page.locator('dialog').evaluate(d=>{d.querySelector('.pd-desktop-close').focus({preventScroll:true});d.scrollTop=0;});
const scrollBefore=await page.locator('dialog').evaluate(d=>d.scrollTop);
const box=await page.locator('.pd-desktop-comparison').boundingBox();
await page.mouse.move(box.x+box.width*.7,box.y+80); await page.mouse.down(); await page.mouse.move(box.x+box.width*.3,box.y+80); await page.mouse.up();
assert.equal(await page.locator('dialog').evaluate(d=>d.scrollTop),scrollBefore,'dragging must preserve modal scroll position');
assert(await page.locator('dialog input').evaluate(e=>document.activeElement===e),'divider keeps keyboard focus');
assert(Math.abs(Number(await page.inputValue('dialog input'))-30)<2);
await page.click('[data-desktop-install]');
assert.match(await page.locator('[data-install-status]').textContent(),/Chrome|browser/);
await page.evaluate(()=>{const e=new Event('beforeinstallprompt',{cancelable:true});e.prompt=async()=>window.prompted=true;e.userChoice=Promise.resolve({outcome:'accepted'});dispatchEvent(e)});
await page.click('[data-desktop-install]');
assert(await page.evaluate(()=>window.prompted));
assert(!/Desktop is ready/.test(await page.locator('[data-install-status]').textContent()));
await page.keyboard.press('Escape');
assert.equal(await page.evaluate(()=>document.activeElement.id),'accountMenuButton');
await page.reload(); await page.waitForTimeout(2000); assert.equal(await page.locator('dialog[open]').count(),0);
await page.click('#accountMenuButton'); assert(await page.locator('dialog').isVisible());
// Native modal tab cycling keeps focus inside.
for(let i=0;i<12;i++){await page.keyboard.press('Tab');assert(await page.evaluate(()=>!!document.activeElement.closest('dialog')))}
for(const theme of ['warm-light','modern-light','modern-dark','warm-dark','rose-pink','baby-blue']){
await page.evaluate(t=>document.documentElement.dataset.appTheme=t,theme);
assert.equal(await page.locator('dialog').evaluate(d=>d.scrollWidth<=d.clientWidth+1),true);
}
await page.setViewportSize({width:390,height:844});const actions=await page.locator('dialog footer').boundingBox();assert(actions.y+actions.height<=844,'actions stay visible on narrow screens');
assert(await page.locator('.pd-desktop-viewport').evaluate(e=>e.scrollWidth>e.clientWidth));
assert(await page.locator('dialog').evaluate(e=>e.scrollWidth<=e.clientWidth+1));
await page.evaluate(()=>{window.closedDialog=new Promise(r=>document.querySelector('dialog').addEventListener('close',r,{once:true}));});
await page.keyboard.press('Escape');await page.evaluate(()=>window.closedDialog);
await page.evaluate(()=>localStorage.setItem('plotdirector.desktop.v1.42',JSON.stringify({shownAt:Date.now()-31*86400000,dismissedAt:Date.now()-31*86400000})));
await page.reload(); assert(await page.locator('.pd-desktop-reminder').isVisible()); assert.equal(await page.locator('dialog').count(),0);
await page.locator('.pd-desktop-reminder .btn-primary').click();
await page.evaluate(()=>dispatchEvent(new Event('appinstalled')));
assert.match(await page.locator('[data-install-status]').textContent(),/Desktop is ready/);
assert(await page.locator('[data-desktop-install]').isHidden());
await context.close();
const standalone=await browser.newContext();
await standalone.addInitScript(()=>Object.defineProperty(navigator,'standalone',{value:true}));
const app=await standalone.newPage(); let screenshots=0;
app.on('request',r=>{if(r.url().includes('/images/desktop/'))screenshots++});
await app.goto(base+'/Timeline'); await app.waitForTimeout(2100);
await app.evaluate(()=>window.PlotDirectorDesktop.open(true));
assert.equal(await app.locator('dialog').count(),0); assert.equal(screenshots,0);
console.log('PASS desktop promotion: eligibility, assets/alignment, pointer/keyboard, reduced motion, focus/Escape, native install/fallback, persistence/reminder, themes/narrow viewport, standalone exclusion.');
} finally {await browser.close();await new Promise(r=>server.close(r));}
})().catch(e=>{console.error(e);process.exitCode=1});