| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- /** 只读核对原文档的画布、实际裁剪区域与原生截图;不发送或登记成果。 */
- import fs from 'node:fs';
- import assert from 'node:assert/strict';
- import { spawnSync } from 'node:child_process';
- import { connectWebView } from './android-webview-cdp.mjs';
- const out = 'test-results/new-account-20261128';
- const state = JSON.parse(fs.readFileSync(out + '/state.json'));
- const task = state.actions.trialDocuments;
- const c = await connectWebView();
- try {
- const route = '/mobile/chat/' + state.trial.agentId + '/' + encodeURIComponent(task.threadRoot);
- await c.evaluate(route => { history.pushState(null, '', route); dispatchEvent(new PopStateEvent('popstate')); }, route);
- await c.waitFor(route => decodeURIComponent(location.pathname) === decodeURIComponent(route), route);
- const reports = [];
- for (const file of state.actions.trialDocumentPreviews.receipt.files) {
- const ext = file.name.endsWith('.pdf') ? 'pdf' : 'docx';
- const name = file.name.replaceAll('_', '-');
- await c.waitFor(name => !!document.querySelector(`[aria-label="预览 ${name}"]`), name);
- await c.tap(`[aria-label="预览 ${name}"]`);
- await c.waitFor(() => !!document.querySelector('[role="dialog"] canvas'));
- await c.evaluate(() => new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(() => resolve(true)))));
- const view = await c.evaluate(() => {
- const box = element => { const r = element.getBoundingClientRect(); return { x: r.x, y: r.y, width: r.width, height: r.height }; };
- const dialog = document.querySelector('[role="dialog"][aria-label^="预览 "]');
- return { viewport: { width: innerWidth, height: innerHeight, dpr: devicePixelRatio }, dialog: box(dialog), canvases: [...dialog.querySelectorAll('canvas')].map(canvas => {
- const data = canvas.getContext('2d').getImageData(0, 0, canvas.width, canvas.height).data;
- let dark = 0, minX = canvas.width, minY = canvas.height, maxX = -1, maxY = -1;
- for (let y = 0; y < canvas.height; y++) for (let x = 0; x < canvas.width; x++) {
- const i = (y * canvas.width + x) * 4;
- if (data[i + 3] > 200 && data[i] < 200 && data[i + 1] < 200 && data[i + 2] < 200) { dark++; minX = Math.min(minX, x); minY = Math.min(minY, y); maxX = Math.max(maxX, x); maxY = Math.max(maxY, y); }
- }
- const parents = []; let p = canvas;
- for (let n = 0; p && n < 9; n++, p = p.parentElement) { const css = getComputedStyle(p); parents.push({ tag: p.tagName, class: p.className, box: box(p), display: css.display, visibility: css.visibility, opacity: css.opacity, overflow: css.overflow, transform: css.transform, scrollTop: p.scrollTop, scrollHeight: p.scrollHeight }); }
- return { width: canvas.width, height: canvas.height, rect: box(canvas), ink: { dark, minX, minY, maxX, maxY }, parents };
- }) };
- });
- await c.screenshot(out + '/document-viewport-' + ext + '.png');
- const native = spawnSync('adb', ['-s', '3e529a34', 'exec-out', 'screencap', '-p'], { timeout: 30000, maxBuffer: 20 * 1024 * 1024 });
- assert.equal(native.status, 0); fs.writeFileSync(out + '/document-native-' + ext + '.png', native.stdout, { flag: 'wx' });
- const response = await fetch(file.url, { signal: AbortSignal.timeout(20000) }); assert.ok(response.ok);
- fs.writeFileSync(out + '/fixtures/original-document.' + ext, Buffer.from(await response.arrayBuffer()), { flag: 'wx' });
- reports.push({ name: file.name, ...view });
- await c.tap('[aria-label="关闭预览"]');
- await c.waitFor(() => !document.querySelector('[role="dialog"][aria-label^="预览 "]'));
- }
- fs.writeFileSync(out + '/document-viewport-diagnostics.json', JSON.stringify(reports, null, 2), { flag: 'wx' });
- console.log(JSON.stringify(reports.map(x => ({ name: x.name, viewport: x.viewport, canvases: x.canvases.map(c => ({ rect: c.rect, ink: c.ink, parents: c.parents.slice(0, 5) })) })), null, 2));
- } finally { c.close(); }
|