File size: 7,298 Bytes
ef987bb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | 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);
});
|