ocr-space-api-wrapper
Version:
Node.js wrapper for ocr.space APIs.
82 lines (79 loc) • 3.82 kB
JavaScript
const assert = require('assert');
const { ocrSpace } = require('../index');
const { base64string } = require('./base64string');
describe('Tests for OCR Space API Wrapper', () => {
it('should throw if input is wrong', async () => {
await assert.rejects(() => ocrSpace());
await assert.rejects(() => ocrSpace(''));
await assert.rejects(() => ocrSpace(1));
await assert.rejects(() => ocrSpace(true));
});
it('should return results with a URL input', async () => {
const res1 = await ocrSpace('http://dl.a9t9.com/ocrbenchmark/eng.png');
assert.ok(res1.ParsedResults);
assert.ok(res1.ParsedResults.length);
assert.strictEqual(res1.OCRExitCode, 1);
assert.strictEqual(res1.IsErroredOnProcessing, false);
assert.strictEqual(res1.SearchablePDFURL, 'Searchable PDF not generated as it was not requested.');
});
it('should return results with a local file input', async () => {
const res1 = await ocrSpace('./test/eng.png');
assert.ok(res1.ParsedResults);
assert.ok(res1.ParsedResults.length);
assert.strictEqual(res1.OCRExitCode, 1);
assert.strictEqual(res1.IsErroredOnProcessing, false);
assert.strictEqual(res1.SearchablePDFURL, 'Searchable PDF not generated as it was not requested.');
});
// Seems like OCR Space API does not support base64 image input anymore
it.skip('should return results with a base64 image input', async () => {
const res1 = await ocrSpace(base64string);
assert.ok(res1.ParsedResults);
assert.ok(res1.ParsedResults.length);
assert.strictEqual(res1.OCRExitCode, 1);
assert.strictEqual(res1.IsErroredOnProcessing, false);
assert.strictEqual(res1.SearchablePDFURL, 'Searchable PDF not generated as it was not requested.');
});
it('should return results with a local file input and options #1', async () => {
const res1 = await ocrSpace('./test/eng.png', { apiKey: 'helloworld', language: 'eng' });
assert.ok(res1.ParsedResults);
assert.ok(res1.ParsedResults.length);
assert.strictEqual(res1.OCRExitCode, 1);
assert.strictEqual(res1.IsErroredOnProcessing, false);
assert.strictEqual(res1.SearchablePDFURL, 'Searchable PDF not generated as it was not requested.');
});
it('should return results with a local file input and options #2', async () => {
const res1 = await ocrSpace('./test/eng.pdf', {
isCreateSearchablePdf: true,
isSearchablePdfHideTextLayer: true,
scale: true,
});
assert.ok(res1.ParsedResults);
assert.ok(res1.ParsedResults.length);
assert.strictEqual(res1.OCRExitCode, 1);
assert.strictEqual(res1.IsErroredOnProcessing, false);
assert.notStrictEqual(res1.SearchablePDFURL, 'Searchable PDF not generated as it was not requested.');
assert.match(res1.SearchablePDFURL, /https?:\/\/.*\.pdf/);
});
it('should return results with a local file input and options with OCREngine 2', async () => {
const res1 = await ocrSpace('./test/eng.pdf', {
isCreateSearchablePdf: true,
isSearchablePdfHideTextLayer: true,
scale: true,
OCREngine: 2,
});
assert.ok(res1.ParsedResults);
assert.ok(res1.ParsedResults.length);
assert.strictEqual(res1.OCRExitCode, 1);
assert.strictEqual(res1.IsErroredOnProcessing, false);
assert.notStrictEqual(res1.SearchablePDFURL, 'Searchable PDF not generated as it was not requested.');
// @todo: this test returns res1.SearchablePDFURL="Not applicable | Generated by Python OCR V1.0"
// assert.match(res1.SearchablePDFURL, /https?:\/\/.*\.pdf/);
});
it('should throw if the request is aborted', async () => {
const controller = new AbortController();
const { signal } = controller;
const promise = ocrSpace('./test/eng.png', { signal });
controller.abort();
await assert.rejects(promise);
});
});