From 779706a3e7c97bb6ea12f27e412313b1d88d20ff Mon Sep 17 00:00:00 2001 From: yiling Date: Sat, 18 Jul 2026 15:07:28 +0800 Subject: [PATCH] test(core): add unit tests for generatePdfSprite and getPdfInfo in TranscodeService --- packages/core/src/transcode/transcode.test.ts | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/packages/core/src/transcode/transcode.test.ts b/packages/core/src/transcode/transcode.test.ts index 2657068f..e8305fc4 100644 --- a/packages/core/src/transcode/transcode.test.ts +++ b/packages/core/src/transcode/transcode.test.ts @@ -2,6 +2,7 @@ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest' import { transcodeService } from './transcode' import * as path from 'path' import * as child_process from 'child_process' +import * as fs from 'fs' import { WorkflowTask } from '@shumai/db' import { setupTestDbHooks } from '@shumai/db/test' import sharp from 'sharp' @@ -347,4 +348,84 @@ describe('TranscodeService', () => { expect.any(Function), ) }) + + it('should generate PDF sprite sheet and poster using pdftoppm, ffmpeg, and sharp', async () => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + ;(child_process.execFile as any).mockImplementation( + ( + cmd: string, + args: string[], + cb: (err: Error | null, res: { stdout: string; stderr: string }) => void, + ) => { + if (cmd === 'pdftoppm') { + // Create dummy page-1.png in temp directory (target output path prefix is args[args.length - 1]) + const pagePrefix = args[args.length - 1] + const pagePath = `${pagePrefix}-1.png` + fs.writeFileSync(pagePath, 'fake-png-data') + cb(null, { stdout: '', stderr: '' }) + } else if (cmd === 'pdfinfo') { + cb(null, { stdout: 'Title: Document\nPages: 15\nPage size: 612 x 792 pts', stderr: '' }) + } else if (cmd === 'ffmpeg') { + cb(null, { stdout: '', stderr: '' }) + } else { + cb(null, { stdout: '', stderr: '' }) + } + }, + ) + + const outputSprite = path.join(tempDir, 'sprite.webp') + const outputPoster = path.join(tempDir, 'poster.webp') + + const result = await transcodeService.generatePdfSprite('input.pdf', outputSprite, outputPoster) + + expect(result.pageCount).toBe(15) + expect(result.originalWidth).toBe(800) + expect(result.originalHeight).toBe(600) + + expect(child_process.execFile).toHaveBeenCalledWith( + 'pdftoppm', + expect.arrayContaining(['-png', '-f', '1', '-l', '100', 'input.pdf']), + expect.any(Function), + ) + expect(child_process.execFile).toHaveBeenCalledWith( + 'ffmpeg', + expect.arrayContaining(['-filter_complex', 'scale=w=480:h=-2,tile=10x10']), + expect.any(Function), + ) + expect(child_process.execFile).toHaveBeenCalledWith( + 'pdfinfo', + ['input.pdf'], + expect.any(Function), + ) + }) + + it('should extract PDF info metadata using pdftoppm and pdfinfo', async () => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + ;(child_process.execFile as any).mockImplementation( + ( + cmd: string, + args: string[], + cb: (err: Error | null, res: { stdout: string; stderr: string }) => void, + ) => { + if (cmd === 'pdftoppm') { + const pagePrefix = args[args.length - 1] + const pagePath = `${pagePrefix}-1.png` + fs.writeFileSync(pagePath, 'fake-png-data') + cb(null, { stdout: '', stderr: '' }) + } else if (cmd === 'pdfinfo') { + cb(null, { stdout: 'Pages: 42\n', stderr: '' }) + } else { + cb(null, { stdout: '', stderr: '' }) + } + }, + ) + + const info = await transcodeService.getPdfInfo('document.pdf') + + expect(info.mimeType).toBe('application/pdf') + expect(info.totalFrames).toBe(42) + expect(info.originalWidth).toBe(800) + expect(info.originalHeight).toBe(600) + expect(info.hasAudio).toBe(false) + }) })