This commit is contained in:
lovebird 2025-04-21 11:00:29 +02:00
parent 05105804c9
commit 20693330dd
187 changed files with 325 additions and 13 deletions

View File

@ -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": [

View File

@ -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 ...
});
});

Binary file not shown.

Before

Width:  |  Height:  |  Size: 244 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 161 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 159 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 144 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 133 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 163 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 143 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 94 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 149 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 158 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 144 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 158 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 154 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 186 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 146 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 134 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 143 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 191 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 184 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 169 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 224 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 100 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 186 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 188 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 145 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 182 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 127 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 68 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 160 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 93 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 193 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 201 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 116 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 154 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 170 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 171 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 186 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 95 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 128 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 128 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 114 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 193 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 152 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 94 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 122 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 170 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 193 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 159 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 202 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 118 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 172 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 132 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 164 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 218 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 161 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 173 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 144 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 132 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 155 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 125 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 186 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 167 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 170 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 171 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 119 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 112 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 156 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 86 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 119 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 210 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 208 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 148 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 128 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 68 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 163 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 131 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 111 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 163 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 192 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 87 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 169 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 129 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 44 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 67 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 174 KiB

Some files were not shown because too many files have changed in this diff Show More