-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocument.service.spec.ts
More file actions
469 lines (439 loc) · 14.9 KB
/
Copy pathdocument.service.spec.ts
File metadata and controls
469 lines (439 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
import { DocumentStatus } from "@generated/client";
import { Test, TestingModule } from "@nestjs/testing";
import { AppLoggerService } from "@/logging/app-logger.service";
import { mockAppLogger } from "@/testUtils/mockAppLogger";
import {
BLOB_STORAGE,
BlobStorageInterface,
} from "../blob-storage/blob-storage.interface";
import { UploadNormalizationLimiter } from "../upload/upload-normalization-limiter";
import { DocumentService } from "./document.service";
import { DocumentDbService } from "./document-db.service";
import { PdfNormalizationService } from "./pdf-normalization.service";
describe("DocumentService", () => {
let service: DocumentService;
let documentDbService: DocumentDbService;
let blobStorage: BlobStorageInterface;
let pdfNormalization: jest.Mocked<
Pick<
PdfNormalizationService,
"validateForUpload" | "normalizeToPdf" | "generateThumbnailWebp"
>
>;
let uploadNormalizationLimiter: jest.Mocked<
Pick<UploadNormalizationLimiter, "run">
>;
beforeEach(async () => {
pdfNormalization = {
validateForUpload: jest.fn().mockResolvedValue(undefined),
normalizeToPdf: jest
.fn()
.mockImplementation((buf: Buffer) => Promise.resolve(Buffer.from(buf))),
generateThumbnailWebp: jest.fn().mockResolvedValue(Buffer.from("webp")),
};
uploadNormalizationLimiter = {
run: jest.fn((task: () => Promise<unknown>) => task()),
} as jest.Mocked<Pick<UploadNormalizationLimiter, "run">>;
documentDbService = {
createDocument: jest.fn(),
findDocument: jest.fn(),
updateDocument: jest.fn(),
deleteDocument: jest.fn(),
findAllDocuments: jest.fn(),
findOcrResult: jest.fn(),
upsertOcrResult: jest.fn(),
} as any;
blobStorage = {
write: jest.fn().mockResolvedValue(undefined),
read: jest.fn(),
exists: jest.fn(),
delete: jest.fn(),
list: jest.fn(),
deleteByPrefix: jest.fn(),
} as any;
const module: TestingModule = await Test.createTestingModule({
providers: [
DocumentService,
{ provide: AppLoggerService, useValue: mockAppLogger },
{ provide: DocumentDbService, useValue: documentDbService },
{ provide: BLOB_STORAGE, useValue: blobStorage },
{ provide: PdfNormalizationService, useValue: pdfNormalization },
{
provide: UploadNormalizationLimiter,
useValue: uploadNormalizationLimiter,
},
],
}).compile();
service = module.get<DocumentService>(DocumentService);
});
afterEach(() => {
jest.clearAllMocks();
});
describe("uploadDocument", () => {
it("should upload a document and save to db", async () => {
const pdfBytes = Buffer.from("%PDF-1.4\n%\xe2\xe3\xcf\xd3\n");
const base64 = pdfBytes.toString("base64");
const mockDoc = {
id: "1",
title: "Test",
original_filename: "file.pdf",
file_path: "documents/1/original.pdf",
normalized_file_path: "documents/1/normalized.pdf",
file_type: "pdf",
file_size: pdfBytes.length,
metadata: {},
source: "api",
status: DocumentStatus.ongoing_ocr,
created_at: new Date(),
updated_at: new Date(),
model_id: "test-model-id",
group_id: "group-1",
};
(documentDbService.createDocument as jest.Mock).mockResolvedValue(
mockDoc,
);
const result = await service.uploadDocument(
"Test",
base64,
"pdf",
"file.pdf",
"test-model-id",
"group-1",
{},
);
expect(result.kind).toBe("success");
expect(result.document.id).toBe("1");
expect(result.document.original_filename).toBe("file.pdf");
expect(result.document.title).toBe("Test");
expect(documentDbService.createDocument).toHaveBeenCalled();
expect(blobStorage.write).toHaveBeenCalledWith(
expect.stringMatching(/^group-1\/ocr\/.+\/original\.pdf$/),
expect.any(Buffer),
);
expect(blobStorage.write).toHaveBeenCalledWith(
expect.stringMatching(/^group-1\/ocr\/.+\/normalized\.pdf$/),
expect.any(Buffer),
);
expect(uploadNormalizationLimiter.run).toHaveBeenCalledTimes(1);
expect(pdfNormalization.generateThumbnailWebp).toHaveBeenCalledWith(
expect.any(Buffer),
"pdf",
);
});
it("writes the normalized blob only after the original write completes", async () => {
const pdfBytes = Buffer.from("%PDF-1.4\n%\xe2\xe3\xcf\xd3\n");
const base64 = pdfBytes.toString("base64");
const mockDoc = {
id: "1",
title: "Test",
original_filename: "file.pdf",
file_path: "documents/1/original.pdf",
normalized_file_path: "documents/1/normalized.pdf",
file_type: "pdf",
file_size: pdfBytes.length,
metadata: {},
source: "api",
status: DocumentStatus.ongoing_ocr,
created_at: new Date(),
updated_at: new Date(),
model_id: "test-model-id",
group_id: "group-1",
};
const writeOrder: string[] = [];
(documentDbService.createDocument as jest.Mock).mockResolvedValue(
mockDoc,
);
(blobStorage.write as jest.Mock).mockImplementation((key: string) => {
if (key.endsWith("/original.pdf")) {
writeOrder.push("original");
return new Promise<void>((resolve) => {
setTimeout(resolve, 10);
});
}
if (key.endsWith("/normalized.pdf")) {
writeOrder.push("normalized");
}
return Promise.resolve();
});
await service.uploadDocument(
"Test",
base64,
"pdf",
"file.pdf",
"test-model-id",
"group-1",
{},
);
expect(writeOrder).toEqual(["original", "normalized"]);
});
it("starts PDF normalization before the original blob write finishes", async () => {
const pdfBytes = Buffer.from("%PDF-1.4\n%\xe2\xe3\xcf\xd3\n");
const base64 = pdfBytes.toString("base64");
const mockDoc = {
id: "1",
title: "Test",
original_filename: "file.pdf",
file_path: "documents/1/original.pdf",
normalized_file_path: "documents/1/normalized.pdf",
file_type: "pdf",
file_size: pdfBytes.length,
metadata: {},
source: "api",
status: DocumentStatus.ongoing_ocr,
created_at: new Date(),
updated_at: new Date(),
model_id: "test-model-id",
group_id: "group-1",
};
let resolveOriginalWrite: (() => void) | undefined;
let normalizationStarted = false;
(documentDbService.createDocument as jest.Mock).mockResolvedValue(
mockDoc,
);
(blobStorage.write as jest.Mock).mockImplementation((key: string) => {
if (key.endsWith("/original.pdf")) {
return new Promise<void>((resolve) => {
resolveOriginalWrite = resolve;
});
}
return Promise.resolve();
});
pdfNormalization.normalizeToPdf.mockImplementation(
async (buf: Buffer) => {
normalizationStarted = true;
return Buffer.from(buf);
},
);
const uploadPromise = service.uploadDocument(
"Test",
base64,
"pdf",
"file.pdf",
"test-model-id",
"group-1",
{},
);
await Promise.resolve();
expect(normalizationStarted).toBe(true);
expect(documentDbService.createDocument).not.toHaveBeenCalled();
resolveOriginalWrite?.();
const result = await uploadPromise;
expect(result.kind).toBe("success");
expect(documentDbService.createDocument).toHaveBeenCalled();
});
it("should throw on invalid base64", async () => {
await expect(
service.uploadDocument(
"Test",
{} as any,
"pdf",
"file.pdf",
"test-model-id",
"group-1",
),
).rejects.toThrow("Invalid base64 file data");
});
});
describe("createDocument", () => {
it("should create a document record directly via db service", async () => {
const mockDoc = {
id: "doc-123",
title: "sample-doc",
original_filename: "doc.pdf",
file_path: "documents/doc-123/original.pdf",
normalized_file_path: null as string | null,
file_type: "pdf",
file_size: 512,
metadata: { source: "ground-truth-generation" },
source: "ground-truth-generation",
status: DocumentStatus.pre_ocr,
apim_request_id: null,
workflow_id: null,
workflow_config_id: "wf-1",
workflow_execution_id: null,
model_id: "prebuilt-layout",
group_id: "group-1",
created_at: new Date(),
updated_at: new Date(),
};
(documentDbService.createDocument as jest.Mock).mockResolvedValue(
mockDoc,
);
const {
created_at: _created_at,
updated_at: _updated_at,
...inputData
} = mockDoc;
const result = await service.createDocument(inputData);
expect(result).toEqual(mockDoc);
expect(documentDbService.createDocument).toHaveBeenCalledWith(
inputData,
undefined,
);
});
it("should forward optional transaction client to db service", async () => {
const mockDoc = {
id: "doc-456",
title: "tx-doc",
original_filename: "tx.pdf",
file_path: "documents/doc-456/original.pdf",
normalized_file_path: null as string | null,
file_type: "pdf",
file_size: 256,
metadata: {},
source: "api",
status: DocumentStatus.pre_ocr,
apim_request_id: null,
workflow_id: null,
workflow_config_id: null,
workflow_execution_id: null,
model_id: "prebuilt-layout",
group_id: "group-1",
created_at: new Date(),
updated_at: new Date(),
};
(documentDbService.createDocument as jest.Mock).mockResolvedValue(
mockDoc,
);
const {
created_at: _created_at,
updated_at: _updated_at,
...inputData
} = mockDoc;
const fakeTx = {} as never;
await service.createDocument(inputData, fakeTx);
expect(documentDbService.createDocument).toHaveBeenCalledWith(
inputData,
fakeTx,
);
});
});
describe("findDocument", () => {
it("should find a document by id", async () => {
const mockDoc = {
id: "1",
title: "Test",
original_filename: "file.pdf",
file_path: "documents/1/original.pdf",
file_type: "pdf",
file_size: 123,
metadata: {},
source: "api",
status: DocumentStatus.ongoing_ocr,
created_at: new Date(),
updated_at: new Date(),
model_id: "test-model-id",
group_id: "group-1",
};
(documentDbService.findDocument as jest.Mock).mockResolvedValue(mockDoc);
const result = await service.findDocument("1");
expect(result).toBeDefined();
expect(result?.id).toBe("1");
expect(documentDbService.findDocument).toHaveBeenCalledWith(
"1",
undefined,
);
});
it("should return null if document not found", async () => {
(documentDbService.findDocument as jest.Mock).mockResolvedValue(null);
const result = await service.findDocument("notfound");
expect(result).toBeNull();
});
});
describe("updateDocument", () => {
it("should update and return the document", async () => {
const mockDoc = {
id: "1",
title: "Updated",
original_filename: "file.pdf",
file_path: "documents/1/original.pdf",
file_type: "pdf",
file_size: 123,
metadata: {},
source: "api",
status: DocumentStatus.ongoing_ocr,
created_at: new Date(),
updated_at: new Date(),
model_id: "test-model-id",
group_id: "group-1",
};
(documentDbService.updateDocument as jest.Mock).mockResolvedValue(
mockDoc,
);
const result = await service.updateDocument("1", { title: "Updated" });
expect(result).not.toBeNull();
expect(result?.title).toBe("Updated");
expect(documentDbService.updateDocument).toHaveBeenCalledWith(
"1",
{
title: "Updated",
},
undefined,
);
});
it("should return null if document not found", async () => {
(documentDbService.updateDocument as jest.Mock).mockResolvedValue(null);
const result = await service.updateDocument("notfound", {
title: "New",
});
expect(result).toBeNull();
});
});
describe("deleteDocument", () => {
it("should delete the document and its blob", async () => {
const mockDoc = {
id: "1",
file_path: "testgroup1/ocr/documents/1/original.pdf",
group_id: "testgroup1",
status: DocumentStatus.extracted,
};
(documentDbService.findDocument as jest.Mock).mockResolvedValue(mockDoc);
(documentDbService.deleteDocument as jest.Mock).mockResolvedValue(true);
(blobStorage.delete as jest.Mock).mockResolvedValue(undefined);
const result = await service.deleteDocument("1");
expect(result).toBe(true);
expect(documentDbService.deleteDocument).toHaveBeenCalledWith("1");
expect(blobStorage.deleteByPrefix).toHaveBeenCalledWith(
"testgroup1/ocr/1",
);
});
it("should return false if document not found", async () => {
(documentDbService.findDocument as jest.Mock).mockResolvedValue(null);
const result = await service.deleteDocument("notfound");
expect(result).toBe(false);
expect(documentDbService.deleteDocument).not.toHaveBeenCalled();
});
it.each([
DocumentStatus.pre_ocr,
DocumentStatus.ongoing_ocr,
])("should refuse to delete a document with status %s", async (status) => {
const mockDoc = {
id: "1",
file_path: "documents/1/original.pdf",
group_id: "group-1",
status,
};
(documentDbService.findDocument as jest.Mock).mockResolvedValue(mockDoc);
await expect(service.deleteDocument("1")).rejects.toThrow(
/currently being processed/,
);
expect(documentDbService.deleteDocument).not.toHaveBeenCalled();
expect(blobStorage.delete).not.toHaveBeenCalled();
});
it("should still return true if blob deletion fails", async () => {
const mockDoc = {
id: "1",
file_path: "testgroup1/ocr/documents/1/original.pdf",
group_id: "testgroup1",
status: DocumentStatus.extracted,
};
(documentDbService.findDocument as jest.Mock).mockResolvedValue(mockDoc);
(documentDbService.deleteDocument as jest.Mock).mockResolvedValue(true);
(blobStorage.deleteByPrefix as jest.Mock).mockRejectedValue(
new Error("blob not found"),
);
const result = await service.deleteDocument("1");
expect(result).toBe(true);
});
});
});