PlotDirector/PlotLine.Tests/Browser/writing-solver-curated-v2.cjs

94 lines
6.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Isolated, loopback-only MVC host. Never target the deployed public site with this suite.
const { chromium } = require('playwright');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const candidate = path.resolve('PlotLine/Content/WritingSolver/Candidates/curated-v2');
const graph = JSON.parse(fs.readFileSync(path.join(candidate,'writing-solver-curated-v2.json'),'utf8'));
const cases = JSON.parse(fs.readFileSync(path.join(candidate,'acceptance-paths.json'),'utf8')).cases;
const base = 'http://127.0.0.1:5238';
(async () => {
const browser = await chromium.launch();
try {
const context=await browser.newContext({javaScriptEnabled:false,viewport:{width:1440,height:1000}});
const page=await context.newPage();
for (const [kind,pages] of [['writing-problems',graph.solutions],['features',graph.features]]) {
for(const record of pages) {
const response=await page.goto(`${base}/${kind}/${record.slug}`);
assert.equal(response.status(),200,record.id);
assert.equal(await page.locator('h1').innerText(),record.title,record.id);
assert.equal(await page.locator('link[rel=canonical]').getAttribute('href'),`https://plotdirector.com/${kind}/${record.slug}`,record.id);
assert.equal(await page.locator('meta[name=description]').getAttribute('content'),record.description,record.id);
assert.equal(await page.locator('meta[property="og:description"]').getAttribute('content'),record.description,record.id);
assert.equal(await page.locator('meta[name=robots]').getAttribute('content'),'noindex, follow',record.id);
assert(await page.locator('script[type="application/ld+json"]').count(),record.id);
assert((await page.locator('body').innerText()).includes(record.summary),record.id);
assert.equal(await page.evaluate(()=>document.documentElement.scrollWidth>innerWidth),false,record.id);
assert(!((await page.locator('main').innerText()).includes('\uFFFD')),`Replacement character in ${record.id}`);
for(const script of await page.locator('script[type="application/ld+json"]').allTextContents()) JSON.parse(script);
if(kind==='writing-problems') {
for(const link of record.features) {
const feature=graph.features.find(f=>f.id===link.feature);
assert(await page.locator(`a[href="/features/${feature.slug}"]`).count(),`${record.id}/${feature.id}`);
}
}
}
}
await context.close();
for(const width of [390,1440]) {
const context=await browser.newContext({viewport:{width,height:1000}});
await context.addInitScript(()=>{
localStorage.setItem('plotdirector.applicationTheme','rose-pink');
window.__shifts=[];
new PerformanceObserver(list=>list.getEntries().forEach(e=>{if(!e.hadRecentInput)window.__shifts.push(e.value);})).observe({type:'layout-shift',buffered:true});
});
const page=await context.newPage();const errors=[];page.on('pageerror',e=>errors.push(e.message));
for(const id of [13,20,23,50,55,56,58,60]) {
await page.goto(base+'/writing-problems');
const test=cases.find(c=>c.id===id);
for(let i=0;i<test.answers.length;i++) {
const answer=test.answers[i];
assert.equal(await page.locator('input[name=node]').getAttribute('value'),answer.node,`case ${id} prefix ${i}`);
for(const choice of answer.choices) await page.locator(`input[name=choices][value="${choice}"]`).check();
await page.locator('.solver-form button[type=submit]').click();
const expected=test.prefixExpectations[i];
assert.equal(await page.locator('input[name=node]').count() ? await page.locator('input[name=node]').getAttribute('value') : null,expected.expectedNext,`case ${id} prefix ${i+1}`);
assert.equal(await page.locator('.solver-result').count(),expected.expectedResults.length,`case ${id} prefix ${i+1}`);
assert.equal(await page.evaluate(()=>document.documentElement.scrollWidth>innerWidth),false,`case ${id} width ${width}`);
}
assert.equal(await page.locator('html').getAttribute('data-site-theme'),'public');
assert.equal(await page.locator('html').getAttribute('data-app-theme'),null);
assert((await page.evaluate(()=>window.__shifts.reduce((a,b)=>a+b,0)))<.1,`layout shift in case ${id}`);
}
// Browser history returns to the actual prior question; revise to neutral knowledge-state advice.
await page.goto(base+'/writing-problems');
for(const answer of cases.find(c=>c.id===20).answers) {
for(const choice of answer.choices) await page.locator(`input[name=choices][value="${choice}"]`).check();
await page.locator('.solver-form button[type=submit]').click();
}
await page.goBack();
assert.equal(await page.locator('input[name=node]').getAttribute('value'),'track-knowledge');
await page.goForward();assert(await page.locator('.solver-result h1').innerText().then(s=>s===graph.solutions.find(s=>s.id==='d22').title));
await page.goBack();await page.locator('input[value="who-knows-fact"]').check();await page.locator('.solver-form button[type=submit]').click();
assert.equal(await page.locator('.solver-result').count(),0);
assert(await page.locator('main').innerText().then(t=>t.includes('isn’t enough evidence')));
// The adoption branch is neutral; record the current renderer's lack of tailored guidance.
await page.goto(base+'/writing-problems');
for(const [node,choice] of [['entry','unsure'],['rescue-happening','reread-cant-see'],['rescue-confidence','using-tool-concern'],['existing-stage','planning'],['existing-barrier','discovery']]) {
assert.equal(await page.locator('input[name=node]').getAttribute('value'),node);
await page.locator(`input[name=choices][value="${choice}"]`).check();
await page.locator('.solver-form button[type=submit]').click();
}
assert.equal(await page.locator('.solver-result').count(),0);
assert.equal(await page.locator('.solver-library a').count(),52);
assert.equal(await page.locator('main a[href="/features/manuscript-import"]').count(),0);
console.log(`NOTE ${width}px: adoption endpoint is neutral but uses generic advice and 52 reading links, without a direct manuscript-import link.`);
assert.equal(errors.length,0,errors.join('\n'));
await context.close();
}
// Existing renderer lists every public solution under a neutral result. Record its size
// as a UX finding rather than silently treating a blank recommendation as a diagnosis.
console.log('PASS curated V2 rendering: all 67 no-JS pages; desktop/mobile neutral, nonlinear, absence, multi-select, revision and Back/Forward journeys; no overflow/CLS; offline noindex.');
} finally {await browser.close();}
})().catch(error=>{console.error(error);process.exit(1);});