test/anthropic.test.mjs
Commit 01f96e7. All files · Download the tarball.
import { test } from "node:test";
import assert from "node:assert/strict";
import { fetchUsage, fetchActiveKeys, ApiError, API_BASE } from "../src/anthropic.js";
import { fakeFetch, usageResponse, keysResponse, minuteOf } from "./helpers.mjs";
const KEY = "sk-ant-admin01-TESTKEY";
test("usage request matches the documented endpoint, params and headers", async () => {
const f = fakeFetch({ usage: () => usageResponse([]) });
const start = minuteOf("2026-11-16T14:45:00Z");
await fetchUsage({ fetchImpl: f, key: KEY, version: "2026-10-21", startMin: start, endMin: start + 16, width: "1m" });
const { url, init } = f.calls[0];
assert.ok(url.startsWith(`${API_BASE}/v1/organizations/usage_report/messages?`));
const q = url.split("?")[1];
assert.match(q, /starting_at=2026-11-16T14%3A45%3A00Z/);
assert.match(q, /ending_at=2026-11-16T15%3A01%3A00Z/);
assert.match(q, /bucket_width=1m/);
assert.equal((q.match(/group_by\[\]=/g) || []).length, 2);
assert.match(q, /group_by\[\]=api_key_id/);
assert.match(q, /group_by\[\]=model/);
assert.match(q, /limit=16/);
assert.equal(init.method, "GET");
assert.equal(init.headers["x-api-key"], KEY);
assert.equal(init.headers["anthropic-version"], "2026-10-21");
assert.match(init.headers["user-agent"], /^LLMSpendTripwire\//);
assert.equal(init.body, undefined, "read-only GET with no body");
});
test("usage: parses documented fields, follows next_page, maps null key to 'none'", async () => {
const s = minuteOf("2026-11-16T14:00:00Z");
const pages = [
usageResponse([{ start: s, results: [{ key: "apikey_A", in: 10, out: 5, read: 3, w5m: 2, w1h: 1 }] }], { has_more: true, next_page: "page_2" }),
usageResponse([{ start: s + 1, results: [{ key: null, model: "claude-haiku-5-5", in: 7 }] }, { start: s + 2, results: [] }]),
];
const f = fakeFetch({ usage: (u) => (u.includes("page=page_2") ? pages[1] : pages[0]) });
const rows = await fetchUsage({ fetchImpl: f, key: KEY, version: "2026-10-21", startMin: s, endMin: s + 3, width: "1m" });
assert.equal(f.calls.length, 2);
assert.match(f.calls[1].url, /page=page_2/);
assert.equal(rows.length, 2);
assert.deepEqual(rows[0], { minute: s, width: 1, key: "apikey_A", model: "claude-sonnet-5-5", t: { uncached_input: 10, cache_write_5m: 2, cache_write_1h: 1, cache_read: 3, output: 5 } });
assert.equal(rows[1].key, "none");
assert.equal(rows[1].model, "claude-haiku-5-5");
});
test("hourly backfill caps limit at 168 buckets", async () => {
const f = fakeFetch({ usage: () => usageResponse([]) });
const s = minuteOf("2026-11-01T00:00:00Z");
await fetchUsage({ fetchImpl: f, key: KEY, version: "2026-10-21", startMin: s, endMin: s + 60 * 200, width: "1h" });
assert.match(f.calls[0].url, /bucket_width=1h/);
assert.match(f.calls[0].url, /limit=168/);
});
test("API errors become ApiError with status and never echo the key", async () => {
const f = fakeFetch({ usage: () => ({}), usageStatus: 401 });
await assert.rejects(
fetchUsage({ fetchImpl: f, key: KEY, version: "2026-10-21", startMin: 0, endMin: 10, width: "1m" }),
(e) => e instanceof ApiError && e.status === 401 && !e.message.includes(KEY) && /authentication_error/.test(e.message),
);
});
test("active key list: documented params and after_id pagination", async () => {
const f = fakeFetch({
usage: () => usageResponse([]),
keys: (u) => (u.includes("after_id=apikey_B")
? keysResponse([{ id: "apikey_C", name: "ci" }])
: keysResponse([{ id: "apikey_A", name: "prod" }, { id: "apikey_B", name: "dev" }], { has_more: true, last_id: "apikey_B" })),
});
const keys = await fetchActiveKeys({ fetchImpl: f, key: KEY, version: "2026-10-21" });
assert.deepEqual(keys.map((k) => k.id), ["apikey_A", "apikey_B", "apikey_C"]);
assert.match(f.calls[0].url, /\/v1\/organizations\/api_keys\?status=active&limit=1000$/);
assert.equal(keys[0].hint, "sk-ant-api03-R2D...igAA");
assert.equal(keys[0].created_by, "user user_01");
});