Skip to content

Commit 48d1495

Browse files
committed
test ocr
1 parent 54f2708 commit 48d1495

9 files changed

Lines changed: 120 additions & 34 deletions

File tree

src/v2/product/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
export { Classification, ClassificationResponse } from "./classification/index.js";
2-
export { Crop, CropResponse, CropItem } from "./crop/index.js";
2+
export * as classification from "./classification/index.js";
3+
4+
export { Crop, CropResponse } from "./crop/index.js";
5+
export * as crop from "./crop/index.js";
6+
37
export { Extraction, ExtractionResponse } from "./extraction/index.js";
8+
export * as extraction from "./extraction/index.js";
9+
410
export { Ocr, OcrResponse } from "./ocr/index.js";
11+
export * as ocr from "./ocr/index.js";
12+
513
export { Split, SplitResponse } from "./split/index.js";
14+
export * as split from "./split/index.js";

src/v2/product/ocr/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ export { Ocr } from "./ocr.js";
22
export { OcrParameters } from "./ocrParameters.js";
33
export { OcrResponse } from "./ocrResponse.js";
44
export { OcrInference } from "./ocrInference.js";
5+
export { OcrResult } from "./ocrResult.js";
6+
export { OcrPage } from "./ocrPage.js";
7+
export { OcrWord } from "./ocrWord.js";

tests/v2/product/classification.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { expect } from "chai";
22
import path from "node:path";
3-
import { V2_PRODUCT_PATH } from "../../index.js";
43
import { ClassificationResponse } from "@/v2/product/index.js";
4+
5+
import { V2_PRODUCT_PATH } from "../../index.js";
56
import { loadV2Response } from "./utils.js";
67

78

tests/v2/product/crop.spec.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { expect } from "chai";
22
import path from "node:path";
3+
import { Polygon } from "@/geometry/index.js";
4+
import { crop } from "@/v2/product/index.js";
5+
36
import { V2_PRODUCT_PATH } from "../../index.js";
4-
import { CropResponse, CropItem } from "@/v2/product/index.js";
57
import { loadV2Response } from "./utils.js";
6-
import { Polygon } from "@/geometry/index.js";
78

