Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

test(browser): 校验后台截图包含测试页内容

zcinta05146dcb8911c284
diff --git a/apps/desktop/scripts/browser-background-screenshot.mjs b/apps/desktop/scripts/browser-background-screenshot.mjs
new file mode 100644
index 0000000000..7da00ff058
--- /dev/null
+++ b/apps/desktop/scripts/browser-background-screenshot.mjs
@@ -0,0 +1,24 @@
+import sharp from 'sharp';
+
+// A native capture must contain the local fixture, not just a correctly sized PNG.
+// These are the two background bands; text and button pixels are intentionally ignored.
+export async function checkBrowserFixtureScreenshot(png) {
+ const { data, info } = await sharp(png).toColourspace('srgb').ensureAlpha().raw()
+ .toBuffer({ resolveWithObject: true });
+ const colours = [[232, 237, 244], [210, 222, 239]];
+ const counts = [0, 0];
+ for (let offset = 0; offset < data.length; offset += info.channels) {
+ if (data[offset + 3] < 250) continue;
+ for (let band = 0; band < colours.length; band++) {
+ if (colours[band].every((value, channel) => Math.abs(data[offset + channel] - value) <= 5)) {
+ counts[band]++;
+ }
+ }
+ }
+ const stripeCoverage = counts.map(count => count / (info.width * info.height));
+ // Both bands occupy about half the page. Allow room for text, controls and colour rounding.
+ if (stripeCoverage.some(coverage => coverage < 0.2)) {
+ throw new Error('FIXTURE_SCREENSHOT_CONTENT_MISMATCH');
+ }
+ return { stripeCoverage };
+}
diff --git a/apps/desktop/scripts/verify-browser-background-viewport.mjs b/apps/desktop/scripts/verify-browser-background-viewport.mjs
index cfe2e75536..cd3d727516 100644
--- a/apps/desktop/scripts/verify-browser-background-viewport.mjs
+++ b/apps/desktop/scripts/verify-browser-background-viewport.mjs
@@ -9,6 +9,7 @@ import { mkdir, mkdtemp, realpath, writeFile } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
+import { checkBrowserFixtureScreenshot } from './browser-background-screenshot.mjs';
const option = name => process.argv.find(arg => arg.startsWith(`--${name}=`))?.slice(name.length + 3);
const port = Number(option('port'));
@@ -169,6 +170,8 @@ try {
check(result.screenshotWidth >= 640 && result.screenshotHeight >= 360, 'NATIVE_SCREENSHOT_TOO_SMALL');
result.screenshot = `background-${count}-${i + 1}.png`;
await writeFile(path.join(out, result.screenshot), png);
+ stage = `capture-content-${count}-${i + 1}`;
+ result.screenshotContent = await checkBrowserFixtureScreenshot(png);
const after = await inGuest(id, `({nonce:probe.nonce,origin:performance.timeOrigin,clicks:probe.clicks})`);
check(after.nonce === before.nonce && after.origin === before.origin && after.clicks === 1, 'BACKGROUND_PAGE_RELOADED');
}