tests
@ -10,7 +10,7 @@
|
||||
"dev": "tsc -p . --watch",
|
||||
"build": "tsc",
|
||||
"start": "node dist/index.js",
|
||||
"test:pdf": "node dist/index.js convert -i tests/e5dc.pdf -o tests/e5dc/ --startPage 3 --endPage 5",
|
||||
"test:pdf": "node dist/index.js convert -i tests/e5dc.pdf -o tests/out/e5dc/ --startPage 3 --endPage 5",
|
||||
"test:basic": "vitest run"
|
||||
},
|
||||
"keywords": [
|
||||
|
||||
@ -1,9 +1,143 @@
|
||||
// Test suite for src/commands/convert.ts
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { describe, it, expect, vi, beforeEach, Mock, beforeAll } from 'vitest';
|
||||
// Import types first
|
||||
import type { ConvertCommandConfig } from '../../src/types.js';
|
||||
import type { Arguments } from 'yargs';
|
||||
import { Buffer } from 'node:buffer';
|
||||
|
||||
// TODO: Add tests
|
||||
describe('Convert Command CLI', () => {
|
||||
it('should be implemented', () => {
|
||||
expect(true).toBe(true); // Placeholder
|
||||
// --- Define Mock Functions ---
|
||||
const mockConvertPdfToImagesFn = vi.fn();
|
||||
const mockExistsSync = vi.fn();
|
||||
const mockReadFile = vi.fn();
|
||||
const mockMkdir = vi.fn();
|
||||
const mockDirname = vi.fn();
|
||||
const mockBasename = vi.fn();
|
||||
const mockExtname = vi.fn();
|
||||
const mockLoggerInfo = vi.fn();
|
||||
const mockLoggerError = vi.fn();
|
||||
const mockProcessExit = vi.spyOn(process, 'exit').mockImplementation((() => {}) as any);
|
||||
|
||||
// Use beforeAll for mocks
|
||||
beforeAll(() => {
|
||||
// Mock dependencies using vi.doMock
|
||||
vi.doMock('../../src/lib/pdf.js', () => ({
|
||||
convertPdfToImages: mockConvertPdfToImagesFn,
|
||||
}));
|
||||
vi.doMock('node:fs', () => ({
|
||||
existsSync: mockExistsSync,
|
||||
}));
|
||||
vi.doMock('node:fs/promises', () => ({
|
||||
readFile: mockReadFile,
|
||||
mkdir: mockMkdir,
|
||||
}));
|
||||
vi.doMock('node:path', () => ({
|
||||
dirname: mockDirname,
|
||||
basename: mockBasename,
|
||||
extname: mockExtname,
|
||||
sep: '/',
|
||||
}));
|
||||
vi.doMock('tslog', () => ({
|
||||
Logger: vi.fn().mockImplementation(() => ({
|
||||
info: mockLoggerInfo,
|
||||
error: mockLoggerError,
|
||||
})),
|
||||
}));
|
||||
});
|
||||
|
||||
// --- Test Suite ---
|
||||
describe('Convert Command CLI Handler', () => {
|
||||
let convertHandler: typeof import('../../src/commands/convert.js').handler;
|
||||
|
||||
// Import the handler after mocks are set
|
||||
beforeAll(async () => {
|
||||
await vi.dynamicImportSettled(); // Ensure mocks are applied
|
||||
const commandModule = await import('../../src/commands/convert.js');
|
||||
convertHandler = commandModule.handler;
|
||||
});
|
||||
|
||||
// --- Helper Function to Run Handler ---
|
||||
async function runHandlerHelper(args: Partial<ConvertCommandConfig & { _: (string | number)[], $0: string }>) {
|
||||
const fullArgs = {
|
||||
_: ['convert'],
|
||||
$0: 'test',
|
||||
dpi: 300,
|
||||
format: 'png',
|
||||
...args,
|
||||
} as Arguments<ConvertCommandConfig>;
|
||||
// Make sure handler is loaded before calling
|
||||
if (!convertHandler) throw new Error('Handler not loaded');
|
||||
await convertHandler(fullArgs);
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
// Reset mocks
|
||||
mockConvertPdfToImagesFn.mockResolvedValue(['output/img_1.png']);
|
||||
mockExistsSync.mockReturnValue(true);
|
||||
mockReadFile.mockResolvedValue(Buffer.from('fake-pdf-data'));
|
||||
mockMkdir.mockResolvedValue(undefined);
|
||||
mockDirname.mockImplementation((p) => p.substring(0, p.lastIndexOf('/') > 0 ? p.lastIndexOf('/') : p.length));
|
||||
mockBasename.mockImplementation((p) => p.substring(p.lastIndexOf('/') > 0 ? p.lastIndexOf('/') + 1 : 0));
|
||||
mockExtname.mockImplementation((p) => {
|
||||
const dotIndex = p.lastIndexOf('.');
|
||||
return dotIndex > 0 ? p.substring(dotIndex) : '';
|
||||
});
|
||||
mockProcessExit.mockClear();
|
||||
});
|
||||
|
||||
// --- Test cases ---
|
||||
it('should call convertPdfToImages with correct args', async () => {
|
||||
const args = {
|
||||
input: 'input.pdf',
|
||||
output: 'output/prefix',
|
||||
dpi: 150,
|
||||
format: 'jpg',
|
||||
startPage: 2,
|
||||
endPage: 5,
|
||||
} as const;
|
||||
await runHandlerHelper(args);
|
||||
expect(mockExistsSync).toHaveBeenCalledWith(args.input);
|
||||
expect(mockReadFile).toHaveBeenCalledWith(args.input);
|
||||
// ... rest of assertions ...
|
||||
expect(mockProcessExit).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should handle missing input file', async () => {
|
||||
mockExistsSync.mockReturnValue(false);
|
||||
const args = { input: 'nonexistent.pdf', output: 'out' };
|
||||
await runHandlerHelper(args);
|
||||
expect(mockConvertPdfToImagesFn).not.toHaveBeenCalled();
|
||||
expect(mockLoggerError).toHaveBeenCalledWith(
|
||||
expect.stringContaining('Error during conversion:'),
|
||||
expect.stringContaining('Input file nonexistent.pdf does not exist'),
|
||||
expect.any(Error)
|
||||
);
|
||||
expect(mockProcessExit).toHaveBeenCalledWith(1);
|
||||
});
|
||||
|
||||
it('should handle conversion error', async () => {
|
||||
const conversionError = new Error('Conversion failed');
|
||||
mockConvertPdfToImagesFn.mockRejectedValue(conversionError);
|
||||
const args = { input: 'in.pdf', output: 'out' };
|
||||
await runHandlerHelper(args);
|
||||
expect(mockConvertPdfToImagesFn).toHaveBeenCalledTimes(1);
|
||||
expect(mockLoggerError).toHaveBeenCalledWith(
|
||||
'Error during conversion:',
|
||||
'Conversion failed',
|
||||
conversionError
|
||||
);
|
||||
expect(mockProcessExit).toHaveBeenCalledWith(1);
|
||||
});
|
||||
|
||||
it('should create output directory correctly when output is a directory path', async () => {
|
||||
const args = { input: 'in.pdf', output: 'output/subdir/' };
|
||||
await runHandlerHelper(args);
|
||||
// ... assertions ...
|
||||
});
|
||||
|
||||
it('should create parent directory when output is a file prefix', async () => {
|
||||
const args = { input: 'in.pdf', output: 'output/subdir/file_prefix' };
|
||||
await runHandlerHelper(args);
|
||||
// ... assertions ...
|
||||
});
|
||||
});
|
||||
|
Before Width: | Height: | Size: 244 KiB |
|
Before Width: | Height: | Size: 161 KiB |
|
Before Width: | Height: | Size: 48 KiB |
|
Before Width: | Height: | Size: 159 KiB |
|
Before Width: | Height: | Size: 144 KiB |
|
Before Width: | Height: | Size: 133 KiB |
|
Before Width: | Height: | Size: 163 KiB |
|
Before Width: | Height: | Size: 38 KiB |
|
Before Width: | Height: | Size: 24 KiB |
|
Before Width: | Height: | Size: 13 KiB |
|
Before Width: | Height: | Size: 143 KiB |
|
Before Width: | Height: | Size: 38 KiB |
|
Before Width: | Height: | Size: 94 KiB |
|
Before Width: | Height: | Size: 149 KiB |
|
Before Width: | Height: | Size: 158 KiB |
|
Before Width: | Height: | Size: 144 KiB |
|
Before Width: | Height: | Size: 158 KiB |
|
Before Width: | Height: | Size: 154 KiB |
|
Before Width: | Height: | Size: 186 KiB |
|
Before Width: | Height: | Size: 146 KiB |
|
Before Width: | Height: | Size: 134 KiB |
|
Before Width: | Height: | Size: 143 KiB |
|
Before Width: | Height: | Size: 32 KiB |
|
Before Width: | Height: | Size: 191 KiB |
|
Before Width: | Height: | Size: 184 KiB |
|
Before Width: | Height: | Size: 169 KiB |
|
Before Width: | Height: | Size: 224 KiB |
|
Before Width: | Height: | Size: 100 KiB |
|
Before Width: | Height: | Size: 186 KiB |
|
Before Width: | Height: | Size: 188 KiB |
|
Before Width: | Height: | Size: 145 KiB |
|
Before Width: | Height: | Size: 182 KiB |
|
Before Width: | Height: | Size: 127 KiB |
|
Before Width: | Height: | Size: 68 KiB |
|
Before Width: | Height: | Size: 160 KiB |
|
Before Width: | Height: | Size: 93 KiB |
|
Before Width: | Height: | Size: 193 KiB |
|
Before Width: | Height: | Size: 201 KiB |
|
Before Width: | Height: | Size: 116 KiB |
|
Before Width: | Height: | Size: 154 KiB |
|
Before Width: | Height: | Size: 170 KiB |
|
Before Width: | Height: | Size: 171 KiB |
|
Before Width: | Height: | Size: 186 KiB |
|
Before Width: | Height: | Size: 95 KiB |
|
Before Width: | Height: | Size: 128 KiB |
|
Before Width: | Height: | Size: 128 KiB |
|
Before Width: | Height: | Size: 114 KiB |
|
Before Width: | Height: | Size: 193 KiB |
|
Before Width: | Height: | Size: 152 KiB |
|
Before Width: | Height: | Size: 94 KiB |
|
Before Width: | Height: | Size: 122 KiB |
|
Before Width: | Height: | Size: 170 KiB |
|
Before Width: | Height: | Size: 193 KiB |
|
Before Width: | Height: | Size: 159 KiB |
|
Before Width: | Height: | Size: 202 KiB |
|
Before Width: | Height: | Size: 118 KiB |
|
Before Width: | Height: | Size: 172 KiB |
|
Before Width: | Height: | Size: 132 KiB |
|
Before Width: | Height: | Size: 164 KiB |
|
Before Width: | Height: | Size: 218 KiB |
|
Before Width: | Height: | Size: 161 KiB |
|
Before Width: | Height: | Size: 173 KiB |
|
Before Width: | Height: | Size: 144 KiB |
|
Before Width: | Height: | Size: 132 KiB |
|
Before Width: | Height: | Size: 155 KiB |
|
Before Width: | Height: | Size: 125 KiB |
|
Before Width: | Height: | Size: 186 KiB |
|
Before Width: | Height: | Size: 167 KiB |
|
Before Width: | Height: | Size: 170 KiB |
|
Before Width: | Height: | Size: 171 KiB |
|
Before Width: | Height: | Size: 119 KiB |
|
Before Width: | Height: | Size: 112 KiB |
|
Before Width: | Height: | Size: 156 KiB |
|
Before Width: | Height: | Size: 86 KiB |
|
Before Width: | Height: | Size: 119 KiB |
|
Before Width: | Height: | Size: 210 KiB |
|
Before Width: | Height: | Size: 28 KiB |
|
Before Width: | Height: | Size: 208 KiB |
|
Before Width: | Height: | Size: 13 KiB |
|
Before Width: | Height: | Size: 53 KiB |
|
Before Width: | Height: | Size: 148 KiB |
|
Before Width: | Height: | Size: 128 KiB |
|
Before Width: | Height: | Size: 68 KiB |
|
Before Width: | Height: | Size: 163 KiB |
|
Before Width: | Height: | Size: 131 KiB |
|
Before Width: | Height: | Size: 28 KiB |
|
Before Width: | Height: | Size: 111 KiB |
|
Before Width: | Height: | Size: 163 KiB |
|
Before Width: | Height: | Size: 192 KiB |
|
Before Width: | Height: | Size: 87 KiB |
|
Before Width: | Height: | Size: 16 KiB |
|
Before Width: | Height: | Size: 169 KiB |
|
Before Width: | Height: | Size: 129 KiB |
|
Before Width: | Height: | Size: 11 KiB |
|
Before Width: | Height: | Size: 6.3 KiB |
|
Before Width: | Height: | Size: 44 KiB |
|
Before Width: | Height: | Size: 67 KiB |
|
Before Width: | Height: | Size: 174 KiB |