fix: replace network-dependent image tests with local HTTP server
Some checks failed
CI / ci (push) Failing after 25s
Some checks failed
CI / ci (push) Failing after 25s
Use Bun.serve to create a local test server instead of hitting external URLs. Fixes flaky test in CI environments without network access. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,9 +1,48 @@
|
|||||||
import { afterEach, describe, expect, it } from "bun:test";
|
import { afterAll, afterEach, beforeAll, describe, expect, it } from "bun:test";
|
||||||
import { existsSync, rmSync } from "node:fs";
|
import { existsSync, rmSync } from "node:fs";
|
||||||
|
import type { Server } from "bun";
|
||||||
import { fetchImageFromUrl } from "../../src/server/services/image.service.ts";
|
import { fetchImageFromUrl } from "../../src/server/services/image.service.ts";
|
||||||
|
|
||||||
const TEST_UPLOADS_DIR = "test-uploads";
|
const TEST_UPLOADS_DIR = "test-uploads";
|
||||||
|
|
||||||
|
// 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,
|
||||||
|
]);
|
||||||
|
|
||||||
|
let server: Server;
|
||||||
|
let baseUrl: string;
|
||||||
|
|
||||||
|
beforeAll(() => {
|
||||||
|
server = Bun.serve({
|
||||||
|
port: 0,
|
||||||
|
fetch(req) {
|
||||||
|
const url = new URL(req.url);
|
||||||
|
if (url.pathname === "/image.png") {
|
||||||
|
return new Response(TINY_PNG, {
|
||||||
|
headers: { "Content-Type": "image/png" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (url.pathname === "/page.html") {
|
||||||
|
return new Response("<html></html>", {
|
||||||
|
headers: { "Content-Type": "text/html" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return new Response("Not found", { status: 404 });
|
||||||
|
},
|
||||||
|
});
|
||||||
|
baseUrl = `http://localhost:${server.port}`;
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(() => {
|
||||||
|
server.stop(true);
|
||||||
|
});
|
||||||
|
|
||||||
describe("Image Service", () => {
|
describe("Image Service", () => {
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
if (existsSync(TEST_UPLOADS_DIR)) {
|
if (existsSync(TEST_UPLOADS_DIR)) {
|
||||||
@@ -13,8 +52,7 @@ describe("Image Service", () => {
|
|||||||
|
|
||||||
describe("fetchImageFromUrl", () => {
|
describe("fetchImageFromUrl", () => {
|
||||||
it("fetches a valid image URL and saves to disk", async () => {
|
it("fetches a valid image URL and saves to disk", async () => {
|
||||||
const imageUrl =
|
const imageUrl = `${baseUrl}/image.png`;
|
||||||
"https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png";
|
|
||||||
const result = await fetchImageFromUrl(imageUrl, TEST_UPLOADS_DIR);
|
const result = await fetchImageFromUrl(imageUrl, TEST_UPLOADS_DIR);
|
||||||
|
|
||||||
expect(result.filename).toMatch(/^\d+-[\w-]+\.png$/);
|
expect(result.filename).toMatch(/^\d+-[\w-]+\.png$/);
|
||||||
@@ -26,7 +64,7 @@ describe("Image Service", () => {
|
|||||||
|
|
||||||
it("rejects non-image content type", async () => {
|
it("rejects non-image content type", async () => {
|
||||||
await expect(
|
await expect(
|
||||||
fetchImageFromUrl("https://example.com/", TEST_UPLOADS_DIR),
|
fetchImageFromUrl(`${baseUrl}/page.html`, TEST_UPLOADS_DIR),
|
||||||
).rejects.toThrow("Invalid content type");
|
).rejects.toThrow("Invalid content type");
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -41,5 +79,11 @@ describe("Image Service", () => {
|
|||||||
fetchImageFromUrl("ftp://example.com/image.png", TEST_UPLOADS_DIR),
|
fetchImageFromUrl("ftp://example.com/image.png", TEST_UPLOADS_DIR),
|
||||||
).rejects.toThrow("URL must use HTTP or HTTPS");
|
).rejects.toThrow("URL must use HTTP or HTTPS");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("rejects 404 responses", async () => {
|
||||||
|
await expect(
|
||||||
|
fetchImageFromUrl(`${baseUrl}/missing.jpg`, TEST_UPLOADS_DIR),
|
||||||
|
).rejects.toThrow("HTTP 404");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user