// Start: dotnet run --project PlotLine.Tests -- --toolbar-browser-host const { chromium } = require('playwright'); const assert = require('node:assert/strict'); (async () => { const browser = await chromium.launch({ executablePath: process.env.CHROMIUM_PATH }); const base = 'http://127.0.0.1:5238'; const context = await browser.newContext({ viewport: { width: 1440, height: 1000 } }); const page = await context.newPage(); const errors = []; page.on('pageerror', error => errors.push(error.message)); const visible = () => page.locator('[data-toolbar-key]:visible').evaluateAll(items => items.map(item => item.dataset.toolbarKey)); const open = async () => { await page.locator('#accountMenuButton').click(); await page.getByRole('button', { name: 'Customise PlotDirector…', exact: true }).click(); await page.locator('#customiseToolbar.show').waitFor(); }; const close = async (name) => { await page.locator('[data-toolbar-form] .modal-footer').getByRole('button', { name, exact: true }).click(); await page.locator('#customiseToolbar').waitFor({ state: 'hidden' }); }; try { await page.goto(base + '/fixture'); await open(); await page.getByRole('button', { name: 'Restore defaults', exact: true }).click(); await close('Save'); assert.deepEqual(await visible(), ['overview','timeline','calendar','characters','threads','plotlines','relationships','review']); assert.equal(await page.locator('[data-toolbar-key="calendar"]').getAttribute('aria-current'), 'page'); assert.match(await page.locator('[data-toolbar-key="calendar"]').getAttribute('href'), /projectId=7/); assert.match(await page.locator('[data-toolbar-key="review"]').getAttribute('href'), /book\/12\/review-centre/); const chrome = await page.locator('header').boundingBox(); assert(chrome.height < 190, 'Desktop chrome stays compact'); await page.screenshot({ path: '/tmp/plotdirector-toolbar-desktop.png' }); await page.getByRole('tab', { name: 'Story', exact: true }).click(); assert(await page.locator('#ribbon-panel-story').isVisible()); assert.match(await page.locator('#ribbon-panel-story').getByRole('link', { name: 'Calendar', exact: true }).getAttribute('href'), /projectId=7/); await page.locator('#appProjectMenu').click(); assert.equal(await page.getByText('Switch project…', { exact: true }).getAttribute('href'), '/Projects'); await page.keyboard.press('Escape'); const setMenus = async (project, book) => { await open(); await page.locator('[data-show-project-menu]').setChecked(project); await page.locator('[data-show-book-menu]').setChecked(book); await Promise.all([page.waitForNavigation(), close('Save')]); assert.equal(await page.locator('#appProjectMenu').count(), project ? 1 : 0); assert.equal(await page.locator('#appBookMenu').count(), book ? 1 : 0); assert.equal(await page.locator('[data-app-navigation]').count(), 1, 'Context remains even when root menus are absent'); }; await setMenus(true, false); await setMenus(false, true); const menuSession = await browser.newContext(); const menuPage = await menuSession.newPage(); await menuPage.goto(base + '/fixture'); assert.equal(await menuPage.locator('#appProjectMenu').count(), 0, 'Project menu choice survives a fresh session'); assert.equal(await menuPage.locator('#appBookMenu').count(), 1, 'Book menu choice survives a fresh session'); await menuSession.close(); await setMenus(false, false); await open(); await page.locator('[data-show-project-menu]').check(); await close('Cancel'); assert.equal(await page.locator('#appProjectMenu').count(), 0, 'Cancel preserves saved application bar choices'); await open(); await page.getByRole('button', { name: 'Restore defaults', exact: true }).click(); assert(await page.locator('[data-show-project-menu]').isChecked()); assert(await page.locator('[data-show-book-menu]').isChecked()); await Promise.all([page.waitForNavigation(), close('Save')]); assert.equal(await page.locator('#appProjectMenu').count(), 1); assert.equal(await page.locator('#appBookMenu').count(), 1); // A Calendar AJAX book change updates both levels of navigation. await page.evaluate(() => { const old = document.querySelector('[data-navigation-book-id]'); const copy = old.cloneNode(true); copy.dataset.navigationBookId = '13'; old.replaceWith(copy); }); await page.waitForFunction(() => document.querySelector('[data-toolbar-key="review"]').href.includes('/13/')); assert.match(await page.locator('[data-app-review-link]').getAttribute('href'), /book\/13\/review-centre/); await open(); await page.locator('[data-command-key="timeline"] input').uncheck(); await page.locator('[data-command-key="locations"] input').check(); await page.locator('[data-command-key="calendar"] [data-move="-1"]').click(); await page.screenshot({ path: '/tmp/plotdirector-toolbar-dialog.png' }); await close('Cancel'); assert.deepEqual(await visible(), ['overview','timeline','calendar','characters','threads','plotlines','relationships','review']); await open(); await page.locator('[data-command-key="timeline"] input').uncheck(); await page.locator('[data-command-key="locations"] input').check(); await page.locator('[data-command-key="calendar"] [data-move="-1"]').click(); await close('Save'); assert.deepEqual(await visible(), ['calendar','overview','characters','threads','plotlines','relationships','review','locations']); await page.reload(); assert.equal((await visible())[0], 'calendar'); const fresh = await browser.newContext(); const secondSession = await fresh.newPage(); await secondSession.goto(base + '/fixture'); assert.equal(await secondSession.locator('[data-toolbar-key]:visible').first().getAttribute('data-toolbar-key'), 'calendar'); await fresh.close(); await context.addCookies([{ name: 'fixture-user', value: '5', url: base }]); await page.reload(); assert.equal((await visible())[0], 'overview', 'Another user has independent defaults'); await context.clearCookies(); await page.reload(); await open(); await page.getByRole('button', { name: 'Restore defaults', exact: true }).click(); await close('Save'); await open(); await page.locator('[data-command-key="overview"]').dragTo(page.locator('[data-command-key="calendar"]')); await close('Save'); assert.deepEqual((await visible()).slice(0,3), ['timeline','calendar','overview']); // Antiforgery is enforced by MVC, not just by a mocked fetch. assert.equal((await context.request.post(base + '/Toolbar/Save', { form: { commandKeys: 'calendar' } })).status(), 400); await open(); await page.getByRole('button', { name: 'Restore defaults', exact: true }).click(); await close('Save'); // Failed saves keep the draft open and leave the saved strip unchanged. await open(); await page.locator('[data-command-key="timeline"] input').uncheck(); await page.route('**/Toolbar/Save', route => route.fulfill({ status: 500, body: 'fixture failure' })); await page.locator('[data-toolbar-form] button[type="submit"]').click(); await page.locator('[data-toolbar-error]').filter({ hasText: 'Unable to save' }).waitFor(); assert((await visible()).includes('timeline')); await page.unroute('**/Toolbar/Save'); await close('Cancel'); await open(); while (await page.locator('[data-toolbar-choices] input:checked').count()) await page.locator('[data-toolbar-choices] input:checked').first().uncheck(); await close('Save'); assert.deepEqual(await visible(), []); await page.reload(); assert(await page.locator('[data-app-toolbar] button').isVisible()); await open(); await page.getByRole('button', { name: 'Restore defaults', exact: true }).click(); await close('Cancel'); assert.deepEqual(await visible(), []); await open(); await page.getByRole('button', { name: 'Restore defaults', exact: true }).click(); await close('Save'); await page.goto(base + '/fixture?project=none'); assert.equal(await page.locator('[data-toolbar-key="calendar"]').getAttribute('href'), null); assert.equal(await page.locator('[data-toolbar-key="calendar"]').getAttribute('aria-disabled'), 'true'); await page.setViewportSize({ width: 390, height: 844 }); await page.locator('#accountMenuButton').click(); assert(await page.getByRole('button', { name: 'Customise PlotDirector…', exact: true }).isVisible()); await page.keyboard.press('Escape'); await page.screenshot({ path: '/tmp/plotdirector-toolbar-mobile.png' }); assert(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth), 'Mobile chrome does not overflow viewport'); await page.setViewportSize({ width: 1440, height: 1000 }); await page.goto(base + '/fixture?page=dashboard'); assert.equal(await page.locator('.dashboard-summary-value').first().textContent(), '2'); assert.equal(await page.getByRole('tab', { name: 'Project', exact: true }).getAttribute('aria-selected'), 'true'); for (const theme of ['modern-light','modern-dark','warm-light','warm-dark']) { const before = page.url(); await page.locator('#accountMenuButton').click(); await page.locator(`[data-app-theme-choice="${theme}"]`).click(); await page.waitForFunction(t => document.documentElement.dataset.appTheme === t && !document.querySelector('[data-app-theme-choice]').disabled, theme); await page.keyboard.press('Escape'); assert.equal(page.url(), before); await page.waitForTimeout(250); await page.screenshot({ path: `/tmp/plotdirector-shell-${theme}.png` }); await page.reload(); assert.equal(await page.locator('html').getAttribute('data-app-theme'), theme); } await page.goto(base + '/fixture?page=timeline'); const timelineThemeSurfaces = { 'modern-light': ['rgb(244, 245, 247)', 'rgb(255, 255, 255)', 'rgb(240, 242, 245)', 'rgb(255, 255, 255)', 'rgb(229, 232, 237)'], 'modern-dark': ['rgb(29, 31, 35)', 'rgb(37, 40, 45)', 'rgb(45, 48, 54)', 'rgb(50, 54, 61)', 'rgb(58, 63, 72)'], 'warm-light': ['rgb(246, 241, 232)', 'rgb(255, 253, 248)', 'rgb(244, 235, 221)', 'rgb(255, 253, 249)', 'rgb(238, 227, 213)'], 'warm-dark': ['rgb(23, 19, 16)', 'rgb(35, 30, 25)', 'rgb(48, 39, 31)', 'rgb(56, 46, 37)', 'rgb(67, 54, 43)'] }; for (const [theme, expected] of Object.entries(timelineThemeSurfaces)) { await page.locator('#accountMenuButton').click(); await page.locator(`[data-app-theme-choice="${theme}"]`).click(); await page.waitForFunction(t => document.documentElement.dataset.appTheme === t && !document.querySelector('[data-app-theme-choice]').disabled, theme); await page.keyboard.press('Escape'); const actual = await page.evaluate(() => ['.timeline-workspace', '.timeline-shell', '.timeline-chapter-title', '.timeline-scene-card'].map(selector => getComputedStyle(document.querySelector(selector)).backgroundColor)); assert.deepEqual(actual, expected.slice(0, 4), `${theme} must preserve the canvas, section, nested-panel, and card hierarchy`); const actionStyles = await page.locator('.workspace-heading .btn').evaluateAll(elements => elements.map(element => { const style = getComputedStyle(element); return [style.color, style.backgroundColor, style.borderColor]; })); assert(actionStyles.every(style => JSON.stringify(style) === JSON.stringify(actionStyles[0])), `${theme} Timeline peer actions must share one treatment`); assert.equal(await page.locator('.inspector-header').evaluate(element => getComputedStyle(element).backgroundColor), expected[2], `${theme} inspector header must use the nested-panel surface`); assert.equal(await page.locator('.timeline-scene-card.focus-dim').evaluate(element => getComputedStyle(element).opacity), '0.68', `${theme} focused-scene dimming must remain legible`); assert.equal(await page.locator('.timeline-metric-edit-status').textContent(), 'Select a single metric to edit its curve.'); assert.equal(await page.locator('.timeline-metric-edit-status').evaluate(element => getComputedStyle(element).backgroundColor), 'rgba(0, 0, 0, 0)', `${theme} metric guidance must read as guidance, not a disabled button`); const markerContrasts = await page.locator('.thread-marker, .asset-marker, .character-marker').evaluateAll(elements => { const channel = value => { value /= 255; return value <= 0.04045 ? value / 12.92 : Math.pow((value + 0.055) / 1.055, 2.4); }; const luminance = value => { const rgb = value.match(/[\d.]+/g).slice(0, 3).map(Number); if (value.startsWith('color(srgb')) for (let index = 0; index < 3; index++) rgb[index] *= 255; return 0.2126 * channel(rgb[0]) + 0.7152 * channel(rgb[1]) + 0.0722 * channel(rgb[2]); }; return elements.map(element => { const style = getComputedStyle(element); const values = [luminance(style.color), luminance(style.backgroundColor)].sort((a, b) => b - a); return (values[0] + 0.05) / (values[1] + 0.05); }); }); assert(markerContrasts.every(ratio => ratio >= 4.5), `${theme} Timeline event markers must meet AA text contrast: ${markerContrasts.join(', ')}`); const metricChipBackgrounds = await page.locator('.metric-toggle-pill').evaluateAll(elements => elements.map(element => getComputedStyle(element).backgroundColor)); assert.notEqual(metricChipBackgrounds[0], metricChipBackgrounds[1], `${theme} checked and unchecked metric chips must remain distinct`); const primarySceneCard = page.locator('.timeline-scene-card:not(.focus-dim)'); await primarySceneCard.hover(); assert.equal(await primarySceneCard.evaluate(element => getComputedStyle(element).backgroundColor), expected[4], `${theme} must expose a visible scene-card hover state`); await primarySceneCard.evaluate(element => element.classList.add('selected')); assert.equal(await primarySceneCard.evaluate(element => getComputedStyle(element).backgroundColor), expected[4], `${theme} must expose a visible selected scene-card state`); await primarySceneCard.evaluate(element => element.classList.remove('selected')); await page.screenshot({ path: `/tmp/plotdirector-timeline-${theme}.png`, fullPage: true }); } const modernDarkRgb = timelineThemeSurfaces['modern-dark'].slice(0, 4).map(value => value.match(/\d+/g).map(Number)); assert(modernDarkRgb.every(([red, green, blue]) => Math.max(red, green, blue) - Math.min(red, green, blue) <= 12), 'Modern Dark structural surfaces must remain neutral'); const warmDarkRgb = timelineThemeSurfaces['warm-dark'].slice(0, 4).map(value => value.match(/\d+/g).map(Number)); assert(warmDarkRgb.every(([red, , blue]) => red > blue) && warmDarkRgb.some(([red, , blue]) => red - blue >= 15), 'Warm Dark structural surfaces must retain a warm bias'); await page.goto(base + '/fixture?page=dashboard'); await page.getByRole('tab', { name: 'Project', exact: true }).focus(); await page.keyboard.press('ArrowRight'); assert.equal(await page.getByRole('tab', { name: 'Book', exact: true }).getAttribute('aria-selected'), 'true'); await page.locator('[data-ribbon-collapse]').click(); await page.waitForFunction(() => !document.querySelector('[data-ribbon-collapse]').disabled); await page.reload(); assert(await page.locator('#ribbon-surface').isHidden()); await page.getByRole('tab', { name: 'World', exact: true }).click(); assert(await page.locator('#ribbon-panel-world').isVisible()); assert.match(await page.locator('#ribbon-panel-world').getByRole('link', { name: 'Locations', exact: true }).getAttribute('href'), /projectId=7/); // Category selection opens a collapsed ribbon temporarily; it does not erase the saved preference. await page.reload(); assert(await page.locator('#ribbon-surface').isHidden()); await page.locator('[data-ribbon-collapse]').click(); await page.waitForFunction(() => !document.querySelector('[data-ribbon-collapse]').disabled); await open(); while (await page.locator('[data-toolbar-choices] input:checked').count()) await page.locator('[data-toolbar-choices] input:checked').first().uncheck(); for(const key of ['maps','floorplans']) await page.locator(`[data-command-key="${key}"] input`).check(); await close('Save'); const mapsShortcut=page.locator('[data-toolbar-key="maps"]'),floorShortcut=page.locator('[data-toolbar-key="floorplans"]'); assert(await mapsShortcut.isVisible()&&await floorShortcut.isVisible()); assert.match(await mapsShortcut.getAttribute('href'),/Maps.*projectId=7/); assert.match(await floorShortcut.getAttribute('href'),/FloorPlans.*projectId=7/); assert.notEqual(await mapsShortcut.locator('path').getAttribute('d'),await floorShortcut.locator('path').getAttribute('d'),'Maps and Floor Plans have distinct quick-access icons'); await open();await page.getByRole('button',{name:'Restore defaults',exact:true}).click();await close('Save'); await page.setViewportSize({ width: 390, height: 844 }); assert(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth)); await page.screenshot({ path: '/tmp/plotdirector-shell-dashboard-mobile.png' }); assert.deepEqual(errors, []); console.log('PASS application toolbar: real Razor, default/custom order, show/hide, drag/keyboard, cancel/reset, fresh sessions, users, menus, project/book context, CSRF, desktop/mobile, four themes, theme persistence, ribbon keyboard/collapse, Dashboard'); } finally { await browser.close(); } })().catch(error => { console.error(error); process.exitCode = 1; });