PlotDirector/PlotLine.Tests/Browser/scene-workspace.cjs
Nick Beckley e48889ac05 Complete desktop workspaces, context menus and plot health
Commit the pending application, help and browser regression work. Pin the patched PKCS dependency for the production release.
2026-10-31 09:27:04 +00:00

198 lines
15 KiB
JavaScript

// Start the isolated --toolbar-browser-host; run with xvfb-run -a node scene-workspace.cjs.
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';
const url = base + '/fixture?page=scene-edit';
const areas = ['overview', 'story', 'people', 'world', 'writer', 'continuity'];
const editors = { SceneDetailsEditor: 'scene-details', ScenePurposeEditor: 'purpose', SceneMetricsEditor: 'metrics', SceneThreadsEditor: 'threads', SceneDependenciesEditor: 'dependencies', SceneCharactersEditor: 'characters', SceneCharacterAttributesEditor: 'attributes', SceneSuggestionsEditor: 'suggestions', SceneKnowledgeEditor: 'knowledge', SceneRelationshipsEditor: 'relationships', SceneLocationsEditor: 'locations', SceneAssetsEditor: 'assets', SceneNotesEditor: 'notes', SceneChecklistEditor: 'checklist', SceneWorkflowEditor: 'workflow', SceneAttachmentsEditor: 'attachments', SceneContinuityEditor: 'warnings' };
const settle = page => page.waitForTimeout(100);
async function installRoutes(page) {
// Load actual compiled Razor editor fragments without touching any database.
await page.route(/\/(Scenes|Warnings)\/Scene\w+Editor\?/, async route => {
const name = new URL(route.request().url()).pathname.split('/').pop();
const response = await page.request.get(url + '&editor=' + editors[name]);
assert.equal(response.status(), 200, name);
await route.fulfill({ response });
});
}
async function select(page, area, pwa) {
if (pwa) {
if (await page.locator('#ribbon-surface').isHidden()) await page.locator('[data-ribbon-tab][aria-selected="true"]').click();
const group = page.locator('.pd-context-group[aria-label="Edit Scene"]');
const collapsed = group.locator('details[data-compact="true"]');
if (await collapsed.count() && !await collapsed.getAttribute('open')) await collapsed.locator('summary').click();
await group.locator('button.pd-context-command').filter({ hasText: new RegExp('^' + area + '$', 'i') }).click();
} else await page.locator(`[data-scene-area-command="${area}"]`).click();
await settle(page);
assert.equal(await page.locator('[data-scene-workspace]').getAttribute('data-scene-area'), area);
}
async function verify(page, pwa) {
const errors = []; page.on('pageerror', error => errors.push(error.message));
await installRoutes(page);
await page.goto(url);
await page.locator('[data-scene-workspace]').waitFor();
if (pwa) {
if (await page.locator('#ribbon-surface').isHidden()) await page.locator('[data-ribbon-tab][aria-selected="true"]').click();
await page.locator('.pd-context-group[aria-label="Edit Scene"]').waitFor();
}
assert.equal(await page.locator('.scene-workspace-nav').isVisible(), !pwa);
assert.equal(await page.locator('.scene-edit-heading h1').isVisible(), !pwa);
assert(await page.locator('[data-scene-identity]').isVisible());
assert.equal(await page.locator('[data-scene-summary-part]:visible').count(), 6);
const overviewBounds = await page.locator('#scene-workspace-canvas').boundingBox();
for (const card of await page.locator('[data-scene-summary-part]').all()) {
const bounds = await card.boundingBox();
assert(bounds.y >= overviewBounds.y - 1 && bounds.y + bounds.height <= overviewBounds.y + overviewBounds.height + 1, 'both summary rows stay within the desktop workbench');
}
if (pwa) {
assert.equal(await page.locator('.pd-context-group[aria-label="Edit Scene"] .pd-context-command').count(), 6);
assert.equal(await page.locator('.pd-context-group[aria-label="Edit Scene"] > .pd-ribbon-group-label').textContent(), 'Edit Scene');
}
const prefix = pwa ? 'pwa' : 'browser';
await page.screenshot({ path: `/tmp/scene-workspace-${prefix}-overview.png`, fullPage: true });
const dashboardColumns = await page.locator('.scene-inspector-v2-grid').evaluate(e => getComputedStyle(e).gridTemplateColumns);
await page.locator('[data-scene-editor="scene-details"]').click();
await page.locator('[data-scene-editor-body] form').waitFor();
assert.equal(await page.locator('.scene-inspector-v2-grid').evaluate(e => getComputedStyle(e).gridTemplateColumns.split(' ').length), dashboardColumns.split(' ').length, 'Overview keeps its three-column arrangement with inspector');
await page.locator('[data-scene-editor-close]').first().click();
for (const area of areas) {
await select(page, area, pwa);
assert.equal(await page.locator('[data-scene-summary-part]:visible').count(), area === 'overview' ? 6 : 1);
if (pwa) assert.equal(await page.locator('.pd-context-group[aria-label="Edit Scene"] [aria-pressed="true"]').count(), 1);
await page.reload(); await settle(page);
assert.equal(await page.locator('[data-scene-workspace]').getAttribute('data-scene-area'), area, 'refresh retains workspace');
}
await select(page, 'overview', pwa);
for (const area of areas.slice(1)) {
await page.locator(`[data-scene-card-area="${area}"]`).click();
assert.equal(await page.locator('[data-scene-workspace]').getAttribute('data-scene-area'), area);
await select(page, 'overview', pwa);
}
// History retains the area, and dashboard editing enters its specialist workspace first.
await page.locator('[data-scene-summary-part="people"] [data-scene-editor="characters"]').first().click();
assert.equal(await page.locator('[data-scene-workspace]').getAttribute('data-scene-area'), 'people');
assert(await page.locator('[data-scene-editor-overlay]').isHidden());
await select(page, 'story', pwa); await page.goBack(); await settle(page);
assert.equal(await page.locator('[data-scene-workspace]').getAttribute('data-scene-area'), 'people');
await page.goForward(); await settle(page);
assert.equal(await page.locator('[data-scene-workspace]').getAttribute('data-scene-area'), 'story');
await select(page, 'people', pwa);
assert.equal(await page.locator('.scene-inspector-v2-character-list > li:visible').count(), 8, 'focused area shows complete list');
const canvas = page.locator('#scene-workspace-canvas');
const before = await canvas.boundingBox();
const columnsBefore = await page.locator('.scene-inspector-v2-grid').evaluate(e => getComputedStyle(e).gridTemplateColumns.split(' ').length);
await page.screenshot({ path: `/tmp/scene-workspace-${prefix}-people.png`, fullPage: true });
await page.locator('[data-scene-editor="relationships"]').click();
await page.locator('[data-scene-editor-body] form').first().waitFor();
assert.equal(await page.locator('[data-scene-workspace]').getAttribute('data-scene-area'), 'people');
const opened = await canvas.boundingBox();
assert.equal(opened.x, before.x); assert.equal(opened.y, before.y);
assert(opened.width < before.width - 300, 'inspector consumes width');
assert.equal(await page.locator('.scene-inspector-v2-grid').evaluate(e => getComputedStyle(e).gridTemplateColumns.split(' ').length), columnsBefore);
assert.equal(await page.locator('[data-scene-summary-part]:visible').count(), 1);
assert(await page.locator('[data-scene-editor-body] input[name="__RequestVerificationToken"]').count());
await page.screenshot({ path: `/tmp/scene-workspace-${prefix}-relationships.png`, fullPage: true });
await page.locator('[data-scene-editor-resizer]').press('ArrowLeft');
assert((await page.locator('[data-scene-editor-overlay]').boundingBox()).width >= 520, 'keyboard resizing retains usable editor');
await page.locator('#RelationshipCharacterAID').selectOption('1');
await page.locator('#RelationshipCharacterBID').selectOption('2');
await page.locator('#RelationshipDescription').fill('A shared discovery');
await page.route('**/Scenes/AddRelationshipEvent', async route => {
assert.match(route.request().postData(), /A shared discovery/);
assert.match(route.request().postData(), /__RequestVerificationToken/);
const response = await page.request.get(url + '&editor=relationships');
await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ success: true, editorHtml: await response.text() }) });
});
await page.getByRole('button', { name: 'Add relationship event' }).click();
await page.waitForFunction(() => !document.querySelector('[data-scene-editor-save]')?.disabled);
assert.equal(await page.locator('[data-scene-workspace]').getAttribute('data-scene-area'), 'people');
assert(await page.locator('[data-scene-editor-overlay]').isVisible(), 'relationship workflow stays open after adding');
await page.locator('[data-scene-editor-close]').first().click();
assert(Math.abs((await canvas.boundingBox()).width - before.width) < 2, 'closing returns all width');
await select(page, 'story', pwa);
await page.locator('[data-scene-editor="purpose"]').last().click();
await page.locator('textarea[name="ScenePurposeNotes"]').fill('Updated purpose');
let valid = false;
await page.route('**/Scenes/SaveScenePurpose', async route => {
assert.match(route.request().postData(), /Updated purpose/);
await route.fulfill({ status: valid ? 200 : 400, contentType: 'application/json', body: JSON.stringify(valid ? { success: true, html: { story: '<article class="scene-inspector-v2-card" data-scene-summary-part="story"><p class="eyebrow">Story</p><h3>Saved purpose</h3></article>' } } : { success: false, message: 'Please review the purpose.' }) });
});
await page.locator('[data-scene-editor-save]').click();
await page.getByText('Please review the purpose.').waitFor();
assert(await page.locator('[data-scene-editor-overlay]').isVisible());
valid = true; await page.locator('[data-scene-editor-save]').click();
await page.locator('[data-scene-editor-overlay]').waitFor({ state: 'hidden' });
assert.equal(await page.locator('[data-scene-workspace]').getAttribute('data-scene-area'), 'story');
assert(await page.getByText('Saved purpose').isVisible(), 'AJAX summary refresh keeps selected area');
// Reload restores the unmodified fixture; Cancel sends no write.
await page.reload(); await select(page, 'story', pwa);
await page.locator('[data-scene-editor="purpose"]').last().click();
await page.locator('textarea[name="ScenePurposeNotes"]').fill('Discard me');
await page.locator('[data-scene-editor-close]').first().click();
await page.locator('[data-scene-editor="purpose"]').last().click();
await page.locator('textarea[name="ScenePurposeNotes"]').waitFor();
assert.equal(await page.locator('textarea[name="ScenePurposeNotes"]').inputValue(), 'Establish the mystery');
await page.locator('[data-scene-editor-close]').first().click();
await select(page, 'continuity', pwa);
assert.equal(await page.locator('[data-scene-summary-part="continuity"] li:visible').count(), 6);
await page.locator('[data-scene-editor="warnings"]').click();
await page.locator('[data-scene-editor-body] .scene-inspector-editor-form').waitFor();
assert.equal(await page.getByRole('button', { name: 'Dismiss', exact: true }).count(), 6, 'existing per-warning actions remain accessible');
assert(await page.getByRole('button', { name: 'Revalidate scene' }).isVisible());
await page.locator('[data-scene-editor-close]').first().click();
for (const [area, keys] of Object.entries({ story: ['purpose', 'metrics', 'threads', 'dependencies'], people: ['characters', 'attributes', 'suggestions', 'knowledge', 'relationships'], world: ['locations', 'assets'], writer: ['notes', 'checklist', 'workflow', 'attachments'] })) {
await select(page, area, pwa);
for (const key of keys) {
await page.locator(`[data-scene-summary-part="${area}"] [data-scene-editor="${key}"]`).last().click();
await page.locator('[data-scene-editor-body] .scene-inspector-editor-form').waitFor();
assert.equal(await page.locator('[data-scene-workspace]').getAttribute('data-scene-area'), area);
await page.locator('[data-scene-editor-close]').first().click();
}
}
for (const theme of ['modern-dark', 'warm-dark', 'modern-light', 'warm-light', 'rose-pink', 'baby-blue']) {
await page.evaluate(theme => { document.documentElement.dataset.appTheme = theme; document.documentElement.dataset.theme = theme.endsWith('dark') ? 'dark' : 'light'; }, theme);
await select(page, 'people', pwa);
await page.locator('[data-scene-editor="relationships"]').click();
await page.locator('[data-scene-editor-body] form').first().waitFor();
const colours = await page.locator('[data-scene-summary-part="people"]').evaluate(e => ({ colour: getComputedStyle(e).color, background: getComputedStyle(e).backgroundColor }));
assert.notEqual(colours.colour, colours.background);
await page.screenshot({ path: `/tmp/scene-workspace-${prefix}-${theme}.png`, fullPage: true });
await page.locator('[data-scene-editor-close]').first().click();
}
for (const width of [1100, 820, 390]) {
await page.setViewportSize({ width, height: 900 }); await settle(page);
assert(await page.locator('[data-scene-summary-part="people"]').isVisible());
await page.locator('[data-scene-editor="relationships"]').click();
await page.locator('[data-scene-editor-body] form').first().waitFor();
assert(await page.locator('[data-scene-editor-close]').first().isVisible());
const bounds = await page.locator('[data-scene-editor-overlay]').boundingBox();
assert(bounds.width <= width && bounds.x >= 0 && bounds.x + bounds.width <= width + 1);
assert(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1), 'no document horizontal overflow');
if (width <= 900) assert(await page.locator('[data-scene-editor-overlay]').evaluate(e => {
const rect = e.getBoundingClientRect();
return !!document.elementFromPoint(rect.left + rect.width / 2, rect.top + 100)?.closest('[data-scene-editor-overlay]');
}), 'canvas commands cannot show through mobile inspector');
await page.screenshot({ path: `/tmp/scene-workspace-${prefix}-${width}.png`, fullPage: true });
await page.locator('[data-scene-editor-close]').first().click();
}
assert.deepEqual(errors, []);
}
(async () => {
const profile = fs.mkdtempSync(path.join(os.tmpdir(), 'pd-scene-workspace-'));
const context = await chromium.launchPersistentContext(profile, { headless: false, viewport: { width: 1600, height: 1000 } });
try {
const browserPage = context.pages()[0]; await verify(browserPage, false);
const cdp = await context.browser().newBrowserCDPSession(); const manifestId = base + '/Projects';
await cdp.send('PWA.install', { manifestId, installUrlOrBundleUrl: url });
await cdp.send('PWA.changeAppUserSettings', { manifestId, displayMode: 'standalone' });
const opened = context.waitForEvent('page'); await cdp.send('PWA.launch', { manifestId, url });
const app = await opened; await app.waitForLoadState();
assert(await app.evaluate(() => matchMedia('(display-mode: standalone)').matches));
await verify(app, true);
console.log('PASS Edit Scene: browser and installed PWA, six areas/cards, shared ribbon state/history, stable inspectors, width recovery, real Story/Relationships/Continuity forms, AJAX validation/save/cancel, six themes and responsive layouts.');
} finally { await context.close(); }
})().catch(error => { console.error(error); process.exit(1); });