UXInteract / task_008 /tests /validate_browser_smoke.js
DojiSum's picture
Publish the current 141-task UXInteract release
ef987bb verified
Raw History Blame Contribute Delete
5.28 kB
const fs = require('fs');
const path = require('path');
const { pathToFileURL } = require('url');
const { chromium } = require('playwright');
const mode = "frontend";
function walk(dir) {
let out = [];
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
if (entry.name === 'tests' || entry.name === 'logs' || entry.name === 'node_modules') continue;
const p = path.join(dir, entry.name);
if (entry.isDirectory()) out = out.concat(walk(p));
else out.push(p);
}
return out;
}
const files = walk(process.cwd());
const html = files.find(f => f.toLowerCase().endsWith('.html'));
const svg = files.find(f => f.toLowerCase().endsWith('.svg'));
const target = mode === 'svg' ? svg : (html || svg);
// This is a Markdown -> PDF batch CLI task; the instruction never requires a web UI.
// The browser smoke check is only applicable when the solution ships an HTML/SVG
// entrypoint. If none exists in cwd, treat the browser smoke as not-applicable and
// pass (the instruction-required PDF structure checks live in validate_ui_behavior.js).
if (!target) {
console.log('browser smoke skipped for task 9: no browser-openable entrypoint (CLI-only solution)');
process.exit(0);
}
(async () => {
const browser = await chromium.launch({ headless: true });
const page = await browser.newPage({ viewport: { width: 960, height: 720 } });
const errors = [];
page.on('pageerror', err => errors.push(err.message));
page.on('console', msg => {
if (msg.type() === 'error') errors.push(msg.text());
});
await page.goto(pathToFileURL(target).href, { waitUntil: 'domcontentloaded' });
await page.waitForTimeout(600);
if (errors.length) throw new Error('browser errors: ' + errors.join(' | '));
const stats = await page.evaluate((mode) => {
const visible = Array.from(document.querySelectorAll('body *')).filter(el => {
const rect = el.getBoundingClientRect();
const style = window.getComputedStyle(el);
return rect.width > 0 && rect.height > 0 && style.visibility !== 'hidden' && style.display !== 'none';
});
const buttons = document.querySelectorAll('button, [role="button"], input, select, textarea').length;
const canvasInfo = Array.from(document.querySelectorAll('canvas')).map(c => {
let nonBlank = false;
try {
const ctx = c.getContext('2d');
if (ctx && c.width && c.height) {
const data = ctx.getImageData(0, 0, Math.min(c.width, 32), Math.min(c.height, 32)).data;
nonBlank = Array.from(data).some((v, i) => i % 4 !== 3 && v !== 0);
}
} catch (e) {}
return { width: c.width, height: c.height, nonBlank };
});
const svgElements = document.querySelectorAll('svg *').length;
return {
bodyTextLength: (document.body && document.body.innerText || '').trim().length,
visibleCount: visible.length,
buttons,
canvasInfo,
svgElements,
localStorageLength: window.localStorage ? window.localStorage.length : 0,
mode,
};
}, mode);
if (stats.visibleCount < (mode === 'svg' ? 3 : 5)) {
throw new Error('too few visible browser elements: ' + stats.visibleCount);
}
if (mode === 'svg' && stats.svgElements < 2) {
throw new Error('too few rendered svg elements: ' + stats.svgElements);
}
if (stats.canvasInfo.length) {
const validCanvas = stats.canvasInfo.some(c => c.width >= 100 && c.height >= 100);
if (!validCanvas) throw new Error('canvas exists but has invalid dimensions');
}
const before = await page.evaluate(() => {
const canvases = Array.from(document.querySelectorAll('canvas')).map(c => {
try { return c.toDataURL(); } catch (e) { return ''; }
});
return JSON.stringify({
text: document.body.innerText,
storage: window.localStorage ? window.localStorage.length : 0,
canvas: canvases,
});
});
const clickable = await page.$('button, [role="button"], input[type="button"], input[type="submit"], canvas, svg, body');
if (clickable) await clickable.click({ position: { x: 20, y: 20 } }).catch(() => {});
await page.keyboard.press('Space').catch(() => {});
await page.keyboard.press('ArrowDown').catch(() => {});
await page.keyboard.press('Enter').catch(() => {});
await page.waitForTimeout(300);
const after = await page.evaluate(() => {
const canvases = Array.from(document.querySelectorAll('canvas')).map(c => {
try { return c.toDataURL(); } catch (e) { return ''; }
});
return JSON.stringify({
text: document.body.innerText,
storage: window.localStorage ? window.localStorage.length : 0,
canvas: canvases,
});
});
if (mode === 'frontend' && stats.buttons === 0 && stats.canvasInfo.length === 0 && stats.bodyTextLength < 20) {
throw new Error('frontend has no meaningful controls, canvas, or text content');
}
if (errors.length) throw new Error('browser errors after interaction: ' + errors.join(' | '));
console.log('browser smoke passed for task 9; interaction_changed=' + (before !== after));
await browser.close();
})().catch(async err => {
console.error(err && err.stack || err);
process.exit(1);
});