Files
MorphDoc/packages/document-visual-diff/tests/raster.test.ts
T
SkyJourney 2c5c1bd317 release: 发布 v0.6.1 DOCX 视觉一致性修复
新增通用 CSS 到 OOXML 翻译修复,统一字体、字距、精确行距、段落、列表、表格、引用、代码块与行内代码连续性,不引入按主题 ID 分支。

新增 MdTP Mono 并统一 Serif、Sans、Mono 三字体包的 Chromium 与 DOCX 使用链;字体声明、嵌入部件和 Word/WPS 实际采用均进入硬门禁。

重建封面整页及正文语义块视觉差分,14 套主题、纵横两个方向、五组页边距共 140 个真实场景全部通过,阻断失败和诊断失败均为零。

源码服务、Docker Web API 与实际安装 Desktop 的 red-briefing 导出均包含 5 个字体部件;Word/WPS 原生渲染和逐页复核通过。修复 Docker 构建上下文与运行层复用软链接,并完善 v0.6.1 版本、发行说明和发布归集。

验证:npm test(116 个文件、616 项测试)、npm run typecheck、npm run build、git diff --check 全部通过。Desktop 安装器与 ZIP、Docker v0.6.1 镜像已生成;Windows 产物仍为未签名内部发行。
2026-08-04 10:30:44 +08:00

147 lines
4.7 KiB
TypeScript

import { createHash } from "node:crypto";
import { createCanvas } from "@napi-rs/canvas";
import { describe, expect, it } from "vitest";
import {
comparePageRasters,
type PdfPageRaster,
} from "../src/index.js";
function raster(rectangleX: number): PdfPageRaster {
const canvas = createCanvas(80, 100);
const context = canvas.getContext("2d");
context.fillStyle = "#ffffff";
context.fillRect(0, 0, 80, 100);
context.fillStyle = "#111111";
context.fillRect(rectangleX, 20, 20, 40);
const png = Uint8Array.from(canvas.toBuffer("image/png"));
return {
widthPx: 80,
heightPx: 100,
dpi: 144,
sha256: createHash("sha256").update(png).digest("hex"),
png,
};
}
function differentlySizedRaster(width: number, height: number): PdfPageRaster {
const canvas = createCanvas(width, height);
const context = canvas.getContext("2d");
context.fillStyle = "#ffffff";
context.fillRect(0, 0, width, height);
context.fillStyle = "#111111";
context.fillRect(width * 0.25, height * 0.2, width * 0.25, height * 0.4);
const png = Uint8Array.from(canvas.toBuffer("image/png"));
return {
widthPx: width,
heightPx: height,
dpi: 144,
sha256: createHash("sha256").update(png).digest("hex"),
png,
};
}
describe("PDF 栅格差异", () => {
it("相同页面的全部指标为无差异", async () => {
const page = raster(10);
const result = await comparePageRasters(page, page);
expect(result.metrics).toMatchObject({
dimensionsMatch: true,
spatialTolerancePx: 4,
meanAbsoluteError: 0,
changedPixelRatio: 0,
inkIou: 1,
edgeIou: 1,
backgroundColorDelta: 0,
foregroundColorDelta: 0,
});
expect(result.artifacts.overlayPng.subarray(0, 8)).toEqual(
new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10]),
);
});
it("分别量化块背景色与背景相对的前景形状", async () => {
const coloredRaster = (background: string, foreground: string) => {
const canvas = createCanvas(80, 40);
const context = canvas.getContext("2d");
context.fillStyle = background;
context.fillRect(0, 0, 80, 40);
context.fillStyle = foreground;
context.fillRect(20, 10, 40, 20);
const png = Uint8Array.from(canvas.toBuffer("image/png"));
return {
widthPx: 80,
heightPx: 40,
dpi: 144,
sha256: createHash("sha256").update(png).digest("hex"),
png,
} satisfies PdfPageRaster;
};
const sameShape = await comparePageRasters(
coloredRaster("#f2f2f2", "#202020"),
coloredRaster("#ffffff", "#202020"),
);
expect(sameShape.metrics.inkIou).toBe(1);
expect(sameShape.metrics.backgroundColorDelta).toBeGreaterThan(10);
expect(sameShape.metrics.foregroundColorDelta).toBeLessThan(1);
});
it("量化位移并生成稳定叠加图与热力图", async () => {
const result = await comparePageRasters(raster(10), raster(20), {
spatialTolerancePx: 0,
});
expect(result.metrics.meanAbsoluteError).toBeGreaterThan(10);
expect(result.metrics.changedPixelRatio).toBeCloseTo(0.1, 2);
expect(result.metrics.inkIou).toBeCloseTo(1 / 3, 2);
expect(result.metrics.edgeIou).toBeLessThan(0.4);
expect(result.artifacts.overlaySha256).toHaveLength(64);
expect(result.artifacts.heatmapSha256).toHaveLength(64);
});
it("只容忍三个像素以内的跨引擎栅格偏移", async () => {
const near = await comparePageRasters(raster(10), raster(13));
const far = await comparePageRasters(raster(10), raster(17));
expect(near.metrics.inkIou).toBe(1);
expect(near.metrics.edgeIou).toBe(1);
expect(far.metrics.inkIou).toBeLessThan(0.75);
expect(far.metrics.edgeIou).toBeLessThan(0.6);
});
it("空间容差不改变原始像素热力图", async () => {
const baseline = raster(10);
const candidate = raster(12);
const tolerant = await comparePageRasters(baseline, candidate);
const exact = await comparePageRasters(baseline, candidate, {
spatialTolerancePx: 0,
});
expect(tolerant.metrics.inkIou).toBeGreaterThan(exact.metrics.inkIou);
expect(tolerant.artifacts.heatmapSha256).toBe(
exact.artifacts.heatmapSha256,
);
});
it("可按物理页面基线规范化一像素的栅格舍入差", async () => {
const result = await comparePageRasters(
differentlySizedRaster(80, 100),
differentlySizedRaster(81, 101),
{
targetWidthPx: 80,
targetHeightPx: 100,
geometryNormalized: true,
},
);
expect(result.metrics).toMatchObject({
baselineWidthPx: 80,
candidateWidthPx: 81,
comparedWidthPx: 80,
dimensionsMatch: false,
geometryNormalized: true,
});
expect(result.metrics.meanAbsoluteError).toBeLessThan(2);
});
});