60 lines
2.9 KiB
TypeScript
60 lines
2.9 KiB
TypeScript
import { readFileSync } from "node:fs"
|
|
import { describe, expect, it } from "vitest"
|
|
import type { ReadDocument } from "../core/ports.js"
|
|
import { foldForComparison, isWeakPage, locateQuote, quoteIsOnPage } from "../core/verify.js"
|
|
|
|
const fixture = JSON.parse(
|
|
readFileSync(new URL("./fixtures/ocr-document.json", import.meta.url), "utf8"),
|
|
) as ReadDocument
|
|
const [first, second] = fixture.pages as [ReadDocument["pages"][number], ReadDocument["pages"][number]]
|
|
|
|
describe("the comparison of a quote with a page", () => {
|
|
it("folds whitespace, case and diacritics, and nothing else", () => {
|
|
expect(foldForComparison(" Šarūnas \n\t ĄČĘĖĮŠŲŪŽ ")).toBe("sarunas aceeisuuz")
|
|
// digits, punctuation and letters that are different letters stay different
|
|
expect(foldForComparison("1 250,00")).not.toBe(foldForComparison("1 520,00"))
|
|
expect(foldForComparison("a-b")).not.toBe(foldForComparison("a b"))
|
|
expect(foldForComparison("ß")).toBe("ß")
|
|
})
|
|
|
|
it("finds a quote that differs from the page only in what is folded", () => {
|
|
expect(quoteIsOnPage(first.text, "reference: qr-2026-04")).toBe(true)
|
|
expect(quoteIsOnPage(first.text, "Issued on: 2026-04-01\nThis line has no label")).toBe(true)
|
|
})
|
|
|
|
it("does not find a quote that is not there, an empty one, or one from another page", () => {
|
|
expect(quoteIsOnPage(first.text, "Reference: QR-2026-05")).toBe(false)
|
|
expect(quoteIsOnPage(first.text, " ")).toBe(false)
|
|
expect(quoteIsOnPage(first.text, "Total amount: 1 250,00 EUR")).toBe(false)
|
|
expect(quoteIsOnPage(second.text, "Total amount: 1 250,00 EUR")).toBe(true)
|
|
})
|
|
})
|
|
|
|
describe("where a quote sits", () => {
|
|
it("is the box of the line, as fractions of the page, with that line's confidence", () => {
|
|
const at = locateQuote(first, "REFERENCE: QR-2026-04")
|
|
expect(at?.box).toEqual({ x: 0.1, y: 200 / 1400, width: 0.5, height: 40 / 1400 })
|
|
expect(at?.confidence).toBe(0.97)
|
|
})
|
|
|
|
it("spans the lines a quote runs over, and averages their confidence", () => {
|
|
const at = locateQuote(first, "Reference: QR-2026-04\nIssued on: 2026-04-01")
|
|
expect(at?.box).toEqual({ x: 0.1, y: 200 / 1400, width: 0.5, height: 100 / 1400 })
|
|
expect(at?.confidence).toBeCloseTo(0.96)
|
|
})
|
|
|
|
it("is nothing for a quote that is not on the lines, and no box for a page with no size", () => {
|
|
expect(locateQuote(first, "Reference: QR-9")).toBeUndefined()
|
|
expect(locateQuote({ ...first, width: 0, height: 0 }, "Reference: QR-2026-04")?.box).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
describe("which pages a model is shown as an image", () => {
|
|
it("is a scan, a page the reader was unsure of, or a page with next to no text", () => {
|
|
expect(isWeakPage(first)).toBe(false)
|
|
expect(isWeakPage(second)).toBe(true) // source: ocr
|
|
expect(isWeakPage({ ...first, lines: first.lines.map((line) => ({ ...line, confidence: 0.5 })) })).toBe(true)
|
|
expect(isWeakPage({ ...first, text: " Hi " })).toBe(true)
|
|
})
|
|
})
|