UXInteract / task_018 /tests /validate_enhanced_cases.js
DojiSum's picture
Publish the current 141-task UXInteract release
ef987bb verified
Raw History Blame Contribute Delete
9.06 kB
const path = require('path');
const { pathToFileURL } = require('url');
const { chromium } = require('playwright');
const fs = require('node:fs');
fs.mkdirSync('/logs/verifier/visual-audit', {recursive: true});
const entryUrl = pathToFileURL(path.join(process.cwd(), 'index.html')).href;
const seed = Number.parseInt(process.env.ENHANCED_SEED || '28031', 10);
function assert(condition, message) {
if (!condition) throw new Error(message);
}
async function runCase(browser, name, viewport, validate) {
const context = await browser.newContext({ viewport });
const networkAttempts = [];
await context.route('**/*', async (route) => {
const url = route.request().url();
if (/^https?:/i.test(url)) {
networkAttempts.push(url);
await route.abort('blockedbyclient');
return;
}
await route.continue();
});
await context.addInitScript(({ fixedSeed }) => {
let state = fixedSeed >>> 0;
Math.random = () => {
state = (state * 1664525 + 1013904223) >>> 0;
return state / 0x100000000;
};
}, { fixedSeed: seed });
const page = await context.newPage();
page.setDefaultTimeout(5000);
const errors = [];
page.on('pageerror', (error) => errors.push(error.message));
try {
await page.goto(entryUrl, { waitUntil: 'domcontentloaded' });
await validate(page);
await page.screenshot({path: `/logs/verifier/visual-audit/${name}.png`, fullPage: true});
assert(networkAttempts.length === 0, `${name}: external network request attempted`);
assert(errors.length === 0, `${name}: browser errors: ${errors.join(' | ')}`);
console.log(`${name} passed`);
} finally {
await context.close();
}
}
async function waitForCollapsedSidebar(page, selector, initialVisibleWidth) {
await page.evaluate(({ selector, initialVisibleWidth }) => new Promise((resolve, reject) => {
const deadline = performance.now() + 4500;
let lastWidth = initialVisibleWidth;
let stableFrames = 0;
let reduced = false;
const sample = () => {
const element = document.querySelector(selector);
if (!element) {
reject(new Error('fixture sidebar must exist'));
return;
}
const rect = element.getBoundingClientRect();
const style = getComputedStyle(element);
const visibleWidth = Math.max(0, Math.min(innerWidth, rect.right) - Math.max(0, rect.left));
reduced = visibleWidth < 40 || style.display === 'none' || style.visibility === 'hidden';
stableFrames = reduced && Math.abs(visibleWidth - lastWidth) < 0.01 ? stableFrames + 1 : 0;
lastWidth = visibleWidth;
if (reduced && stableFrames >= 4) resolve();
else if (performance.now() >= deadline) reject(new Error('sidebar did not reach a stable collapsed state'));
else requestAnimationFrame(sample);
};
sample();
}), { selector, initialVisibleWidth });
}
(async () => {
assert(Number.isInteger(seed), 'ENHANCED_SEED must be a fixed integer');
const browser = await chromium.launch({
headless: true,
args: ['--no-sandbox', '--disable-background-networking', '--disable-component-update', '--no-first-run'],
});
try {
// ENHANCED_CASE_1: the sidebar and content remain in a left-to-right layout.
await runCase(browser, 'ENHANCED_CASE_1', { width: 1200, height: 800 }, async (page) => {
const boxes = await page.evaluate(() => {
const sidebar = document.querySelector('[data-testid="sidebar"]')?.getBoundingClientRect();
const main = document.querySelector('[data-testid="main-content"]')?.getBoundingClientRect();
return sidebar && main ? {
sidebar: { left: sidebar.left, right: sidebar.right, top: sidebar.top, bottom: sidebar.bottom },
main: { left: main.left, top: main.top, bottom: main.bottom },
} : null;
});
assert(boxes, 'fixture sidebar and main content must exist');
assert(boxes.sidebar.left <= 1, 'sidebar must stay against the left edge');
assert(boxes.main.left >= boxes.sidebar.right - 1, 'main content must be to the right of the sidebar');
assert(Math.min(boxes.sidebar.bottom, boxes.main.bottom) > Math.max(boxes.sidebar.top, boxes.main.top),
'sidebar and main content must vertically overlap instead of stacking');
});
// ENHANCED_CASE_2: activating the provided toggle visibly collapses the sidebar.
await runCase(browser, 'ENHANCED_CASE_2', { width: 900, height: 700 }, async (page) => {
const sidebar = page.locator('[data-testid="sidebar"]');
const before = await sidebar.boundingBox();
assert(before && before.width > 1, 'sidebar must initially be visible');
await page.locator('[data-testid="sidebar-toggle"]').click();
await waitForCollapsedSidebar(page, '[data-testid="sidebar"]', before.width);
const collapsed = await sidebar.evaluate((element, initialWidth) => {
const rect = element.getBoundingClientRect();
const style = getComputedStyle(element);
const visibleWidth = Math.max(0, Math.min(innerWidth, rect.right) - Math.max(0, rect.left));
return visibleWidth < 40 || style.display === 'none' || style.visibility === 'hidden';
}, before.width);
assert(collapsed, 'sidebar must collapse after its toggle is clicked');
});
// ENHANCED_CASE_3: responsive extremes preserve narrow usability and wide centering.
await runCase(browser, 'ENHANCED_CASE_3', { width: 420, height: 700 }, async (page) => {
const narrow = await page.evaluate(() => {
const mainElement = document.querySelector('[data-testid="main-content"]');
const main = mainElement?.getBoundingClientRect();
const minWidth = mainElement ? Number.parseFloat(getComputedStyle(mainElement).minWidth) : NaN;
return main ? {
mainLeft: main.left,
mainRight: main.right,
mainWidth: main.width,
minWidth,
scrollWidth: document.documentElement.scrollWidth,
clientWidth: document.documentElement.clientWidth,
} : null;
});
assert(narrow, 'main content must exist at a narrow viewport');
assert(Number.isFinite(narrow.minWidth) && narrow.minWidth > 0,
'main content must define a fixed positive minimum width');
assert(narrow.mainWidth + 1 >= narrow.minWidth,
'rendered main content must honor its fixed minimum width');
assert(narrow.mainLeft >= -1 && narrow.mainRight <= narrow.clientWidth + 1,
'main content must remain fully visible, not clipped to hide overflow');
assert(narrow.scrollWidth <= narrow.clientWidth + 1, 'narrow viewport must not expose horizontal page scrolling');
await page.screenshot({path: '/logs/verifier/visual-audit/narrow-content.png', fullPage: true});
const sidebar = page.locator('[data-testid="sidebar"]');
const initiallyOpen = await sidebar.evaluate(element => {
const rect = element.getBoundingClientRect(), style = getComputedStyle(element);
return style.display !== 'none' && style.visibility !== 'hidden' && rect.right > 40 && rect.left < innerWidth;
});
if (!initiallyOpen) {
await page.locator('[data-testid="sidebar-toggle"]').click();
await page.waitForFunction(() => {
const el = document.querySelector('[data-testid="sidebar"]');
const rect = el.getBoundingClientRect(), style = getComputedStyle(el);
return style.display !== 'none' && style.visibility !== 'hidden' && rect.right > 40;
}, null, {timeout: 3000});
}
await page.screenshot({path: '/logs/verifier/visual-audit/narrow-open.png', fullPage: true});
await page.locator('[data-testid="sidebar-toggle"]').click();
await waitForCollapsedSidebar(page, '[data-testid="sidebar"]', 240);
await page.screenshot({path: '/logs/verifier/visual-audit/narrow-closed.png', fullPage: true});
await page.setViewportSize({ width: 1800, height: 900 });
await page.goto(entryUrl, { waitUntil: 'domcontentloaded' });
const wide = await page.evaluate(() => {
const sidebar = document.querySelector('[data-testid="sidebar"]')?.getBoundingClientRect();
const main = document.querySelector('[data-testid="main-content"]')?.getBoundingClientRect();
return sidebar && main ? {
sidebarLeft: sidebar.left,
sidebarRight: sidebar.right,
mainCenter: main.left + main.width / 2,
viewportWidth: document.documentElement.clientWidth,
} : null;
});
assert(wide, 'sidebar and main content must exist at a wide viewport');
assert(wide.sidebarLeft <= 1, 'wide viewport sidebar must remain left aligned');
const availableCenter = (wide.sidebarRight + wide.viewportWidth) / 2;
assert(Math.abs(wide.mainCenter - availableCenter) <= 2, 'main content must be centered in the area beside the sidebar');
});
} finally {
await browser.close();
}
})().catch((error) => {
console.error(error && error.stack || error);
process.exit(1);
});