LLM Spend TripwireChecklistCodeHosted waitlist

test/units.test.mjs

Commit 01f96e7. All files · Download the tarball.

import { test } from "node:test";
import assert from "node:assert/strict";
import { parseConfig, parseHours, parseDays } from "../src/config.js";
import { priceTable, priceFor, estimateUsd, DEFAULT_PRICES } from "../src/prices.js";
import { webhookKind, payloadFor, formatMessage, FOOTER } from "../src/notify.js";
import { isWorkTime } from "../src/detect.js";

test("config: defaults and bounds", () => {
  const c = parseConfig({});
  assert.equal(c.spikeMultiplier, 5);
  assert.equal(c.baselineMinutes, 360);
  assert.equal(c.minTokensPerMinute, 50000);
  assert.equal(c.cooldownMinutes, 60);
  assert.equal(c.timezone, "UTC");
  assert.equal(c.workHours, null, "off-hours alerts are off until WORK_HOURS is set");
  assert.equal(c.anthropicVersion, "2026-10-21");
  const d = parseConfig({ SPIKE_MULTIPLIER: "abc", BASELINE_MINUTES: "5", TIMEZONE: "Not/AZone", LOOKBACK_MINUTES: "5", SETTLE_MINUTES: "10" });
  assert.equal(d.spikeMultiplier, 5);
  assert.equal(d.baselineMinutes, 60, "clamped to the 60-minute minimum");
  assert.equal(d.timezone, "UTC");
  assert.ok(d.lookbackMinutes > d.settleMinutes);
});

test("config: hours and days", () => {
  assert.deepEqual(parseHours("08:00-19:30"), { start: 480, end: 1170 });
  assert.deepEqual(parseHours("22:00-06:00"), { start: 1320, end: 360 });
  assert.equal(parseHours("8-19"), null);
  assert.equal(parseHours("09:00-09:00"), null);
  assert.deepEqual([...parseDays("1-5")], [1, 2, 3, 4, 5]);
  assert.deepEqual([...parseDays("6,7")], [6, 7]);
  assert.deepEqual([...parseDays("junk")], [1, 2, 3, 4, 5]);
});

test("prices: snapshot prefix match, unknown fallback, estimate math, overrides", () => {
  const t = priceTable("");
  assert.equal(priceFor("claude-sonnet-5-5", t).price, DEFAULT_PRICES["claude-sonnet-5-5"]);
  assert.equal(priceFor("claude-haiku-5-5-20261001", t).price, DEFAULT_PRICES["claude-haiku-5-5"]);
  const u = priceFor("some-new-model", t);
  assert.equal(u.known, false);
  assert.equal(u.price, DEFAULT_PRICES["*"]);
  // 1M input + 1M output on Sonnet 5.5 = $2 + $10.
  assert.equal(estimateUsd({ uncached_input: 1e6, output: 1e6 }, t["claude-sonnet-5-5"]), 12);
  assert.equal(estimateUsd({ cache_read: 1e6, cache_write_5m: 1e6, cache_write_1h: 1e6 }, t["claude-opus-5-5"]), 0.2 + 5 + 8);
  const o = priceTable('{"claude-sonnet-4-5": {"input": 3, "output": 15}}');
  assert.equal(o["claude-sonnet-4-5"].input, 3);
  assert.equal(o["claude-sonnet-4-5"].cache_read, DEFAULT_PRICES["*"].cache_read, "missing fields fall back");
  assert.deepEqual(priceTable("{not json"), { ...DEFAULT_PRICES });
});

test("webhook: only Slack/Discord incoming webhook URLs are accepted", () => {
  assert.equal(webhookKind("https://hooks.slack.com/services/T0/B0/XXX"), "slack");
  assert.equal(webhookKind("https://discord.com/api/webhooks/1/abc"), "discord");
  assert.equal(webhookKind("http://hooks.slack.com/services/T0/B0/XXX"), null);
  assert.equal(webhookKind("https://evil.example/services/x"), null);
  assert.equal(webhookKind("https://hooks.slack.com.evil.example/services/x"), null);
  assert.equal(webhookKind("https://user:pw@hooks.slack.com/services/x"), null);
  assert.equal(webhookKind(""), null);
});

test("webhook payloads: Discord mentions off and 2000-char limit; Slack escaping", () => {
  const d = payloadFor("discord", "@everyone " + "x".repeat(3000));
  assert.deepEqual(d.allowed_mentions, { parse: [] });
  assert.ok(d.content.length <= 2000);
  const s = payloadFor("slack", "key \"<!channel> & co\"");
  assert.equal(s.text, "key \"&lt;!channel&gt; &amp; co\"");
});

test("message: labels costs as estimates and caps lines", () => {
  const alerts = Array.from({ length: 12 }, (_, i) => ({ type: "new_model", model: `m${i}`, keys: ["apikey_A"] }));
  const msg = formatMessage(alerts, new Map(), 10);
  assert.match(msg, /12 alerts/);
  assert.match(msg, /\.\.\.and 2 more/);
  assert.ok(msg.includes(FOOTER));
  assert.match(FOOTER, /estimates/);
  assert.match(FOOTER, /daily/);
});

test("work time: timezone, weekdays and overnight windows", () => {
  const cfg = parseConfig({ TIMEZONE: "America/New_York", WORK_HOURS: "08:00-19:00", WORK_DAYS: "1-5" });
  const m = (iso) => Math.floor(Date.parse(iso) / 60000);
  assert.equal(isWorkTime(m("2026-11-16T15:00:00Z"), cfg), true, "Mon 10:00 ET");
  assert.equal(isWorkTime(m("2026-11-17T03:00:00Z"), cfg), false, "Mon 22:00 ET");
  assert.equal(isWorkTime(m("2026-11-21T15:00:00Z"), cfg), false, "Saturday");
  const night = parseConfig({ TIMEZONE: "UTC", WORK_HOURS: "22:00-06:00", WORK_DAYS: "1-7" });
  assert.equal(isWorkTime(m("2026-11-16T23:30:00Z"), night), true);
  assert.equal(isWorkTime(m("2026-11-16T12:00:00Z"), night), false);
});