repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

pi/agent/extensions/web/__tests__/minimax.test.ts

Raw
File outline
import { afterEach, describe, expect, it } from "bun:test";
import {
  isMinimaxAvailable,
  minimaxSearch,
  minimaxUnderstandImage,
  resolveMinimaxApiKey,
  resolveMinimaxBaseUrl,
} from "../minimax.ts";

type FetchCall = { url: string; init: RequestInit; body?: Record<string, unknown> };

function ctx(apiKey = "mm-key", baseUrl = "https://api.minimax.io/anthropic") {
  return {
    modelRegistry: {
      getApiKeyForProvider: async (provider: string) => provider === "minimax" ? apiKey : undefined,
      getAll: () => [{ provider: "minimax", baseUrl }],
    },
  } as any;
}

function ctxWithoutKey() {
  return {
    modelRegistry: {
      getApiKeyForProvider: async () => undefined,
      getAll: () => [],
    },
  } as any;
}

function installJsonFetch(calls: FetchCall[], response: unknown, ok = true) {
  const prev = globalThis.fetch;
  globalThis.fetch = (async (url: string | URL | Request, init?: RequestInit) => {
    const body = init?.body ? JSON.parse(String(init.body)) : undefined;
    calls.push({ url: String(url), init: init ?? {}, body });
    return {
      ok,
      status: ok ? 200 : 500,
      statusText: ok ? "OK" : "ERR",
      json: async () => response,
    } as Response;
  }) as typeof fetch;
  return () => { globalThis.fetch = prev; };
}

describe("minimax client", () => {
  const originalMinimaxKey = process.env.MINIMAX_API_KEY;
  let restoreFetch: (() => void) | undefined;
  afterEach(() => {
    restoreFetch?.();
    if (originalMinimaxKey === undefined) delete process.env.MINIMAX_API_KEY;
    else process.env.MINIMAX_API_KEY = originalMinimaxKey;
  });

  it("resolves API key through model registry", async () => {
    delete process.env.MINIMAX_API_KEY;
    expect(await resolveMinimaxApiKey(ctx("abc"))).toBe("abc");
    expect(await isMinimaxAvailable(ctx("abc"))).toBe(true);
    expect(await isMinimaxAvailable(ctxWithoutKey())).toBe(false);
  });

  it("falls back to MINIMAX_API_KEY", async () => {
    process.env.MINIMAX_API_KEY = "env-mm-key";
    expect(await resolveMinimaxApiKey(ctxWithoutKey())).toBe("env-mm-key");
  });

  it("normalizes anthropic provider base URL to /v1 endpoints", async () => {
    const calls: FetchCall[] = [];
    restoreFetch = installJsonFetch(calls, {
      organic: [],
      related_searches: [],
      base_resp: { status_code: 0, status_msg: "ok" },
    });

    await minimaxSearch("pi docs", ctx("key", "https://api.minimax.io/anthropic"));

    expect(calls[0]?.url).toBe("https://api.minimax.io/v1/coding_plan/search");
    expect(calls[0]?.body).toEqual({ q: "pi docs" });
    expect((calls[0]?.init.headers as Record<string, string>).Authorization).toBe("Bearer key");
  });

  it("returns configured MiniMax model base URL", () => {
    expect(resolveMinimaxBaseUrl(ctx("key", "https://example.test/v1"))).toBe("https://example.test/v1");
  });

  it("understand_image posts data URLs to MiniMax VLM", async () => {
    const calls: FetchCall[] = [];
    restoreFetch = installJsonFetch(calls, {
      content: "a cat",
      base_resp: { status_code: 0, status_msg: "ok" },
    });

    const result = await minimaxUnderstandImage("describe", "data:image/png;base64,abc", ctx("key", "https://api.minimax.io"));

    expect(result).toBe("a cat");
    expect(calls[0]?.url).toBe("https://api.minimax.io/v1/coding_plan/vlm");
    expect(calls[0]?.body).toEqual({ prompt: "describe", image_url: "data:image/png;base64,abc" });
  });
});