// Isolated browser interaction checks. Mock persistence is not live Development acceptance. const { chromium } = require('playwright'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); const script = fs.readFileSync(path.resolve(__dirname, '../../PlotLine/wwwroot/js/context-threads.js'), 'utf8'); (async () => { const browser = await chromium.launch({ headless: true }); try { const context = await browser.newContext(); const links = new Map([[11, new Set([1])], [22, new Set()]]); const errors = [], writes = [], guards = []; let reject = false, readonly = false; const threads = [ { plotThreadID: 1, plotLineID: 10, plotLineName: 'Journey', title: 'A question' }, { plotThreadID: 2, plotLineID: 10, plotLineName: 'Journey', title: 'Promise' }, { plotThreadID: 3, plotLineID: 20, plotLineName: 'Discovery', title: 'Hidden answer' } ]; function state(sceneID) { return { scope: { label: `Chapter 1 · Scene ${sceneID}: Saved scene`, canEdit: !readonly }, threads: threads.map(t => ({ ...t, linked: links.get(sceneID).has(t.plotThreadID), previousScenes: sceneID === 22 && links.get(11).has(t.plotThreadID) ? [{ chapterNumber: 1, chapterTitle: 'Opening', sceneNumber: 1, sceneTitle: 'Earlier scene' }] : [] })) }; } await context.route('http://context.test/**', async route => { const url = new URL(route.request().url()); if (url.pathname === '/ContextThreads/State') return route.fulfill({ json: state(Number(url.searchParams.get('sceneId'))) }); if (url.pathname === '/ContextThreads/Add') { const body = route.request().postDataJSON(); writes.push(body); assert.equal(route.request().headers()['requestverificationtoken'], 'fixture-token'); if (reject) return route.fulfill({ status: 409, json: { message: 'Thread was archived. Review selection.' } }); for (const id of body.threadIDs) links.get(body.sceneID).add(id); return route.fulfill({ json: { saved: true } }); } if (url.pathname !== '/') throw Error(`Unexpected navigation: ${url.pathname}`); return route.fulfill({ contentType: 'text/html', body: '
' }); }); const page = await context.newPage(); page.on('pageerror', e => errors.push(e.message)); await page.goto('http://context.test/'); await page.addScriptTag({ content: script }); async function mount(sceneID, pane = 0) { await page.evaluate(({ sceneID, pane }) => { let host = document.querySelector(`[data-test-pane="${pane}"]`); if (!host) { host = document.createElement('section'); host.dataset.testPane = pane; document.querySelector('main').append(host); } const old = host.firstElementChild; if (old) window.PlotDirectorContextLifecycles.threads.dispose(old); host.innerHTML = `

`; window.guardEvents ??= []; host.firstElementChild.addEventListener('context-panel-state', e => window.guardEvents.push(e.detail.dirty)); window.PlotDirectorContextLifecycles.threads.mount(host.firstElementChild); }, { sceneID, pane }); await page.locator(`[data-test-pane="${pane}"] [data-thread-add]`).waitFor({ state: 'visible' }); if (!readonly) await page.waitForFunction(pane => !document.querySelector(`[data-test-pane="${pane}"] [data-thread-add]`).disabled, pane); } await mount(11); assert.equal(await page.getByText('Journey / A question', { exact: true }).count(), 1); await page.getByRole('button', { name: 'Add thread', exact: true }).click(); await page.getByText('Chapter 1 · Scene 11: Saved scene', { exact: true }).waitFor(); const dialog = page.getByRole('dialog'); await dialog.locator('summary').filter({ hasText: 'Journey' }).click(); assert.equal(await dialog.getByLabel(/A question.*Already linked/).isDisabled(), true); await dialog.getByLabel('Promise', { exact: true }).check(); await dialog.getByRole('searchbox').fill('answer'); await dialog.locator('summary').filter({ hasText: 'Discovery' }).waitFor({ state: 'visible' }); await dialog.getByLabel('Hidden answer', { exact: true }).check(); await dialog.getByRole('button', { name: 'Add to scene', exact: true }).click(); await dialog.waitFor({ state: 'detached' }); assert.deepEqual(writes[0], { bookID: 12, sceneID: 11, threadIDs: [2, 3] }); assert.equal(await page.locator('[data-thread-rows] article').count(), 3); assert.equal(await page.locator('[data-thread-add]').evaluate(n => document.activeElement === n), true); guards.push(...await page.evaluate(() => window.guardEvents)); assert.deepEqual(guards, [true, false]); await mount(22); await page.getByRole('button', { name: 'Add thread', exact: true }).click(); await dialog.getByRole('searchbox').fill('question'); await dialog.getByLabel('A question', { exact: true }).check(); reject = true; await dialog.getByRole('button', { name: 'Add to scene', exact: true }).click(); await dialog.getByText('Thread was archived. Review selection.', { exact: true }).waitFor(); assert.equal(links.get(22).size, 0); reject = false; await dialog.getByRole('button', { name: 'Add to scene', exact: true }).click(); await dialog.waitFor({ state: 'detached' }); await page.getByText('Ch 1: Opening · Scene 1: Earlier scene', { exact: true }).waitFor(); await mount(22); // Reload saved state through a fresh lifecycle. await page.getByText('Ch 1: Opening · Scene 1: Earlier scene', { exact: true }).waitFor(); // An external saved-data hint updates the existing node without stealing focus. links.get(22).add(2); await page.locator('[data-thread-add]').focus(); assert.equal(await page.evaluate(() => window.PlotDirectorContextLifecycles.threads.onInvalidation(document.querySelector('[data-test-pane="0"]').firstElementChild)), true); await page.waitForFunction(() => document.querySelectorAll('[data-test-pane="0"] [data-thread-rows] article').length === 2); assert.equal(await page.locator('[data-thread-add]').evaluate(n => document.activeElement === n), true); await mount(11, 1); assert.equal(await page.locator('[data-test-pane="0"] [data-thread-rows] article').count(), 2); assert.equal(await page.locator('[data-test-pane="1"] [data-thread-rows] article').count(), 3); await page.locator('[data-test-pane="1"] [data-thread-add]').click(); await page.keyboard.press('Escape'); await dialog.waitFor({ state: 'detached' }); readonly = true; await mount(11, 1); await page.waitForFunction(() => document.querySelector('[data-test-pane="1"] [data-thread-rows] article')); assert.equal(await page.locator('[data-test-pane="1"] [data-thread-add]').isDisabled(), true); assert.deepEqual(errors, []); console.log('PASS Context Threads nested search, parent context, linked state, multi-select, explicit target, guards, failure recovery, history, reload, split panes and read-only state (isolated fixtures)'); } finally { await browser.close(); } })().catch(e => { console.error(e); process.exitCode = 1; });