mirror of
https://github.com/bitbaum/ai-kit.git
synced 2026-12-04 20:08:51 +01:00
* feat(usage): what the budget numbers mean, defined once (1.21.0) Loki's Settings → AI and OrangeCat's /settings/usage each grew the half a meter leaves out — does today's pace last, the week, when it refills, what the assistant still reads, levers with their consequence — separately, in October 2026, with the same rules and slightly different sentences. The product-neutral half moves here so the next app starts with it: - paceForecast / paceLine: lasts the day, or runs out at "12:00 UTC"; silent before there is a rate (PACE_MIN_USED, PACE_MIN_DAY_FRACTION, or minUsed). - weekSummary: total, active days, days the cap was reached; null for an empty or unreadable week, never "0". - refillsIn: "in 4 hours", "in under a minute", "now", or null — never a guessed time. - admissionsLeft: answers a remainder buys, counted the way a gate admits (one at the estimate, then per measured answer), not a bare division. - historyWindowLine: what the assistant still reads of a conversation. - countOf, clockAt, nextUtcMidnight; UsageAction / UsageExplanation types; UTC_RESET_EXPLANATION. Numbers go through the caller's formatter so a page never groups digits two ways. Plan names, prices and which levers are open stay in each app. Additive: minor release. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015QRNwA4BorkdoSCd8j5Xvu * fix(usage): the reset sentence claims only what the counter does UTC_RESET_EXPLANATION gave a reason — "that is when the model providers' free allowances refill" — which is false of Groq, whose limits refill on a rolling clock. OrangeCat's usage page had shipped the same reason and took it back in bitbaum/orangecat#1318; a shared sentence that is wrong is wrong in every app at once, so it now states only that the count is kept per UTC day. weekSummary and the README also say the other #1318 lesson: seven rows ending today are "last 7 days", not "this week". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015QRNwA4BorkdoSCd8j5Xvu * refactor(usage): utcResetExplanation(unit) instead of a fixed sentence OrangeCat counts messages and Loki a share; "Unused allowance does not roll over" was right for neither. The reset is now utcResetExplanation("messages") → "Unused messages don't roll over.", defaulting to "allowance". Still claims only what the counter does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015QRNwA4BorkdoSCd8j5Xvu * test(usage): countOf's formatter test no longer depends on ICU data CI (Node 24) groups de-CH with ' and this sandbox (Node 22) with ’, so the test asserted a runtime's locale data rather than that countOf uses the formatter it is given. It now passes a formatter with no locale behind it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015QRNwA4BorkdoSCd8j5Xvu --------- Co-authored-by: Claude <noreply@anthropic.com>
141 lines
6.1 KiB
JavaScript
141 lines
6.1 KiB
JavaScript
/**
|
||
* What the budget numbers mean (src/usage.ts).
|
||
*
|
||
* The property under test is that every app phrases a budget the same honest
|
||
* way: no forecast from one sample, an empty week is absent rather than zero,
|
||
* a remainder is counted the way a gate admits it, and a refill time is never
|
||
* guessed. Numbers are checkable by hand: a 10-unit day observed at 06:00 UTC
|
||
* (a quarter of the day gone).
|
||
*/
|
||
import { test } from "node:test";
|
||
import assert from "node:assert/strict";
|
||
|
||
import {
|
||
admissionsLeft,
|
||
clockAt,
|
||
countOf,
|
||
historyWindowLine,
|
||
nextUtcMidnight,
|
||
paceForecast,
|
||
paceLine,
|
||
refillsIn,
|
||
weekSummary,
|
||
utcResetExplanation,
|
||
} from "@bitbaum/ai-kit";
|
||
|
||
const SIX_AM = new Date("2026-10-12T06:00:00Z");
|
||
|
||
test("paceForecast says nothing before there is a rate to project", () => {
|
||
assert.equal(paceForecast({ used: 1, limit: 10, now: SIX_AM }), null);
|
||
assert.equal(paceForecast({ used: 3, limit: 10, now: new Date("2026-10-12T00:30:00Z") }), null);
|
||
assert.equal(paceForecast({ used: 10, limit: 10, now: SIX_AM }), null, "already at the limit");
|
||
assert.equal(paceForecast({ used: 4, limit: 0, now: SIX_AM }), null, "no limit to reach");
|
||
});
|
||
|
||
test("paceForecast says the day lasts, or when the limit is reached", () => {
|
||
// 2 by 06:00 → 8 for the day, under 10.
|
||
assert.deepEqual(paceForecast({ used: 2, limit: 10, now: SIX_AM }), { lastsTheDay: true });
|
||
// 5 by 06:00 → 10 reached at 12:00.
|
||
const f = paceForecast({ used: 5, limit: 10, now: SIX_AM });
|
||
assert.equal(f.lastsTheDay, false);
|
||
assert.equal(f.clock, "12:00 UTC");
|
||
assert.equal(f.reachesAt, 0.5);
|
||
});
|
||
|
||
test("paceForecast honours a caller's own minimum sample", () => {
|
||
// In tokens, two units is nothing; a caller says what a sample is.
|
||
assert.equal(paceForecast({ used: 30, limit: 100_000, now: SIX_AM, minUsed: 40_000 }), null);
|
||
});
|
||
|
||
test("paceLine phrases both outcomes with the product's subject and verb agreement", () => {
|
||
const lasts = paceForecast({ used: 2, limit: 10, now: SIX_AM });
|
||
assert.equal(
|
||
paceLine(lasts, { subject: "your 10 messages", plural: true }),
|
||
"At today's pace your 10 messages last the whole day.",
|
||
);
|
||
assert.equal(
|
||
paceLine(lasts, { subject: "your share" }),
|
||
"At today's pace your share lasts the whole day.",
|
||
);
|
||
const runs = paceForecast({ used: 5, limit: 10, now: SIX_AM });
|
||
assert.equal(
|
||
paceLine(runs, { subject: "your share", then: " — pacing slows you before that" }),
|
||
"At today's pace your share runs out around 12:00 UTC — pacing slows you before that.",
|
||
);
|
||
assert.equal(paceLine(null, { subject: "x" }), null);
|
||
});
|
||
|
||
test("weekSummary counts use, active days and capped days — and an empty week is absent", () => {
|
||
const days = [10, 0, 4, 10, 2, 0, 3].map((used) => ({ used }));
|
||
assert.deepEqual(weekSummary(days, 10), { total: 29, activeDays: 5, cappedDays: 2 });
|
||
assert.deepEqual(weekSummary(days), { total: 29, activeDays: 5, cappedDays: 0 });
|
||
assert.equal(weekSummary([{ used: 0 }, { used: 0 }], 10), null);
|
||
assert.equal(weekSummary(null, 10), null);
|
||
});
|
||
|
||
test("admissionsLeft counts the way a gate admits, not by bare division", () => {
|
||
// 40k left, gate estimates 20k, real answers cost 2k: 1 + 10, not 20.
|
||
assert.equal(admissionsLeft(40_000, 20_000, 2_000), 11);
|
||
// Below the estimate the gate refuses, however cheap the turns.
|
||
assert.equal(admissionsLeft(15_000, 20_000, 2_000), 0);
|
||
assert.equal(admissionsLeft(20_000, 20_000, 2_000), 1);
|
||
assert.equal(admissionsLeft(75_000, 20_000, 6_000), 10);
|
||
});
|
||
|
||
test("refillsIn never guesses, and says the sub-minute case honestly", () => {
|
||
const now = Date.parse("2026-10-12T06:00:00Z");
|
||
assert.equal(refillsIn(null, now), null);
|
||
assert.equal(refillsIn("not a date", now), null);
|
||
assert.equal(refillsIn(now - 1, now), "now");
|
||
assert.equal(refillsIn(now + 40_000, now), "in under a minute");
|
||
assert.equal(refillsIn(now + 12 * 60_000, now), "in 12 minutes");
|
||
assert.equal(refillsIn(new Date(now + 18 * 3_600_000), now), "in 18 hours");
|
||
assert.equal(refillsIn(now + 3 * 86_400_000, now), "in 3 days");
|
||
});
|
||
|
||
test("nextUtcMidnight and clockAt follow the UTC day", () => {
|
||
assert.equal(
|
||
nextUtcMidnight(new Date("2026-10-12T23:59:59Z")).toISOString(),
|
||
"2026-10-13T00:00:00.000Z",
|
||
);
|
||
assert.equal(
|
||
nextUtcMidnight(new Date("2026-10-12T00:00:00Z")).toISOString(),
|
||
"2026-10-13T00:00:00.000Z",
|
||
);
|
||
assert.equal(clockAt(0.7), "16:48 UTC");
|
||
assert.equal(clockAt(2), "00:00 UTC", "clamped: the end of the day reads as midnight");
|
||
});
|
||
|
||
test("historyWindowLine states the window in the product's words", () => {
|
||
assert.equal(
|
||
historyWindowLine({ assistant: "Cat", items: 20, unit: "message", memory: "Memory" }),
|
||
"In a conversation Cat reads your last 20 messages; anything older it no longer sees. " +
|
||
"What should outlast that belongs in Memory, which Cat recalls in every chat.",
|
||
);
|
||
assert.equal(
|
||
historyWindowLine({ assistant: "Loki", items: 8, unit: "turn", trimmedChars: 600 }),
|
||
"In a conversation Loki reads your last 8 turns, each trimmed to about 600 characters; " +
|
||
"anything older it no longer sees.",
|
||
);
|
||
});
|
||
|
||
test("countOf uses the app's formatter and plural", () => {
|
||
// A formatter with no locale data behind it: ICU differs between Node
|
||
// releases (de-CH groups with ' on one and ’ on another), and what is under
|
||
// test is that countOf USES the formatter it is given, not ICU.
|
||
const underscores = (n) => String(n).replace(/\B(?=(\d{3})+(?!\d))/g, "_");
|
||
assert.equal(countOf(12_000, "token", { format: underscores }), "12_000 tokens");
|
||
assert.equal(countOf(1, "day"), "1 day");
|
||
assert.equal(countOf(2, "body", { plural: "bodies" }), "2 bodies");
|
||
});
|
||
|
||
test("the reset explanation claims only what the counter does, in the product's unit", () => {
|
||
const generic = utcResetExplanation();
|
||
assert.match(generic.body, /per UTC day/);
|
||
assert.match(generic.body, /Unused allowance doesn't roll over\.$/);
|
||
assert.match(utcResetExplanation("messages").body, /Unused messages don't roll over\.$/);
|
||
// Groq refills on its own rolling clock, so "the providers refill at
|
||
// midnight" would be false in every app that shows this sentence.
|
||
assert.doesNotMatch(generic.body, /refill/i);
|
||
});
|