Files
convert/test/api/endToEnd.test.js
T

90 lines
3.3 KiB
JavaScript

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 { getPool, closePool } 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 pool;
let config;
beforeAll(async () => {
config = { ...loadConfig(), storageDir: await fs.mkdtemp(path.join(os.tmpdir(), 'converter-e2e-')) };
await ensureStorageDirs(config);
pool = getPool(config);
app = createApp(config, pool);
});
afterAll(async () => {
await closePool();
await fs.rm(config.storageDir, { recursive: true, force: true });
});
beforeEach(async () => {
await pool.query('DELETE FROM conversion_jobs');
});
async function waitForDone(id, maxAttempts = 20) {
for (let attempt = 0; attempt < maxAttempts; attempt += 1) {
await processPendingJobs(pool, 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.png');
}, 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.docx');
await fs.unlink(docxPath);
}, 20000);
});