summaryrefslogtreecommitdiffhomepage
path: root/packages/auth-apikey/src/resolver.test.ts
blob: bef734d63ef9565636c0dd64c07c768de3a88761 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { describe, expect, it } from "vitest";
import { resolveApiKeyCredentials } from "./resolver.js";

describe("resolveApiKeyCredentials", () => {
  it("resolves key with default baseURL from minimal env", () => {
    const env = { DISPATCH_API_KEY: "sk-test-123" };
    const result = resolveApiKeyCredentials(env);
    expect(result).toEqual({
      type: "api-key",
      apiKey: "sk-test-123",
      baseURL: "https://opencode.ai/zen/go/v1",
    });
  });

  it("honors an explicit DISPATCH_BASE_URL", () => {
    const env = {
      DISPATCH_API_KEY: "sk-test-456",
      DISPATCH_BASE_URL: "https://custom.example.com/v2",
    };
    const result = resolveApiKeyCredentials(env);
    expect(result).toEqual({
      type: "api-key",
      apiKey: "sk-test-456",
      baseURL: "https://custom.example.com/v2",
    });
  });

  it("throws a clear error when DISPATCH_API_KEY is absent", () => {
    const env = {};
    expect(() => resolveApiKeyCredentials(env)).toThrow("DISPATCH_API_KEY");
  });

  it("throws when DISPATCH_API_KEY is empty string", () => {
    const env = { DISPATCH_API_KEY: "" };
    expect(() => resolveApiKeyCredentials(env)).toThrow("DISPATCH_API_KEY");
  });
});