116 lines
6.5 KiB
JavaScript
116 lines
6.5 KiB
JavaScript
// Isolated interaction coverage. The in-memory fixture does not verify SQL persistence.
|
|
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 page = await browser.newPage();
|
|
const errors = [];
|
|
page.on('pageerror', error => errors.push(error.message));
|
|
const writes = [];
|
|
const invalidations = [];
|
|
const placements = new Map([[1, { sceneID: 11 }]]);
|
|
let reject = true;
|
|
let canEdit = true;
|
|
const threads = ['A <saved> question', 'Promise', 'Hidden answer'];
|
|
await page.route('http://context.test/**', async route => {
|
|
const url = new URL(route.request().url());
|
|
if (url.pathname === '/ContextThreads/State') {
|
|
const sceneID = Number(url.searchParams.get('sceneId'));
|
|
return route.fulfill({ json: {
|
|
scope: { label: `Chapter 1 · Scene ${sceneID}`, canEdit },
|
|
threads: threads.map((title, index) => {
|
|
const plotThreadID = index + 1;
|
|
const target = placements.get(plotThreadID);
|
|
const includedInChapter = target?.chapterID === 101;
|
|
const includedInScene = target?.sceneID === sceneID;
|
|
return {
|
|
plotThreadID, title, plotLineName: 'Journey', includedInChapter, includedInScene,
|
|
canRemove: canEdit && (includedInChapter || includedInScene), assigned: !!target,
|
|
assignedElsewhere: !!target && !includedInChapter && !includedInScene,
|
|
assignmentLabel: target?.sceneID ? `Scene ${target.sceneID}` : null,
|
|
hasManuscriptAssociation: plotThreadID === 1 && sceneID === 11,
|
|
previousScenes: plotThreadID === 1 && sceneID === 22
|
|
? [{ chapterID: 101, sceneID: 11, chapterNumber: 1, chapterTitle: 'Opening', sceneNumber: 1, sceneTitle: 'Earlier scene' }] : [],
|
|
chapterBlock: plotThreadID === 3 ? 'Place Promise earlier in this book.' : null
|
|
};
|
|
})
|
|
} });
|
|
}
|
|
if (['/ContextThreads/Add', '/ContextThreads/Remove'].includes(url.pathname)) {
|
|
const body = route.request().postDataJSON();
|
|
writes.push(body);
|
|
assert.equal(route.request().headers().requestverificationtoken, 'fixture-token');
|
|
if (url.pathname.endsWith('/Add')) {
|
|
if (reject) return route.fulfill({ status: 409, json: { message: 'The saved assignment changed.' } });
|
|
for (const id of body.threadIDs)
|
|
placements.set(id, body.chapter ? { chapterID: body.chapterID } : { sceneID: body.sceneID });
|
|
} else {
|
|
placements.delete(body.threadID);
|
|
}
|
|
return route.fulfill({ json: { saved: true } });
|
|
}
|
|
assert.equal(url.pathname, '/');
|
|
return route.fulfill({ contentType: 'text/html', body: '<!doctype html><main data-context-workspace></main>' });
|
|
});
|
|
await page.goto('http://context.test/');
|
|
await page.addScriptTag({ content: script });
|
|
await page.evaluate(() => {
|
|
window.invalidations = [];
|
|
document.querySelector('main').contextController = { invalidate: hint => window.invalidations.push(hint) };
|
|
});
|
|
async function mount(sceneID) {
|
|
await page.evaluate(scene => {
|
|
const host = document.querySelector('main');
|
|
if (host.firstElementChild) window.PlotDirectorContextLifecycles.threads.dispose(host.firstElementChild);
|
|
host.innerHTML = `<div data-book-id="12" data-chapter-id="101" data-scene-id="${scene}">
|
|
<input name="__RequestVerificationToken" value="fixture-token" type="hidden">
|
|
<button data-thread-add disabled>Include threads</button><p data-thread-error></p><div data-thread-rows></div></div>`;
|
|
window.PlotDirectorContextLifecycles.threads.mount(host.firstElementChild);
|
|
}, sceneID);
|
|
await page.getByRole('heading', { name: 'Included here' }).waitFor();
|
|
}
|
|
await mount(11);
|
|
const add = page.getByRole('button', { name: 'Include threads', exact: true });
|
|
await page.getByRole('button', { name: 'Remove A <saved> question from scene' }).waitFor();
|
|
await add.click();
|
|
const dialog = page.getByRole('dialog');
|
|
assert.equal(await dialog.getByRole('checkbox', { name: /A <saved> question/ }).count(), 0);
|
|
assert.equal(await dialog.getByRole('checkbox', { name: /Hidden answer/ }).isDisabled(), true);
|
|
await dialog.getByRole('searchbox').fill('promise');
|
|
await dialog.getByRole('checkbox', { name: /Promise · Journey/ }).check();
|
|
const include = dialog.getByRole('button', { name: 'Include selected' });
|
|
await include.click();
|
|
await dialog.getByText('The saved assignment changed.').waitFor();
|
|
assert.equal(placements.has(2), false);
|
|
reject = false;
|
|
await include.click();
|
|
await dialog.waitFor({ state: 'detached' });
|
|
assert.deepEqual(writes.at(-1), { bookID: 12, chapterID: 101, sceneID: 11, threadIDs: [2], chapter: true });
|
|
assert.equal(await add.evaluate(button => document.activeElement === button), true);
|
|
await mount(22);
|
|
await page.getByRole('button', { name: 'Remove Promise from chapter' }).waitFor();
|
|
await page.locator('summary', { hasText: 'Assigned elsewhere and scene history' }).click();
|
|
const earlier = page.getByRole('link', { name: 'Scene 1: Earlier scene' });
|
|
assert.equal(await earlier.getAttribute('href'), '/Scenes/Edit/11?retainNarrativeFocus=true');
|
|
await page.getByRole('button', { name: 'Remove Promise from chapter' }).click();
|
|
await page.getByText('No threads included here.').waitFor();
|
|
assert.deepEqual(writes.at(-1), { bookID: 12, chapterID: 101, sceneID: 22, threadID: 2, chapter: true });
|
|
assert.deepEqual([...placements.keys()], [1]);
|
|
invalidations.push(...await page.evaluate(() => window.invalidations));
|
|
assert.deepEqual(invalidations, [{ bookId: 12, kind: 'threads' }, { bookId: 12, kind: 'threads' }]);
|
|
canEdit = false;
|
|
await mount(11);
|
|
assert.equal(await add.isDisabled(), true);
|
|
assert.equal(await page.getByRole('button', { name: 'Remove A <saved> question from scene' }).count(), 0);
|
|
assert.deepEqual(errors, []);
|
|
console.log('PASS Context Threads search, blocked selection, exclusive assignment, chapter persistence across scenes, invalidation, history, and read-only controls (mocked)');
|
|
} finally {
|
|
await browser.close();
|
|
}
|
|
})().catch(error => { console.error(error); process.exitCode = 1; });
|