158 lines
5.3 KiB
JavaScript
158 lines
5.3 KiB
JavaScript
import { describe, it, expect, beforeAll, afterAll, beforeEach } from 'vitest';
|
|
import fs from 'node:fs/promises';
|
|
import path from 'node:path';
|
|
import os from 'node:os';
|
|
import sharp from 'sharp';
|
|
import { getPool, closePool } from '../src/db.js';
|
|
import { loadConfig } from '../src/config.js';
|
|
import { ensureStorageDirs, uploadPath, outputPath } from '../src/storage.js';
|
|
import { createJob, getJobByUuid, getJobErrorLog } from '../src/jobs/jobRepository.js';
|
|
import { registerImageConverters } from '../src/converters/image.js';
|
|
import { processPendingJobs } from '../src/worker.js';
|
|
|
|
let pool;
|
|
let config;
|
|
|
|
beforeAll(async () => {
|
|
registerImageConverters();
|
|
config = { ...loadConfig(), storageDir: await fs.mkdtemp(path.join(os.tmpdir(), 'converter-worker-')) };
|
|
await ensureStorageDirs(config);
|
|
pool = getPool(config);
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await closePool();
|
|
await fs.rm(config.storageDir, { recursive: true, force: true });
|
|
});
|
|
|
|
beforeEach(async () => {
|
|
await pool.query('DELETE FROM conversion_jobs');
|
|
});
|
|
|
|
async function createPendingImageJob(uuid, sourceFormat, targetFormat) {
|
|
const fixturePath = path.join(import.meta.dirname, 'fixtures', 'sample.png');
|
|
const inputFilePath = uploadPath(config, uuid, sourceFormat);
|
|
await fs.copyFile(fixturePath, inputFilePath);
|
|
const { size: inputSizeBytes } = await fs.stat(inputFilePath);
|
|
|
|
await createJob(pool, {
|
|
uuid,
|
|
family: 'image',
|
|
sourceFormat,
|
|
targetFormat,
|
|
originalFilename: `photo.${sourceFormat}`,
|
|
inputPath: `${uuid}.${sourceFormat}`,
|
|
inputMimeType: 'image/png',
|
|
inputSizeBytes,
|
|
expiresAt: new Date(Date.now() + 3600 * 1000),
|
|
});
|
|
}
|
|
|
|
describe('processPendingJobs', () => {
|
|
it('converts a pending image job to done', async () => {
|
|
const uuid = '99999999-9999-4999-8999-999999999999';
|
|
await createPendingImageJob(uuid, 'png', 'webp');
|
|
|
|
const processedCount = await processPendingJobs(pool, config);
|
|
|
|
expect(processedCount).toBe(1);
|
|
const job = await getJobByUuid(pool, uuid);
|
|
expect(job.status).toBe('done');
|
|
expect(job.outputPath).toBe(`${uuid}.webp`);
|
|
expect(job.outputMimeType).toBe('image/webp');
|
|
expect(job.outputSizeBytes).toBeGreaterThan(0);
|
|
expect(Number(job.conversionDurationSeconds)).toBeGreaterThanOrEqual(0);
|
|
|
|
const stat = await fs.stat(outputPath(config, uuid, 'webp'));
|
|
expect(stat.size).toBeGreaterThan(0);
|
|
});
|
|
|
|
it('passes the job quality through to the converter, shrinking output for a low quality value', async () => {
|
|
const lowUuid = 'eeeeeeee-eeee-4eee-8eee-eeeeeeeeeeee';
|
|
const defaultUuid = 'ffffffff-ffff-4fff-8fff-ffffffffffff';
|
|
|
|
const noisyBuffer = await sharp({
|
|
create: {
|
|
width: 256,
|
|
height: 256,
|
|
channels: 3,
|
|
noise: { type: 'gaussian', mean: 128, sigma: 40 },
|
|
},
|
|
})
|
|
.png()
|
|
.toBuffer();
|
|
|
|
for (const uuid of [lowUuid, defaultUuid]) {
|
|
await fs.writeFile(uploadPath(config, uuid, 'png'), noisyBuffer);
|
|
}
|
|
|
|
await createJob(pool, {
|
|
uuid: lowUuid,
|
|
family: 'image',
|
|
sourceFormat: 'png',
|
|
targetFormat: 'jpg',
|
|
originalFilename: 'noisy.png',
|
|
inputPath: `${lowUuid}.png`,
|
|
inputMimeType: 'image/png',
|
|
inputSizeBytes: noisyBuffer.length,
|
|
expiresAt: new Date(Date.now() + 3600 * 1000),
|
|
quality: 5,
|
|
});
|
|
await createJob(pool, {
|
|
uuid: defaultUuid,
|
|
family: 'image',
|
|
sourceFormat: 'png',
|
|
targetFormat: 'jpg',
|
|
originalFilename: 'noisy.png',
|
|
inputPath: `${defaultUuid}.png`,
|
|
inputMimeType: 'image/png',
|
|
inputSizeBytes: noisyBuffer.length,
|
|
expiresAt: new Date(Date.now() + 3600 * 1000),
|
|
});
|
|
|
|
await processPendingJobs(pool, { ...config, workerConcurrency: 2 });
|
|
|
|
const lowJob = await getJobByUuid(pool, lowUuid);
|
|
const defaultJob = await getJobByUuid(pool, defaultUuid);
|
|
expect(lowJob.status).toBe('done');
|
|
expect(defaultJob.status).toBe('done');
|
|
expect(lowJob.outputSizeBytes).toBeLessThan(defaultJob.outputSizeBytes);
|
|
});
|
|
|
|
it('marks a job failed with a safe message and a detailed log when the converter throws', async () => {
|
|
const uuid = 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa';
|
|
await createJob(pool, {
|
|
uuid,
|
|
family: 'image',
|
|
sourceFormat: 'png',
|
|
targetFormat: 'webp',
|
|
originalFilename: 'missing.png',
|
|
inputPath: `${uuid}.png`,
|
|
inputMimeType: 'image/png',
|
|
inputSizeBytes: 11,
|
|
expiresAt: new Date(Date.now() + 3600 * 1000),
|
|
});
|
|
// Note: input file is intentionally never written, so sharp will throw ENOENT.
|
|
|
|
await processPendingJobs(pool, config);
|
|
|
|
const job = await getJobByUuid(pool, uuid);
|
|
expect(job.status).toBe('failed');
|
|
expect(job.errorMessage).toBe('Conversion failed, please try again.');
|
|
|
|
const errorLog = await getJobErrorLog(pool, job.id);
|
|
expect(errorLog).toMatch(/input file is missing/i);
|
|
});
|
|
|
|
it('only picks up as many jobs as workerConcurrency allows', async () => {
|
|
await createPendingImageJob('bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb', 'png', 'webp');
|
|
await createPendingImageJob('cccccccc-cccc-4ccc-8ccc-cccccccccccc', 'png', 'webp');
|
|
await createPendingImageJob('dddddddd-dddd-4ddd-8ddd-dddddddddddd', 'png', 'webp');
|
|
|
|
const limitedConfig = { ...config, workerConcurrency: 2 };
|
|
const processedCount = await processPendingJobs(pool, limitedConfig);
|
|
|
|
expect(processedCount).toBe(2);
|
|
});
|
|
});
|