PlotDirector/PlotLine.Tests/Browser/context-location-correction.cjs

59 lines
8.6 KiB
JavaScript

// Real Development auth/SQL/SignalR; only the guarded disposable shared-focus fixture.
const {chromium}=require('playwright'),fs=require('node:fs'),assert=require('node:assert/strict');
const base=process.env.PD_SHARED_FOCUS_URL||'http://127.0.0.1:5258';
(async()=>{
const who=JSON.parse(fs.readFileSync(process.env.PD_CONTEXT_IDENTITY)),s=JSON.parse(fs.readFileSync('/tmp/shared-focus-browser-scope.json'));
assert.equal(who.Database,'PlotDirector_Development');assert.equal(who.UserID,s.user);assert(who.Email.startsWith('pwa-browser-'));
const browser=await chromium.launch();let step='login';const errors=[];
try{
const c=await browser.newContext({viewport:{width:1450,height:1000}});
c.on('page',p=>p.on('pageerror',e=>errors.push(e.message)));
await c.route('**/signalr.min.js',r=>r.fulfill({contentType:'application/javascript',body:fs.readFileSync(require.resolve('@microsoft/signalr/dist/browser/signalr.min.js'),'utf8')}));
const login=await c.newPage();await login.goto(base+'/Account/Login');await login.locator('[name=Email]').fill(who.Email);await login.locator('[name=Password]').fill(who.Password);
await Promise.all([login.waitForURL(u=>u.pathname!='/Account/Login'),login.locator('button[type=submit]').click()]);
const url=`${base}/Writing/Context?projectId=${s.project}&bookId=${s.book}&sceneId=${s.scene2}`;
async function ready(p){await p.waitForFunction(()=>window.PlotDirectorSharedFocus&&document.querySelector('[data-context-workspace]')?.contextController?.connection?.state==='Connected');await p.waitForSelector('[data-context-tab-fragment]');await p.waitForFunction(()=>!document.querySelector('[data-shared-focus-hold]').disabled);}
async function open(){const p=await c.newPage();await p.goto(url);await ready(p);await p.selectOption('[data-context-tab]','characters');await p.waitForSelector(`[data-context-character="${s.character1}"]`);return p;}
const a=await open();
if(process.argv.includes('--empty-library')){
await a.locator(`[data-context-edit="location"][data-character-id="${s.character1}"]`).click();await a.waitForSelector('[data-context-edit-form] select[name=Value]');
assert.equal(await a.locator('[name=Value] option').count(),1);assert.equal(await a.locator('[name=Value] option').getAttribute('value'),'');
assert.match(await a.locator('[data-context-edit-form]').innerText(),/No locations are available in this project's Locations Library/);
console.log('PASS real empty project Locations Library: explicit empty state and clearing option.');return;
}
const b=await open();
await a.selectOption('[data-context-scene]',String(s.scene2));await a.waitForFunction(id=>PlotDirectorSharedFocus.local.sceneID===id,s.scene2);
await b.click('[data-shared-focus-hold]');assert.equal(await b.locator('[data-shared-focus-hold]').innerText(),'Release');
for(const p of [a,b])assert.equal(await p.locator('[data-shared-focus-hold] svg').count(),0);
await b.selectOption('[data-context-layout]','columns');await b.selectOption('[data-context-pane="1"] [data-context-tab]','characters');await b.waitForSelector(`[data-context-pane="1"] [data-context-character="${s.character1}"]`);
async function expand(p,pane,id){const row=p.locator(`[data-context-pane="${pane}"] [data-context-character="${id}"]`);await row.evaluate(n=>n.open=true);return row;}
for(const [p,pane] of [[a,0],[b,0],[b,1]])await expand(p,pane,s.character1);
const snapshot=p=>p.evaluate(()=>({local:PlotDirectorSharedFocus.local,shared:PlotDirectorSharedFocus.shared,held:PlotDirectorSharedFocus.held}));
const beforeA=await snapshot(a),beforeB=await snapshot(b);
async function edit(id=s.character1){await expand(a,0,id);await a.locator(`[data-context-pane="0"] [data-context-edit="location"][data-character-id="${id}"]`).click();await a.waitForSelector('[data-context-edit-form] select[name=Value]');}
async function save(value){await a.selectOption('[data-context-edit-form] [name=Value]',value);await a.locator('[data-context-edit-form] button[type=submit]').click();await a.waitForFunction(()=>!document.querySelector('[data-context-edit-dialog]').open);}
async function now(p,pane,id,value){await p.waitForFunction(({pane,id,value})=>{const n=document.querySelector(`[data-context-pane="${pane}"] [data-character-detail="${id}"] .context-location-rail > div:nth-child(2)`);return n?.textContent.includes(value);},{pane,id,value});}
step='original failure: no previous current location still gets the whole project library';await edit();
const options=await a.locator('[name=Value] option').evaluateAll(ns=>ns.map(n=>({text:n.textContent,value:n.value})));
assert.equal(options[0].value,'');assert.match(options[0].text,/No recorded location/);assert(options.length>=4);
const room=options.find(o=>o.text.includes('Fixture room'));assert(room&&room.text.includes('Fixture house'),'hierarchy path shown');const garden=options.find(o=>o.text.includes('Fixture garden'));assert(garden);
step='canonical save refreshes single and both split panes, including held window';await save(room.value);
for(const [p,pane] of [[a,0],[b,0],[b,1]]){await now(p,pane,s.character1,'Fixture room');assert.equal(await p.locator(`[data-context-pane="${pane}"] [data-context-character="${s.character1}"]`).evaluate(n=>n.open),true,'selected character remains expanded');}
assert.deepEqual(await snapshot(a),beforeA);assert.deepEqual(await snapshot(b),beforeB);
const rail=a.locator(`[data-character-detail="${s.character1}"] .context-location-rail`);assert.match(await rail.innerText(),/Previous[\s\S]*Fixture house[\s\S]*Now[\s\S]*Fixture room[\s\S]*Next[\s\S]*Fixture garden/);
step='refresh/reopening persists without changing Hold or scene';await b.reload();await ready(b);await now(b,0,s.character1,'Fixture room');assert.equal(await b.locator('[data-shared-focus-hold]').innerText(),'Release');assert.deepEqual(await snapshot(b),beforeB);
await edit();assert.equal(await a.locator('[name=Value]').inputValue(),room.value);await a.click('[data-context-edit-cancel]');
const reopened=await open();await now(reopened,0,s.character1,'Fixture room');
step='other character remains unrecorded and can be corrected independently';await edit(s.character2);assert.equal(await a.locator('[name=Value]').inputValue(),'');await save(garden.value);await now(b,1,s.character2,'Fixture garden');await now(a,0,s.character1,'Fixture room');
step='clear persists and updates held panes';await edit();await save('');for(const [p,pane] of [[a,0],[b,0],[b,1]])await now(p,pane,s.character1,'No recorded location here');
await edit();assert.equal(await a.locator('[name=Value]').inputValue(),'');await a.click('[data-context-edit-cancel]');assert.deepEqual(await snapshot(a),beforeA);assert.deepEqual(await snapshot(b),beforeB);
const state=await a.evaluate(async s=>{const results=[];for(const scene of [s.scene1,s.scene3]){const response=await fetch(`/Writing/Context/EditCharacter?bookId=${s.book}&sceneId=${scene}&characterId=${s.character1}&field=location`);const doc=new DOMParser().parseFromString(await response.text(),'text/html');results.push(doc.querySelector('[name=Value] option:checked')?.textContent);}return results;},s);
assert.match(state[0],/Fixture house/);assert.match(state[1],/Fixture garden/);
await b.click('[data-shared-focus-hold]');await b.waitForFunction(()=>!PlotDirectorSharedFocus.held);assert.equal(await b.locator('[data-shared-focus-hold]').innerText(),'Hold');assert.equal(await b.locator('[data-shared-focus-hold] svg').count(),0);
step='main ribbon remains Hold/Resume with icons and three Word caret actions';await login.goto(`${base}/Writer/Review?projectId=${s.project}&bookId=${s.book}&chapterId=${s.chapter1}`);
const main=login.locator('.pd-ribbon-panel:not([hidden]) [data-shared-focus-hold]');await main.waitFor({state:'visible'});await login.waitForFunction(()=>!document.querySelector('.pd-ribbon-panel:not([hidden]) [data-shared-focus-hold]').disabled);
assert.equal(await main.locator('svg [data-focus-mode-icon]').count(),1);await main.click();assert.equal(await main.innerText(),'Resume');await main.click();await login.waitForFunction(()=>!PlotDirectorSharedFocus.held);assert.equal(await main.innerText(),'Hold');assert.equal(await login.locator('[data-ribbon-word]').count(),3);
assert.deepEqual(errors,[]);console.log('PASS real Context location correction: canonical library without history, hierarchy, save/clear/reopen, chronology, independent other character/scenes, single/split panes, held live invalidation, focus/selection preserved, text-only Hold/Release, main ribbon icons and Word caret controls unchanged.');
}catch(e){console.error('FAILED '+step+': '+e.stack);process.exitCode=1;}finally{await browser.close();}
})();