import { test } from "node:test"; import assert from "node:assert/strict"; import { priceOf, PRICES } from "./prices"; import { Meter } from "./meter"; const close = (a: number, b: number) => assert.ok(Math.abs(a - b) < 1e-9, `${a} ≈ ${b}`); test("priceOf: the longest prefix wins", () => { assert.deepEqual(priceOf("claude-opus-5-5"), PRICES["claude-opus-5-5"]); assert.deepEqual(priceOf("claude-opus-5"), PRICES["claude-opus-5"]); assert.deepEqual(priceOf("claude-sonnet-5-5-20261001"), PRICES["claude-sonnet-5-5"]); assert.deepEqual(priceOf("claude-fable-5-1"), PRICES["claude-fable-5-1"]); assert.deepEqual(priceOf("claude-fable-5"), PRICES["claude-fable-5"]); }); test("priceOf: an unknown Claude model is priced as Opus 5; a local model is free", () => { assert.deepEqual(priceOf("claude-unheard-of-9"), PRICES["claude-opus-5"]); assert.deepEqual(priceOf("qwen3:8b"), { input: 0, output: 0, cacheReadMult: 0 }); }); test("Meter: input, output, cache writes at 1.25x (5m) and reads at the model's multiplier", () => { const m = new Meter(); // Sonnet 5.5: $2 in, $10 out, reads 0.05x. const usd = m.add("claude-sonnet-5-5", { input_tokens: 1_000_000, output_tokens: 100_000, cache_creation_input_tokens: 1_000_000, cache_read_input_tokens: 1_000_000, }); close(usd, 2 + 1 + 2 * 1.25 + 2 * 0.05); close(m.usd, usd); }); test("Meter: writes at 2x when the run's TTL is 1h, or by the API's split when it reports one", () => { const m = new Meter(); close(m.add("claude-sonnet-5", { cache_creation_input_tokens: 1_000_000 }, { cacheTtl: "1h" }), 4); close(m.add("claude-sonnet-5", { cache_creation_input_tokens: 1_000_000 }), 2.5); // The split wins over the TTL hint. close( m.add( "claude-sonnet-5", { cache_creation_input_tokens: 1_000_000, cache_creation: { ephemeral_5m_input_tokens: 500_000, ephemeral_1h_input_tokens: 500_000 }, }, { cacheTtl: "1h" }, ), 0.5 * 2 * 1.25 + 0.5 * 2 * 2, ); }); test("Meter: over() against the cap, never without one; summary per model", () => { const capped = new Meter({ maxUsd: 1 }); capped.add("claude-sonnet-5", { output_tokens: 85_000 }); // $0.85 assert.equal(capped.over(0.85), true); assert.equal(capped.over(), false); capped.add("claude-sonnet-5", { output_tokens: 15_000 }); assert.equal(capped.over(), true); const free = new Meter(); free.add("claude-opus-5", { output_tokens: 1_000_000 }); assert.equal(free.over(), false); const m = new Meter(); m.add("qwen3:8b", { input_tokens: 5000, output_tokens: 200 }); m.add("claude-sonnet-5", { input_tokens: 100, cache_read_input_tokens: 300, output_tokens: 10 }); const s = m.summary(); assert.equal(s.byModel["qwen3:8b"].usd, 0); assert.equal(s.byModel["qwen3:8b"].calls, 1); assert.equal(s.byModel["claude-sonnet-5"].cacheHit, 0.75); // The summary is a copy. s.byModel["qwen3:8b"].calls = 99; assert.equal(m.summary().byModel["qwen3:8b"].calls, 1); });