${themedSwatch("#10a37f")} OpenAI • ` + `${themedSwatch("#d97757")} Anthropic • OR $5
`, ); // Each line is drawn in its provider's color and labeled with its own MTD. assertStringIncludes(v.extra ?? "", `pointer-events:none">$${DOM}`); assertStringIncludes(v.extra ?? "", `pointer-events:none">$${2 * DOM}`); assertStringIncludes(v.extra ?? "", "#10a37f"); assertStringIncludes(v.extra ?? "", "#d97757"); assertEquals(v.duration, 45 * DAY); // the chart spans the trailing 45 days, inclusive // Each key goes to its own provider, in that provider's auth scheme. assertEquals(heads["api.openai.com"].authorization, "Bearer oa"); assertEquals(heads["api.anthropic.com"]["x-api-key"], "an"); assertEquals(heads["api.anthropic.com"]["anthropic-version"], "2023-06-01"); assertEquals(heads["openrouter.ai"].authorization, "Bearer or"); }, ); }); Deno.test("model spend: the projected total is judged against MODEL_MONTHLY_BUDGET", async () => { await withFetch(allThree, async () => { const over = await modelSpend.collect(ctx({ ...ALL_KEYS, MODEL_MONTHLY_BUDGET: "0" })); assertEquals(over.status, "bad"); // $3/day projected against a $0 budget const under = await modelSpend.collect(ctx({ ...ALL_KEYS, MODEL_MONTHLY_BUDGET: "100000" })); assertEquals(under.status, "good"); }); }); Deno.test("model spend: a provider that errors -> $??? and gray, the rest still chart and total", async () => { await withFetch( { ...allThree, "api.anthropic.com": () => new Response("nope", { status: 500 }) }, async () => { // The budget is blown by the providers that did answer, but Anthropic's spend // is unknown, so the tile grays out rather than claiming a verdict. const v = await modelSpend.collect(ctx({ ...ALL_KEYS, MODEL_MONTHLY_BUDGET: "0" })); assertEquals(v.status, "unknown"); assertEquals(v.aside, `$${DOM + 5} MTD`); // Anthropic adds nothing assert(v.value?.startsWith("≥"), `the total is a lower bound, got ${v.value}`); assertStringIncludes( v.extra ?? "", `${themedSwatch("#10a37f")} OpenAI • Anthropic $??? • OR $5
`, ); assertEquals(v.duration, 45 * DAY); // OpenAI's line is still drawn assertStringIncludes(v.extra ?? "", "#10a37f"); }, ); }); // OpenAI's cost report stops six days back: the days after it have no bucket, // so the provider has written nothing about them. const openaiStalled: Handler = () => json({ data: WINDOW.slice(0, WINDOW.length - 6).map((d) => oaBucket(d, 1)), has_more: false, next_page: null }); // A bucket for a day the org spent nothing. The endpoint carries one for every // day in the range it covers, so a day with no figures is a reported $0. const oaEmptyBucket = (day: string) => ({ start_time: Date.parse(`${day}T00:00:00Z`) / 1000, results: [] }); // $1/day until the last ten days, which are reported and empty. const openaiQuiet: Handler = () => json({ data: WINDOW.map((d, i) => i < WINDOW.length - 10 ? oaBucket(d, 1) : oaEmptyBucket(d)), has_more: false, next_page: null, }); Deno.test("model spend: a provider whose report stopped -> $??? and gray, never days of $0", async () => { await withFetch({ "api.openai.com": openaiStalled, "openrouter.ai": openrouterFive }, async () => { const v = await modelSpend.collect(ctx({ OPENAI_ADMIN_KEY: "oa", OPENROUTER_KEY: "or" })); assertEquals(v.status, "unknown"); // OpenAI's recent days are unknown, not quiet assert(v.value?.startsWith("≥"), `the total is a lower bound, got ${v.value}`); assertEquals(v.extra, `OpenAI $??? • OR $5
`); // no line, no total }); }); Deno.test("model spend: a provider reporting empty days is quiet, and still reads", async () => { await withFetch({ "api.openai.com": openaiQuiet }, async () => { const v = await modelSpend.collect(ctx({ OPENAI_ADMIN_KEY: "oa" })); assertEquals(v.status, "good"); assert(v.value?.startsWith("~"), `nothing configured is missing, got ${v.value}`); assertEquals((v.extra ?? "").includes("OpenAI $???"), false); assertStringIncludes(v.extra ?? "", `${themedSwatch("#10a37f")} OpenAI`); }); }); Deno.test("model spend: a provider with no bucket at all draws no line of $0", async () => { // OpenAI answers with an empty report. Anthropic's days supply the chart's // day axis, but they are Anthropic's days: OpenAI reported on none of them, // so its total is $0 and its line is absent rather than flat along the floor. await withFetch({ "api.openai.com": () => json({ data: [] }), "api.anthropic.com": anthropicPaged }, async () => { const v = await modelSpend.collect(ctx({ OPENAI_ADMIN_KEY: "oa", ANTHROPIC_ADMIN_KEY: "an" })); assertEquals(v.status, "good"); assertEquals(v.aside, `$${2 * DOM} MTD`); // Anthropic's $2/day alone // OpenAI's color survives in the key's swatch and nowhere in the chart. assertEquals((v.extra ?? "").match(/#10a37f/gi)?.length, 1); assertStringIncludes(v.extra ?? "", "#d97757"); }); }); Deno.test("model spend: a one-key deployment still turns green (an unset key doesn't gate the budget)", async () => { await withFetch({ "openrouter.ai": openrouterFive }, async () => { const v = await modelSpend.collect(ctx({ OPENROUTER_KEY: "or" })); assertEquals(v.status, "good"); assertEquals(v.aside, `$5 MTD`); assert(v.value?.startsWith("~"), `nothing configured is missing, got ${v.value}`); // OpenRouter has no daily series, so there is no chart and nothing to span. assertEquals(v.extra, `OR $5
`); assertEquals(v.duration, 0); }); }); Deno.test("model spend: one day of data draws no chart, so the provider's total moves into the key", async () => { const today = new Date(TODAY).toISOString().slice(0, 10); await withFetch({ "api.openai.com": () => json({ data: [oaBucket(today, 12)] }) }, async () => { const v = await modelSpend.collect(ctx({ OPENAI_ADMIN_KEY: "oa" })); assertEquals(v.status, "good"); assertEquals(v.extra, `${themedSwatch("#10a37f")} OpenAI $12
`); assertEquals(v.aside, `$12 MTD`); assertEquals(v.duration, 0); }); }); // A provider fails in two ways, and neither may read as a real $0: the request is // refused, or it returns a 200 whose body carries no figures (a permission-filtered // view, say). Each provider is put through both. const REFUSED: Record${themedSwatch("#10a37f")} OpenAI • Anthropic $???
`, ); }, ); }); // A half-finished federation setup still counts Anthropic as configured, so the // tile grays with Anthropic $??? instead of dropping the provider silently, and // the Admin key beside it is not used. Deno.test("model spend: a partial federation setup shows Anthropic $??? and sends nothing to Anthropic", async () => { // Both hosts answer, so a request to either would succeed rather than fail // quietly into the same $???; the recorded calls are what prove none is made. const touched: string[] = []; const record: Handler = (url) => { touched.push(url.href); return json({ access_token: "sk-ant-oat01-x", data: [] }); }; await withFetch({ "api.openai.com": openaiPaged, "api.anthropic.com": record, "metadata.google.internal": record }, async () => { const v = await modelSpend.collect(ctx({ ANTHROPIC_FEDERATION_RULE_ID: "fdrl_x", ANTHROPIC_ADMIN_KEY: "stray", OPENAI_ADMIN_KEY: "oa", })); assertEquals(v.status, "unknown"); assertStringIncludes(v.extra ?? "", "Anthropic $???"); assertEquals(touched, []); }); });