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

16 lines
4.0 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()});});
await context.route('**/Writing/Context/EditRelationship*',async r=>{if(r.request().method()==='POST')posts++;await r.fulfill({response:await r.fetch({url:base+'/context-relationship-edit-fixture',method:'GET'})});});
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='family and narrative split';await page.selectOption('[data-context-tab]','relationships');await page.waitForSelector('[data-context-relationships]');assert.match(await page.locator('[data-context-relationships]').textContent(),/Family connections[\s\S]*Parent[\s\S]*Narrative connections[\s\S]*Friend/);assert.equal(await page.locator('[data-context-relationship]:visible').count(),2);await page.locator('[data-context-relationship="9"] summary').first().click();assert.match(await page.locator('[data-context-relationship="9"]').innerText(),/Directional connection/);assert.match(await page.locator('[data-context-relationship="9"]').innerText(),/no structured marker/);
step='focused relationship modal';await page.locator('[data-context-relationship="9"] [data-context-relationship-edit="scene"]').click();await page.waitForSelector('[data-context-edit-form]');assert.equal(await page.locator('[name=Notes]').inputValue(),'Saved scene note');await page.click('[data-context-edit-cancel]');assert.equal(posts,0);
step='independent filtering and responsive layouts';await page.selectOption('[data-context-layout]','columns');await page.selectOption('[data-context-pane="1"] [data-context-tab]','relationships');await page.locator('[data-context-pane="1"] [data-relationship-filter]').fill('Friend');assert.equal(await page.locator('[data-context-pane="1"] [data-context-relationship]:visible').count(),1);assert.equal(await page.locator('[data-context-pane="0"] [data-context-relationship]:visible').count(),2);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(40);assert(await page.evaluate(()=>document.documentElement.scrollWidth<=innerWidth+2));}}
assert.deepEqual(errors,[]);console.log('PASS Relationships browser: family/narrative categories, direction, explicit evidence limits, focused saved-value modal/cancel, independent filters and full/half/stacked/mobile layouts');
} catch(e){console.error('FAILED '+step+': '+e.stack);process.exitCode=1;}finally{await browser.close();}})();