UXInteract / task_017 /tests /validate_enhancements.js
DojiSum's picture
Publish the current 141-task UXInteract release
ef987bb verified
Raw History Blame Contribute Delete
5.1 kB
const path = require('path');
const { pathToFileURL } = require('url');
const chromium = require("playwright").chromium;
const entry = path.join(process.cwd(), 'index.html');
function assert(condition, message) {
if (!condition) throw new Error(message);
}
async function openFixture(browser) {
const context = await browser.newContext({ viewport: { width: 1000, height: 760 } });
const attemptedNetwork = [];
await context.route(/^https?:\/\//, route => {
attemptedNetwork.push(route.request().url());
return route.abort('blockedbyclient');
});
const page = await context.newPage();
const errors = [];
page.on('pageerror', error => errors.push(error.message || String(error)));
page.on('console', message => {
if (message.type() === 'error') errors.push(message.text());
});
await page.goto(pathToFileURL(entry).href, { waitUntil: 'domcontentloaded' });
return { context, page, attemptedNetwork, errors };
}
async function finishCase(context, attemptedNetwork, errors) {
assert(attemptedNetwork.length === 0, `HTTP(S) requests are forbidden: ${attemptedNetwork.join(', ')}`);
assert(errors.length === 0, `browser errors: ${errors.join(' | ')}`);
await context.close();
}
function editorIn(block) {
return block.locator('textarea:visible,input:visible,[contenteditable="true"]:visible');
}
async function clickOutside(page) {
await page.mouse.click(5, 5);
await page.waitForTimeout(50);
}
(async () => {
const browser = await chromium.launch({
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage'],
});
try {
// ENHANCED_CASE_1: whitespace-only content returns to the default empty state.
{
const state = await openFixture(browser);
const item = state.page.locator('[data-testid^="block-"]').first();
assert(await item.count(), 'no paragraph blocks are available');
await item.click();
const editor = editorIn(item);
assert(await editor.count(), 'clicking a paragraph did not open an editor');
await editor.fill(' \n\t ');
await clickOutside(state.page);
assert(await editorIn(item).count() === 0, 'whitespace-only paragraph stayed in editing state after outside click');
assert((await item.innerText()).trim() === '', 'whitespace-only paragraph did not return to the default empty state');
await finishCase(state.context, state.attemptedNetwork, state.errors);
}
// ENHANCED_CASE_2: direct switching commits every provided paragraph independently.
{
const state = await openFixture(browser);
const items = state.page.locator('[data-testid^="block-"]');
const count = await items.count();
assert(count >= 2, 'the provided multi-paragraph editor is missing blocks');
const expected = Array.from({ length: count }, (_, index) => `paragraph ${index + 1}`);
for (let index = 0; index < count; index += 1) {
const block = items.nth(index);
await block.click();
const editor = editorIn(block);
assert(await editor.count(), `paragraph ${index + 1} did not enter editing state`);
await editor.fill(expected[index]);
}
await clickOutside(state.page);
for (let index = 0; index < count; index += 1) {
const block = items.nth(index);
assert(await editorIn(block).count() === 0, `paragraph ${index + 1} stayed open after switching/outside click`);
assert((await block.innerText()).trim() === expected[index], `paragraph ${index + 1} did not save independently`);
}
await finishCase(state.context, state.attemptedNetwork, state.errors);
}
// ENHANCED_CASE_3: a saved paragraph can be reopened, updated, saved, and closed again.
{
const state = await openFixture(browser);
const item = state.page.locator('[data-testid^="block-"]').first();
await item.click();
let editor = editorIn(item);
assert(await editor.count(), 'paragraph did not enter editing state');
await editor.fill('first saved value');
await clickOutside(state.page);
assert((await item.innerText()).trim() === 'first saved value' && await editorIn(item).count() === 0, 'first outside click did not save and close the paragraph');
await item.click();
editor = editorIn(item);
assert(await editor.count(), 'saved paragraph could not be reopened');
assert((await editor.evaluate(el => 'value' in el ? el.value : el.textContent)).trim() === 'first saved value', 'reopened paragraph did not restore its saved text');
await editor.fill('updated saved value');
await clickOutside(state.page);
assert((await item.innerText()).trim() === 'updated saved value' && await editorIn(item).count() === 0, 'updated paragraph was not preserved after outside click');
await finishCase(state.context, state.attemptedNetwork, state.errors);
}
} finally {
await browser.close();
}
console.log('enhanced cases passed for task 26');
})().catch(error => {
console.error(error && error.stack || error);
process.exit(1);
});