PlotDirector/PlotLine.Tests/Browser/context-intent-palette-acceptance.cjs
2026-12-02 21:19:11 +00:00

95 lines
6.1 KiB
JavaScript

// Backend palette milestone only. This does not certify the unfinished Intent UI.
'use strict';
const assert = require('node:assert/strict');
const {open} = require('./dev-acceptance.cjs');
let session, stage = 'login';
(async () => {
try {
session = await open();
const project = await session.project();
const book = await session.create('Books', '?projectId='+project, {BookTitle:'Intent palette acceptance',BookNumber:1});
const chapter = await session.create('Chapters', '?bookId='+book, {ChapterTitle:'Empty chapter palette',ChapterNumber:1});
const otherBook = await session.create('Books', '?projectId='+project, {BookTitle:'Other scoped book',BookNumber:2});
const otherChapter = await session.create('Chapters', '?bookId='+otherBook, {ChapterTitle:'Other chapter',ChapterNumber:1});
const base = {bookID:book, chapterID:chapter};
async function read(query={}, expected=200) {
const url = '/Writing/Context/Intent/Palette?'+new URLSearchParams({...base,...query});
const result = await session.page.evaluate(async url => {
const response = await fetch(url, {credentials:'same-origin'});
const json=(response.headers.get('content-type')||'').includes('application/json');
return {status:response.status,redirected:response.redirected,path:new URL(response.url).pathname,
data:response.status===200&&json?await response.json():null};
}, url);
if(result.status!==expected) console.error(JSON.stringify({stage,expected,status:result.status,redirected:result.redirected,path:result.path}));
assert.equal(result.status, expected);
return result.data;
}
stage='chapter-only/no-prose read';
assert.deepEqual((await read()).entries, []);
stage='canonical equal-label fixtures';
const name='Palette shared name';
const person = await session.create('Characters','?projectId='+project,{CharacterName:name},async page=>{
const sex=page.locator('[name=CustomSex]');
if(await sex.isEnabled() && await sex.getAttribute('required')!==null) await sex.fill('Unspecified');
});
const place = await session.create('Locations','?projectId='+project,{LocationName:name});
const literal = await session.create('Locations','?projectId='+project,{LocationName:'Palette literal %'});
stage='wider story identity and literal search';
const wider = await read({widerStory:true, search:'SHARED NAME'});
assert.deepEqual(new Set(wider.entries.map(e=>e.key)), new Set(['character:'+person,'location:'+place]));
assert(wider.entries.every(e=>e.possiblyAmbiguous && !e.isCandidate && !e.inScope));
assert.equal(wider.hasMore,false);
assert.deepEqual((await read({widerStory:true,search:'%'})).entries.map(e=>e.key),['location:'+literal]);
assert.deepEqual((await read()).entries, []);
async function scene(location, title) {
await session.goto('/Scenes/Create?chapterId='+chapter);
const page=session.page;
await page.locator('#SceneTitle').fill(title);
await page.locator('#PrimaryLocationID').selectOption(String(location));
await page.getByRole('button',{name:'Create scene',exact:true}).click();
await page.waitForURL('**/Scenes/Edit/*');
return Number(new URL(page.url()).pathname.split('/').at(-1));
}
stage='recorded chapter/scene associations';
const first=await scene(place,'Palette first scene'), second=await scene(literal,'Palette second scene');
let scoped=await read({sceneID:first});
assert.deepEqual(scoped.entries.map(e=>e.key),['location:'+place]);
assert(scoped.entries[0].inScope && scoped.entries[0].possiblyAmbiguous);
assert.deepEqual(scoped.entries[0].sources,[{sceneID:first,provenance:'Scene location'}]);
assert.deepEqual((await read({sceneID:second})).entries.map(e=>e.key),['location:'+literal]);
assert.deepEqual(new Set((await read()).entries.map(e=>e.key)),new Set(['location:'+place,'location:'+literal]));
stage='invalid and mismatched scope denial';
stage='chapter/book mismatch denial';await read({chapterID:otherChapter},403);
stage='book/chapter mismatch denial';await read({bookID:otherBook},403);
stage='scene/chapter mismatch denial';await read({chapterID:otherChapter,bookID:otherBook,sceneID:first},403);
stage='invalid scene rejection';await read({sceneID:0},400);
stage='oversized search rejection';await read({search:'x'.repeat(201)},400);
stage='fresh login persistence and read-only source';
await session.close(); session=await open();
scoped=await read({sceneID:first});
assert.equal(scoped.entries[0].key,'location:'+place,'Fresh palette identity');
// Independently reload the normal authorised source form from the server. This
// endpoint is the same partial loaded by the existing scene-details editor.
assert.equal((await session.goto('/Scenes/SceneDetailsEditor?sceneId='+first)).status(),200);
const editor=session.page.locator('form[data-scene-editor-form]');
assert.equal(await editor.locator('[name=PrimaryLocationID]').inputValue(),String(place),'Source location');
assert.equal(await editor.locator('[name=SceneTitle]').inputValue(),'Palette first scene','Source title');
console.log(JSON.stringify({passed:true,milestone:'palette read path only',checks:[
'chapter without prose or scenes','canonical identity separation and hidden-label ambiguity',
'case-insensitive wider search and literal percent','chapter and optional scene associations/provenance',
'book/chapter/scene mismatch denial and query limits','fresh login reads authoritative saved sources'
],featureUrl:'https://dev.plotdirector.com/Writing/Context/Intent/Palette?'+new URLSearchParams(base),
limitations:['interactive checklist/palette component and candidate fixture acceptance remain outstanding']}));
} catch(error) {
console.error('Intent palette acceptance failed at '+stage+' ('+error.name+'). No credentials logged.');
if(error.name==='AssertionError') console.error(error.message);
process.exitCode=1;
} finally {
if(session) {
try { await session.cleanup(); console.log('PASS disposable Intent palette project cleanup'); }
catch { console.error('Intent palette cleanup needs documented resume.'); process.exitCode=1; }
finally { await session.close(); }
}
}
})().catch(()=>{console.error('Intent palette session failed; preserve run journal.');process.exitCode=1;});