// Real Razor shell; hold the companion response to verify stable initial ribbon geometry. const { chromium } = require('playwright'); const assert = require('node:assert/strict'); const base = process.env.PD_RIBBON_BASE || 'http://127.0.0.1:5238'; (async () => { const browser = await chromium.launch(); try { for (const pwa of [false, true]) { const context = await browser.newContext({ viewport: { width: 1440, height: 1000 } }); if (pwa) await context.addInitScript(() => Object.defineProperty(navigator, 'standalone', { value: true })); let release, arrived; const pending = new Promise(resolve => { release = resolve; }); const requested = new Promise(resolve => { arrived = resolve; }); await context.route('**/InvestigationCompanion/Summary?*', async route => { arrived(); await pending; await route.fulfill({ contentType: 'application/json', body: JSON.stringify({ active: false }) }); }); const page = await context.newPage(); const url = `${base}/Writer?projectId=7&bookId=12`; await page.goto(url, { waitUntil: 'domcontentloaded' }); await requested; const opener = page.locator('[data-guidance-open]'); assert.equal(await opener.count(), 1); assert(await opener.isVisible()); assert.equal(await opener.evaluate(n => n.previousElementSibling.textContent.trim()), 'Reflect'); assert.equal(await opener.evaluate(n => n.closest('[role=tabpanel]').id), 'ribbon-panel-writer'); const geometry = () => page.locator('#ribbon-panel-writer .pd-ribbon-command').evaluateAll(ns => ns.map(n => { const r = n.getBoundingClientRect(); return [r.x, r.y, r.width, r.height]; })); const before = await geometry(); release(); await page.waitForFunction(() => window.PlotDirectorGuidanceSummary); assert.deepEqual(await geometry(), before, 'Summary response must not move ribbon commands'); await page.reload({ waitUntil: 'networkidle' }); assert(await opener.isVisible()); for (const section of ['book', 'story', 'project', 'characters', 'world', 'tools']) { await page.locator(`#ribbon-tab-${section}`).click(); assert.equal(await opener.isVisible(), false, section); assert.equal(await page.locator(`#ribbon-panel-${section} [data-guidance-open]`).count(), 0); } await page.locator('#ribbon-tab-writer').click(); await page.waitForLoadState('networkidle'); assert(await opener.isVisible()); const target = await opener.getAttribute('href'); assert(target.includes('/Writer/Explore/Investigate'), 'No active companion retains saved-investigation navigation'); await context.unroute('**/InvestigationCompanion/Summary?*'); await context.route('**/InvestigationCompanion/Summary?*', route => route.fulfill({ contentType: 'application/json', body: JSON.stringify({ active: true, resultsUrl: target, summary: { id: 'ribbon-fixture', bookID: 12, bookTitle: 'Test book', concern: 'Test concern', message: '', findings: [] } }) })); await page.evaluate(() => window.dispatchEvent(new Event('investigation-guidance-updated'))); await page.waitForFunction(() => window.PlotDirectorGuidanceSummary?.summary?.id === 'ribbon-fixture'); await page.locator('[data-guidance-close]').click(); const priorUrl = page.url(); await opener.click(); assert.equal(page.url(), priorUrl, 'Active companion opens without navigation'); assert.equal(await page.locator('[data-investigation-companion]').getAttribute('data-state'), 'open'); console.log(`PASS Investigation initial render, delayed summary geometry, refresh and section switching (${pwa ? 'standalone PWA emulation' : 'browser'})`); await context.close(); } } finally { await browser.close(); } })().catch(e => { console.error(e); process.exitCode = 1; });