Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

2.0 KB
Raw Blame
import { describe, expect, test } from "vitest";
import {
  ApiKeyCredential,
  BoxliteRestOptions,
  type Credential,
} from "../lib/index.js";

describe("ApiKeyCredential", () => {
  test("getToken returns the key with no expiry", () => {
    const cred = new ApiKeyCredential("blk_live_secret");
    const tok = cred.getToken();
    expect(tok.token).toBe("blk_live_secret");
    // API keys never expire — the core fetches once and caches.
    expect(tok.expiresAt ?? null).toBeNull();
  });

  test("fromEnv unset returns null", () => {
    delete process.env.BOXLITE_API_KEY;
    expect(ApiKeyCredential.fromEnv()).toBeNull();
  });

  test("fromEnv set returns a credential", () => {
    process.env.BOXLITE_API_KEY = "env-key";
    const cred = ApiKeyCredential.fromEnv();
    expect(cred).not.toBeNull();
    expect(cred!.getToken().token).toBe("env-key");
    delete process.env.BOXLITE_API_KEY;
  });

  test("structurally satisfies the Credential interface", () => {
    // The whole point of the abstraction: a function typed against
    // `Credential` accepts ApiKeyCredential with no nominal `implements`.
    const useCredential = (c: Credential): string => c.getToken().token;
    expect(useCredential(new ApiKeyCredential("k"))).toBe("k");
  });
});

describe("BoxliteRestOptions", () => {
  test("carries url, credential, and pathPrefix", () => {
    const cred = new ApiKeyCredential("blk_live_x");
    const opts = new BoxliteRestOptions({
      url: "https://api.example.com",
      credential: cred,
      pathPrefix: "acme",
    });
    expect(opts.url).toBe("https://api.example.com");
    expect(opts.credential).toBe(cred);
    expect(opts.pathPrefix).toBe("acme");
  });

  test("credential and pathPrefix are optional (unauthenticated, empty prefix)", () => {
    const opts = new BoxliteRestOptions({ url: "http://localhost:8100" });
    expect(opts.url).toBe("http://localhost:8100");
    expect(opts.credential).toBeUndefined();
    expect(opts.pathPrefix).toBeUndefined();
  });
});