UXInteract / task_020 /tests /validate_enhanced_cases.js
DojiSum's picture
Publish the current 141-task UXInteract release
ef987bb verified
Raw History Blame Contribute Delete
8.09 kB
const path = require('path');
const { pathToFileURL } = require('url');
const { chromium } = require('playwright');
const entryUrl = pathToFileURL(path.join(process.cwd(), 'index.html')).href;
const seed = Number.parseInt(process.env.ENHANCED_SEED || '31031', 10);
function assert(condition, message) {
if (!condition) throw new Error(message);
}
async function runCase(browser, name, validate) {
const context = await browser.newContext({ viewport: { width: 1200, height: 800 } });
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);
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 = reduced || visibleWidth < initialVisibleWidth - 1 || style.display === 'none' ||
style.visibility === 'hidden' || element.getAttribute('aria-hidden') === 'true';
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 shell have no intervening gap.
await runCase(browser, 'ENHANCED_CASE_1', async (page) => {
const gap = await page.evaluate(() => {
const sidebar = document.querySelector('[data-testid="sidebar"]')?.getBoundingClientRect();
const shell = document.querySelector('[data-testid="content-shell"]')?.getBoundingClientRect();
return sidebar && shell ? shell.left - sidebar.right : null;
});
assert(gap !== null, 'fixture sidebar and content shell must exist');
assert(Math.abs(gap) <= 1, `sidebar/content gap must be removed; got ${gap}`);
});
// ENHANCED_CASE_2: the former 40px content-left padding is fully removed.
await runCase(browser, 'ENHANCED_CASE_2', async (page) => {
const paddingLeft = await page.locator('[data-testid="main-content"]').evaluate((element) =>
parseFloat(getComputedStyle(element).paddingLeft) || 0);
assert(paddingLeft <= 1, `content left padding must be removed; got ${paddingLeft}px`);
const visualInset = await page.evaluate(() => {
const shell = document.querySelector('[data-testid="content-shell"]').getBoundingClientRect();
const button = document.querySelector('[data-testid="collapse-sidebar"]').getBoundingClientRect();
return button.left - shell.left;
});
assert(visualInset <= 8, `content children must not recreate the removed 40px inset; got ${visualInset}px`);
});
// ENHANCED_CASE_3: collapsing never shifts content right and prompt display preserves collapse.
await runCase(browser, 'ENHANCED_CASE_3', async (page) => {
const shell = page.locator('[data-testid="content-shell"]');
const initialLeft = await shell.evaluate((element) => element.getBoundingClientRect().left);
const initialSidebarWidth = await page.locator('[data-testid="sidebar"]').evaluate((element) => {
const rect = element.getBoundingClientRect();
return Math.max(0, Math.min(innerWidth, rect.right) - Math.max(0, rect.left));
});
assert(initialSidebarWidth > 1, 'sidebar must initially be visible');
await page.evaluate(() => {
window.__enhancedCollapseSamples = [];
window.__enhancedSampling = true;
const sample = () => {
const element = document.querySelector('[data-testid="content-shell"]');
if (element) window.__enhancedCollapseSamples.push(element.getBoundingClientRect().left);
if (window.__enhancedSampling) requestAnimationFrame(sample);
};
sample();
});
await page.locator('[data-testid="collapse-sidebar"]').click();
await waitForCollapsedSidebar(page, '[data-testid="sidebar"]', initialSidebarWidth);
const samples = await page.evaluate(() => {
window.__enhancedSampling = false;
return window.__enhancedCollapseSamples.slice();
});
assert(samples.length > 0, 'collapse trajectory must produce at least one layout sample');
assert(samples.every((left) => left <= initialLeft + 1), 'content must not shift right at any sampled point during sidebar collapse');
const collapsedSidebarWidth = await page.locator('[data-testid="sidebar"]').evaluate((element) => {
const rect = element.getBoundingClientRect();
return Math.max(0, Math.min(innerWidth, rect.right) - Math.max(0, rect.left));
});
await page.locator('[data-testid="show-prompt"]').click();
await page.waitForFunction(() => {
const prompt = document.querySelector('[data-testid="prompt-input"]');
if (!prompt) return false;
const rect = prompt.getBoundingClientRect();
const style = getComputedStyle(prompt);
return !prompt.hidden && rect.width > 0 && rect.height > 0 &&
style.display !== 'none' && style.visibility !== 'hidden';
});
const state = await page.evaluate(() => {
const sidebar = document.querySelector('[data-testid="sidebar"]')?.getBoundingClientRect();
const prompt = document.querySelector('[data-testid="prompt-input"]');
if (!sidebar || !prompt) return null;
const promptRect = prompt.getBoundingClientRect();
const promptStyle = getComputedStyle(prompt);
return {
sidebarVisibleWidth: Math.max(0, Math.min(innerWidth, sidebar.right) - Math.max(0, sidebar.left)),
promptVisible: !prompt.hidden && promptRect.width > 0 && promptRect.height > 0 &&
promptStyle.display !== 'none' && promptStyle.visibility !== 'hidden',
};
});
assert(state, 'fixture sidebar and prompt input must exist');
assert(state.promptVisible, 'prompt input must become visible');
assert(state.sidebarVisibleWidth <= collapsedSidebarWidth + 1,
'showing prompt must preserve collapsed-sidebar state');
});
} finally {
await browser.close();
}
})().catch((error) => {
console.error(error && error.stack || error);
process.exit(1);
});