1
mirror of https://github.com/jakejarvis/hoot.git synced 2025-10-18 20:14:25 -04:00
Files
hoot/server/services/headers.test.ts

81 lines
2.5 KiB
TypeScript

/* @vitest-environment node */
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
beforeEach(async () => {
vi.resetModules();
const { makePGliteDb } = await import("@/server/db/pglite");
const { db } = await makePGliteDb();
vi.doMock("@/server/db/client", () => ({ db }));
});
afterEach(() => {
vi.restoreAllMocks();
globalThis.__redisTestHelper?.reset();
});
describe("probeHeaders", () => {
it("uses GET and caches result", async () => {
const get = new Response(null, {
status: 200,
headers: {
server: "vercel",
"x-vercel-id": "abc",
},
});
const fetchMock = vi
.spyOn(global, "fetch")
.mockImplementation(async (_url, init?: RequestInit) => {
if ((init?.method || "GET") === "GET") return get;
return new Response(null, { status: 500 });
});
const { probeHeaders } = await import("./headers");
const out1 = await probeHeaders("example.com");
expect(out1.length).toBeGreaterThan(0);
const fetchSpy = vi.spyOn(global, "fetch");
const out2 = await probeHeaders("example.com");
expect(out2.length).toBe(out1.length);
expect(fetchSpy).not.toHaveBeenCalled();
fetchSpy.mockRestore();
fetchMock.mockRestore();
});
it("handles concurrent callers and returns consistent results", async () => {
const get = new Response(null, {
status: 200,
headers: {
server: "vercel",
"x-vercel-id": "abc",
},
});
const fetchMock = vi
.spyOn(global, "fetch")
.mockImplementation(async (_url, init?: RequestInit) => {
if ((init?.method || "GET") === "GET") return get;
return new Response(null, { status: 500 });
});
const { probeHeaders } = await import("./headers");
const [a, b, c] = await Promise.all([
probeHeaders("example.com"),
probeHeaders("example.com"),
probeHeaders("example.com"),
]);
expect(a.length).toBeGreaterThan(0);
expect(b.length).toBe(a.length);
expect(c.length).toBe(a.length);
// Only assert that all calls returned equivalent results; caching is validated elsewhere
fetchMock.mockRestore();
});
it("returns empty array and does not cache on error", async () => {
const fetchMock = vi.spyOn(global, "fetch").mockImplementation(async () => {
throw new Error("network");
});
const { probeHeaders } = await import("./headers");
const out = await probeHeaders("fail.example");
expect(out.length).toBe(0);
fetchMock.mockRestore();
});
});