import { describe, it, expect, beforeAll, afterAll, beforeEach } from 'vitest'; import request from 'supertest'; import fs from 'node:fs/promises'; import path from 'node:path'; import os from 'node:os'; import { Document, Paragraph, TextRun, Packer } from 'docx'; import { createApp } from '../../src/app.js'; import { getPrismaClient, closePrismaClient } from '../../src/db.js'; import { loadConfig } from '../../src/config.js'; import { ensureStorageDirs } from '../../src/storage.js'; import { processPendingJobs } from '../../src/worker.js'; let app; let prisma; let config; beforeAll(async () => { config = { ...loadConfig(), storageDir: await fs.mkdtemp(path.join(os.tmpdir(), 'converter-e2e-')) }; await ensureStorageDirs(config); prisma = getPrismaClient(config); app = createApp(config, prisma); }); afterAll(async () => { await closePrismaClient(); await fs.rm(config.storageDir, { recursive: true, force: true }); }); beforeEach(async () => { await prisma.conversionJob.deleteMany(); }); async function waitForDone(id, maxAttempts = 20) { for (let attempt = 0; attempt < maxAttempts; attempt += 1) { await processPendingJobs(prisma, config); const response = await request(app).get(`/api/jobs/${id}`); if (response.body.status === 'done' || response.body.status === 'failed') { return response.body; } } throw new Error(`Job ${id} did not finish after ${maxAttempts} worker passes`); } describe('end-to-end: image family', () => { it('uploads a PNG, converts it to WebP, and downloads the result', async () => { const fixturePath = path.join(import.meta.dirname, '..', 'fixtures', 'sample.png'); const uploadResponse = await request(app) .post('/api/jobs') .field('targetFormats', JSON.stringify(['webp'])) .attach('files', fixturePath, 'photo.png'); const { id } = uploadResponse.body.jobs[0]; const finalStatus = await waitForDone(id); expect(finalStatus.status).toBe('done'); const downloadResponse = await request(app).get(`/api/jobs/${id}/download`); expect(downloadResponse.status).toBe(200); expect(downloadResponse.headers['content-type']).toBe('image/webp'); expect(downloadResponse.headers['content-disposition']).toContain('photo.webp'); }, 20000); }); describe('end-to-end: document family', () => { it('uploads a DOCX, converts it to PDF, and downloads the result', async () => { const doc = new Document({ sections: [{ children: [new Paragraph({ children: [new TextRun('End to end fixture text')] })] }], }); const buffer = await Packer.toBuffer(doc); const docxPath = path.join(config.storageDir, 'e2e-fixture.docx'); await fs.writeFile(docxPath, buffer); const uploadResponse = await request(app) .post('/api/jobs') .field('targetFormats', JSON.stringify(['pdf'])) .attach('files', docxPath, 'report.docx'); const { id } = uploadResponse.body.jobs[0]; const finalStatus = await waitForDone(id); expect(finalStatus.status).toBe('done'); const downloadResponse = await request(app).get(`/api/jobs/${id}/download`); expect(downloadResponse.status).toBe(200); expect(downloadResponse.headers['content-type']).toBe('application/pdf'); expect(downloadResponse.headers['content-disposition']).toContain('report.pdf'); await fs.unlink(docxPath); }, 20000); });