89
describe("MindeeV2 - Crop Response", async () => {
910
it("should load a single result", async () => {
1011
const response = await loadV2Response(
11-
CropResponse,
12+
crop.CropResponse,
1213
path.join(V2_PRODUCT_PATH, "crop", "crop_single.json")
1314
);
1415
// Validate inference metadata
@@ -21,14 +22,14 @@ describe("MindeeV2 - Crop Response", async () => {
2122
expect(response.inference.file.mimeType).to.equal("image/jpeg");
2223

2324
// Validate crops
24-
const crops = response.inference.result.crops;
25+
const crops: crop.CropItem[] = response.inference.result.crops;
2526
expect(crops).to.be.an("array").that.has.lengthOf(1);
2627

27-
const crop: CropItem = crops[0];
28-
expect(crop.objectType).to.equal("invoice");
29-
expect(crop.location.page).to.equal(0);
28+
const firstCrop = crops[0];
29+
expect(firstCrop.objectType).to.equal("invoice");
30+
expect(firstCrop.location.page).to.equal(0);
3031

31-
const polygon: Polygon = crop.location.polygon!;
32+
const polygon: Polygon = firstCrop.location.polygon!;
3233
expect(polygon.length).to.equal(4);
3334
expect(polygon.length).to.equal(4);
3435
expect(polygon[0][0]).to.equal(0.15);
@@ -43,7 +44,7 @@ describe("MindeeV2 - Crop Response", async () => {
4344

4445
it("should load multiple results", async () => {
4546
const response = await loadV2Response(
46-
CropResponse,
47+
crop.CropResponse,
4748
path.join(V2_PRODUCT_PATH, "crop", "crop_multiple.json")
4849
);
4950
// Validate inference metadata
@@ -55,11 +56,11 @@ describe("MindeeV2 - Crop Response", async () => {
5556
expect(response.inference.file.pageCount).to.equal(1);
5657
expect(response.inference.file.mimeType).to.equal("image/jpeg");
5758

58-
const crops: CropItem[] = response.inference.result.crops;
59+
const crops: crop.CropItem[] = response.inference.result.crops;
5960
expect(crops).to.be.an("array").that.has.lengthOf(2);
6061

6162
// Validate first crop item
62-
const firstCrop: CropItem = crops[0];
63+
const firstCrop: crop.CropItem = crops[0];
6364
expect(firstCrop.objectType).to.equal("invoice");
6465
expect(firstCrop.location.page).to.equal(0);
6566
const firstPolygon: Polygon = firstCrop.location.polygon!;
@@ -74,7 +75,7 @@ describe("MindeeV2 - Crop Response", async () => {
7475
expect(firstPolygon[3][1]).to.equal(0.979);
7576

7677
// Validate second crop item
77-
const secondCrop: CropItem = crops[1];
78+
const secondCrop: crop.CropItem = crops[1];
7879
expect(secondCrop.objectType).to.equal("invoice");
7980
expect(secondCrop.location.page).to.equal(0);
8081
const secondPolygon: Polygon = secondCrop.location.polygon!;

tests/v2/product/extraction.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import {
99
SimpleField,
1010
} from "@/v2/parsing/inference/field/index.js";
1111
import { field } from "@/v2/parsing/index.js";
12-
import { V2_PRODUCT_PATH } from "../../index.js";
1312
import { ExtractionResponse } from "@/v2/product/index.js";
13+
14+
import { V2_PRODUCT_PATH } from "../../index.js";
1415
import { loadV2Response } from "./utils.js";
1516

1617
const findocPath = path.join(V2_PRODUCT_PATH, "extraction", "financial_document");

tests/v2/client/inferenceParameter.spec.ts renamed to tests/v2/product/extractionParameter.spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ import path from "path";
33
import { V2_PRODUCT_PATH } from "../../index.js";
44
import { expect } from "chai";
55
import { promises as fs } from "fs";
6-
import { ExtractionParameters, DataSchema } from "@/v2/product/extraction/index.js";
6+
import { extraction } from "@/v2/product/index.js";
77

88
let expectedDataSchemaDict: StringDict;
99
let expectedDataSchemaString: string;
10-
let expectedDataSchemaObject: DataSchema;
10+
let expectedDataSchemaObject: extraction.DataSchema;
1111

12-
describe("MindeeV2 - Inference Parameter", () => {
12+
describe("MindeeV2 - Extraction Parameter", () => {
1313
const modelIdValue = "test-model-id";
1414

1515
describe("Polling Options", () => {
1616
it("should provide sensible defaults", () => {
1717

18-
const paramsInstance = new ExtractionParameters({
18+
const paramsInstance = new extraction.ExtractionParameters({
1919
modelId: modelIdValue,
2020
});
2121
expect(paramsInstance.modelId).to.equal(modelIdValue);
@@ -34,26 +34,26 @@ describe("MindeeV2 - Inference Parameter", () => {
3434
);
3535
expectedDataSchemaDict = JSON.parse(fileContents.toString());
3636
expectedDataSchemaString = JSON.stringify(expectedDataSchemaDict);
37-
expectedDataSchemaObject = new DataSchema(expectedDataSchemaDict);
37+
expectedDataSchemaObject = new extraction.DataSchema(expectedDataSchemaDict);
3838
});
3939

4040
it("shouldn't replace when unset", () => {
41-
const params = new ExtractionParameters({
41+
const params = new extraction.ExtractionParameters({
4242
modelId: modelIdValue,
4343
});
4444
expect(params.dataSchema).to.be.undefined;
4545
});
4646

4747
it("should equate no matter the type", () => {
48-
const paramsDict = new ExtractionParameters({
48+
const paramsDict = new extraction.ExtractionParameters({
4949
modelId: modelIdValue,
5050
dataSchema: expectedDataSchemaDict,
5151
});
52-
const paramsString = new ExtractionParameters({
52+
const paramsString = new extraction.ExtractionParameters({
5353
modelId: modelIdValue,
5454
dataSchema: expectedDataSchemaString,
5555
});
56-
const paramsObject = new ExtractionParameters({
56+
const paramsObject = new extraction.ExtractionParameters({
5757
modelId: modelIdValue,
5858
dataSchema: expectedDataSchemaObject,
5959
});

tests/v2/product/ocr.spec.ts

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,60 @@
11
import { expect } from "chai";
22
import path from "node:path";
3+
import { ocr } from "@/v2/product/index.js";
4+
import { Polygon } from "@/geometry/index.js";
5+
36
import { V2_PRODUCT_PATH } from "../../index.js";
4-
import { OcrResponse } from "@/v2/product/index.js";
57
import { loadV2Response } from "./utils.js";
68

79

810
describe("MindeeV2 - OCR Response", async () => {
911
it("should load a single result", async () => {
1012
const response = await loadV2Response(
11-
OcrResponse,
13+
ocr.OcrResponse,
1214
path.join(V2_PRODUCT_PATH, "ocr", "ocr_single.json")
1315
);
14-
const pages = response.inference.result.pages;
16+
// Validate inference metadata
17+
expect(response.inference.id).to.equal("12345678-1234-1234-1234-123456789abc");
18+
expect(response.inference.model.id).to.equal("test-model-id");
19+
20+
// Validate file metadata
21+
expect(response.inference.file.name).to.equal("default_sample.jpg");
22+
expect(response.inference.file.pageCount).to.equal(1);
23+
expect(response.inference.file.mimeType).to.equal("image/jpeg");
24+
25+
// Validate pages
26+
const pages: ocr.OcrPage[] = response.inference.result.pages;
1527
expect(pages).to.be.an("array").that.has.lengthOf(1);
28+
29+
// Validate first page
30+
const firstPage: ocr.OcrPage = pages[0];
31+
expect(firstPage.words).to.be.an("array");
32+
33+
// Check first word
34+
const firstWord: ocr.OcrWord = firstPage.words[0];
35+
expect(firstWord.content).to.equal("Shipper:");
36+
const firstPolygon: Polygon = firstWord.polygon;
37+
expect(firstPolygon.length).to.equal(4);
38+
39+
// Check another word (5th word: "INC.")
40+
const fifthWord: ocr.OcrWord = firstPage.words[4];
41+
expect(fifthWord.content).to.equal("INC.");
42+
const fifthPolygon: Polygon = fifthWord.polygon;
43+
expect(fifthPolygon.length).to.equal(4);
1644
});
1745

1846
it("should load multiple results", async () => {
1947
const response = await loadV2Response(
20-
OcrResponse,
48+
ocr.OcrResponse,
2149
path.join(V2_PRODUCT_PATH, "ocr", "ocr_multiple.json")
2250
);
23-
const pages = response.inference.result.pages;
51+
const pages: ocr.OcrPage[] = response.inference.result.pages;
2452
expect(pages).to.be.an("array").that.has.lengthOf(3);
53+
54+
// Validate that each page has words and content
55+
pages.forEach((page: ocr.OcrPage): void => {
56+
expect(page.words).to.be.an("array");
57+
expect(page.content).to.be.a("string");
58+
});
2559
});
2660
});

tests/v2/product/split.spec.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
import { expect } from "chai";
22
import path from "node:path";
3+
import { split } from "@/v2/product/index.js";
4+
35
import { V2_PRODUCT_PATH } from "../../index.js";
4-
import { SplitResponse } from "@/v2/product/index.js";
56
import { loadV2Response } from "./utils.js";
67

78

89
describe("MindeeV2 - Split Response", async () => {
910
it("should load a single result", async () => {
1011
const response = await loadV2Response(
11-
SplitResponse,
12+
split.SplitResponse,
1213
path.join(V2_PRODUCT_PATH, "split", "split_single.json")
1314
);
14-
const splits = response.inference.result.splits;
15+
const splits: split.SplitRange[] = response.inference.result.splits;
1516
expect(splits).to.be.an("array").that.has.lengthOf(1);
1617
});
1718

1819
it("should load multiple results", async () => {
1920
const response = await loadV2Response(
20-
SplitResponse,
21+
split.SplitResponse,
2122
path.join(V2_PRODUCT_PATH, "split", "split_multiple.json")
2223
);
23-
const splits = response.inference.result.splits;
24+
const splits: split.SplitRange[] = response.inference.result.splits;
2425
expect(splits).to.be.an("array").that.has.lengthOf(3);
2526
});
2627
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { expect } from "chai";
2+
import { split } from "@/v2/product/index.js";
3+
4+
describe("MindeeV2 - Split Parameter", () => {
5+
const modelIdValue = "test-model-id";
6+
7+
describe("Polling Options", () => {
8+
it("should provide sensible defaults", () => {
9+
10+
const paramsInstance = new split.SplitParameters({
11+
modelId: modelIdValue,
12+
});
13+
expect(paramsInstance.modelId).to.equal(modelIdValue);
14+
expect(paramsInstance.getValidatedPollingOptions()).to.deep.equal({
15+
delaySec: 1.5,
16+
initialDelaySec: 2,
17+
maxRetries: 80
18+
});
19+
});
20+
});
21+
22+
describe("Invalid Options", () => {
23+
it("should not set invalid options", () => {
24+
25+
const paramsInstance = new split.SplitParameters({
26+
modelId: modelIdValue,
27+
// @ts-expect-error - rag is not a valid option
28+
rag: true,
29+
});
30+
expect(paramsInstance.modelId).to.equal(modelIdValue);
31+
// @ts-expect-error - rag is not a valid option
32+
expect(paramsInstance.rag).to.be.undefined;
33+
});
34+
});
35+
36+
});

0 commit comments

Comments
 (0)