Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions app/src/lib/mock-usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@ interface MockAccount {

const accounts: Record<string, MockAccount[]> = {};
// devl has a second login per agent; other boxes sign in with the same
// default logins, so the screen counts those once.
// default logins and Claude Code's work login, so the screen counts those
// once.
const accountsOf = (box: string) =>
(accounts[box] ??= (
[
{ agent: "claude", id: "default", dir: `${HOME}/.claude`, exists: true, signed_in: true, email: "you@example.com", name: "You", org: null, billing: "stripe_subscription", tier: "default_claude_max_20x" },
{ agent: "claude", id: "work", dir: `${HOME}/.berth/accounts/claude/work`, exists: true, signed_in: true, email: "you@work.example", name: "You", org: "Work", billing: "stripe_subscription", tier: "default_claude_max_5x" },
// Its profile is kept but Claude Code lost the login: sign in again.
{ agent: "claude", id: "lapsed", dir: `${HOME}/.berth/accounts/claude/lapsed`, exists: true, signed_in: false, signed_out: true, email: "you@lapsed.example", name: "You", org: "Lapsed", billing: "stripe_subscription", tier: "default_claude_max_5x" },
{ agent: "codex", id: "default", dir: `${HOME}/.codex`, exists: true, signed_in: true, method: "chatgpt", email: "you@example.com", plan: "pro" },
{ agent: "codex", id: "api", dir: `${HOME}/.berth/accounts/codex/api`, exists: true, signed_in: true, method: "api key", email: null, plan: null },
] as MockAccount[]
).filter((a) => box === "devl" || a.id === "default"));
).filter((a) => box === "devl" || a.id === "default" || (a.agent === "claude" && a.id === "work")));
Comment thread
tinhthanh marked this conversation as resolved.
Resolved

export function usageCall(box: string, method: string, path: string, body: unknown, delay: <T>(v: T) => Promise<T>): Promise<unknown> | undefined {
if (path !== "env") return undefined;
Expand Down Expand Up @@ -88,7 +91,7 @@ function report(box: string) {
const busy = (weekend ? 0.25 : 0.6 + r()) * (gpu ? 0.35 : 1);
const rows: [string, string, string, number][] = [
["claude", "default", "claude-opus-5-5", 1],
["claude", gpu ? "default" : "work", "claude-sonnet-5-5", 0.5],
["claude", "work", "claude-sonnet-5-5", 0.5],
["claude", "default", "claude-haiku-4-5-20251001", 0.08],
["codex", "default", "gpt-5.5-codex", 0.45],
];
Expand Down Expand Up @@ -139,16 +142,43 @@ function report(box: string) {
s("claude", "default", WT[0], "Reproduce the double-charge bug", 75, 1.6, [40_210, 190_442, 15_002_871, 902_114], 22.6, ["claude-opus-5-5"]),
],
limits: [
{
agent: "claude",
account: "default",
email: "you@example.com",
at: at(0.02),
limits: {
primary: { used_percent: 31, window_minutes: 300, resets_at: Math.round(now.getTime() / 1000 + 2 * 3600) },
secondary: { used_percent: 64, window_minutes: 10080, resets_at: Math.round(now.getTime() / 1000 + 3 * 86400) },
extra: [{ used_percent: 32, window_minutes: 10080, resets_at: Math.round(now.getTime() / 1000 + 3 * 86400), label: "Weekly limit (Fable)" }],
},
},
{
agent: "codex",
account: "default",
email: "you@example.com",
at: at(gpu ? 5 : 0.05),
limits: {
plan_type: "pro",
primary: { used_percent: 34, window_minutes: 300, resets_at: Math.round(now.getTime() / 1000 + 3 * 3600 + 1200) },
secondary: { used_percent: 12, window_minutes: 10080, resets_at: Math.round(now.getTime() / 1000 + 4 * 86400) },
},
},
...(gpu
? [
{
agent: "claude" as const,
account: "work",
Comment thread
tinhthanh marked this conversation as resolved.
Resolved
email: "you@work.example",
at: at(0.1),
limits: {
primary: { used_percent: 12, window_minutes: 300, resets_at: Math.round(now.getTime() / 1000 + 4 * 3600) },
secondary: { used_percent: 41, window_minutes: 10080, resets_at: Math.round(now.getTime() / 1000 + 5 * 86400) },
},
},
]
: []),
],
signed_out: gpu ? [] : [{ agent: "claude" as const, account: "lapsed", email: "you@lapsed.example", org: null }],
};
}
42 changes: 42 additions & 0 deletions app/src/lib/usage-limits.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// node --experimental-strip-types --test src/lib/usage-limits.test.ts (pnpm test)
import assert from "node:assert/strict";
import { test } from "node:test";

import type { Account, Limits, Report } from "../../../plugins/usage/src/box.ts";
import { dedupeLimits, type Source } from "../../../plugins/usage/src/data.ts";

