import test from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { inflateSync } from 'node:zlib';
import { encodeRgbaStripsToPng, reportLongImagePixelLimit } from '../src/analysisReportPng.ts';
import { requiresFullResolutionStripCapture } from '../src/analysisReportFiles.ts';
import {
assertReportFooterAfterLastRecord,
assertReportMeasurementAtOneX,
createReportSvgStripMarkupFactory,
findReportTailMarkerTop,
isDocumentNodeFollowing,
normalizeReportRectOffset,
requiresMeasuredReportSnapshot,
reportSvgDataUrl,
resolveReportCanvasPixelRatio,
resolveReportSnapshotHeight,
} from '../src/analysisReportCapture.ts';
const signature = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]);
const captureSource = readFileSync(new URL('../src/analysisReportCapture.ts', import.meta.url), 'utf8');
function crc32(bytes) {
let crc = 0xffffffff;
for (const byte of bytes) {
crc ^= byte;
for (let bit = 0; bit < 8; bit += 1) crc = crc & 1 ? 0xedb88320 ^ (crc >>> 1) : crc >>> 1;
}
return (crc ^ 0xffffffff) >>> 0;
}
function parsePng(bytes) {
assert.deepEqual(bytes.subarray(0, 8), signature);
const chunks = [];
let offset = 8;
while (offset < bytes.length) {
const length = bytes.readUInt32BE(offset);
const type = bytes.toString('ascii', offset + 4, offset + 8);
const data = bytes.subarray(offset + 8, offset + 8 + length);
const expectedCrc = bytes.readUInt32BE(offset + 8 + length);
assert.equal(crc32(bytes.subarray(offset + 4, offset + 8 + length)), expectedCrc, `${type} CRC`);
chunks.push({ type, data });
offset += length + 12;
if (type === 'IEND') break;
}
assert.equal(offset, bytes.length, 'no bytes after IEND');
return chunks;
}
function makePixels(width, height) {
const pixels = new Uint8Array(width * height * 4);
for (let y = 0; y < height; y += 1) {
for (let x = 0; x < width; x += 1) {
const offset = (y * width + x) * 4;
pixels[offset] = (x * 31 + y * 13) & 0xff;
pixels[offset + 1] = (x * 7 + y * 47) & 0xff;
pixels[offset + 2] = (x * 73 + y * 3) & 0xff;
pixels[offset + 3] = (x + y * 19) & 0xff;
}
}
return pixels;
}
async function* splitPixels(pixels, width, height, stripHeight) {
for (let y = 0; y < height; y += stripHeight) {
const rows = Math.min(stripHeight, height - y);
const start = y * width * 4;
yield { y, height: rows, data: pixels.subarray(start, start + rows * width * 4) };
}
}
test('full-resolution strip capture is limited to reports the legacy canvas would shrink', () => {
assert.equal(requiresFullResolutionStripCapture(900, 11912), false);
assert.equal(requiresFullResolutionStripCapture(900, 91800), true);
assert.equal(reportLongImagePixelLimit, 100_000_000);
});
test('raw-record reports always use measured snapshot layout while unrelated short reports keep the fast path', () => {
assert.equal(requiresMeasuredReportSnapshot(true, false), true, 'short raw appendices also reflow after cloning');
assert.equal(requiresMeasuredReportSnapshot(true, true), true);
assert.equal(requiresMeasuredReportSnapshot(false, true), true, 'overlong reports still use bounded strips');
assert.equal(requiresMeasuredReportSnapshot(false, false), false, 'unrelated short reports retain the old canvas path');
});
test('raw-record output matches the 1x tail probe while other reports retain their normal canvas scale', () => {
assert.equal(resolveReportCanvasPixelRatio(true, 1.2567), 1);
assert.equal(resolveReportCanvasPixelRatio(true, 1), 1);
assert.equal(resolveReportCanvasPixelRatio(true, 0.8), 0.8, 'long raw reports continue through the 1x strip path');
assert.equal(resolveReportCanvasPixelRatio(false, 1.2567), 1.2567);
assert.match(captureSource, /resolveReportCanvasPixelRatio\(hasSourceRecordList, measuredSize\.pixelRatio\)/);
});
test('SVG strip viewports crop one full-height foreignObject without changing report content', () => {
const fullReport = '';
const source = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(fullReport)}`;
const viewportFor = createReportSvgStripMarkupFactory(source, 900, 92000);
const first = viewportFor(0, 2048);
assert.match(first, /