LLM Spend TripwireChecklistCodeHosted waitlist

test/sample.test.mjs

Commit 01f96e7. All files · Download the tarball.

import { test } from "node:test";
import assert from "node:assert/strict";
import { replaySample, sampleUsageResponse } from "../src/sample.js";
import { handleFetch } from "../src/index.js";
import { fakeDb, fakeFetch } from "./helpers.mjs";

test("sample responses carry every documented usage field", () => {
  const body = sampleUsageResponse("https://api.anthropic.com/v1/organizations/usage_report/messages?starting_at=2026-11-16T15:00:00Z&ending_at=2026-11-16T15:02:00Z&bucket_width=1m");
  assert.equal(body.data.length, 2);
  assert.deepEqual(Object.keys(body).sort(), ["data", "has_more", "next_page"]);
  const r = body.data[0].results[0];
  for (const f of ["account_id", "api_key_id", "cache_creation", "cache_read_input_tokens", "context_window", "inference_geo", "model", "output_tokens", "server_tool_use", "service_account_id", "service_tier", "speed", "uncached_input_tokens", "workspace_id"]) assert.ok(f in r, f);
});

test("replay runs the full path and returns the alert payloads without network", async () => {
  const net = async () => { throw new Error("no network in replay"); };
  const r = await replaySample({ fetchImpl: net });
  assert.equal(r.posted, false);
  assert.equal(r.runs.seed.ok, true);
  assert.equal(r.runs.check.ok, true);
  assert.equal(r.payloads.length, 2);
  const text = r.payloads[1].payload.text;
  assert.match(text, /New API key: key "temp-test"/);
  assert.match(text, /New model: claude-fable-5-2/);
  assert.match(text, /Spike: key "prod-backend" .* used 481,500 tokens .*\(11\.6x its average of 41,500\/min/);
  assert.match(text, /about \$3\.25/);
  assert.match(text, /about \$10\.80/);
});

test("replay --post sends only to the given webhook", async () => {
  const hook = "https://discord.com/api/webhooks/9/test";
  const f = fakeFetch({ usage: () => { throw new Error("must not call Anthropic"); } });
  const r = await replaySample({ webhookUrl: hook, post: true, fetchImpl: f });
  assert.equal(r.kind, "discord");
  assert.equal(f.calls.length, 2);
  assert.ok(f.calls.every((c) => c.url === hook));
  assert.deepEqual(JSON.parse(f.calls[1].init.body).allowed_mentions, { parse: [] });
});

test("POST /dry-run?sample=1 needs the secret and posts only with post=1", async () => {
  const f = fakeFetch({ usage: () => { throw new Error("must not call Anthropic"); } });
  const env = { TEST_SECRET: "s3cret-value", ALERT_WEBHOOK_URL: "https://hooks.slack.com/services/T0/B0/X", DB: fakeDb() };
  const req = (q, auth = "Bearer s3cret-value") => new Request(`https://t.example/dry-run${q}`, { method: "POST", headers: { authorization: auth } });
  assert.equal((await handleFetch(req("?sample=1", "Bearer wrong"), env, { fetchImpl: f })).status, 401);
  const a = await (await handleFetch(req("?sample=1"), env, { fetchImpl: f })).json();
  assert.equal(a.ok, true);
  assert.equal(a.posted, false);
  assert.equal(f.calls.length, 0);
  const b = await (await handleFetch(req("?sample=1&post=1"), env, { fetchImpl: f })).json();
  assert.equal(b.posted, true);
  assert.equal(f.calls.length, 2);
  assert.ok(f.calls.every((c) => c.url === env.ALERT_WEBHOOK_URL));
  assert.equal(env.DB.writes, 0, "replay never touches the real D1");
});