repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/gratis/__tests__/models.test.ts

Raw
File outline
import { describe, expect, it } from "vitest";
import { kiloModels } from "../src/kilo.ts";
import {
	BUILT_IN_MODELS,
	isPermanentFailure,
	matches,
	parseModelPatterns,
} from "../src/models.ts";

describe("model patterns", () => {
	it.each([
		["kilo/kilo-auto/free", "kilo/kilo-auto/free", true],
		["kilo/kilo-auto/free", "kilo/kilo-auto/free2", false],
		["openrouter/*:free", "openrouter/qwen/coder:free", true],
		["openrouter/*:free", "openrouter/qwen/coder", false],
		["nvidia/*nemotron*", "nvidia/nvidia/nemotron-3-ultra", true],
		["*", "anything/at-all", true],
		["a*a", "a", false],
	])("%s matches %s: %s", (pattern, key, expected) => {
		expect(matches(pattern, key)).toBe(expected);
	});

	it("lists only provider/model entries", () => {
		expect(BUILT_IN_MODELS.every((entry) => entry.includes("/"))).toBe(true);
	});

	it("parses comma lists from env and string arrays from settings", () => {
		expect(parseModelPatterns(" groq/a , kilo/* ,", "env")).toEqual([
			"groq/a",
			"kilo/*",
		]);
		expect(parseModelPatterns(["kilo/*"], "user")).toEqual(["kilo/*"]);
		expect(parseModelPatterns("kilo/*", "user")).toBeUndefined();
		expect(parseModelPatterns([1], "project")).toBeUndefined();
		expect(() => parseModelPatterns(["no-provider"], "user")).toThrow(
			'expected provider/model patterns, got "no-provider"',
		);
	});
});

describe("failure classification", () => {
	it.each([
		['404: {"status":404,"title":"Not Found"}', true],
		["The model has reached its end of life", true],
		["Your API key was reported as leaked", true],
		["Request timed out", true],
		["403 Forbidden", true],
		["429 rate limit", false],
		["503 ResourceExhausted", false],
		['Mistral API error (429): {"code":"1300"}', false],
		[undefined, false],
	])("%s is permanent: %s", (message, expected) => {
		expect(isPermanentFailure(message)).toBe(expected);
	});
});

describe("Kilo catalog", () => {
	const model = (overrides: Record<string, unknown>) => ({
		id: "vendor/coder:free",
		name: "Coder (free)",
		isFree: true,
		pricing: { prompt: "0", completion: "0" },
		architecture: { input_modalities: ["text"], output_modalities: ["text"] },
		supported_parameters: ["tools", "max_tokens"],
		context_length: 131_072,
		top_provider: { max_completion_tokens: 100_000 },
		...overrides,
	});

	it("keeps free, text-only, tool-capable models with bounded output", () => {
		const models = kiloModels({
			data: [
				model({}),
				model({ id: "paid/model", isFree: false, pricing: { prompt: "1" } }),
				model({ id: "no/tools", supported_parameters: ["max_tokens"] }),
				model({
					id: "image/out",
					architecture: {
						input_modalities: ["text"],
						output_modalities: ["image"],
					},
				}),
				model({ id: "no/context", context_length: 0 }),
				{ name: "missing id" },
			],
		});
		expect(models).toEqual([
			{
				id: "vendor/coder:free",
				name: "Coder (free)",
				reasoning: false,
				input: ["text"],
				cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
				contextWindow: 131_072,
				maxTokens: 32_768,
			},
		]);
	});

	it("ignores malformed catalogs", () => {
		expect(kiloModels(undefined)).toEqual([]);
		expect(kiloModels({ data: "nope" })).toEqual([]);
	});
});