- storage.service.ts: use dynamic import() inside each function so the current @aws-sdk mock is always picked up regardless of module load order - images.test.ts + image.service.test.ts: replace module-level storage.service mock with @aws-sdk/client-s3 mock to avoid contaminating storage.service.test.ts - routes/auth.test.ts: remove unnecessary oauth.service mock (no test uses verifyAccessToken) which was contaminating oauth.service.test.ts - middleware/auth.test.ts: complete oauth.service mock shape with all exports All 464 tests now pass in a single bun test run. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
132 lines
3.5 KiB
TypeScript
132 lines
3.5 KiB
TypeScript
import {
|
|
afterAll,
|
|
beforeAll,
|
|
beforeEach,
|
|
describe,
|
|
expect,
|
|
it,
|
|
mock,
|
|
} from "bun:test";
|
|
import type { Server } from "bun";
|
|
|
|
// Mock @aws-sdk/* so storage.service (used by image.service) doesn't make real
|
|
// S3 calls — avoids contaminating storage.service.test.ts with a module-level mock.
|
|
const mockSend = mock(() => Promise.resolve({}));
|
|
mock.module("@aws-sdk/client-s3", () => ({
|
|
S3Client: class MockS3Client {
|
|
send = mockSend;
|
|
},
|
|
PutObjectCommand: class {
|
|
input: Record<string, unknown>;
|
|
constructor(input: Record<string, unknown>) {
|
|
this.input = input;
|
|
}
|
|
},
|
|
DeleteObjectCommand: class {
|
|
input: Record<string, unknown>;
|
|
constructor(input: Record<string, unknown>) {
|
|
this.input = input;
|
|
}
|
|
},
|
|
GetObjectCommand: class {
|
|
input: Record<string, unknown>;
|
|
constructor(input: Record<string, unknown>) {
|
|
this.input = input;
|
|
}
|
|
},
|
|
}));
|
|
mock.module("@aws-sdk/s3-request-presigner", () => ({
|
|
getSignedUrl: mock(() => Promise.resolve("https://example.com/test.png")),
|
|
}));
|
|
|
|
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,
|
|
]);
|
|
|
|
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", () => {
|
|
beforeEach(() => {
|
|
mockSend.mockClear();
|
|
});
|
|
|
|
describe("fetchImageFromUrl", () => {
|
|
it("fetches a valid image URL and uploads to storage", async () => {
|
|
const imageUrl = `${baseUrl}/image.png`;
|
|
const result = await fetchImageFromUrl(imageUrl);
|
|
|
|
expect(result.filename).toMatch(/^\d+-[\w-]+\.png$/);
|
|
expect(result.sourceUrl).toBe(imageUrl);
|
|
|
|
// Verify S3 PutObjectCommand was issued with correct args
|
|
expect(mockSend).toHaveBeenCalledTimes(1);
|
|
const command = mockSend.mock.calls[0][0] as {
|
|
input: Record<string, unknown>;
|
|
};
|
|
expect(command.input.Body).toBeInstanceOf(Buffer);
|
|
expect(command.input.Key).toBe(result.filename);
|
|
expect(command.input.ContentType).toBe("image/png");
|
|
});
|
|
|
|
it("rejects non-image content type", async () => {
|
|
await expect(fetchImageFromUrl(`${baseUrl}/page.html`)).rejects.toThrow(
|
|
"Invalid content type",
|
|
);
|
|
});
|
|
|
|
it("rejects invalid URL format", async () => {
|
|
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"),
|
|
).rejects.toThrow("URL must use HTTP or HTTPS");
|
|
});
|
|
|
|
it("rejects 404 responses", async () => {
|
|
await expect(fetchImageFromUrl(`${baseUrl}/missing.jpg`)).rejects.toThrow(
|
|
"HTTP 404",
|
|
);
|
|
});
|
|
});
|
|
});
|