const { chromium } = require('playwright'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const os = require('node:os'); const path = require('node:path'); const base = 'http://127.0.0.1:5238'; async function settled(page) { await page.evaluate(() => new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)))); } async function openGroup(page, label) { await ready(page); const menu = page.locator(`.pd-context-group[aria-label="${label}"] > details[data-compact="true"]`); if (await menu.count() && await menu.getAttribute('open') === null) await menu.locator(':scope > summary').click(); } async function ready(page) { await page.locator('[data-pwa-context-host]:not([hidden])').waitFor({ state: 'attached' }); await settled(page); } (async () => { const profile = fs.mkdtempSync(path.join(os.tmpdir(), 'pd-desktop-')); const context = await chromium.launchPersistentContext(profile, { headless: false, viewport: { width: 1440, height: 1000 } }); const cdp = await context.browser().newBrowserCDPSession(); const manifestId = base + '/Projects'; const failures = []; context.on('page', page => page.on('pageerror', error => failures.push(error.message))); const urls = ['/fixture?page=dashboard', '/fixture?desktop=Books', '/fixture?desktop=StoryBible', '/fixture?desktop=PlotLineWorkspace', '/fixture?desktop=Characters', '/fixture?desktop=Locations', '/fixture?desktop=FloorPlans', '/fixture?desktop=Warnings', '/fixture?desktop=Archives', '/fixture?page=timeline-render', ...['PlotThreads','StoryCalendar','StoryPoint','ProjectMetrics','Analytics','Dynamics','WritingPlans','SchedulePreview','ReviewCentre','ProjectEdit'].map(name => '/fixture?desktop=' + name)]; try { const tab = context.pages()[0]; const browserPositions = new Map(); for (const url of urls) { await tab.goto(base + url); assert(await tab.locator('main h1').first().isVisible(), url + ' browser title'); if (await tab.locator('main .breadcrumb-trail').count()) assert(await tab.locator('main .breadcrumb-trail').first().isVisible(), url + ' browser breadcrumb'); assert(await tab.locator('[data-pwa-context-host]').isHidden()); browserPositions.set(url, (await tab.locator('[data-pwa-page-content]').boundingBox()).height); const header = tab.locator('main [data-pwa-header], main [data-pwa-task]').first(); assert(await header.isVisible(), url + ' browser header'); } await cdp.send('PWA.install', { manifestId, installUrlOrBundleUrl: base + '/fixture' }); await cdp.send('PWA.changeAppUserSettings', { manifestId, displayMode: 'standalone' }); const opened = context.waitForEvent('page'); await cdp.send('PWA.launch', { manifestId, url: base + '/fixture?desktop=StoryBible' }); const app = await opened; await app.waitForLoadState(); assert(await app.evaluate(() => matchMedia('(display-mode: standalone)').matches)); const themes = await app.locator('[data-app-theme-choice]').evaluateAll(nodes => nodes.map(node => node.dataset.appThemeChoice)); assert.equal(themes.length, 6); await require('./pwa-first-paint.cjs')(app, tab); for (const url of urls) { await app.goto(base + url); if (await app.locator('#ribbon-surface').isHidden()) await app.locator('[data-ribbon-tab][aria-selected=true]').click(); await ready(app); const identities = await app.locator('.pd-ribbon-panel:not([hidden]) .pd-ribbon-command[data-command-identity]').evaluateAll(nodes => nodes.map(node => node.dataset.commandIdentity)); assert.equal(new Set(identities).size, identities.length, url + ' composed ribbon duplicates'); if (await app.locator('main [data-pwa-header]').count()) assert(await app.locator('main [data-pwa-header]').first().isHidden(), url + ' PWA header'); if (await app.locator('main [data-pwa-task]').count()) assert(await app.locator('main [data-pwa-task] h1').first().isVisible(), url + ' meaningful task heading'); if (await app.locator('main .breadcrumb-trail').count()) assert(await app.locator('main .breadcrumb-trail').first().isHidden(), url + ' PWA breadcrumb'); // The primary workspace now begins within a few pixels of the sticky shell. const reclaimed = await app.evaluate(() => { const main = document.querySelector('main'); const first = [...main.querySelectorAll(':scope > *')].find(el => getComputedStyle(el).display !== 'none'); return first ? first.getBoundingClientRect().top - main.getBoundingClientRect().top : 0; }); assert(reclaimed < 70, `${url} content starts ${reclaimed}px below main`); for (const theme of themes) { await app.evaluate(theme => { const root = document.documentElement; root.dataset.appTheme = theme; root.dataset.theme = root.dataset.bsTheme = theme.endsWith('-dark') ? 'dark' : 'light'; window.dispatchEvent(new Event('plotdirector:theme-changed')); }, theme); for (const width of [2560, 1440, 900]) { await app.setViewportSize({ width, height: 1000 }); await settled(app); const geometry = await app.evaluate(() => { const panel = document.querySelector('.pd-ribbon-panel:not([hidden])'); const groups = [...panel.querySelectorAll('.pd-ribbon-group')].filter(node => node.getBoundingClientRect().width); return { height: panel.getBoundingClientRect().height, fits: panel.scrollWidth <= panel.clientWidth + 1, tops: groups.map(node => node.getBoundingClientRect().top), parent: document.querySelector('[data-pwa-context-host]').parentElement === panel, titleCount: document.querySelectorAll('[data-pwa-context-title]').length, stripHeight: document.querySelector('[data-pwa-context-host]').getBoundingClientRect().height, pageFits: document.documentElement.scrollWidth <= innerWidth }; }); assert(geometry.parent && geometry.titleCount === 0 && geometry.stripHeight === 0, `${url} context must be groups inside active panel, without a title/toolbar box`); assert(geometry.height <= 86, `${url}/${width} ribbon grew to ${geometry.height}px`); assert(geometry.fits && geometry.pageFits, `${url}/${theme}/${width} overflow`); assert(Math.max(...geometry.tops) - Math.min(...geometry.tops) < 1, `${url}/${width} created a second ribbon row`); const summary = app.locator('.pd-ribbon-panel:not([hidden]) details[data-compact="true"] > summary').first(); if (await summary.count()) { await summary.click(); await app.waitForFunction(() => document.querySelector('.pd-ribbon-panel:not([hidden]) details[data-compact="true"][open] summary')?.getAttribute('aria-expanded') === 'true'); const panel = app.locator('.pd-ribbon-panel:not([hidden]) details[data-compact="true"][open] > .pd-context-menu-content').last(); assert(await panel.evaluate(el => { const r = el.getBoundingClientRect(); return r.right <= innerWidth + 1 && el.contains(document.elementFromPoint(r.left + 12, r.top + 12)); })); await app.keyboard.press('Escape'); assert(await summary.evaluate(el => document.activeElement === el)); } } } await app.setViewportSize({ width: 2560, height: 1000 }); await settled(app); assert.equal(await app.locator('.pd-context-group > details[data-compact="true"]:not([data-always-compact="true"])').count(), 0, `${url} prematurely collapsed contextual groups at wide desktop width`); await app.screenshot({ path: `/tmp/pwa-desktop-${url.includes('desktop=') ? url.split('desktop=')[1] : url.split('page=')[1]}.png` }); const contextTarget = app.locator('[data-pwa-page-content] [data-pd-context]').first(); const contextMain = app.locator('[data-pwa-page-content]'); await require('./pwa-context-dev-checks.cjs').dispatch(await contextTarget.count() ? contextTarget : contextMain); if (await app.locator('body > .pd-context-popup').count()) { await app.screenshot({path:`/tmp/pd-context-page-${url.includes('desktop=') ? url.split('desktop=')[1] : url.split('page=')[1]}.png`}); await app.keyboard.press('Escape'); } await app.locator('[data-ribbon-collapse]').click(); assert(await app.locator('#ribbon-surface').isHidden()); await app.locator('[data-ribbon-tab=story]').click(); await ready(app); } await app.goto(base + '/fixture?page=dashboard'); if (await app.locator('#ribbon-surface').isHidden()) await app.locator('[data-ribbon-tab=project]').click(); await openGroup(app, 'Manage'); assert.equal(await app.locator('[data-pwa-context-controls] a[href*="Projects/Edit"]').count(), 0); assert.equal(await app.locator('.pd-ribbon-panel:not([hidden]) a[data-command-operation="books.create"]').count(), 1, 'Add Book must appear once, preferring the active base command'); assert.equal(await app.locator('main a[data-command-operation="books.create"]:visible').count(), 0, 'The Books-section duplicate is also accounted for'); assert.equal(await app.locator('[data-pwa-context-controls] a[href*="ProjectBackup"]').count(), 0); assert.equal(await app.locator('[data-pwa-context-controls]').getByRole('link', { name: 'Phase 1 Checklist' }).count(), 1); assert.equal(await app.locator('[data-pwa-context-controls]').getByRole('button', { name: 'Archive Project', exact: true }).count(), 1); // The original confirmation modal must cancel without posting, then submit unchanged data. let archived = null; await app.route('**/Projects/Archive', route => { archived = new URLSearchParams(route.request().postData()); return route.fulfill({ contentType: 'text/html', body: 'Archived fixture' }); }); await app.locator('[data-pwa-context-controls]').getByRole('button', { name: 'Archive Project', exact: true }).click(); await app.locator('#plotlineConfirmModal').waitFor({ state: 'visible' }); assert.equal(archived, null); await app.locator('#plotlineConfirmModal button[data-bs-dismiss]').first().click(); await app.locator('#plotlineConfirmModal').waitFor({ state: 'hidden' }); assert.equal(archived, null); await openGroup(app, 'Manage'); await app.locator('[data-pwa-context-controls]').getByRole('button', { name: 'Archive Project', exact: true }).click(); await app.locator('#plotlineConfirmModal [data-confirm-submit]').click(); await app.waitForURL('**/Projects/Archive'); assert.equal(archived.get('id'), '7'); assert(archived.get('__RequestVerificationToken')); await app.goto(base + '/fixture?page=dashboard&owner=false'); if (await app.locator('#ribbon-surface').isHidden()) await app.locator('[data-ribbon-tab=project]').click(); assert.equal(await app.locator('[data-pwa-context-controls]').getByRole('button', { name: 'Archive Project', exact: true }).count(), 0); assert.equal(await app.locator('.pd-ribbon-command[href*="ProjectBackup"]').count(), 0); await app.goto(base + '/fixture?page=dashboard'); if (await app.locator('#ribbon-surface').isHidden()) await app.locator('[data-ribbon-tab=project]').click(); await openGroup(app, 'Manage'); // Reuse the exact original ribbon node, without creating a duplicate or generic category link. await app.evaluate(() => window.originalEditCommand = document.querySelector('#ribbon-panel-project a[href*="Projects/Edit"]')); await app.locator('[data-ribbon-tab=world]').click(); await ready(app); assert.equal(await app.locator('.pd-ribbon-command[href*="Projects/Edit"]').count(), 1); assert(await app.evaluate(() => document.querySelector('[data-pwa-context-controls] a[href*="Projects/Edit"]') === window.originalEditCommand)); await app.locator('[data-ribbon-tab=project]').click(); await ready(app); assert(await app.evaluate(() => document.querySelector('#ribbon-panel-project > .pd-ribbon-group a[href*="Projects/Edit"]') === window.originalEditCommand)); await app.goto(base + '/fixture?desktop=Books'); if (await app.locator('#ribbon-surface').isHidden()) await app.locator('[data-ribbon-tab=book]').click(); await openGroup(app, 'Manage'); assert(await app.locator('[data-pwa-context-controls]').getByRole('link', { name: 'Story health', exact: true }).getAttribute('href').then(href => href.includes('bookId=12'))); await app.goto(base + '/fixture?page=timeline-render'); if (await app.locator('#ribbon-surface').isHidden()) await app.locator('[data-ribbon-tab=story]').click(); await app.evaluate(() => { const select = document.querySelector('main .timeline-filter select'); select.add(new Option('Another book', '13')); window.dispatchEvent(new Event('plotdirector:context-changed')); select.form.addEventListener('submit', event => { event.preventDefault(); window.scopeSubmitted = Object.fromEntries(new FormData(event.target)); }); }); await app.locator('[data-pwa-context-controls] select').selectOption('13'); await app.locator('[data-pwa-context-controls] button', { hasText: 'View' }).click(); assert.equal(await app.evaluate(() => window.scopeSubmitted.BookID), '13'); assert.equal(await app.evaluate(() => window.scopeSubmitted.ProjectID), '7'); await app.goto(base + '/fixture?desktop=StoryBible'); if (await app.locator('#ribbon-surface').isHidden()) await app.locator('[data-ribbon-tab=tools]').click(); await app.evaluate(() => document.querySelector('main form.story-bible-search').addEventListener('submit', event => { event.preventDefault(); window.submitted = Object.fromEntries(new FormData(event.target)); })); const search = app.locator('[data-pwa-context-controls] input'); await search.fill('beth'); assert.equal(await app.locator('[data-pwa-context-controls] .pd-context-submit').count(), 0, 'No separate full-sized Search card'); assert.equal(await app.locator('.pd-context-search input[type=search]').count(), 1); await search.press('Enter'); assert.equal(await app.evaluate(() => window.submitted.q), 'beth'); await search.fill('beth integrated'); await app.locator('.pd-context-search-submit').click(); assert.equal(await app.evaluate(() => window.submitted.q), 'beth integrated'); const exportIcons = await app.locator('.pd-context-group[aria-label=Export] .pd-context-command').evaluateAll(nodes => nodes.map(node => node.dataset.ribbonIcon).filter(Boolean)); assert(exportIcons.includes('print') && exportIcons.includes('markdown') && exportIcons.includes('text-export') && exportIcons.includes('research') && exportIcons.includes('timeline-export')); assert(!exportIcons.includes('bible')); assert.equal(await app.evaluate(() => window.submitted.projectId), '7'); await openGroup(app, 'Jump to'); await app.locator('[data-pwa-context-controls] a[href="#revision"]').click(); assert.equal(await app.evaluate(() => document.activeElement.id), 'revision'); assert.equal(await app.locator('[data-pwa-context-controls] details[data-compact="true"][open]').count(), 0); assert(await app.locator('.pd-context-help').getAttribute('href').then(href => /^\/help\//i.test(href))); assert.equal(await app.locator('.pd-context-help').getAttribute('data-help-open'), 'storyBible.overview'); const helpUrl = await app.locator('.pd-context-help').getAttribute('href'); await app.route('**' + helpUrl, route => route.fulfill({ contentType: 'text/html', body: 'Story Bible help' })); await app.locator('.pd-context-help').click(); await app.waitForURL('**' + helpUrl); await app.goto(base + '/fixture?desktop=StoryBible'); if (await app.locator('#ribbon-surface').isHidden()) await app.locator('[data-ribbon-tab=tools]').click(); // Calendar-style partial replacement must discard disconnected commands immediately. await app.evaluate(() => { document.querySelector('main').innerHTML = '

Replacement

New scope
'; }); await app.waitForFunction(() => document.querySelector('[data-pwa-context-controls] a[href*="projectId=99"]')); assert.equal(await app.locator('[data-pwa-context-controls] input').count(), 0); assert.equal(await app.locator('[data-pwa-context-controls] a[href*="projectId=99"]').count(), 1); // Large scope selector uses a searchable popover; state changes stay owned by the source. await app.evaluate(() => { document.querySelector('main').innerHTML = '

Scope fixture

'; document.querySelector('main form').addEventListener('submit', event => { event.preventDefault(); window.largeScope = new FormData(event.target).get('bookId'); }); }); await app.locator('.pd-context-selector').waitFor({ state: 'visible' }); await app.locator('.pd-context-selector summary').click(); await app.locator('.pd-context-selector input').fill('Book 19'); assert.equal(await app.locator('.pd-context-selector option:not([hidden])').count(), 1); await app.locator('.pd-context-selector select').selectOption('19'); await app.keyboard.press('Escape'); await app.locator('[data-pwa-context-controls] button', { hasText: 'Apply scope' }).click(); assert.equal(await app.evaluate(() => window.largeScope), '19'); await openGroup(app, 'Page tools'); assert(await app.locator('[data-pwa-context-controls]').getByRole('button', { name: 'Waiting', exact: true }).isDisabled()); await app.evaluate(() => { const source = document.getElementById('state-command'); source.disabled = false; source.textContent = 'Ready'; }); await app.waitForFunction(() => [...document.querySelectorAll('[data-pwa-context-controls] button')].some(node => node.textContent === 'Ready' && !node.disabled)); await app.evaluate(() => { document.querySelector('main').innerHTML = '

Meaningful profile

Author profile content

Edit profile
'; }); await app.waitForFunction(() => document.querySelector('[data-pwa-context-controls] a[href="/Characters/Edit/99"]')); assert(await app.locator('#profile-description').isVisible()); assert(await app.locator('main [data-pwa-context="Manage"]').isHidden()); assert.equal(await app.locator('[data-pwa-context-controls] a[href="/Characters/Edit/99"]').count(), 1); await ready(app); assert.equal(await app.locator('.pd-context-group[aria-label="Manage"] > details').getAttribute('data-always-compact'), 'true'); assert.deepEqual(failures, []); console.log('PASS PWA desktop: real installed/browser separation; 20 real Razor pages; semantic composed-command uniqueness and single Enter/integrated-button search; all six themes at 2560/1440/900; one active ribbon row without title strip; direct wide commands and measured responsive overflow; compact menus, keyboard Escape/focus, collapsed ribbon, route/scope deduplication, search form behavior, section jump focus, page help, fragment lifecycle.'); } finally { await cdp.send('PWA.uninstall', { manifestId }).catch(() => {}); await context.close(); fs.rmSync(profile, { recursive: true, force: true }); } })().catch(error => { console.error(error); process.exitCode = 1; });