UXInteract / task_022 /tests /validate_enhanced_cases.js
DojiSum's picture
Publish the current 141-task UXInteract release
ef987bb verified
Raw History Blame Contribute Delete
7.3 kB
const path = require('path');
const { pathToFileURL } = require('url');
const { chromium } = require('playwright');
const fs = require('node:fs');
const inputText = input => input.evaluate(el => 'value' in el ? el.value : el.textContent || '');
fs.mkdirSync('/logs/verifier/visual-audit', {recursive: true});
function assert(condition, message) {
if (!condition) throw new Error(message);
}
async function openPage(browser) {
const page = await browser.newPage({ viewport: { width: 923, height: 641 } });
await page.goto(pathToFileURL(path.join(process.cwd(), 'index.html')).href, {
waitUntil: 'domcontentloaded'
});
return page;
}
async function discoverComposer(page) {
return page.evaluateHandle(() => {
const visible = element => {
const rect = element.getBoundingClientRect();
const style = getComputedStyle(element);
return rect.width > 0 && rect.height > 0 && style.display !== 'none' &&
style.visibility !== 'hidden';
};
const editables = [...document.querySelectorAll('input, textarea, [contenteditable="true"]')]
.filter(visible)
.filter(element => !element.disabled && !element.readOnly);
const candidates = editables.map(input => {
let area = input.parentElement;
while (area && getComputedStyle(area).position !== 'fixed') {
area = area.parentElement;
}
return { input, area };
}).filter(candidate => candidate.area);
if (candidates.length !== 1) {
throw new Error(`expected one reusable editable control in a floating area, found ${candidates.length}`);
}
return candidates[0];
});
}
async function composerParts(handle) {
return {
input: await handle.getProperty('input'),
area: await handle.getProperty('area')
};
}
// ENHANCED_CASE_1: the floating region is a live pending editor, not a second sent-message rendering.
async function casePendingEditor(browser) {
const page = await openPage(browser);
const composer = await discoverComposer(page);
const { input, area } = await composerParts(composer);
const sentinel = 'pending <draft> 你好';
await input.fill(sentinel);
assert(await inputText(input) === sentinel, 'pending input must retain arbitrary editable text before send');
const duplicateOutside = await page.evaluate(({ area, text }) => {
const visible = element => {
const rect = element.getBoundingClientRect();
const style = getComputedStyle(element);
return rect.width > 0 && rect.height > 0 && style.display !== 'none' && style.visibility !== 'hidden';
};
return [...document.querySelectorAll('body *')].some(element =>
!area.contains(element) && visible(element) && element.children.length === 0 &&
(element.textContent || '').trim() === text
);
}, { area, text: sentinel });
assert(!duplicateOutside, 'unsent draft must not already appear in message history');
await page.screenshot({path: '/logs/verifier/visual-audit/pending.png', fullPage: true});
await page.close();
}
// ENHANCED_CASE_2: sending appends the exact pending text outside the composer and clears it.
async function caseSendRoundTrip(browser) {
const page = await openPage(browser);
const composer = await discoverComposer(page);
const { input, area } = await composerParts(composer);
const sentinel = 'send & keep <literal> 世界';
await input.fill(sentinel);
await page.evaluate(area => {
window.__enhancedNodesBeforeSend = new Set(document.querySelectorAll('*'));
const controls = [...area.querySelectorAll('button, [role="button"], input[type="submit"], input[type="button"]')]
.filter(element => {
const rect = element.getBoundingClientRect();
const style = getComputedStyle(element);
return rect.width > 0 && rect.height > 0 && style.display !== 'none' && style.visibility !== 'hidden' && !element.disabled;
});
const send = controls.find(element => element.dataset.testid === 'send-button') ||
controls.find(element => /^(send|发送)$/i.test(element.textContent.trim()));
if (!send) throw new Error('composer needs an identifiable send control');
send.click();
}, area);
await page.waitForTimeout(50);
assert(await inputText(input) === '', 'successful send must clear the pending input');
const additions = await page.evaluate(({ area, text }) =>
[...document.querySelectorAll('*')].filter(element =>
!window.__enhancedNodesBeforeSend.has(element) && !area.contains(element) &&
(element.textContent || '').trim() === text
).length,
{ area, text: sentinel });
assert(additions >= 1, 'send must append a message containing the exact input text outside the composer');
await page.screenshot({path: '/logs/verifier/visual-audit/sent.png', fullPage: true});
await page.close();
}
// ENHANCED_CASE_3: the composer stays viewport-anchored and keeps its rounded-or-square corner state while scrolling.
async function caseFloatingAndCorners(browser) {
const page = await openPage(browser);
const composer = await discoverComposer(page);
const { input, area } = await composerParts(composer);
const before = await area.evaluate(element => {
const rect = element.getBoundingClientRect();
const style = getComputedStyle(element);
return { top: rect.top, left: rect.left, position: style.position, radius: style.borderRadius };
});
assert(before.position === 'fixed', 'composer must preserve fixed floating positioning');
assert(before.radius.split(/\s+/).every(value => Number.isFinite(parseFloat(value))),
'composer must expose a valid rounded-or-square corner state');
await page.evaluate(() => window.scrollTo(0, document.documentElement.scrollHeight));
await page.waitForTimeout(50);
const after = await area.evaluate(element => {
const rect = element.getBoundingClientRect();
return { top: rect.top, left: rect.left, radius: getComputedStyle(element).borderRadius };
});
assert(Math.abs(after.top - before.top) <= 1 && Math.abs(after.left - before.left) <= 1,
'floating composer must remain at the same viewport position after document scroll');
assert(after.radius === before.radius, 'scrolling must not discard the current corner state');
await input.fill('keep draft');
await page.evaluate(()=>window.__setComposerCorners('square'));
assert(await area.evaluate(e=>getComputedStyle(e).borderRadius.split(/\s+/).every(v=>parseFloat(v)===0)),'square state must have zero radius');
await page.evaluate(()=>window.__setComposerCorners('rounded'));
assert(await area.evaluate(e=>getComputedStyle(e).borderRadius.split(/\s+/).some(v=>parseFloat(v)>0)),'rounded state must have positive radius');
assert(await inputText(input)==='keep draft','corner switch must preserve same draft');
await page.screenshot({path: '/logs/verifier/visual-audit/corners-preserve-draft.png', fullPage: true});
await page.close();
}
(async () => {
const browser = await chromium.launch({ headless: true });
try {
await casePendingEditor(browser);
await caseSendRoundTrip(browser);
await caseFloatingAndCorners(browser);
} finally {
await browser.close();
}
console.log('enhanced cases passed for task 33');
})().catch(error => {
console.error(error && error.stack || error);
process.exit(1);
});