const{chromium}=require('playwright'),fs=require('fs'),assert=require('node:assert/strict'); (async()=>{const browser=await chromium.launch();try{const page=await browser.newPage({viewport:{width:1200,height:800}});let date='1983-11-19',reads=0;await page.route('**/Writing/Context/Calendar/State*',r=>{reads++;return r.fulfill({json:{books:[],scenes:[{bookID:1,bookLabel:'Book 1',chapterID:1,chapterNumber:1,chapterTitle:'One',sceneID:1,sceneNumber:1,title:'First',date},{bookID:1,bookLabel:'Book 1',chapterID:1,chapterNumber:1,chapterTitle:'One',sceneID:2,sceneNumber:2,title:'Second',date},{bookID:2,bookLabel:'Book 2',chapterID:2,chapterNumber:1,chapterTitle:'Two',sceneID:3,sceneNumber:1,title:'Other book',date}]}});});await page.route('https://calendar.test/',r=>r.fulfill({body:'',contentType:'text/html'}));await page.goto('https://calendar.test'); const partial=fs.readFileSync('PlotLine/Views/Writing/_ContextCalendar.cshtml','utf8').split('\n').slice(2).join('\n');await page.setContent('
'+partial+'
');await page.addStyleTag({content:fs.readFileSync('PlotLine/wwwroot/css/context-calendar.css','utf8')});for(const file of ['context-calendar-model.js','context-calendar.js'])await page.addScriptTag({path:'PlotLine/wwwroot/js/'+file});await page.evaluate(()=>{window.node=document.querySelector('[data-context-calendar]');window.life=PlotDirectorContextLifecycles.calendar;life.mount(node,{position:{bookId:1,chapterId:1,sceneId:1}});});await page.locator('[data-calendar-date="1983-11-19"]').waitFor();assert.equal(await page.locator('[data-calendar-date="1983-11-19"] .context-calendar-chapter').textContent(),'Ch 1');assert.equal(await page.locator('[data-calendar-date="1983-11-19"] small').count(),0); await page.selectOption('[data-calendar-books]','all');assert.equal(await page.locator('[data-calendar-date="1983-11-19"] small').textContent(),' +1');await page.locator('[data-calendar-date="1983-11-20"]').click();await page.evaluate(()=>life.onPosition(node,{bookId:1,chapterId:2,sceneId:99}));assert.equal(await page.locator('[data-calendar-month]').inputValue(),'1983-11');assert.equal(await page.locator('[data-calendar-date="1983-11-20"]').getAttribute('aria-pressed'),'true'); await page.locator('[data-calendar-month]').fill('2020-08');await page.locator('[data-calendar-month]').dispatchEvent('change');assert.equal(await page.locator('[data-calendar-grid] button').count(),42);assert((await page.locator('[data-calendar-grid]').boundingBox()).height<=400);await page.evaluate(()=>life.onPosition(node,{bookId:1,chapterId:1,sceneId:1}));assert.equal(await page.locator('[data-calendar-month]').inputValue(),'2020-08');date='1984-01-01';await page.evaluate(()=>life.onInvalidation(node));await page.waitForFunction(()=>document.querySelector('[data-calendar-status]').textContent.includes('1984'));assert.equal(await page.locator('[data-calendar-month]').inputValue(),'2020-08');await page.locator('[data-calendar-return]').click();assert.equal(await page.locator('[data-calendar-month]').inputValue(),'1984-01');assert.equal(await page.locator('[data-calendar-date="1984-01-01"]').getAttribute('aria-current'),'date');assert(reads>=2); await page.evaluate(()=>{life.dispose(node);life.mount(node,{position:{bookId:1,chapterId:1,sceneId:1}});});await page.locator('[data-calendar-date="1984-01-01"]').waitFor();console.log('PASS Calendar lifecycle: deduplication, all-books collision, local hold, undated position, six-row geometry, saved refresh preserves browsing, Return to Scene and remount');}finally{await browser.close();}})().catch(e=>{console.error(e);process.exitCode=1;});