Files

191 lines
7.8 KiB
TypeScript

import { expect, test } from "bun:test";
import type { ExistingModel } from "../src/sync/index.js";
import { buildMorphModel, buildMorphTranscription, fetchMorphModels, morph, MorphResponse, type MorphModel } from "../src/sync/providers/morph.js";
function context(entries: Record<string, ExistingModel>) {
return { existing: (id: string) => entries[id], authored: (id: string) => entries[id] };
}
function morphModel(overrides: Partial<MorphModel> = {}): MorphModel {
return {
id: "morph-dsv4flash",
name: "Morph: DeepSeek V4 Flash 0731",
input_modalities: [{
type: "text",
supported_inputs: { max_context_length: { value: 1_048_576 } },
pricing: [
{ type: "prompt", cost_usd: "0.000000141953125" },
{ type: "cached_prompt", cost_usd: "0.0000000359375" },
{ type: "cache_write", cost_usd: "0" },
],
}],
output_modalities: [{
type: "text",
max_length: { value: 1_048_576 },
pricing: [{ type: "completion", cost_usd: "0.000000399625" }],
}],
is_ready: true,
...overrides,
};
}
// A full definition rather than a base_model overlay, so these assert the
// mapping itself instead of how factorBaseModel diffs against a lab file.
function authored(overrides: Record<string, unknown> = {}): ExistingModel {
return {
name: "DeepSeek V4 Flash 0731",
reasoning: true,
reasoning_options: [{ type: "effort", values: ["none", "low", "high", "xhigh", "max"] }],
tool_call: true,
structured_output: true,
open_weights: true,
cost: { input: 0.1, output: 0.3, cache_read: 0.02 },
limit: { context: 1_000_000, output: 384_000 },
modalities: { input: ["text"], output: ["text"] },
...overrides,
} as ExistingModel;
}
test("takes exact list prices and limits from the feed, keeping authored reasoning", () => {
expect(buildMorphModel(morphModel(), authored())).toMatchObject({
reasoning_options: [{ type: "effort", values: ["none", "low", "high", "xhigh", "max"] }],
cost: { input: 0.141953125, output: 0.399625, cache_read: 0.0359375 },
limit: { context: 1_048_576, output: 1_048_576 },
});
});
test("drops the cache price when the feed lists no cached SKU", () => {
const model = morphModel({
id: "morph-v3-fast",
input_modalities: [{
type: "text",
supported_inputs: { max_context_length: { value: 262_144 } },
pricing: [{ type: "prompt", cost_usd: "0.0000008" }],
}],
output_modalities: [{
type: "text",
max_length: { value: 131_072 },
pricing: [{ type: "completion", cost_usd: "0.0000012" }],
}],
});
const built = buildMorphModel(model, authored({ cost: { input: 0.8, output: 1.2 } }));
expect(built.cost).toMatchObject({ input: 0.8, output: 1.2, cache_read: undefined });
});
test("fails on a feed entry without a text price", () => {
const model = morphModel({ output_modalities: [{ type: "text", max_length: { value: 1 } }] });
expect(() => buildMorphModel(model, authored())).toThrow("missing a text price or limit");
});
test("prices a decision model's input only and keeps its authored output limit", () => {
const model = morphModel({
id: "reflex",
input_modalities: [{
type: "text",
supported_inputs: { max_context_length: { value: 65_536 } },
pricing: [
{ type: "prompt", cost_usd: "0.000000042" },
{ type: "cached_prompt", cost_usd: "0.000000008" },
],
}, { type: "image" }],
output_modalities: [{ type: "decisions" }],
});
const built = buildMorphModel(model, authored({ cost: { input: 0.042, output: 0 }, limit: { context: 65_536, output: 0 } }));
expect(built).toMatchObject({
cost: { input: 0.042, output: 0, cache_read: 0.008 },
limit: { context: 65_536, output: 0 },
});
});
test("skips OpenRouter aliases, approval-only and unready entries without reporting them missing", () => {
expect(morph.sourceID(morphModel({ id: "morph-dsv4flash-0731" }))).toBeUndefined();
expect(morph.sourceID(morphModel({ id: "morph-dsv41flash-abliterated" }))).toBeUndefined();
expect(morph.sourceID(morphModel({ id: "morph-kimik3-abliterated" }))).toBeUndefined();
expect(morph.sourceID(morphModel({ is_ready: false }))).toBeUndefined();
expect(morph.sourceID(morphModel({ id: "morph-new" }))).toBe("morph-new");
expect(morph.translateModel(morphModel({ id: "morph-new" }), context({}))).toBeUndefined();
});
test("reads the list-price feed, not the OpenRouter feed", async () => {
const urls: string[] = [];
const fetcher = (async (url: string) => {
urls.push(url);
return Response.json({ data: [morphModel()] });
}) as unknown as typeof fetch;
const response = await fetchMorphModels(fetcher);
expect(urls).toEqual(["https://www.morphllm.com/api/models/json?prices=list"]);
expect(response.data.map((model) => model.id)).toEqual(["morph-dsv4flash"]);
});
// morph-transcribe as served on 2026-11-30: an audio input states a duration
// and size limit, not a context length.
const transcribe = {
schema_version: "2.4",
id: "morph-transcribe",
name: "Morph: Nemotron 3.5 Transcribe",
input_modalities: [{
type: "audio",
supported_inputs: {
sources: { type: "enum", values: ["url", "base64"] },
formats: { type: "enum", values: ["audio/wav", "audio/mpeg"] },
max_duration_seconds: { value: 3600, unit: "second" },
max_content_size_bytes: { value: 188743680, unit: "byte" },
},
pricing: [{ type: "prompt", unit: "token", cost_usd: "0.00005" }],
}],
output_modalities: [{ type: "transcription", streaming: true }],
is_ready: true,
};
test("parses an audio input without a context length", () => {
const parsed = MorphResponse.parse({ data: [morphModel(), transcribe] });
expect(parsed.data.map((model) => model.id)).toEqual(["morph-dsv4flash", "morph-transcribe"]);
});
test("still requires a context length on text inputs that state supported_inputs", () => {
const model = { ...morphModel(), input_modalities: [{ type: "text", supported_inputs: {}, pricing: [] }] };
expect(() => MorphResponse.parse({ data: [model] })).toThrow();
});
test("keeps the hand-authored MIME list that the v1 view would collapse", () => {
const modalities = { input: ["text", "image/jpeg", "image/png"], output: ["text"] };
const translated = morph.translateModel(morphModel(), {
...context({ "morph-dsv4flash": authored() }),
v2: () => ({ schema: 2, base_model: "deepseek/deepseek-v4-flash-0731", modalities }),
});
expect(translated?.v2Set).toEqual({ modalities });
});
const transcribeFile = {
schema: 2,
v2_only: true,
base_model: "nvidia/nemotron-3.5-transcribe-30b-a4b",
api: { transcriptions: { sdk: { aisdk: "@ai-sdk/openai", opencodeai: "@opencode/ai/protocols/openai-transcription" } } },
cost: { audio: { input: { unit: "second", price: 0.00004 } } },
};
test("prices a transcription model per second of audio and keeps the hand-authored file", () => {
const translated = morph.translateModel(MorphResponse.parse({ data: [transcribe] }).data[0]!, {
...context({}),
v2: (id) => id === "morph-transcribe" ? transcribeFile : undefined,
});
expect(translated).toEqual({
id: "morph-transcribe",
v2: { ...transcribeFile, cost: { audio: { input: { unit: "second", price: 0.00005 } } } },
});
});
test("leaves a new transcription model for a hand-authored file", () => {
const model = MorphResponse.parse({ data: [transcribe] }).data[0]!;
expect(morph.translateModel(model, { ...context({}), v2: () => undefined })).toBeUndefined();
expect(morph.sourceID(model)).toBe("morph-transcribe");
});
test("fails on a transcription entry with a completion price", () => {
const model = MorphResponse.parse({
data: [{ ...transcribe, output_modalities: [{ type: "transcription", pricing: [{ type: "completion", cost_usd: "0.00001" }] }] }],
}).data[0]!;
expect(() => buildMorphTranscription(model, transcribeFile)).toThrow("needs exactly an audio prompt price");
});