PlotDirector/PlotLine.Tests/Browser/writing-context-components.cjs

24 lines
6.6 KiB
JavaScript

const {chromium}=require('playwright'),assert=require('node:assert/strict'),path=require('node:path');
const base=process.env.PD_BROWSER_BASE||'http://127.0.0.1:5238';
(async()=>{const browser=await chromium.launch();try{
for(const width of [430,1440])for(const theme of ['modern-light','modern-dark','warm-light','warm-dark']){
const page=await browser.newPage({viewport:{width,height:900}});let commands=0;const requests={plots:0,metrics:0,map:0};
await page.route('**/Writing/Context/Navigate',async r=>{commands++;await r.fulfill({json:{message:'Navigation requested'}});});
await page.route('**/Writing/Context/Scene?*',async r=>{const id=new URL(r.request().url()).searchParams.get('sceneId');await r.fulfill({response:await r.fetch({url:base+'/writing-desk-fixture/context?fragment=true&floor=true&sceneId='+id})});});
await page.route(/\/Writing\/Context\/(PlotLines|Metrics|Map)\?/,async r=>{const url=new URL(r.request().url()),kind=url.pathname.endsWith('PlotLines')?'plots':url.pathname.endsWith('Metrics')?'metrics':'map';requests[kind]++;await r.fulfill({response:await r.fetch({url:base+'/writing-context-component/'+kind+url.search})});});
await page.goto(base+'/writing-desk-fixture/context?floor=true');
await page.evaluate(theme=>{document.documentElement.dataset.appTheme=theme;document.documentElement.dataset.theme=theme.endsWith('dark')?'dark':'light';const root=document.querySelector('main');root.dataset.writingContext='';root.dataset.contextUrl='/Writing/Context/Scene';root.dataset.scopeProjectId='7';root.dataset.scopeBookId='12';const host=document.createElement('section');host.dataset.writingContextContent='';while(root.firstChild)host.append(root.firstChild);root.append(host);const status=document.createElement('p');status.dataset.writingContextStatusText='';root.prepend(status);const input=document.createElement('input');input.type='hidden';input.name='__RequestVerificationToken';input.value='fixture';root.append(input);},theme);
for(const script of ['story-position','writing-follow','maps-engine','writing-context-map','writing-context-plots','writing-context-components','writing-context'])await page.addScriptTag({path:path.resolve(__dirname,'../../PlotLine/wwwroot/js/'+script+'.js')});
const beat=async id=>{await page.evaluate(id=>window.dispatchEvent(new CustomEvent('native-companion-presence',{detail:[{instanceId:'fixture',sessionId:'word',bookId:12,chapterId:id===23?11:10,sceneId:id,active:true,wordAvailable:true,wordSaved:true,expires:new Date(Date.now()+45000).toISOString()}]})),id);await page.waitForFunction(id=>document.querySelector('[data-writing-context-fragment]').dataset.sceneId===String(id),id);};
await page.click('[data-writing-context-tab=plots]');await page.waitForSelector('[data-plot-line]');assert.ok(await page.getByText('The letter changes hands',{exact:false}).count());
await page.click('[data-plot-category=elsewhere]');await page.locator('[data-plot-scrubber]').fill('1');assert.match(await page.locator('[data-plot-position]').textContent(),/Exploring/);assert.equal(commands,0,'scrubbing cannot navigate Word');
await page.evaluate(()=>window.savedPlot=document.querySelector('[data-writing-component=plots]'));await beat(23);assert.ok(await page.evaluate(()=>savedPlot===document.querySelector('[data-writing-component=plots]')));assert.equal(await page.locator('[data-plot-category=elsewhere]').getAttribute('aria-pressed'),'true');assert.equal(requests.plots,1,'scene following retains loaded plot data and filters');await page.locator('[data-writing-component=plots] [data-context-refresh]').click();await page.waitForFunction(()=>document.querySelector('[data-writing-component=plots]').dataset.loaded==='true');assert.equal(requests.plots,2);assert.equal(await page.locator('[data-plot-category=elsewhere]').getAttribute('aria-pressed'),'true');assert.equal(await page.locator('[data-plot-scene="1"]').getAttribute('aria-current'),'location');
await page.click('[data-writing-context-tab=metrics]');await page.waitForSelector('[data-context-metric-select]');await page.selectOption('[data-context-metric-select]','2');await page.locator('[data-writing-component=metrics] [data-context-scrubber]').fill('1');assert.equal(await page.locator('[data-context-metric="1"] circle').count(),1,'missing saved intensity is not a default point');assert.equal(await page.locator('[data-context-metric="2"] circle').count(),1);await beat(22);assert.equal(await page.inputValue('[data-context-metric-select]'),'2');assert.equal(requests.metrics,1);assert.equal(commands,0);
await page.click('[data-writing-context-tab=maps]');await page.waitForSelector('[data-context-map-canvas]');assert.equal(await page.locator('[data-map-feature]').count(),2);await page.selectOption('[data-context-map-feature]','11111111-1111-4111-8111-111111111111');assert.match(await page.locator('[data-context-map-detail]').textContent(),/Ten minutes/);await page.click('[data-context-map-zoom=".8"]');await page.selectOption('[data-context-map-lens]','people');const viewport=await page.locator('[data-context-map-canvas]').getAttribute('viewBox');await beat(23);await page.waitForFunction(()=>document.querySelector('[data-writing-component=map]').dataset.loaded==='true'&&JSON.parse(document.querySelector('[data-context-map-data]').textContent).sceneID===23);assert.equal(await page.locator('[data-context-map-canvas]').getAttribute('viewBox'),viewport,'map viewport survives scene refresh');assert.equal(await page.inputValue('[data-context-map-lens]'),'people');assert.equal(commands,0);
await page.selectOption('[data-context-map-choice]','2');await page.waitForSelector('[data-writing-view-state="map-viewport-2"]');assert.equal(await page.locator('[data-map-feature]').count(),2,'changing maps rebinds read-only canvas controls');
await page.click('[data-writing-context-tab=place]');assert.equal(await page.locator('.is-current-room').count(),1);await page.locator('[data-context-floor-zoom]').fill('150');await beat(22);assert.equal(await page.inputValue('[data-context-floor-zoom]'),'150');assert.equal(await page.locator('.writing-context-floor').evaluate(n=>n.style.getPropertyValue('--floor-zoom')),'1.5');
assert.equal(await page.locator('iframe').count(),0);assert.equal(await page.evaluate(()=>document.documentElement.scrollWidth>innerWidth+2),false,`component overflow ${width} ${theme}`);await page.close();
}
console.log('PASS actual Razor Plot Lines, recorded-only metrics, exploratory scrubbing, stable loaded panels, map lenses/relationships/zoom, map switching, floor room mapping and persistent zoom across scenes, two widths and all four themes; no exploratory Word commands');
}finally{await browser.close();}})().catch(e=>{console.error(e);process.exit(1);});