Files
GearBox/tests/routes/images.test.ts
Jean-Luc Makiola 5f63e6f75d
Some checks failed
CI / ci (push) Failing after 1m35s
CI / e2e (push) Has been skipped
CI / deploy (push) Has been skipped
fix: resolve Bun mock isolation contamination across test files
- 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>
2026-04-19 16:19:39 +02:00

115 lines
3.2 KiB
TypeScript

import { beforeEach, describe, expect, mock, test } from "bun:test";
import { Hono } from "hono";
import { createTestDb } from "../helpers/db.ts";
// Mock @aws-sdk/* to prevent real S3 connections — avoids module-level mock
// contamination of storage.service.test.ts (storage.service uses dynamic imports).
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 { imageRoutes } = await import("../../src/server/routes/images.ts");
let app: Hono;
beforeEach(async () => {
const { db, userId } = await createTestDb();
app = new Hono();
app.use("*", async (c, next) => {
c.set("db", db);
c.set("userId", userId);
await next();
});
app.route("/api/images", imageRoutes);
mockSend.mockClear();
});
describe("POST /api/images/from-url", () => {
test("returns 400 for missing URL", async () => {
const res = await app.request("/api/images/from-url", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({}),
});
expect(res.status).toBe(400);
});
test("returns 400 for invalid URL", async () => {
const res = await app.request("/api/images/from-url", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ url: "not-a-url" }),
});
expect(res.status).toBe(400);
});
});
describe("POST /api/images", () => {
test("returns 400 when no image file provided", async () => {
const formData = new FormData();
const res = await app.request("/api/images", {
method: "POST",
body: formData,
});
expect(res.status).toBe(400);
const json = await res.json();
expect(json.error).toBe("No image file provided");
});
test("returns 400 for invalid file type", async () => {
const formData = new FormData();
formData.append(
"image",
new Blob(["test"], { type: "text/plain" }),
"test.txt",
);
const res = await app.request("/api/images", {
method: "POST",
body: formData,
});
expect(res.status).toBe(400);
const json = await res.json();
expect(json.error).toContain("Invalid file type");
});
test("uploads valid image and calls storage service", async () => {
const formData = new FormData();
const pngBlob = new Blob([new Uint8Array([0x89, 0x50, 0x4e, 0x47])], {
type: "image/png",
});
formData.append("image", pngBlob, "test.png");
const res = await app.request("/api/images", {
method: "POST",
body: formData,
});
expect(res.status).toBe(201);
const json = await res.json();
expect(json.filename).toMatch(/^\d+-[\w-]+\.png$/);
expect(mockSend).toHaveBeenCalledTimes(1);
});
});