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

16 lines
3.7 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='canonical names aliases and shared spellings';await page.selectOption('[data-context-tab]','names');await page.waitForSelector('[data-context-names]');assert.match(await page.locator('[data-context-names]').textContent(),/Known variant/);assert.match(await page.locator('[data-context-names]').textContent(),/Shared spelling/);await page.locator('[data-name-filter]').fill('Known variant');assert.equal(await page.locator('[data-name-row]:visible').count(),1);await page.locator('[data-name-row]:visible summary').click();
step='structured character focus without replacing names';await page.evaluate(()=>{window.sent=[];document.querySelector('[data-context-workspace]').addEventListener('context-command',e=>sent.push(e.detail));});await page.locator('[data-name-focus="5"]').click();assert.equal(await page.evaluate(()=>sent[0].characterID),5);assert.equal(await page.locator('[data-context-tab]').first().inputValue(),'names');
step='focused alias modal and full editor access';await page.locator('[data-context-edit="alias:11"]').click();await page.waitForSelector('[data-context-edit-form]');await page.click('[data-context-edit-cancel]');assert.equal(posts,0);assert(await page.locator('a[href*="/Names?projectId=7"]').count());
step='mobile and same-tool independence';await page.selectOption('[data-context-layout]','columns');await page.selectOption('[data-context-pane="1"] [data-context-tab]','names');await page.locator('[data-context-pane="1"] [data-name-filter]').fill('name 10');assert.equal(await page.locator('[data-context-pane="0"] [data-name-filter]').inputValue(),'Known variant');for(const width of [1366,760,390]){await page.setViewportSize({width,height:800});await page.waitForTimeout(50);assert(await page.evaluate(()=>document.documentElement.scrollWidth<=innerWidth+2));}
assert.deepEqual(errors,[]);console.log('PASS Names browser: canonical/alias lookup, shared spelling review, structured focus, focused alias form/cancel, full workspace link, mobile and independent panes');
} catch(e){console.error('FAILED '+step+': '+e.stack);process.exitCode=1;}finally{await browser.close();}})();