import { afterEach, describe, expect, it } from "vitest"; import { isMinimaxAvailable, minimaxSearch, resolveMinimaxApiKey, resolveMinimaxBaseUrl, } from "../minimax.ts"; type FetchCall = { url: string; init: RequestInit; body?: Record; }; 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"), ); const call = calls[0]; if (!call) throw new Error("missing fetch call"); expect(call.url).toBe("https://api.minimax.io/v1/coding_plan/search"); expect(call.body).toEqual({ q: "pi docs" }); expect((call.init.headers as Record).Authorization).toBe( "Bearer key", ); }); it("returns configured MiniMax model base URL", () => { expect(resolveMinimaxBaseUrl(ctx("key", "https://example.test/v1"))).toBe( "https://example.test/v1", ); }); });