62 lines
2.3 KiB
TypeScript
62 lines
2.3 KiB
TypeScript
// Test suite for src/commands/convert.ts
|
|
import { describe, it, expect, vi, beforeEach, Mock, beforeAll, afterAll } from 'vitest';
|
|
import { handler as convertHandler } from '../../../src/commands/pdf2jpg.js';
|
|
import type { ConvertCommandConfig } from '../../../src/lib/pdf/types.js';
|
|
import type { Arguments } from 'yargs';
|
|
import { rmSync, existsSync, mkdirSync } from 'node:fs';
|
|
import * as path from 'node:path';
|
|
|
|
const testOutputDir = path.join(process.cwd(), 'tests', 'pdf', 'out', 'convert-test');
|
|
const inputPdf = path.join(process.cwd(), 'tests', 'pdf', 'e5dc.pdf');
|
|
|
|
describe('Convert Command CLI Handler', () => {
|
|
|
|
beforeAll(() => {
|
|
if (existsSync(testOutputDir)) {
|
|
rmSync(testOutputDir, { recursive: true, force: true });
|
|
}
|
|
mkdirSync(testOutputDir, { recursive: true });
|
|
});
|
|
|
|
afterAll(() => {
|
|
if (existsSync(testOutputDir)) {
|
|
rmSync(testOutputDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
async function runHandlerHelper(args: Partial<ConvertCommandConfig & { _: (string | number)[], $0: string, output?: string }>) {
|
|
const fullArgs = {
|
|
_: ['convert'],
|
|
$0: 'test',
|
|
dpi: 300,
|
|
format: 'png',
|
|
...args,
|
|
} as Arguments<ConvertCommandConfig & { output?: string }>;
|
|
|
|
await convertHandler(fullArgs);
|
|
}
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it('should convert a PDF to images', async () => {
|
|
const outputPattern = path.join(testOutputDir, 'page_${PAGE}.png');
|
|
const args = {
|
|
input: inputPdf,
|
|
output: outputPattern,
|
|
};
|
|
await runHandlerHelper(args);
|
|
expect(existsSync(path.join(testOutputDir, 'page_1.png'))).toBe(true);
|
|
expect(existsSync(path.join(testOutputDir, 'page_2.png'))).toBe(true);
|
|
}, 30000);
|
|
|
|
it('should handle missing input file', async () => {
|
|
const processExit = vi.spyOn(process, 'exit').mockImplementation((() => { throw new Error('process.exit() was called.') }) as any);
|
|
const args = { input: 'nonexistent.pdf' };
|
|
await expect(runHandlerHelper(args)).rejects.toThrow('process.exit() was called.');
|
|
expect(processExit).toHaveBeenCalledWith(1);
|
|
processExit.mockRestore();
|
|
});
|
|
|
|
});
|