PlotDirector/PlotLine.Tests/Browser/context-calendar-development.cjs
2026-12-01 19:50:30 +00:00

25 lines
11 KiB
JavaScript

// Real Development forms, saved chronology, isolated registered project and ordinary writer.
const assert=require('node:assert/strict'),{open}=require('./dev-acceptance.cjs');
let session,stage='login';
(async()=>{try{
session=await open();let p=session.page;const project=await session.project();
const book=await session.create('Books','?projectId='+project,{BookTitle:'Calendar first',BookNumber:1}),otherBook=await session.create('Books','?projectId='+project,{BookTitle:'Calendar second',BookNumber:2});
const chapter=await session.create('Chapters','?bookId='+book,{ChapterTitle:'Same chapter',ChapterNumber:1}),otherChapter=await session.create('Chapters','?bookId='+otherBook,{ChapterTitle:'Other book chapter',ChapterNumber:1}),undatedChapter=await session.create('Chapters','?bookId='+book,{ChapterTitle:'Undated chapter',ChapterNumber:2});
async function scene(ch,title,date){await session.goto('/Scenes/Create?chapterId='+ch);await p.locator('#SceneTitle').fill(title);await p.locator('[name=Summary]').fill('Saved calendar summary for '+title+'\n'+('Summary detail line.\n'.repeat(60)));if(date){const exact=await p.locator('#TimeModeID option').evaluateAll(ns=>ns.find(n=>/exact/i.test(n.textContent))?.value);assert(exact);await p.locator('#TimeModeID').selectOption(exact);await p.locator('#StartDateTime').fill(date+'T10:00');}await p.getByRole('button',{name:'Create scene',exact:true}).click();await p.waitForURL('**/Scenes/Edit/*');return Number(new URL(p.url()).pathname.split('/').at(-1));}
stage='saved chronology';const first=await scene(chapter,'First dated scene','1983-11-19'),second=await scene(chapter,'Second dated scene','1983-11-19'),other=await scene(otherChapter,'Other book collision','1983-11-19'),undated=await scene(chapter,'Undated with chapter fallback'),empty=await scene(undatedChapter,'Undated without fallback');
const feature=`/Writing/Context?projectId=${project}&bookId=${book}&chapterId=${chapter}&sceneId=${first}&pinned=true`,ctx=await p.context().newPage();await ctx.setViewportSize({width:1280,height:800});await ctx.goto('https://dev.plotdirector.com'+feature);await ctx.selectOption('[data-context-pane="0"] [data-context-tab]','calendar');const pane=ctx.locator('[data-context-pane="0"]'),day=d=>pane.locator(`[data-calendar-date="${d}"]`);
stage='follow, deduplication and collision';await day('1983-11-19').waitFor();assert.equal(await day('1983-11-19').getAttribute('aria-current'),'date');assert.equal(await day('1983-11-19').locator('.context-calendar-chapter').textContent(),'Ch 1');assert.equal(await day('1983-11-19').locator('small').count(),0);await pane.locator('[data-calendar-books]').selectOption('all');assert.equal(await day('1983-11-19').locator('.context-calendar-chapter:visible').count(),2);assert.equal(await day('1983-11-19').locator('.context-calendar-more:visible').count(),0);await day('1983-11-19').locator('.context-calendar-chapter').first().click();await pane.getByText('Scene 1 · First dated scene',{exact:true}).click();assert.equal(await pane.getByRole('link',{name:'Open scene in main window'}).count(),1);assert.match(await pane.locator('.context-calendar-scene-summary').first().textContent(),/Saved calendar summary for First dated scene/);
await ctx.waitForFunction(()=>document.querySelector('[data-context-workspace]').contextController.connection?.state==='Connected');const posts=[];ctx.on('request',r=>{if(r.method()==='POST'&&/NarrativeFocus|NativeCompanion.*Navigate/.test(r.url())&&!r.url().endsWith('/NarrativeFocus/Shared/Validate'))posts.push(new URL(r.url()).pathname);});const focus=await ctx.evaluate(()=>({...document.querySelector('[data-context-workspace]').contextController.position}));const shared=()=>ctx.evaluate(async({project,book})=>(await(await fetch(`/NarrativeFocus/Shared?projectID=${project}&bookID=${book}`)).json()),{project,book});const before=await shared();await day('1983-11-19').locator('.context-calendar-chapter').last().click();assert.equal(await pane.locator('[data-calendar-details]>details').last().evaluate(n=>n.open),true);assert.deepEqual(posts,[]);assert.equal((await shared()).version,before.version);
const scroll=await pane.locator('[data-calendar-details]').evaluate(n=>{n.querySelectorAll('details').forEach(d=>d.open=true);n.scrollTop=100;return {top:n.scrollTop,height:n.clientHeight,scroll:n.scrollHeight};});assert(scroll.scroll>scroll.height&&scroll.top>0);
stage='browse empty dates and six-row viewport';await day('1983-11-20').locator('.context-calendar-day-number').click();await pane.locator('[data-calendar-month]').fill('2020-08');await pane.locator('[data-calendar-month]').dispatchEvent('change');assert.equal(await pane.locator('[data-calendar-grid] [data-calendar-date]').count(),42);const geometry=await pane.locator('[data-calendar-grid]').evaluate(n=>({bottom:n.getBoundingClientRect().bottom,scroll:n.scrollHeight,height:n.clientHeight,viewport:innerHeight}));assert(geometry.bottom<=geometry.viewport);assert(geometry.scroll<=geometry.height+1);await day('2020-08-15').locator('.context-calendar-day-number').click();assert.match(await pane.locator('[data-calendar-details]').textContent(),/No dated/);await pane.locator('[data-calendar-from]').fill('2020-08-01');await pane.locator('[data-calendar-from]').dispatchEvent('change');assert.equal(await pane.locator('[data-calendar-elapsed]').textContent(),'14 days · 2 weeks, 0 days');
stage='real saved date refresh while held';await session.goto('/Scenes/SceneDetailsEditor?sceneId='+first);const exact=await p.locator('[name=TimeModeID] option').evaluateAll(ns=>ns.find(n=>/exact/i.test(n.textContent))?.value);await p.locator('[name=TimeModeID]').selectOption(exact);await p.locator('[name=StartDateTime]').fill('1984-02-29T10:00');await Promise.all([p.waitForResponse(r=>r.url().includes('/Scenes/SaveSceneDetails')&&r.status()===200),p.getByRole('button',{name:'Save details',exact:true}).click()]);await ctx.waitForFunction(()=>document.querySelector('[data-calendar-status]').textContent.includes('29 February 1984'));assert.equal(await pane.locator('[data-calendar-month]').inputValue(),'2020-08');assert.equal(await pane.locator('[data-calendar-from]').inputValue(),'2020-08-01');assert.equal(await day('2020-08-15').getAttribute('data-selected'),'true');assert.deepEqual(await ctx.evaluate(()=>({...document.querySelector('[data-context-workspace]').contextController.position})),focus);assert.deepEqual(posts,[]);const after=await shared();assert.equal(after.version,before.version);assert.equal(after.sceneID,before.sceneID);
stage='sibling-book date save refresh';await session.goto('/Scenes/SceneDetailsEditor?sceneId='+other);const otherExact=await p.locator('[name=TimeModeID] option').evaluateAll(ns=>ns.find(n=>/exact/i.test(n.textContent))?.value);await p.locator('[name=TimeModeID]').selectOption(otherExact);await p.locator('[name=StartDateTime]').fill('1984-03-01T10:00');await Promise.all([ctx.waitForResponse(r=>r.url().includes('/Calendar/State')&&r.status()===200),p.getByRole('button',{name:'Save details',exact:true}).click()]);await pane.locator('[data-calendar-month]').fill('1984-03');await pane.locator('[data-calendar-month]').dispatchEvent('change');assert.match(await day('1984-03-01').textContent(),/Book 2/);assert.equal((await shared()).version,before.version);assert.deepEqual(posts,[]);
stage='Return to Scene and leap day';await pane.locator('[data-calendar-return]').click();assert.equal(await pane.locator('[data-calendar-month]').inputValue(),'1984-02');assert.equal(await day('1984-02-29').getAttribute('aria-current'),'date');
stage='main scene link retains focus';await session.goto('/Locations?projectId='+project);await p.waitForFunction(()=>window.PlotDirectorPresenceConnection?.state==='Connected');if(!await pane.locator('[data-calendar-details]>details').first().evaluate(n=>n.open))await pane.locator('[data-calendar-details]>details summary').first().click();if(!await pane.getByText('Scene 1 · First dated scene',{exact:true}).evaluate(n=>n.parentElement.open))await pane.getByText('Scene 1 · First dated scene',{exact:true}).click();const contextUrl=ctx.url();await pane.getByRole('link',{name:'Open scene in main window'}).click();await p.waitForURL('**/Scenes/Edit/'+first+'?retainNarrativeFocus=true');assert.equal(ctx.url(),contextUrl);assert.equal((await shared()).version,before.version);assert.deepEqual(posts,[]);
stage='chapter fallback and completely undated retention';await ctx.selectOption('[data-context-scene]',String(undated));await ctx.waitForFunction(()=>document.querySelector('[data-calendar-status]').textContent.includes('chapter (first dated scene)'));assert.equal(await pane.locator('[data-calendar-month]').inputValue(),'1984-02');await ctx.selectOption('[data-context-scene]',String(empty));await ctx.waitForFunction(()=>document.querySelector('[data-calendar-status]').textContent.includes('undated; month retained'));assert.equal(await pane.locator('[data-calendar-month]').inputValue(),'1984-02');
stage='split panes and themes';await ctx.selectOption('[data-context-layout]','columns');await ctx.selectOption('[data-context-pane="1"] [data-context-tab]','calendar');await ctx.locator('[data-context-pane="1"] [data-calendar-grid] button').first().waitFor();await pane.locator('[data-calendar-month]').fill('1983-11');await pane.locator('[data-calendar-month]').dispatchEvent('change');assert.equal(await ctx.locator('[data-context-pane="1"] [data-calendar-month]').inputValue(),new Date().toISOString().slice(0,7));for(const layout of ['columns','rows']){await ctx.selectOption('[data-context-layout]',layout);for(const theme of ['plotdirector','modern-light','modern-dark','warm-light','warm-dark','rose-pink','baby-blue'])await ctx.evaluate(t=>{localStorage.setItem('plotdirector.applicationTheme',t);window.dispatchEvent(new StorageEvent('storage',{key:'plotdirector.applicationTheme',newValue:t}));},theme);}await ctx.setViewportSize({width:460,height:850});assert.equal(await ctx.evaluate(()=>document.documentElement.scrollWidth<=innerWidth+1),true);
stage='fresh login persistence';await session.close();session=await open();p=session.page;await session.goto('/Scenes/SceneDetailsEditor?sceneId='+first);assert.match(await p.locator('[name=StartDateTime]').inputValue(),/^1984-02-29T10:00/);await session.goto(feature);await p.selectOption('[data-context-pane="0"] [data-context-tab]','calendar');await p.locator('[data-calendar-date="1984-02-29"]').waitFor();assert.equal(await p.locator('[data-calendar-month]').inputValue(),'1984-02');
console.log(JSON.stringify({passed:true,checks:['real saved chronology and fresh-login persistence','third Saturday, leap date and six rows','chapter dedup and all-book collisions','empty date and elapsed days','held real save refresh preserves selected month and day','zero focus mutation during browsing refresh and main link','Return to Scene','chapter fallback and undated month retention','independent split panes rows columns seven themes and narrow'],featureUrl:'https://dev.plotdirector.com'+feature,limitation:'No independent chapter date exists; fallback is first dated scene in chapter. No live Word instance; zero focus publication/navigation requests verified.'}));
}catch(error){console.error('Calendar 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 project cleanup');}catch{console.error('Cleanup incomplete; preserve journal.');process.exitCode=1;}finally{await session.close();}}}
})();