Files
MorphDoc/packages/document-visual-diff/tests/text.test.ts
T

65 lines
1.4 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
aggregatePdfTextLines,
buildPageContentText,
normalizePdfText,
type PdfTextItemSnapshot,
} from "../src/index.js";
function item(
text: string,
x: number,
y: number,
width = 20,
height = 10,
): PdfTextItemSnapshot {
return {
text,
normalizedText: normalizePdfText(text),
bounds: { x, y, width, height },
baselineY: y + height,
hasEol: false,
};
}
describe("PDF 文本行聚合", () => {
it("按坐标聚合中文文本并规范化兼容字符", () => {
const lines = aggregatePdfTextLines(
[
item("ABC", 10, 100, 30),
item("中文", 41, 100, 20),
item("第二行", 10, 120, 40),
],
842,
);
expect(lines.map((line) => line.normalizedText)).toEqual([
"ABC中文",
"第二行",
]);
});
it("只在页眉页脚坐标带识别独立页码", () => {
const lines = aggregatePdfTextLines(
[
item("章节 1", 72, 400, 50),
item("— 1 —", 280, 731, 35, 14),
],
842,
);
expect(lines.map((line) => line.role)).toEqual([
"content",
"page-number",
]);
expect(buildPageContentText(lines)).toBe("章节 1");
});
it("不会把页脚中的普通说明误判为页码", () => {
const lines = aggregatePdfTextLines(
[item("内部资料 1", 72, 800, 70)],
842,
);
expect(lines[0]?.role).toBe("content");
});
});