AkurAI Build
Menu

popagent

public

Latest change 62f685314a2172a23c717ce4fe12027a662a406c - Make tests clean up persistent state by AkurAI Build

import { afterEach, describe, expect, test } from "bun:test";
import { requireApiKey } from "./auth";

const originalApiKey = process.env.POPAGENT_API_KEY;

describe("requireApiKey", () => {
  afterEach(() => {
    if (originalApiKey === undefined) delete process.env.POPAGENT_API_KEY;
    else process.env.POPAGENT_API_KEY = originalApiKey;
  });

  test("allows when key is unset", () => {
    delete process.env.POPAGENT_API_KEY;
    expect(requireApiKey(new Request("http://localhost/api/models"))).toBeUndefined();
  });

  test("rejects missing or wrong keys", () => {
    process.env.POPAGENT_API_KEY = "test-key";
    expect(requireApiKey(new Request("http://localhost/api/models"))?.status).toBe(401);
    expect(requireApiKey(new Request("http://localhost/api/models", { headers: { "x-popagent-key": "wrong" } }))?.status).toBe(401);
  });

  test("accepts the configured key", () => {
    process.env.POPAGENT_API_KEY = "test-key";
    expect(requireApiKey(new Request("http://localhost/api/models", { headers: { "x-popagent-key": "test-key" } }))).toBeUndefined();
  });
});