import { afterEach, describe, expect, it } from "bun:test"; import { existsSync, rmSync } from "node:fs"; import { fetchImageFromUrl } from "../../src/server/services/image.service.ts"; const TEST_UPLOADS_DIR = "test-uploads"; describe("Image Service", () => { afterEach(() => { if (existsSync(TEST_UPLOADS_DIR)) { rmSync(TEST_UPLOADS_DIR, { recursive: true }); } }); describe("fetchImageFromUrl", () => { it("fetches a valid image URL and saves to disk", async () => { const imageUrl = "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"; const result = await fetchImageFromUrl(imageUrl, TEST_UPLOADS_DIR); expect(result.filename).toMatch(/^\d+-[\w-]+\.png$/); expect(result.sourceUrl).toBe(imageUrl); const filePath = `${TEST_UPLOADS_DIR}/${result.filename}`; expect(existsSync(filePath)).toBe(true); }); it("rejects non-image content type", async () => { await expect( fetchImageFromUrl("https://example.com/", TEST_UPLOADS_DIR), ).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"); }); it("rejects non-HTTP protocols", async () => { await expect( fetchImageFromUrl("ftp://example.com/image.png", TEST_UPLOADS_DIR), ).rejects.toThrow("URL must use HTTP or HTTPS"); }); }); });