import { expect } from "bun:test"; export const gatewayUrl = process.env.GATEWAY_URL ?? "http://caddy:8888"; export const adminUrl = process.env.ADMIN_URL ?? "http://caddy:2019"; export const sourceImageUrl = process.env.SOURCE_IMAGE_URL ?? "http://fixture:8080/image.png"; export function imageRequestUrl(width = 103) { return `${gatewayUrl}/unsafe/resize:fit:${width}:0:0/q:80/plain/${sourceImageUrl}`; } export async function waitForReady() { const deadline = Date.now() + 30_000; let lastError; while (Date.now() < deadline) { try { const response = await fetch(`${adminUrl}/souin-api/souin/`); if (response.ok) { await response.arrayBuffer(); return; } lastError = new Error(`admin responded with ${response.status}`); } catch (error) { lastError = error; } await new Promise((resolve) => setTimeout(resolve, 500)); } throw new Error(`Caddy admin API is not ready: ${lastError}`); } export async function purgeAll() { const response = await fetch(`${adminUrl}/souin-api/souin/flush`, { method: "PURGE", }); await response.arrayBuffer(); expect(response.status).toBe(204); } export async function listKeys() { const response = await fetch(`${adminUrl}/souin-api/souin/`); expect(response.status).toBe(200); return response.json(); } export async function requestImage(width = 103) { const response = await fetch(imageRequestUrl(width)); await response.arrayBuffer(); return { cacheStatus: response.headers.get("cache-status") ?? "", status: response.status, }; } export function expectStored(cacheStatus) { expect(cacheStatus).toContain("fwd=uri-miss"); expect(cacheStatus).toContain("stored"); expect(cacheStatus).not.toContain("NUTS-INSERTION-ERROR"); } export function expectHit(cacheStatus, storage) { expect(cacheStatus).toContain("hit"); expect(cacheStatus).toContain(`detail=${storage}`); }