import assert from "node:assert/strict"; import { test } from "node:test"; import { createJiti } from "jiti"; import { fileURLToPath } from "node:url"; // Host packages are not installed here (pi injects them at load time), so import index.ts through jiti — // the same TS loader pi uses — with the host imports aliased to a minimal stub. const stub = fileURLToPath(new URL("./stubs/pi-ai.mjs", import.meta.url)); const jiti = createJiti(import.meta.url, { // Longest-prefix first: pi-ai/compat must precede pi-ai or the bare key swallows the subpath. alias: { "@earendil-works/pi-ai/compat": stub, "@earendil-works/pi-ai": stub, "@earendil-works/pi-coding-agent": stub, }, }); const { toModel, toCredential, fetchModels } = await jiti.import("../index.ts"); test("toModel: per-token price becomes per-million, image input from architecture, reasoning by id", () => { const m = toModel({ id: "OpenAI/GPT-5.4-Nano", context_length: 400000, architecture: { input_modalities: ["text", "image"] }, pricing: { prompt: "0.00000016", completion: "0.000001" } }); assert.equal(m.cost.input, 0.16); assert.equal(m.cost.output, 1); assert.deepEqual(m.input, ["text", "image"]); assert.equal(m.reasoning, true); assert.equal(m.baseUrl, "https://inference-api.nousresearch.com/v1"); const free = toModel({ id: "upstage/solar-mini4:free" }); assert.deepEqual(free.input, ["text"]); assert.equal(free.reasoning, false); assert.equal(free.cost.input, 0); assert.equal(free.contextWindow, 131072); }); test("toCredential: keeps the previous refresh token when the server rotates only the access token", () => { const prev = { type: "oauth", access: "old", refresh: "rt_keep", expires: 0 }; const rotated = toCredential({ access_token: "new", expires_in: 100 }); assert.equal(rotated.refresh, ""); const kept = toCredential({ access_token: "new", expires_in: 100 }, prev); assert.equal(kept.refresh, "rt_keep"); assert.equal(kept.access, "new"); assert.ok(kept.expires > Date.now()); }); test("fetchModels: returns [] and never touches the network when neither a credential nor $NOUS_API_KEY is set", async () => { const before = globalThis.fetch; const saved = process.env.NOUS_API_KEY; delete process.env.NOUS_API_KEY; globalThis.fetch = () => { throw new Error("must not fetch"); }; try { assert.deepEqual(await fetchModels(undefined, new AbortController().signal), []); } finally { if (saved !== undefined) process.env.NOUS_API_KEY = saved; globalThis.fetch = before; } }); test("fetchModels: drops hermes entries and maps the rest", async () => { const before = globalThis.fetch; globalThis.fetch = async () => ({ ok: true, json: async () => ({ data: [{ id: "hermes/agent" }, { id: "qwen/qwen3.5-35b-a3b" }] }) }); try { const models = await fetchModels({ type: "api_key", key: "k" }, new AbortController().signal); assert.deepEqual(models.map((m) => m.id), ["qwen/qwen3.5-35b-a3b"]); } finally { globalThis.fetch = before; } });