forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathocr.service.spec.ts
More file actions
98 lines (83 loc) · 2.96 KB
/
Copy pathocr.service.spec.ts
File metadata and controls
98 lines (83 loc) · 2.96 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
import { Test, TestingModule } from '@nestjs/testing';
import { OcrService } from './ocr.service';
import { ReceiptParser } from './parsers/receipt-parser';
// Mock Tesseract.js
jest.mock('tesseract.js', () => ({
createWorker: jest.fn().mockResolvedValue({
recognize: jest.fn().mockResolvedValue({
data: {
text: 'Item 1 10.00\nItem 2 15.00\nTotal 25.00',
confidence: 85,
},
}),
terminate: jest.fn().mockResolvedValue(undefined),
}),
}));
// Mock Sharp
jest.mock('sharp', () => {
return jest.fn().mockImplementation(() => ({
metadata: jest.fn().mockResolvedValue({ width: 1000, height: 1500, format: 'jpeg' }),
greyscale: jest.fn().mockReturnThis(),
normalise: jest.fn().mockReturnThis(),
resize: jest.fn().mockReturnThis(),
sharpen: jest.fn().mockReturnThis(),
png: jest.fn().mockReturnThis(),
toBuffer: jest.fn().mockResolvedValue(Buffer.from('processed-image')),
}));
});
describe('OcrService', () => {
let service: OcrService;
let receiptParser: ReceiptParser;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [OcrService, ReceiptParser],
}).compile();
service = module.get<OcrService>(OcrService);
receiptParser = module.get<ReceiptParser>(ReceiptParser);
});
afterEach(async () => {
await service.cleanup();
});
describe('initialize', () => {
it('should initialize Tesseract worker', async () => {
await service.initialize();
// Worker should be initialized
expect(service).toBeDefined();
});
it('should not reinitialize if already initialized', async () => {
await service.initialize();
await service.initialize();
// Should not throw error
expect(service).toBeDefined();
});
});
describe('scanReceipt', () => {
it('should process receipt image and return parsed data', async () => {
const mockImageBuffer = Buffer.from('fake-image-data');
const result = await service.scanReceipt(mockImageBuffer);
expect(result).toBeDefined();
expect(result.items).toBeDefined();
expect(result.total).toBeDefined();
expect(result.confidence).toBeGreaterThanOrEqual(0);
expect(result.confidence).toBeLessThanOrEqual(1);
});
it('should handle image preprocessing errors gracefully', async () => {
const mockImageBuffer = Buffer.from('fake-image-data');
// Mock sharp to throw error
const sharp = require('sharp');
sharp.mockImplementationOnce(() => {
throw new Error('Image processing failed');
});
// Should still attempt to process
await expect(service.scanReceipt(mockImageBuffer)).resolves.toBeDefined();
});
});
describe('cleanup', () => {
it('should terminate worker and cleanup resources', async () => {
await service.initialize();
await service.cleanup();
// Should not throw error
expect(service).toBeDefined();
});
});
});