repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/web/__tests__/minimax.test.ts
RawFile outline
- FetchCall line 10
- ctx line 16
- ctxWithoutKey line 26
- installJsonFetch line 35
- minimax client line 55
- originalMinimaxKey line 56
- restoreFetch line 57
- afterEach line 58
- resolves API key through model registry line 64
- falls back to MINIMAX_API_KEY line 71
- normalizes anthropic provider base URL to /v1 endpoints line 76
- returns configured MiniMax model base URL line 98
- understand_image posts data URLs to MiniMax VLM line 104
import { afterEach, describe, expect, it } from "vitest";
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"),
);
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<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",
});
});
});