53 lines
1.7 KiB
JavaScript
53 lines
1.7 KiB
JavaScript
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
import fs from 'node:fs/promises';
|
|
import path from 'node:path';
|
|
import os from 'node:os';
|
|
import { ensureStorageDirs, uploadPath, outputPath, deleteIfExists } from '../src/storage.js';
|
|
|
|
let tmpDir;
|
|
let config;
|
|
|
|
beforeEach(async () => {
|
|
tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'converter-storage-'));
|
|
config = { storageDir: tmpDir };
|
|
});
|
|
|
|
afterEach(async () => {
|
|
await fs.rm(tmpDir, { recursive: true, force: true });
|
|
});
|
|
|
|
describe('storage', () => {
|
|
it('creates uploads and outputs directories', async () => {
|
|
await ensureStorageDirs(config);
|
|
|
|
const uploadsStat = await fs.stat(path.join(tmpDir, 'uploads'));
|
|
const outputsStat = await fs.stat(path.join(tmpDir, 'outputs'));
|
|
|
|
expect(uploadsStat.isDirectory()).toBe(true);
|
|
expect(outputsStat.isDirectory()).toBe(true);
|
|
});
|
|
|
|
it('builds upload and output paths as <uuid>.<ext>', () => {
|
|
const id = '11111111-1111-4111-8111-111111111111';
|
|
|
|
expect(uploadPath(config, id, 'png')).toBe(path.join(tmpDir, 'uploads', `${id}.png`));
|
|
expect(outputPath(config, id, 'pdf')).toBe(path.join(tmpDir, 'outputs', `${id}.pdf`));
|
|
});
|
|
|
|
it('deletes an existing file', async () => {
|
|
await ensureStorageDirs(config);
|
|
const filePath = uploadPath(config, 'file-to-delete', 'txt');
|
|
await fs.writeFile(filePath, 'content');
|
|
|
|
await deleteIfExists(filePath);
|
|
|
|
await expect(fs.stat(filePath)).rejects.toThrow();
|
|
});
|
|
|
|
it('does not throw when deleting a missing file', async () => {
|
|
const filePath = uploadPath(config, 'does-not-exist', 'txt');
|
|
|
|
await expect(deleteIfExists(filePath)).resolves.toBeUndefined();
|
|
});
|
|
});
|