mono/packages/media/tests/pdf/cli/variables.test.ts
2025-07-04 21:39:23 +02:00

53 lines
2.1 KiB
TypeScript

import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import { execSync } from 'node:child_process';
import { existsSync, rmSync, readdirSync, mkdirSync } from 'node:fs';
import * as path from 'node:path';
const packageRoot = process.cwd(); // Assumes test runs from package root
const inputPdf = path.join('tests', 'pdf', 'RS485-780.pdf');
const outputDir = path.join(packageRoot, 'tests', 'pdf', 'out', 'RS485-780');
const outputPattern = path.join(outputDir, '${SRC_NAME}-${PAGE}.jpg');
// Expected number of pages for RS485-780.pdf
const expectedPageCount = 29;
const expectedBaseName = 'RS485-780';
const expectedFormat = 'jpg'; // Default format
describe('CLI Integration Test - Variable Output Path', () => {
beforeAll(() => {
if (existsSync(outputDir)) {
rmSync(outputDir, { recursive: true, force: true });
}
mkdirSync(outputDir, { recursive: true });
});
afterAll(() => {
if (existsSync(outputDir)) {
// rmSync(outputDir, { recursive: true, force: true }); // Optional: clean up after tests
}
});
it('should create images in the correct directory with the correct filenames using variable substitution', () => {
const command = `node dist-in/main.js pdf2jpg --input "${inputPdf}" --output "${outputPattern}"`;
try {
execSync(command, { encoding: 'utf8', stdio: 'pipe' });
} catch (error: any) {
console.error('Command execution failed:', error.stderr || error.stdout || error.message);
expect.fail(`Command execution failed: ${error.message}`);
}
// 1. Check if the output directory exists
expect(existsSync(outputDir), `Output directory "${outputDir}" should exist`).toBe(true);
// 2. Check the number of files created
const files = readdirSync(outputDir);
expect(files.length, `Should have created ${expectedPageCount} files`).toBe(expectedPageCount);
// 3. Check filenames
for (let i = 1; i <= expectedPageCount; i++) {
const expectedFilename = `${expectedBaseName}-${i}.${expectedFormat}`;
expect(files).toContain(expectedFilename);
}
});
});