feat(17-02): refactor image service and routes to use S3 storage service

- Replace Bun.write/mkdir with uploadImage() from storage.service
- Remove uploadsDir parameter from fetchImageFromUrl
- Update tests to mock storage service instead of checking filesystem
This commit is contained in:
2026-04-05 12:20:31 +02:00
parent 544dd5bcd9
commit 5ce3f92a78
4 changed files with 117 additions and 39 deletions

View File

@@ -1,18 +1,32 @@
import { afterAll, afterEach, beforeAll, describe, expect, it } from "bun:test";
import { existsSync, rmSync } from "node:fs";
import {
afterAll,
beforeAll,
beforeEach,
describe,
expect,
it,
mock,
} from "bun:test";
import type { Server } from "bun";
import { fetchImageFromUrl } from "../../src/server/services/image.service.ts";
const TEST_UPLOADS_DIR = "test-uploads";
// Mock the storage service before importing image service
const mockUploadImage = mock(() => Promise.resolve());
mock.module("../../src/server/services/storage.service", () => ({
uploadImage: mockUploadImage,
}));
const { fetchImageFromUrl } = await import(
"../../src/server/services/image.service.ts"
);
// 1x1 transparent PNG (smallest valid PNG)
const TINY_PNG = new Uint8Array([
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49,
0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06,
0x00, 0x00, 0x00, 0x1f, 0x15, 0xc4, 0x89, 0x00, 0x00, 0x00, 0x0a, 0x49, 0x44,
0x41, 0x54, 0x78, 0x9c, 0x62, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01, 0xe2, 0x21,
0xbc, 0x33, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60,
0x82,
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
0x08, 0x06, 0x00, 0x00, 0x00, 0x1f, 0x15, 0xc4, 0x89, 0x00, 0x00, 0x00,
0x0a, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x62, 0x00, 0x00, 0x00, 0x02,
0x00, 0x01, 0xe2, 0x21, 0xbc, 0x33, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45,
0x4e, 0x44, 0xae, 0x42, 0x60, 0x82,
]);
let server: Server;
@@ -44,45 +58,48 @@ afterAll(() => {
});
describe("Image Service", () => {
afterEach(() => {
if (existsSync(TEST_UPLOADS_DIR)) {
rmSync(TEST_UPLOADS_DIR, { recursive: true });
}
beforeEach(() => {
mockUploadImage.mockClear();
});
describe("fetchImageFromUrl", () => {
it("fetches a valid image URL and saves to disk", async () => {
it("fetches a valid image URL and uploads to storage", async () => {
const imageUrl = `${baseUrl}/image.png`;
const result = await fetchImageFromUrl(imageUrl, TEST_UPLOADS_DIR);
const result = await fetchImageFromUrl(imageUrl);
expect(result.filename).toMatch(/^\d+-[\w-]+\.png$/);
expect(result.sourceUrl).toBe(imageUrl);
const filePath = `${TEST_UPLOADS_DIR}/${result.filename}`;
expect(existsSync(filePath)).toBe(true);
// Verify uploadImage was called with correct args
expect(mockUploadImage).toHaveBeenCalledTimes(1);
const [buffer, filename, contentType] =
mockUploadImage.mock.calls[0] as unknown[];
expect(buffer).toBeInstanceOf(Buffer);
expect(filename).toBe(result.filename);
expect(contentType).toBe("image/png");
});
it("rejects non-image content type", async () => {
await expect(
fetchImageFromUrl(`${baseUrl}/page.html`, TEST_UPLOADS_DIR),
fetchImageFromUrl(`${baseUrl}/page.html`),
).rejects.toThrow("Invalid content type");
});
it("rejects invalid URL format", async () => {
await expect(
fetchImageFromUrl("not-a-url", TEST_UPLOADS_DIR),
).rejects.toThrow("Invalid URL format");
await expect(fetchImageFromUrl("not-a-url")).rejects.toThrow(
"Invalid URL format",
);
});
it("rejects non-HTTP protocols", async () => {
await expect(
fetchImageFromUrl("ftp://example.com/image.png", TEST_UPLOADS_DIR),
fetchImageFromUrl("ftp://example.com/image.png"),
).rejects.toThrow("URL must use HTTP or HTTPS");
});
it("rejects 404 responses", async () => {
await expect(
fetchImageFromUrl(`${baseUrl}/missing.jpg`, TEST_UPLOADS_DIR),
fetchImageFromUrl(`${baseUrl}/missing.jpg`),
).rejects.toThrow("HTTP 404");
});
});