A fast, helpful, and open-source document parser
feat: unit tests for output; unit tests for processing (part 1)
Clelia (Astra) Bertelli9777327df511
diff --git a/src/output/json.test.ts b/src/output/json.test.tsnew file mode 100644index 0000000..646dc4a--- /dev/null+++ b/src/output/json.test.ts@@ -0,0 +1,143 @@+import { describe, it, expect } from "vitest";+import { buildJSON, formatJSON } from "./json";++const results = [+ { text: "Hello World", bbox: [10, 20, 200, 40], confidence: 0.98 },+ { text: "Sample text", bbox: [10, 50, 180, 70], confidence: 0.85 },+ { text: "Page footer", bbox: [10, 750, 300, 770], confidence: 0.76 },+];++const textItems = results+ .filter((r) => r.confidence > 0.1) // Filter low confidence+ .filter((r) => {+ // Filter out OCR text that already exists in native PDF text+ const ocrText = r.text.trim().toLowerCase();+ return ocrText.length > 0;+ })+ .map((r) => ({+ str: r.text,+ x: r.bbox[0],+ y: r.bbox[1],+ width: r.bbox[2] - r.bbox[0],+ height: r.bbox[3] - r.bbox[1],+ w: r.bbox[2] - r.bbox[0],+ h: r.bbox[3] - r.bbox[1],+ fontName: "OCR",+ fontSize: r.bbox[3] - r.bbox[1],+ }));++const textItemsJSON = results.map((r) => ({+ text: r.text,+ x: r.bbox[0],+ y: r.bbox[1],+ width: r.bbox[2] - r.bbox[0],+ height: r.bbox[3] - r.bbox[1],+ fontName: "OCR",+ fontSize: r.bbox[3] - r.bbox[1],+}));++const pages = [+ {+ pageNum: 1,+ width: 612,+ height: 792,+ text: "Sample text for page 1",+ textItems: textItems,+ boundingBoxes: [{ x1: 0, y1: 0, x2: 300, y2: 400 }],+ },+ {+ pageNum: 2,+ width: 612,+ height: 792,+ text: "Sample text for page 2",+ textItems: textItems,+ boundingBoxes: [{ x1: 0, y1: 0, x2: 300, y2: 400 }],+ },+ {+ pageNum: 3,+ width: 612,+ height: 792,+ text: "Sample text for page 3",+ textItems: textItems,+ boundingBoxes: [{ x1: 0, y1: 0, x2: 300, y2: 400 }],+ },+ {+ pageNum: 4,+ width: 612,+ height: 792,+ text: "Sample text for page 4",+ textItems: textItems,+ boundingBoxes: [{ x1: 0, y1: 0, x2: 300, y2: 400 }],+ },+ {+ pageNum: 5,+ width: 612,+ height: 792,+ text: "Sample text for page 5",+ textItems: textItems,+ boundingBoxes: [{ x1: 0, y1: 0, x2: 300, y2: 400 }],+ },+];++const pagesJSON = {+ pages: [+ {+ page: 1,+ width: 612,+ height: 792,+ text: "Sample text for page 1",+ textItems: textItemsJSON,+ boundingBoxes: [{ x1: 0, y1: 0, x2: 300, y2: 400 }],+ },+ {+ page: 2,+ width: 612,+ height: 792,+ text: "Sample text for page 2",+ textItems: textItemsJSON,+ boundingBoxes: [{ x1: 0, y1: 0, x2: 300, y2: 400 }],+ },+ {+ page: 3,+ width: 612,+ height: 792,+ text: "Sample text for page 3",+ textItems: textItemsJSON,+ boundingBoxes: [{ x1: 0, y1: 0, x2: 300, y2: 400 }],+ },+ {+ page: 4,+ width: 612,+ height: 792,+ text: "Sample text for page 4",+ textItems: textItemsJSON,+ boundingBoxes: [{ x1: 0, y1: 0, x2: 300, y2: 400 }],+ },+ {+ page: 5,+ width: 612,+ height: 792,+ text: "Sample text for page 5",+ textItems: textItemsJSON,+ boundingBoxes: [{ x1: 0, y1: 0, x2: 300, y2: 400 }],+ },+ ],+};++const parseResult = {+ pages: pages,+ text: "hello world",+ json: undefined,+};++describe("test json utilities", () => {+ it("test buildJSON", () => {+ const result = buildJSON(pages);+ expect(result).toStrictEqual(pagesJSON);+ });++ it("test formatJSON", () => {+ const result = formatJSON(parseResult);+ expect(result).toBe(JSON.stringify(pagesJSON, null, 2));+ });+});diff --git a/src/output/text.test.ts b/src/output/text.test.tsnew file mode 100644index 0000000..e5b71a5--- /dev/null+++ b/src/output/text.test.ts@@ -0,0 +1,70 @@+import { describe, expect, it } from "vitest";+import { formatPageText, formatText } from "./text";++const pages = [+ {+ pageNum: 1,+ width: 612,+ height: 792,+ text: "Sample text for page 1",+ textItems: [],+ boundingBoxes: [{ x1: 0, y1: 0, x2: 300, y2: 400 }],+ },+ {+ pageNum: 2,+ width: 612,+ height: 792,+ text: "Sample text for page 2",+ textItems: [],+ boundingBoxes: [{ x1: 0, y1: 0, x2: 300, y2: 400 }],+ },+ {+ pageNum: 3,+ width: 612,+ height: 792,+ text: "Sample text for page 3",+ textItems: [],+ boundingBoxes: [{ x1: 0, y1: 0, x2: 300, y2: 400 }],+ },+ {+ pageNum: 4,+ width: 612,+ height: 792,+ text: "Sample text for page 4",+ textItems: [],+ boundingBoxes: [{ x1: 0, y1: 0, x2: 300, y2: 400 }],+ },+ {+ pageNum: 5,+ width: 612,+ height: 792,+ text: "Sample text for page 5",+ textItems: [],+ boundingBoxes: [{ x1: 0, y1: 0, x2: 300, y2: 400 }],+ },+];++const parseResult = {+ pages: pages,+ text: "hello world",+ json: undefined,+};++describe("test text utilites", () => {+ it("test formatText", () => {+ const result = formatText(parseResult);+ expect(result).toBe(+ pages+ .map((page) => {+ const header = `\n--- Page ${page.pageNum} ---\n`;+ return header + page.text;+ })+ .join("\n\n")+ );+ });++ it("test formatPageText", () => {+ const result = formatPageText(pages[0]);+ expect(result).toBe(pages[0].text);+ });+});diff --git a/src/processing/cleanText.test.ts b/src/processing/cleanText.test.tsnew file mode 100644index 0000000..9f692aa--- /dev/null+++ b/src/processing/cleanText.test.ts@@ -0,0 +1,49 @@+import { describe, expect, it } from "vitest";+import { ParsedPage } from "../core/types.js";+import { cleanRawText } from "./cleanText.js";+import { DEFAULT_CONFIG } from "../core/config.js";++const mockPages: ParsedPage[] = [+ {+ // Normal page with margins+ pageNum: 1,+ width: 612,+ height: 792,+ text: " Hello World \n Foo Bar \n",+ textItems: [],+ },+ {+ // Empty page+ pageNum: 2,+ width: 612,+ height: 792,+ text: " \n \n",+ textItems: [],+ },+ {+ // Single line+ pageNum: 3,+ width: 612,+ height: 792,+ text: " Hello ",+ textItems: [],+ },+];++const expectedTexts: string[] = [+ // minX=3, minY=0, maxY=1 → slice(3) + trimEnd+ "Hello World\nFoo Bar",+ // entirely empty → ""+ "",+ // minX=2, minY=0, maxY=0 → slice(2) + trimEnd+ "Hello",+];++describe("test cleanText", () => {+ it("test cleanRawText", () => {+ cleanRawText(mockPages, DEFAULT_CONFIG);+ for (let i = 0; i < mockPages.length; i++) {+ expect(mockPages[i].text).toBe(expectedTexts[i]);+ }+ });+});diff --git a/src/processing/markupUtils.test.ts b/src/processing/markupUtils.test.tsnew file mode 100644index 0000000..5e6991c--- /dev/null+++ b/src/processing/markupUtils.test.ts@@ -0,0 +1,32 @@+import { describe, expect, it } from "vitest";+import { MarkupData } from "../core/types.js";+import { applyMarkupTags } from "./markupUtils.js";++const text = "hello";++describe("test markupUtils", () => {+ it("test strikeout", () => {+ const data: MarkupData = { strikeout: true };+ expect(applyMarkupTags(data, text)).toBe(`~~${text}~~`);+ });++ it("test underline", () => {+ const data: MarkupData = { underline: true };+ expect(applyMarkupTags(data, text)).toBe(`__${text}__`);+ });++ it("test squiggly", () => {+ const data: MarkupData = { squiggly: true };+ expect(applyMarkupTags(data, text)).toBe(`__${text}__`);+ });++ it("test highlight", () => {+ const data: MarkupData = { highlight: "yes" };+ expect(applyMarkupTags(data, text)).toBe(`==${text}==`);+ });++ it("test none", () => {+ const data: MarkupData = {};+ expect(applyMarkupTags(data, text)).toBe(text);+ });+});diff --git a/src/processing/octUtils.test.ts b/src/processing/octUtils.test.tsnew file mode 100644index 0000000..9cf8752--- /dev/null+++ b/src/processing/octUtils.test.ts@@ -0,0 +1,80 @@+import { describe, expect, it } from "vitest";+import { parseImageOcrBlocks, easyOcrResultLinesToList } from "./ocrUtils";+import { EasyOcrResultLine, Image } from "../engines/pdf/interface";++const mockImage: Image = {+ x: 0,+ y: 0,+ width: 200,+ height: 100,+ scaleFactor: 2,+ coords: { x: 10, y: 20, w: 100, h: 50 },+ ocrRaw: [+ [+ [+ [0, 0],+ [40, 0],+ [40, 20],+ [0, 20],+ ],+ "Hello",+ 0.95,+ ] as EasyOcrResultLine,+ ],+};++const expectedBlock = {+ c: "Hello",+ x: 0 / 2 + 10, // = 10+ rx: Math.round(0 / 2), // = 0+ y: 0 / 2 + 20, // = 20+ ry: Math.round(0 / 2), // = 0+ w: 40 / 2, // = 20+ rw: Math.round(40 / 2), // = 20+ h: 20 / 2, // = 10+ rh: Math.round(20 / 2), // = 10+ confidence: 0.95,+ fromOcr: true,+};++const mockStdOut = "([[0, 10], [40, 10], [40, 30], [0, 30]], 'Hello', 0.95)";++const expectedResult: EasyOcrResultLine[] = [+ [+ [+ [0, 10],+ [40, 10],+ [40, 30],+ [0, 30],+ ],+ "Hello",+ "0.95", // note: string, since ocrMatch[10] is not parsed with Number()+ ] as EasyOcrResultLine,+];++describe("test ocrUtils", () => {+ it("test parseImageOcrBlocks success", () => {+ const result = parseImageOcrBlocks(mockImage);+ expect(result.length).toBe(1);+ expect(result[0]).toStrictEqual(expectedBlock);+ });++ it("test parseImageOcrBlocks failure", () => {+ const imageNoRawOcr = {+ ...mockImage,+ ocrRaw: [],+ };+ const result = parseImageOcrBlocks(imageNoRawOcr);+ expect(result.length).toBe(0);+ });++ it("test EasyOcrResultLine success", () => {+ const result = easyOcrResultLinesToList(mockStdOut);+ expect(result).toStrictEqual(expectedResult);+ });++ it("test EasyOcrResultLine success", () => {+ const result = easyOcrResultLinesToList("");+ expect(result.length).toBe(0);+ });+});diff --git a/src/processing/textUtils.test.ts b/src/processing/textUtils.test.tsnew file mode 100644index 0000000..1b09fbb--- /dev/null+++ b/src/processing/textUtils.test.ts@@ -0,0 +1,28 @@+import { describe, expect, it } from "vitest";+import { strToPostScript, strToSubscriptString } from "./textUtils";++describe("test processing textUtils", () => {+ it("test strToSubscriptString full conversion", () => {+ const originalStr = "hello123";+ const finalStr = strToSubscriptString(originalStr);+ expect(finalStr).toBe("ₕₑₗₗₒ₁₂₃");+ });++ it("test strToSubscriptString fail conversion", () => {+ const originalStr = "hello123!";+ const finalStr = strToSubscriptString(originalStr);+ expect(finalStr).toBe("hello123!");+ });++ it("test strToPostScript full conversion", () => {+ const originalStr = "hello123";+ const finalStr = strToPostScript(originalStr);+ expect(finalStr).toBe("ʰᵉˡˡᵒ¹²³");+ });++ it("test strToPostScript fail conversion", () => {+ const originalStr = "hello123!";+ const finalStr = strToPostScript(originalStr);+ expect(finalStr).toBe("hello123!");+ });+});