PlotDirector/PlotLine.Tests/Browser/record-workspaces.cjs

75 lines
7.8 KiB
JavaScript

// Real compiled Razor forms; loopback fixture has no database/workers.
const { chromium }=require('playwright');
const assert=require('node:assert/strict');
const base='http://127.0.0.1:5238';
const records=['Projects','Books','Chapters','Characters','Locations','StoryAssets','PlotLines','PlotThreads','ProjectMetrics'];
async function frames(page){await page.evaluate(()=>new Promise(r=>requestAnimationFrame(()=>requestAnimationFrame(r))))}
async function recordGroup(page){const group=page.locator('.pd-context-group[aria-label="Record"] details[data-compact="true"]');if(await group.count()&&await group.getAttribute('open')===null)await group.locator('summary').click()}
(async()=>{
const browser=await chromium.launch();const failures=[];
try{
const web=await browser.newContext({viewport:{width:1920,height:1080}});const tab=await web.newPage();tab.on('pageerror',e=>failures.push(e.message));
for(const record of records){
const response=await tab.goto(`${base}/fixture?record=${record}`);assert.equal(response.status(),200,record);await frames(tab);
assert.equal(await tab.evaluate(()=>window.PlotDirectorPwa.standalone),false);
assert(await tab.locator('main button[type=submit]').filter({hasText:/Save/}).isVisible(),record+' browser Save remains');
if(await tab.locator('.pd-record-fields').count())assert.equal(await tab.locator('.pd-record-fields').evaluate(e=>getComputedStyle(e).display),'flex');
const width=await tab.locator('main form.pd-record-form').first().evaluate(e=>e.getBoundingClientRect().width);assert(width<=980.5,record+' browser width unchanged');
}
// Compare against the existing Browser CSS at resized widths, rather than treating Desktop rules as a Browser redesign.
await tab.goto(base+'/fixture?record=Characters');await tab.addStyleTag({content:'.pd-record-group-label{display:none}'});
for(const width of [1440,800,390]){
await tab.setViewportSize({width,height:1080});await frames(tab);
const sample=()=>tab.locator('main form.pd-record-form').evaluate(e=>({width:e.clientWidth,scroll:e.scrollWidth,fields:[...e.querySelectorAll('.form-control,.form-select')].map(n=>{const r=n.getBoundingClientRect();return[r.x,r.y,r.width,r.height]})}));
const before=await sample();await tab.locator('link[href="/css/record-workspace.css"]').evaluate(e=>e.disabled=true);await frames(tab);assert.deepEqual(await sample(),before,'Browser layout unchanged at '+width);await tab.locator('link[href="/css/record-workspace.css"]').evaluate(e=>e.disabled=false);
assert(before.scroll<=before.width+1,'Browser character image controls fit the form at '+width+'px');
}
const desktop=await browser.newContext({viewport:{width:1920,height:1080}});await desktop.addInitScript(()=>Object.defineProperty(navigator,'standalone',{value:true}));const app=await desktop.newPage();app.on('pageerror',e=>failures.push(e.message));
for(const record of records){
assert.equal((await app.goto(`${base}/fixture?record=${record}`)).status(),200,record);await frames(app);
await recordGroup(app);
const save=app.locator('[data-pwa-context-controls] button').filter({hasText:/^Save /});assert.equal(await save.count(),1,record+' single Save');assert(await save.isVisible());assert(await save.locator('svg').count());
assert(await app.locator('main button[type=submit]').filter({hasText:/Save/}).isHidden(),record+' source accounted for');
if(await app.locator('.pd-record-fields').count()){
assert.equal(await app.locator('.pd-record-fields').evaluate(e=>getComputedStyle(e).display),'grid');
assert((await app.locator('main form.pd-record-form').first().boundingBox()).width>1200,record+' canvas uses available space');
}
for(const width of [1440,800,390]){
await app.setViewportSize({width,height:1000});await frames(app);
assert(await app.locator('main form.pd-record-form').first().evaluate(e=>e.scrollWidth<=e.clientWidth+1),record+' no ordinary form overflow at '+width);
}
await app.setViewportSize({width:1920,height:1080});
}
await app.goto(base+'/fixture?record=Books');await recordGroup(app);
// Invalid submit reaches existing unobtrusive field validation, and a valid submit reaches the original form/antiforgery fields.
await app.locator('[name=BookTitle]').fill('');await app.locator('[data-pwa-context-controls] button').filter({hasText:'Save book'}).click();
await app.locator('[data-valmsg-for=BookTitle].field-validation-error').waitFor();
assert(await app.locator('[data-valmsg-for=BookTitle]').isVisible());
await app.locator('[name=BookTitle]').fill('Revised book');
const posted=app.waitForRequest(r=>r.method()==='POST'&&new URL(r.url()).pathname==='/Books/Save');
await app.route('**/Books/Save',r=>r.fulfill({contentType:'text/html',body:'Saved fixture'}));
await recordGroup(app);await app.locator('[data-pwa-context-controls] button').filter({hasText:'Save book'}).click();
const body=(await posted).postData();assert.match(body,/name="BookTitle"\r\n\r\nRevised book/);assert.match(body,/name="BookID"\r\n\r\n12/);assert(body.includes('name="__RequestVerificationToken"'));
await app.goto(base+'/fixture?record=Books');await app.locator('[name=BookTitle]').fill('Enter-saved book');
const enterPost=app.waitForRequest(r=>r.method()==='POST'&&new URL(r.url()).pathname==='/Books/Save');await app.locator('[name=BookTitle]').press('Enter');assert((await enterPost).postData().includes('Enter-saved book'));
await app.goto(base+'/fixture?record=Characters');await app.setViewportSize({width:3200,height:1080});
assert(await app.locator('form.pd-record-form').evaluate(e=>e.getBoundingClientRect().width<=parseFloat(getComputedStyle(e).maxWidth)+1),'ultra-wide bounded form');
await app.setViewportSize({width:1920,height:1080});
for(const theme of ['warm-light','modern-light','modern-dark','warm-dark','rose-pink','baby-blue']){
await app.evaluate(t=>{const r=document.documentElement;r.dataset.appTheme=t;r.dataset.theme=t.endsWith('-dark')?'dark':'light';r.dataset.bsTheme=r.dataset.theme;dispatchEvent(new CustomEvent('plotdirector:theme-changed',{detail:{theme:t}}));},theme);await app.screenshot({path:`/tmp/pd-record-characters-${theme}.png`,fullPage:true});
assert(await app.locator('[name=CharacterName]').evaluate(e=>getComputedStyle(e).backgroundColor!=='rgba(0, 0, 0, 0)'));
}
// New layouts are stable before activation, not assembled by a late DOM pass.
let release, reached;const gate=new Promise(r=>release=r),blocked=new Promise(r=>reached=r);
await app.route('**/record-parser-gate.js',async r=>{reached();await gate;await r.fulfill({contentType:'application/javascript',body:''})});
await app.route('**/fixture?record=Books&paint=1',async r=>{const response=await r.fetch();await r.fulfill({response,body:(await response.text()).replace('</main>','<script src="/record-parser-gate.js"></script></main>')})});
const loading=app.goto(base+'/fixture?record=Books&paint=1');await blocked;await frames(app);
const geometry=()=>app.locator('main form.pd-record-form').first().evaluate(e=>({width:e.getBoundingClientRect().width,fields:[...e.querySelectorAll('.pd-record-fields>div')].map(n=>{const r=n.getBoundingClientRect();return[r.x,r.y,r.width,r.height]})}));
const before=await geometry();assert.equal(await app.evaluate(()=>document.readyState),'loading');
assert.equal(await app.locator('[data-pwa-context-controls] button').filter({hasText:'Save book'}).count(),1);
release();await loading;await frames(app);assert.deepEqual(await geometry(),before,'record layout unchanged across activation');
assert.deepEqual(failures,[]);
console.log('PASS record workspaces: nine real Razor forms, Browser widths/actions, Desktop grid/ribbon icons, 1920/1440/800/390 widths, validation and original form POST, all six themes, stable parser-paused first paint.');
}finally{await browser.close()}
})().catch(e=>{console.error(e);process.exitCode=1});