Files
kochwas/tests/integration/image-downloader.test.ts
2026-04-17 15:09:31 +02:00

65 lines
2.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { createServer, type Server } from 'node:http';
import type { AddressInfo } from 'node:net';
import { mkdtemp, readFile, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { downloadImage } from '../../src/lib/server/images/image-downloader';
// 1×1 PNG (transparent)
const PNG_BYTES = Buffer.from(
'89504e470d0a1a0a0000000d49484452000000010000000108060000001f15c489' +
'0000000d49444154789c6300010000000500010d0a2db40000000049454e44ae426082',
'hex'
);
let server: Server;
let baseUrl: string;
let dir: string;
beforeEach(async () => {
server = createServer();
await new Promise<void>((r) => server.listen(0, '127.0.0.1', r));
const addr = server.address() as AddressInfo;
baseUrl = `http://127.0.0.1:${addr.port}`;
dir = await mkdtemp(join(tmpdir(), 'kochwas-img-'));
});
afterEach(async () => {
await new Promise<void>((r) => server.close(() => r()));
await rm(dir, { recursive: true, force: true });
});
describe('downloadImage', () => {
it('downloads and stores a PNG with sha256 filename', async () => {
server.on('request', (_req, res) => {
res.writeHead(200, { 'content-type': 'image/png' });
res.end(PNG_BYTES);
});
const fname = await downloadImage(`${baseUrl}/img.png`, dir);
expect(fname).not.toBeNull();
expect(fname!.endsWith('.png')).toBe(true);
const saved = await readFile(join(dir, fname!));
expect(saved.equals(PNG_BYTES)).toBe(true);
});
it('is idempotent — returns same filename, does not rewrite', async () => {
server.on('request', (_req, res) => {
res.writeHead(200, { 'content-type': 'image/png' });
res.end(PNG_BYTES);
});
const first = await downloadImage(`${baseUrl}/img.png`, dir);
const second = await downloadImage(`${baseUrl}/img.png`, dir);
expect(second).toBe(first);
});
it('returns null on 404', async () => {
server.on('request', (_req, res) => {
res.writeHead(404);
res.end();
});
const fname = await downloadImage(`${baseUrl}/missing.png`, dir);
expect(fname).toBeNull();
});
});