mono/packages/media/tests/pdf/lib/pdf.test.ts
2025-07-04 22:02:20 +02:00

79 lines
3.4 KiB
TypeScript

// Test suite for src/lib/pdf.ts
import { describe, it, expect, vi, beforeEach, Mock, beforeAll, afterAll } from 'vitest';
import { convertPdfToImages, PdfToImageOptions } from '../../../src/lib/pdf/convert.js';
import { Logger } from 'tslog';
import { Buffer } from 'node:buffer';
import { rmSync, existsSync, mkdirSync, readFileSync } from 'node:fs';
import * as path from 'node:path';
const testOutputDir = path.join(process.cwd(), 'tests', 'pdf', 'out', 'pdf-lib-test');
const testPdfPath = path.join(process.cwd(), 'tests', 'pdf', 'RS485-780.pdf');
describe('convertPdfToImages Function', () => {
let pdfData: Buffer;
beforeAll(() => {
if (existsSync(testOutputDir)) {
rmSync(testOutputDir, { recursive: true, force: true });
}
mkdirSync(testOutputDir, { recursive: true });
pdfData = readFileSync(testPdfPath);
});
afterAll(() => {
if (existsSync(testOutputDir)) {
rmSync(testOutputDir, { recursive: true, force: true });
}
});
const baseOptions: Omit<PdfToImageOptions, 'outputPathTemplate' | 'baseVariables'> = {
dpi: 300,
format: 'png',
scale: 1,
};
it('should convert all pages to PNG by default', async () => {
const options: PdfToImageOptions = {
...baseOptions,
outputPathTemplate: path.join(testOutputDir, 'image_${PAGE}.png'),
baseVariables: {}
};
const result = await convertPdfToImages(pdfData, options);
expect(result.length).toBe(29);
expect(existsSync(path.join(testOutputDir, 'image_1.png'))).toBe(true);
expect(existsSync(path.join(testOutputDir, 'image_29.png'))).toBe(true);
});
it('should convert specified page range to JPG', async () => {
const options: PdfToImageOptions = {
...baseOptions,
outputPathTemplate: path.join(testOutputDir, 'jpg_images/page_${PAGE}.jpg'),
baseVariables: {},
format: 'jpg',
startPage: 2,
endPage: 4,
};
const result = await convertPdfToImages(pdfData, options);
expect(result.length).toBe(3);
expect(existsSync(path.join(testOutputDir, 'jpg_images', 'page_2.jpg'))).toBe(true);
expect(existsSync(path.join(testOutputDir, 'jpg_images', 'page_4.jpg'))).toBe(true);
});
it('should throw error for invalid startPage', async () => {
const options: PdfToImageOptions = { ...baseOptions, outputPathTemplate: 'err', baseVariables: {}, startPage: 0 };
await expect(convertPdfToImages(pdfData, options))
.rejects.toThrow('startPage (0) is out of valid range (1-29)');
});
it('should throw error for invalid endPage', async () => {
const options: PdfToImageOptions = { ...baseOptions, outputPathTemplate: 'err', baseVariables: {}, endPage: 30 };
await expect(convertPdfToImages(pdfData, options))
.rejects.toThrow('endPage (30) is out of valid range (1-29)');
});
it('should throw error if startPage > endPage', async () => {
const options: PdfToImageOptions = { ...baseOptions, outputPathTemplate: 'err', baseVariables: {}, startPage: 4, endPage: 2 };
await expect(convertPdfToImages(pdfData, options))
.rejects.toThrow('startPage (4) cannot be greater than endPage (2)');
});
});