Files
smartocr/test/test.ts
T

62 lines
1.8 KiB
TypeScript

import { expect, tap } from '@push.rocks/tapbundle';
import * as smartocr from '../ts/index.js';
import type { ISmartAiOcrEngine } from '@push.rocks/smartai/ocr';
let testOcrInstance: smartocr.SmartOcr;
tap.test('should create a valid instance of Smartocr', async () => {
testOcrInstance = await smartocr.SmartOcr.createAndInit();
expect(testOcrInstance).toBeInstanceOf(smartocr.SmartOcr);
});
tap.test('should recognize image buffers through SmartAI OCR', async () => {
const calls: unknown[] = [];
const smartAiOcrEngine: ISmartAiOcrEngine = {
recognizeImage: async (input) => {
calls.push(input);
return {
text: 'hello from smartai',
confidence: 0.92,
pages: [
{
index: 0,
text: 'hello from smartai',
confidence: 0.92,
},
],
raw: {
pages: [
{
index: 0,
markdown: 'hello from smartai',
},
],
model: 'mock-ocr',
},
};
},
};
const result = await testOcrInstance.recognizeImageBufferWithSmartAi(Buffer.from('image data'), {
mimeType: 'image/png',
smartAiOcrEngine,
});
expect(result.text).toEqual('hello from smartai');
expect(result.confidence).toEqual(0.92);
expect(calls[0]).toEqual({
dataBase64: Buffer.from('image data').toString('base64'),
mimeType: 'image/png',
});
});
tap.test('should ocr a pdfBuffer', async () => {
const smartfile = await import('@push.rocks/smartfile');
const smartfileFactory = smartfile.SmartFileFactory.nodeFs();
const pdfFile = await smartfileFactory.fromFilePath('./test/demo_without_textlayer.pdf');
const resultBuffer = await testOcrInstance.processPdfBuffer(pdfFile.contentBuffer);
console.log(resultBuffer);
});
tap.start();