PlotDirector/PlotLine.Tests/Browser/manuscript-reader.cjs

75 lines
5.3 KiB
JavaScript
Raw Permalink 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.

const assert = require('node:assert/strict');
const { chromium } = require('playwright');
const base = process.env.PD_BROWSER_FIXTURE_URL || 'http://127.0.0.1:5238';
(async () => {
const browser = await chromium.launch();
try {
for (const pwa of [false, true]) for (const width of [390, 800, 1440]) {
const context = await browser.newContext({viewport:{width,height:1000}});
if(pwa) await context.addInitScript(() => Object.defineProperty(navigator, 'standalone', {value:true}));
const page = await context.newPage();
const readingControls = async () => {
if (!pwa) return page;
const menu=page.locator('details.pd-context-menu').filter({has:page.locator('[data-command-identity="handler:reader-focus"]')});
await menu.waitFor({state:'visible'});
if (!await menu.evaluate(node=>node.open)) await menu.locator('summary').click();
return menu;
};
for(const theme of ['modern-light','modern-dark','warm-light','warm-dark']) {
const response = await page.goto(`${base}/manuscript/reader?bookId=12&chapterId=21`);
assert.equal(response.status(),200);
assert.match(response.headers()['cache-control'],/no-store/);
assert.match(response.headers()['x-robots-tag'],/noindex/);
await page.evaluate(theme => PlotDirectorApplyApplicationTheme(theme), theme);
await page.locator('[data-review-columns]').waitFor(); // The reader alias now uses the measured shared workspace.
const paragraphs = page.locator('.reader-paragraph');
assert.equal(await paragraphs.count(),3);
assert.match(await paragraphs.nth(1).textContent(), /First paragraph: <script>unsafe\(\)<\/script>/);
assert.equal(await page.locator('.reader-page script').count(),0);
assert.match(await paragraphs.nth(2).textContent(),/Second paragraph/);
assert.equal(await page.locator('.reader-page').evaluate(node => getComputedStyle(node).userSelect),'auto');
const selected = await page.locator('.reader-page').evaluate(node => {
const range = document.createRange(); range.selectNodeContents(node);
const selection = window.getSelection(); selection.removeAllRanges(); selection.addRange(range); return selection.toString();
});
assert.match(selected,/First paragraph/); assert.match(selected,/Second paragraph/);
assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth), 'No horizontal overflow');
const box = await page.locator('.reader-page').boundingBox(); assert.ok(box.width <= 740,'Reading column bounded');
assert.ok(await page.locator('.manuscript-reader time').first().getAttribute('datetime') === '2026-11-13T10:12:13Z');
await readingControls();
await page.getByTitle('Enter or exit Focused Reading',{exact:true}).filter({visible:true}).click();
assert.equal(await page.locator('body').evaluate(n=>n.classList.contains('reader-focused')),true);
await page.keyboard.press('Escape');
assert.equal(await page.locator('#reader-focus').getAttribute('aria-pressed'),'false');
// Integrated Review retains chapter-level navigation specifically inside Focus Reading.
await page.locator('#reader-focus').evaluate(n=>n.click());
await page.getByTitle('Read the next chapter',{exact:true}).filter({visible:true}).click();
await page.waitForURL(/chapterId=22/);
assert.match(await page.locator('.reader-page').textContent(),/Another chapter only/);
assert.doesNotMatch(await page.locator('.reader-page').textContent(),/First paragraph/);
// The specialised chapter GET form stays inline within Focus Reading.
assert.equal(await page.locator('.manuscript-reader').getAttribute('data-focus'),'true');
await page.getByLabel('Chapter',{exact:true}).filter({visible:true}).selectOption('23');
await page.getByRole('button',{name:'Read',exact:true}).click();
await page.waitForURL(/chapterId=23/);
assert.match(await page.locator('.reader-empty').textContent(),/No accepted manuscript text/);
assert.equal(await page.getByRole('link',{name:'Next chapter →',exact:true}).count(),0);
await page.getByTitle('Read the previous chapter',{exact:true}).filter({visible:true}).click();
await page.waitForURL(/chapterId=22/);
}
await context.addCookies([{name:'fixture-user',value:'5',url:base}]);
assert.equal((await page.goto(`${base}/manuscript/reader?bookId=12&chapterId=21`)).status(),404);
await context.clearCookies();
assert.equal((await page.goto(`${base}/manuscript/reader?bookId=13&chapterId=21`)).status(),404);
assert.equal((await page.goto(`${base}/manuscript/reader?bookId=12&chapterId=999`)).status(),404);
const unavailable = await page.goto(`${base}/manuscript/reader?bookId=12&chapterId=21&reader-error=true`);
assert.equal(unavailable.status(),503);
assert.equal(await page.getByRole('link',{name:'Retry',exact:true}).count(),1);
assert.doesNotMatch(await page.locator('body').textContent(),/First paragraph|Synthetic unavailable/);
const post = await context.request.post(`${base}/manuscript/reader?bookId=12`); assert.equal(post.status(),405);
await context.close();
}
console.log('PASS Manuscript Reader: real Razor/controller, escaped selectable text, chapter navigation, empty state, timestamps, private headers, account isolation, GET-only, focus, 3 widths × 4 themes × browser/PWA');
} finally { await browser.close(); }
})().catch(error => { console.error(error);process.exitCode=1; });