const win = { used_percent: 10, window_minutes: 300, resets_at: 2_000_000_000 };
const limits = (account: string, at: string, org?: string): Limits => ({ agent: "claude", account, at, ...(org ? { org } : {}), limits: { primary: win, secondary: null } });
const report = (l: Limits[]): Report => ({ generated: "", days: 7, since: "2026-10-08", today: "2026-10-14", tz: "+00", partial: false, pending: 0, files: 0, daily: [], sessions: [], limits: l });
const source = (box: string, l: Limits[]) => ({ box, report: report(l) }) as unknown as Source;
const account = (id: string, email: string): Account => ({ agent: "claude", id, email }) as Account;

test("one email signed in to two plans shows both on the Usage screen, each named by its account", () => {
const accounts = { devl: [account("ana-team", "ana@acme.test"), account("ana-max", "ana@acme.test")] };
const got = dedupeLimits("claude", [source("devl", [limits("ana-team", "2026-10-14T10:00:00Z", "o-team"), limits("ana-max", "2026-10-14T10:00:00Z", "o-max")])], accounts, 7);
assert.deepEqual(got.map((l) => l.label).sort(), ["ana@acme.test · ana-max", "ana@acme.test · ana-team"]);
assert.equal(new Set(got.map((l) => l.key)).size, 2);
});

test("one login read on two boxes is one row, as most recently read", () => {
const accounts = { devl: [account("default", "bo@acme.test")], gpu: [account("default", "bo@acme.test")] };
const got = dedupeLimits("claude", [source("devl", [limits("default", "2026-10-14T10:00:00Z", "o-1")]), source("gpu", [limits("default", "2026-10-14T11:00:00Z", "o-1")])], accounts, 7);
assert.equal(got.length, 1);
assert.deepEqual(got[0].boxes, ["devl", "gpu"]);
assert.equal(got[0].limits.at, "2026-10-14T11:00:00Z");
// One login needs no name.
assert.equal(got[0].label, undefined);
});

test("two plans of one email, each the default login of its own box, are told apart by box", () => {
const accounts = { devl: [account("default", "ana@acme.test")], gpu: [account("default", "ana@acme.test")] };
const got = dedupeLimits("claude", [source("devl", [limits("default", "2026-10-14T10:00:00Z", "o-team")]), source("gpu", [limits("default", "2026-10-14T10:00:00Z", "o-max")])], accounts, 7);
assert.deepEqual(got.map((l) => l.label).sort(), ["ana@acme.test · default (devl)", "ana@acme.test · default (gpu)"]);
});

test("a box that listed no accounts still has its login named by the report's email", () => {
const read = (at: string): Limits => ({ ...limits("default", at, "o-1"), email: "bo@acme.test" });
const got = dedupeLimits("claude", [source("devl", [read("2026-10-14T10:00:00Z")]), source("gpu", [read("2026-10-14T11:00:00Z")])], {}, 7);
assert.equal(got.length, 1);
assert.deepEqual(got[0].boxes, ["devl", "gpu"]);
});
19 changes: 19 additions & 0 deletions app/src/lib/usage-plan.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// node --experimental-strip-types --test src/lib/usage-plan.test.ts (pnpm test)
import assert from "node:assert/strict";
import { test } from "node:test";

import type { Account } from "../../../plugins/usage/src/box.ts";
import { plan } from "../../../plugins/usage/src/data.ts";

const claude = (over: Partial<Account>): Account => ({ agent: "claude", id: "x", dir: "/x", exists: true, signed_in: true, billing: "stripe_subscription", ...over }) as Account;

test("a team seat is named by its organization, not the Max-like tier it records", () => {
assert.equal(plan(claude({ tier: "default_claude_max_5x", org_type: "claude_team" }))?.label, "Claude Team");
assert.equal(plan(claude({ tier: "default_claude_max_5x", org_type: "claude_enterprise" }))?.label, "Claude Enterprise");
});

test("a personal plan is named by its tier", () => {
assert.equal(plan(claude({ tier: "default_claude_max_20x", org_type: "claude_max" }))?.label, "Claude Max 20x");
assert.equal(plan(claude({ tier: "default_claude_max_5x" }))?.label, "Claude Max 5x");
assert.equal(plan(claude({ tier: "default_claude_pro", org_type: "claude_pro" }))?.label, "Claude Pro");
});
17 changes: 17 additions & 0 deletions app/src/lib/usage-py.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// node --experimental-strip-types --test src/lib/usage-py.test.ts (pnpm test)
// Runs usage.py's own tests (plugins/usage/box/usage_test.py) in the app's
// test run, so the box-side script is checked where CI checks everything else.
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import { dirname, join } from "node:path";
import { test } from "node:test";
import { fileURLToPath } from "node:url";

const box = join(dirname(fileURLToPath(import.meta.url)), "..", "..", "..", "plugins", "usage", "box");
const python = spawnSync("python3", ["--version"]).status === 0;

test("usage.py reads Claude Code's /usage", { skip: !python && "no python3" }, () => {
// A hung run fails the test rather than holding up the whole suite.
const r = spawnSync("python3", ["-m", "unittest", "-q", "usage_test.py"], { cwd: box, encoding: "utf8", timeout: 120_000 });
assert.equal(r.status, 0, r.stderr || r.stdout);
});
Loading