PlotDirector/PlotLine.Tests/Browser/context-live-editing.cjs

20 lines
6.8 KiB
JavaScript

// Opt-in Development-only canonical persistence acceptance. Requires an explicitly disposable identity and scope.
const {chromium}=require('playwright'),fs=require('node:fs'),assert=require('node:assert/strict');
(async()=>{assert(process.env.PD_CONTEXT_IDENTITY&&process.env.PD_CONTEXT_SCOPE,'Disposable Development inputs required');const who=JSON.parse(fs.readFileSync(process.env.PD_CONTEXT_IDENTITY)),s=JSON.parse(fs.readFileSync(process.env.PD_CONTEXT_SCOPE));assert.equal(who.Database,'PlotDirector_Development');assert.equal(who.UserID,s.user);assert(who.Email.startsWith('pwa-browser-'));assert(s.characters?.length===2);const browser=await chromium.launch();let step='login';try{
const pages=[],counts=[];
for(let i=0;i<2;i++){const c=await browser.newContext({viewport:{width:1280,height:900}}),p=await c.newPage();pages.push(p);await p.goto('https://dev.plotdirector.com/Account/Login');await p.locator('[name=Email]').fill(who.Email);await p.locator('[name=Password]').fill(who.Password);await Promise.all([p.waitForURL(u=>u.pathname!='/Account/Login'),p.locator('button[type=submit]').click()]);const count={hub:0,word:0,reads:0};counts.push(count);p.on('request',r=>{if(r.url().includes('/hubs/')&&r.url().includes('/negotiate'))count.hub++;if(r.url().includes('/Writing/Context/Navigate')||r.url().includes('/WriterWord/Navigate'))count.word++;if(r.url().includes('/Writing/Context/Tabs/'))count.reads++;});await p.goto(`https://dev.plotdirector.com/Writing/Context?projectId=${s.project}&bookId=${s.book}&sceneId=${s.scene}`);await p.waitForSelector('[data-context-tab-fragment]');await p.selectOption('[data-context-tab]','characters');await p.waitForSelector('[data-character-explorer]');await p.waitForFunction(()=>document.querySelector('[data-context-workspace]').contextController.connection?.state==='Connected');await p.evaluate(()=>{window.successes=0;const prior=window.PlotDirector.toast.success;window.PlotDirector.toast.success=(...a)=>{successes++;prior(...a);};});}
const [p,remote]=pages,id=s.characters[0],row=`[data-context-character="${id}"]`,stamp='Synthetic Context correction '+Date.now();
async function open(field){if(!await p.locator(row).evaluate(n=>n.open))await p.locator(row+' summary').click();await p.locator(row+` [data-context-edit="${field}"]`).click();await p.waitForSelector('[data-context-edit-form]');}
async function save(){await p.locator('[data-context-edit-form] [type=submit]').click();await p.waitForFunction(()=>!document.querySelector('[data-context-edit-dialog]').open);}
step='live canonical description and quiet remote refresh';await open('description');await p.locator('[name=Value]').fill(stamp);await save();await remote.waitForFunction(stamp=>document.querySelector('[data-character-explorer]').textContent.includes(stamp),stamp);assert.equal(await p.evaluate(()=>successes),1);assert.equal(await remote.evaluate(()=>successes),0);
step='cancel persists nothing';await open('description');assert.equal(await p.locator('[name=Value]').inputValue(),stamp);await p.locator('[name=Value]').fill('Discarded synthetic draft');p.once('dialog',d=>d.accept());await p.click('[data-context-edit-cancel]');await open('description');assert.equal(await p.locator('[name=Value]').inputValue(),stamp);await p.click('[data-context-edit-cancel]');
step='names alias add and persisted spelling';await p.selectOption('[data-context-tab]','names');await p.waitForSelector('[data-context-names]');await p.locator('[data-name-filter]').fill('Synthetic Context person one');await p.locator('[data-name-row]:visible summary').click();await p.locator(`[data-context-edit="alias-add"][data-character-id="${id}"]`).click();await p.waitForSelector('[data-context-edit-form]');const alias='Synthetic alias '+Date.now();await p.locator('[name=Value]').fill(alias);await save();await p.waitForFunction(alias=>document.querySelector('[data-context-names]').textContent.includes(alias),alias);
step='live inline validation retains failed draft';await p.locator('[data-name-filter]').fill('Synthetic Context person one');if(!await p.locator(`[data-context-edit="name"][data-character-id="${id}"]`).isVisible())await p.locator('[data-name-row]:visible summary').click();await p.locator(`[data-context-edit="name"][data-character-id="${id}"]`).click();await p.waitForSelector('[data-context-edit-form]');await p.locator('[name=Value]').fill('');await p.locator('[data-context-edit-form] [type=submit]').click();await p.waitForSelector('.validation-summary-errors');assert(await p.locator('[data-context-edit-dialog]').isVisible());assert.equal(await p.locator('[name=Value]').inputValue(),'');p.once('dialog',d=>d.accept());await p.click('[data-context-edit-cancel]');
if(process.env.PD_CONTEXT_RELATIONSHIPS==='1'){
step='live one-relationship create and correction';await p.selectOption('[data-context-tab]','relationships');await remote.selectOption('[data-context-tab]','relationships');await p.waitForSelector('[data-context-relationships]');await remote.waitForSelector('[data-context-relationships]');
const existing=p.locator('[data-context-relationship]');if(await existing.count()===0){await p.locator('[data-context-relationship-edit="scene"]').last().click();await p.waitForSelector('[data-context-edit-form]');await p.selectOption('[name=SourceID]',String(s.characters[0]));await p.selectOption('[name=TargetID]',String(s.characters[1]));const type=await p.locator('[name=TypeID] option').evaluateAll(ns=>ns.find(n=>n.value&&!n.textContent.includes('Family'))?.value);await p.selectOption('[name=TypeID]',type);const state=await p.locator('[name=StateID] option').evaluateAll(ns=>ns.find(n=>n.value)?.value);await p.selectOption('[name=StateID]',state);await p.locator('[name=Notes]').fill('Synthetic relationship evidence');await save();}
await p.waitForSelector('[data-context-relationship]');const rr=p.locator('[data-context-relationship]').first();if(!await rr.evaluate(n=>n.open))await rr.locator('summary').first().click();await rr.locator('[data-context-relationship-edit="scene"]').click();await p.waitForSelector('[data-context-edit-form]');const note='Synthetic relationship correction '+Date.now();await p.locator('[name=Notes]').fill(note);await save();await remote.waitForFunction(note=>document.querySelector('[data-context-relationships]').textContent.includes(note),note);assert.equal(await remote.evaluate(()=>successes),0);
}
assert(counts.every(c=>c.word===0&&c.hub===1));console.log('PASS Development canonical editing: focused persisted values, cancel/unsaved protection, alias add, failed inline name draft retained, one local success/quiet second authenticated session, one connection each, no Word navigation'+(process.env.PD_CONTEXT_RELATIONSHIPS==='1'?', relationship create/correct persisted through existing services':''));
} catch(e){console.error('FAILED '+step+': '+e.message);process.exitCode=1;}finally{await browser.close();}})();