import { expect, test } from "bun:test"; import { PlatformAuth, RateLimiter } from "./auth"; import { TOKEN, NEW_HOST_TOKEN } from "./testing/fixtures"; const request = (token: string) => new Request("https://pod.place/v1/resolvers", { headers: { authorization: `Bearer ${token}` } }); test("platform credentials require no publisher IDs, show lists, or origin headers", () => { const auth = new PlatformAuth([{ platform: "maincasts.com", token: TOKEN }]); expect(auth.authenticate(request(TOKEN))).toEqual({ platform: "maincasts.com" }); expect(() => auth.authenticate(request(NEW_HOST_TOKEN))).toThrow(); }); test("multiple tokens can identify the same platform during key rotation", () => { const auth = new PlatformAuth([ { platform: "maincasts.com", token: TOKEN }, { platform: "maincasts.com", token: NEW_HOST_TOKEN }, ]); expect(auth.authenticate(request(TOKEN)).platform).toBe(auth.authenticate(request(NEW_HOST_TOKEN)).platform); }); test("duplicate, malformed, and missing platform credentials fail closed", () => { expect(() => new PlatformAuth([{ platform: "maincasts.com", token: TOKEN }, { platform: "another-host.com", token: TOKEN }])).toThrow(); expect(() => new PlatformAuth([{ platform: "maincasts.com", token: "short" }])).toThrow(); expect(() => new PlatformAuth([{ platform: "maincasts.com", token: TOKEN + " " }])).toThrow(); expect(() => new PlatformAuth([{ platform: "https://maincasts.com", token: TOKEN }])).toThrow(); }); test("rate limits are isolated by platform", () => { const limiter = new RateLimiter(1); limiter.check("maincasts.com"); expect(() => limiter.check("maincasts.com")).toThrow(); expect(() => limiter.check("another-host.com")).not.toThrow(); });