PlotDirector/PlotLine.Tests/Browser/context-workspace-development.cjs

38 lines
6.3 KiB
JavaScript

// Opt-in Development smoke. Only edits a specifically named disposable synthetic scene.
const {chromium}=require('playwright'),fs=require('node:fs'),assert=require('node:assert/strict');
(async()=>{
const identity=JSON.parse(fs.readFileSync(process.env.PD_CONTEXT_IDENTITY));
const scope=JSON.parse(fs.readFileSync(process.env.PD_CONTEXT_SCOPE));
assert.equal(identity.Database,'PlotDirector_Development');assert.equal(identity.UserID,scope.user);
assert(identity.Email.startsWith('pwa-browser-')&&identity.Email.endsWith('@example.invalid'));
const base='https://dev.plotdirector.com',browser=await chromium.launch();let step='login',restore;
try {
const context=await browser.newContext({viewport:{width:1366,height:768}}),page=await context.newPage();
await page.goto(base+'/Account/Login');await page.locator('[name=Email]').fill(identity.Email);await page.locator('[name=Password]').fill(identity.Password);
await Promise.all([page.waitForURL(u=>u.pathname!=='/Account/Login'),page.locator('button[type=submit]').click()]);
step='ownership and synthetic identity';await page.goto(base+'/Projects/Details/'+scope.project);assert(await page.getByText('Context foundation disposable qualification',{exact:true}).count());
const url=`${base}/Writing/Context?projectId=${scope.project}&bookId=${scope.book}&sceneId=${scope.scene}`;
let connections=0,reads=0;const errors=[];page.on('pageerror',e=>errors.push(e.message));page.on('request',r=>{if(r.url().includes('/hubs/')&&r.url().includes('/negotiate'))connections++;if(r.url().includes('/Writing/Context/Tabs/overview'))reads++;});
step='real authorized Overview';await page.goto(url);await page.locator('[data-context-pane="0"] [data-context-tab-fragment]').waitFor();
const title=await page.locator('[data-context-pane="0"] [data-context-tab-fragment]>h2').innerText();assert.equal(title,'Synthetic foundation scene');
await page.waitForTimeout(500);assert.equal(connections,1,'one real browser SignalR connection');
step='Characters and split';await page.selectOption('[data-context-layout]','columns');await page.locator('[data-context-pane="1"] [data-context-tab-fragment]').waitFor();assert.match(await page.locator('[data-context-pane="1"]').innerText(),/No characters recorded/);
await page.selectOption('[data-context-pane="1"] [data-context-tab]','overview');await page.waitForFunction(()=>document.querySelectorAll('[data-context-tab-fragment]>h2').length===2);
step='independent window';const opened=page.waitForEvent('popup');await page.click('[data-context-new]');const popup=await opened;await popup.waitForSelector('[data-context-tab-fragment]');assert.equal(await popup.locator('[data-context-layout]').inputValue(),'single');
const editor=await context.newPage();await editor.goto(base+`/Scenes/Edit/${scope.scene}`);await editor.click('[data-scene-editor="scene-details"]');const form=editor.locator('[data-scene-editor-form]').filter({has:editor.locator('[name="SceneTitle"]')});await form.waitFor();
const saved=await form.evaluate(n=>({action:n.action,fields:Object.fromEntries([...new FormData(n)].filter(([,v])=>typeof v==='string'))}));assert.equal(saved.fields.SceneTitle,title);restore=async()=>{const response=await page.request.post(saved.action,{form:saved.fields});assert(response.ok(),'synthetic title restore failed');};
await page.waitForTimeout(300);const before=reads;
step='real cross-window invalidation';const response=await editor.request.post(saved.action,{form:{...saved.fields,SceneTitle:title+' updated through editor'}});assert(response.ok());
await page.waitForFunction(()=>[...document.querySelectorAll('[data-context-tab-fragment]>h2')].every(n=>n.textContent.includes('updated through editor')));
await popup.waitForFunction(()=>document.querySelector('[data-context-tab-fragment]>h2').textContent.includes('updated through editor'));
assert.equal(reads-before,1,'same tab in two panes shares one real refresh request');await restore();restore=null;await page.waitForFunction(()=>[...document.querySelectorAll('[data-context-tab-fragment]>h2')].every(n=>n.textContent==='Synthetic foundation scene'));
step='persistence';await page.selectOption('[data-context-layout]','rows');const id=await page.evaluate(()=>document.querySelector('[data-context-workspace]').contextController.windowId);await page.reload();await page.waitForSelector('[data-context-tab-fragment]');assert.equal(await page.locator('[data-context-layout]').inputValue(),'rows');assert.equal(await page.evaluate(()=>document.querySelector('[data-context-workspace]').contextController.windowId),id);
step='snapshot and real authorization';const pinned=await context.newPage();let snapshotConnections=0;pinned.on('request',r=>{if(r.url().includes('/hubs/')&&r.url().includes('/negotiate'))snapshotConnections++;});await pinned.goto(url+'&pinned=true');await pinned.waitForSelector('[data-context-tab-fragment]');assert.equal(snapshotConnections,0);
const native=await page.request.get(`${base}/Writing/Context/Tabs/overview?bookId=${scope.book}&sceneId=${scope.scene}&instanceId=11111111-1111-4111-8111-111111111111&sessionId=22222222-2222-4222-8222-222222222222`);assert.equal(native.status(),409);
const foreign=await page.request.get(`${base}/Writing/Context/Tabs/overview?bookId=2147483647&sceneId=${scope.scene}`);assert([403,404].includes(foreign.status()));
const old=await page.request.get(`${base}/Writing/Intent?projectId=${scope.project}&bookId=${scope.book}&sceneId=${scope.scene}`,{maxRedirects:0});assert.equal(old.status(),302);assert.match(old.headers().location,/Writing\/Context/);
const anonymous=await browser.newContext();const denied=await anonymous.request.get(`${base}/Writing/Context/Tabs/characters?sceneId=${scope.scene}`,{maxRedirects:0});assert([302,401].includes(denied.status()));await anonymous.close();
assert.deepEqual(errors,[]);await context.close();console.log('PASS Development actual saved Razor data, one real SignalR connection, two panes/shared read, cross-window editor invalidation, independent window, persistence, pinned snapshot, native session/access denial and Intent redirect; synthetic title restored');
} catch(error){console.error('FAILED at '+step+': '+error.message);process.exitCode=1;} finally{if(restore)await restore();await browser.close();}
})().catch(()=>{console.error('Development Context smoke could not complete');process.exitCode=1;});