PlotDirector/PlotLine.Tests/Browser/context-characters.cjs

20 lines
5.3 KiB
JavaScript

const {chromium}=require('playwright'),assert=require('node:assert/strict'),fs=require('node:fs'),path=require('node:path');
const base=process.env.PD_BROWSER_BASE||'http://127.0.0.1:5238';
(async()=>{const browser=await chromium.launch();let step='setup';try{
const context=await browser.newContext({viewport:{width:1366,height:768}});let posts=0,fail=true;
await context.route('**/cdn.jsdelivr.net/**signalr*',r=>r.fulfill({contentType:'application/javascript',body:fs.readFileSync(path.join(__dirname,'node_modules/@microsoft/signalr/dist/browser/signalr.min.js'))}));
for(const url of ['**/native-companion/status','**/native-companion/connection-state','**/Writing/Context/Position?*'])await context.route(url,r=>r.fulfill({json:url.endsWith('status')?[]:{connected:false,known:false}}));
await context.route('**/Writing/Context/Tabs/*',async r=>{const u=new URL(r.request().url());await r.fulfill({response:await r.fetch({url:base+'/context-tab-fixture/'+u.pathname.split('/').at(-1)+u.search})});});
await context.route('**/Writing/Context/EditCharacter*',async r=>{const post=r.request().method()==='POST';if(post)posts++;if(post&&!fail)return r.fulfill({json:{saved:true,bookId:12}});const response=await r.fetch({url:base+'/context-edit-fixture?field=description'+(post?'&invalid=true':''),method:'GET'});await r.fulfill({status:post?422:200,contentType:'text/html',body:await response.text()});});
const page=await context.newPage(),errors=[];page.on('pageerror',e=>errors.push(e.message));await page.goto(base+'/Writing/Context');await page.selectOption('[data-context-tab]','characters');await page.waitForSelector('[data-character-explorer]');
step='scene-first compact long catalogue';assert.equal(await page.locator('[data-context-character]').count(),12);assert.match(await page.locator('[data-context-character="5"]').innerText(),/POV/);assert.match(await page.locator('[data-context-character="6"] summary').innerText(),/Mentioned only/);assert.match(await page.locator('[data-context-character="5"]').innerText(),/Earlier hall[\s\S]*Saved room[\s\S]*Following garden/);
step='location canonical command and no-handler fallback';await page.evaluate(()=>{window.locationCommands=[];document.querySelector('[data-context-workspace]').addEventListener('context-command',e=>locationCommands.push(e.detail));});await page.locator('[data-context-character="5"] [data-context-location="77"]').click();await page.waitForSelector('[data-context-interaction-status] a');assert.deepEqual(await page.evaluate(()=>locationCommands[0]),{action:'location.focusAtStoryPoint',locationID:77,storyPoint:{chapterID:10,sceneID:22}});assert.equal(await page.locator('[data-context-tab]').first().inputValue(),'characters');
step='independent same-tool panes';await page.selectOption('[data-context-layout]','columns');await page.selectOption('[data-context-pane="1"] [data-context-tab]','characters');await page.locator('[data-context-pane="1"] [data-character-filter]').fill('name 10');assert.equal(await page.locator('[data-context-pane="1"] [data-context-character]:visible').count(),1);assert.equal(await page.locator('[data-context-pane="0"] [data-context-character]:visible').count(),12);
step='container widths, stacked, mobile, zoom';for(const width of [1366,760,390]){await page.setViewportSize({width,height:800});for(const layout of ['single','columns','rows']){await page.selectOption('[data-context-layout]',layout);await page.waitForTimeout(60);assert(await page.evaluate(()=>document.documentElement.scrollWidth<=innerWidth+2),width+'/'+layout);}}
await page.evaluate(()=>document.documentElement.style.zoom='1.25');assert(await page.evaluate(()=>document.documentElement.scrollWidth<=innerWidth+2));await page.evaluate(()=>document.documentElement.style.zoom='1');
step='correct values, cancel and draft protection';await page.selectOption('[data-context-layout]','single');await page.locator('[data-context-pane="0"] [data-context-edit="description"]').first().click();await page.waitForSelector('[data-context-edit-form]');assert.equal(await page.locator('[name=Value]').inputValue(),'Saved value');await page.locator('[name=Value]').fill('Entered draft');page.once('dialog',d=>d.dismiss());await page.click('[data-context-edit-cancel]');assert(await page.locator('[data-context-edit-dialog]').isVisible());assert.equal(posts,0);
step='failed save retains modal and draft inline';await page.click('[data-context-edit-form] [type=submit]');await page.waitForSelector('[data-context-edit-form] .validation-summary-errors');assert.equal(await page.locator('[name=Value]').inputValue(),'Entered draft');assert(await page.locator('[data-context-edit-dialog]').isVisible());
step='successful save closes with one success toast';fail=false;await page.evaluate(()=>{window.successes=0;window.PlotDirector.toast.success=()=>successes++;});await page.click('[data-context-edit-form] [type=submit]');await page.waitForFunction(()=>!document.querySelector('[data-context-edit-dialog]').open);assert.equal(await page.evaluate(()=>successes),1);assert.equal(posts,2);
assert.deepEqual(errors,[]);console.log('PASS Characters browser: twelve characters, independent panes, container layouts/mobile/zoom, modal values/cancel/unsaved protection/inline failure/single success (isolated HTTP save fixture; canonical persistence separately tested)');
} catch(e){console.error('FAILED '+step+': '+e.stack);process.exitCode=1;}finally{await browser.close();}})();