42 lines
7.8 KiB
JavaScript
42 lines
7.8 KiB
JavaScript
const {chromium}=require('playwright'),assert=require('node:assert/strict'),path=require('node:path');
|
|
const base='http://127.0.0.1:5238';
|
|
(async()=>{const browser=await chromium.launch();try{
|
|
for(const pwa of [false,true])for(const embedded of [false,true]){
|
|
const ctx=await browser.newContext({viewport:{width:1440,height:1000}});if(pwa)await ctx.addInitScript(()=>Object.defineProperty(navigator,'standalone',{value:true}));const page=await ctx.newPage();let reads=0,mutations=0,navigation=0;
|
|
page.on('request',r=>{if(!['GET','HEAD'].includes(r.method())&&!r.url().includes('/hubs/'))mutations++;});
|
|
await page.route('**/signalr.min.js',r=>r.fulfill({contentType:'application/javascript',body:require('fs').readFileSync(require.resolve('@microsoft/signalr/dist/browser/signalr.min.js'),'utf8')}));
|
|
await page.route('**/Writing/Context/Navigate',r=>{navigation++;return r.fulfill({json:{message:'Navigation requested'}});});
|
|
await page.route('**/native-companion/status',r=>r.fulfill({json:[]}));
|
|
await page.route('**/native-companion/connection-state',r=>r.fulfill({json:{state:'Disconnected',known:false}}));
|
|
await page.route('**/Writing/Context/Scene?*',async r=>r.fulfill({response:await r.fetch({url:base+'/writing-desk-fixture/context?fragment=true&sceneId='+new URL(r.request().url()).searchParams.get('sceneId')})}));
|
|
await page.route('**/Writing/Context/PlotLines?*',async r=>{reads++;await r.fulfill({response:await r.fetch({url:base+'/writing-context-component/plots?contextCases=true'})});});
|
|
await page.goto(base+'/Writing/Context?bookId=12'+(embedded?'&embedded=true':''));
|
|
// Complete the initial real status poll before injecting simulated native presence.
|
|
// A delayed disconnected packet otherwise cancels the first scene request.
|
|
await page.waitForFunction(()=>Array.isArray(document.querySelector('#native-companion-status')?.nativePresence));
|
|
await page.evaluate(()=>{const root=document.querySelector('[data-writing-context]');root.dataset.followMode='live';});
|
|
// The normal Context fragment and presence event path choose a saved scene.
|
|
const beat=async id=>{await page.evaluate(id=>window.dispatchEvent(new CustomEvent('native-companion-presence',{detail:[{instanceId:'fixture',sessionId:'doc',bookId:12,chapterId:10,sceneId:id,active:true,wordSaved:true,wordAvailable: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 beat(23);await page.click('[data-writing-context-tab=plots]');await page.waitForSelector('[data-plot-context-data]',{state:'attached'});await page.waitForFunction(()=>document.querySelectorAll('[data-plot-line]').length===6);
|
|
assert.equal(await page.locator('[data-plot-count=active]').textContent(),'(3)');assert.equal(await page.locator('[data-plot-count=nearby]').textContent(),'(3)');assert.equal(await page.locator('[data-plot-count=elsewhere]').textContent(),'(1)');
|
|
assert.equal(await page.locator('[data-plot-line="6"]').count(),0);assert.equal(await page.locator('[data-plot-line="5"] .is-now').textContent().then(s=>s.includes('No recorded development')),true);
|
|
const active=page.locator('[data-plot-line="1"]');await active.getByText('Current first',{exact:true}).waitFor();await active.getByText('Current second',{exact:true}).waitFor();assert.match(await active.locator('.is-before').textContent(),/Earlier active development/);assert.match(await active.locator('.is-after').textContent(),/Upcoming in manuscript order.*Upcoming active development/);
|
|
assert.match(await page.locator('[data-plot-line="2"] .is-after').textContent(),/No later recorded development/);assert.match(await page.locator('[data-plot-line="3"] .is-before').textContent(),/No earlier recorded development/);
|
|
assert.match(await page.locator('[data-plot-line="4"] .writing-plot-status').textContent(),/previous 1 scene ago.*next in 5 scenes/);
|
|
await page.click('[data-plot-all]');assert.equal(await page.locator('[data-plot-line]').count(),7);assert.equal(await page.locator('[data-plot-scene-strip] button').count(),6);
|
|
for(const theme of ['modern-light','modern-dark','warm-light','warm-dark'])for(const width of [390,768,1710]){
|
|
await page.evaluate(t=>{document.documentElement.dataset.appTheme=t;document.documentElement.dataset.theme=t.endsWith('dark')?'dark':'light';document.documentElement.dataset.bsTheme=document.documentElement.dataset.theme;},theme);await page.setViewportSize({width,height:1000});assert.equal(await page.evaluate(()=>document.documentElement.scrollWidth>innerWidth+2),false,`${theme} ${width}`);
|
|
const colors=await active.locator('.is-now').evaluate(el=>{const probe=document.createElement('span');el.append(probe);probe.style.color='var(--pd-selected)';const expected=getComputedStyle(probe).color;probe.remove();return [getComputedStyle(el).backgroundColor,expected];});assert.equal(colors[0],colors[1]);const stripColors=await page.locator('[data-plot-scene-strip] [aria-current]').evaluate(el=>{const p=document.createElement('i');el.append(p);p.style.color='var(--pd-accent)';const accent=getComputedStyle(p).color;p.style.color='var(--pd-on-accent)';const foreground=getComputedStyle(p).color;p.remove();return [getComputedStyle(el).backgroundColor,accent,getComputedStyle(el).color,foreground];});assert.equal(stripColors[0],stripColors[1]);assert.equal(stripColors[2],stripColors[3]);
|
|
if(process.env.PD_PLOT_SCREENSHOTS&&width===1710&&!pwa&&embedded)await page.screenshot({path:process.env.PD_PLOT_SCREENSHOTS+'/'+theme+'.png',fullPage:true});
|
|
}
|
|
const priorMutations=mutations;await page.locator('[data-plot-scrubber]').fill('8');assert.match(await page.locator('[data-plot-position]').textContent(),/Exploring: Ch 12 · Sc 9/);assert.equal(await page.locator('[data-plot-step="1"]').isDisabled(),true);assert.equal(reads,1);assert.equal(mutations,priorMutations,'Exploring cannot mutate saved data or request Word navigation');
|
|
await page.click('[data-plot-return]');assert.match(await page.locator('[data-plot-position]').textContent(),/Current scene: Ch 3 · Sc 2.5/);
|
|
await page.click('[data-context-go-scene]');await page.waitForFunction(()=>document.querySelector('[data-writing-context-status-text]').textContent==='Navigation requested');assert.equal(navigation,1,'Only the explicit Word action requests navigation');
|
|
await page.evaluate(()=>window.retainedPlot=document.querySelector('[data-writing-component=plots]'));for(const id of [22,900,47,23])await beat(id);assert(await page.evaluate(()=>retainedPlot===document.querySelector('[data-writing-component=plots]')));assert.equal(reads,1);assert.equal(await page.locator('[data-plot-category=elsewhere]').getAttribute('aria-pressed'),'true');
|
|
await page.evaluate(()=>{for(const sceneId of [22,47,23,900])window.dispatchEvent(new CustomEvent('native-companion-presence',{detail:[{instanceId:'fixture',sessionId:'doc',bookId:12,chapterId:10,sceneId,active:true,wordSaved:true,wordAvailable:true,expires:new Date(Date.now()+45000).toISOString()}]}));});
|
|
await page.waitForFunction(()=>document.querySelector('[data-writing-context-fragment]').dataset.sceneId==='900');await page.waitForFunction(()=>document.querySelector('[data-plot-position]').textContent.includes('Saved scene 900'));assert.equal(reads,1,'Burst cursor events reuse the book payload and the latest scene wins');
|
|
await page.locator('[data-plot-scrubber]').fill('0');assert.equal(await page.locator('[data-plot-step="-1"]').isDisabled(),true);await ctx.close();
|
|
}
|
|
console.log('PASS canonical contextual cards: every development, association-only, previous/future/both/empty, real numbering and distances, category counts, 4 themes x 3 widths, browser/PWA/embedded, rapid following, local exploration/return, single read, read-only controls.');
|
|
}finally{await browser.close();}})().catch(e=>{console.error(e);process.exitCode=1;});
|