import { describe, expect, test } from "bun:test"; import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { accessTokenFromCredentials, afterCacheDay, machineFingerprint, parseClaudeAccount, parseOAuthUsage, parseStatsCache, probeClaudeUsage, resetMachineFingerprintCache, scanProjectSessions, usageWindowFrom, } from "../src/claude_usage"; import { withLastGoodQuota } from "../src/metrics"; import { claudeAccounts, claudeFleetUsage, claudeMachineKey, claudeMaxPlanCostUsd, claudeQuotaByAccount, claudeQuotaFor, claudeTokenTotal, claudeWindowUsage, claudeSourceBreakdown, claudeUsageForBroadcast, type ClaudeDailyUsage, type ClaudeTokenTotals, type ClaudeUsage, } from "@bbbug/shared/protocol"; import { classifyClaudeSession } from "@bbbug/shared/claude_usage_source"; describe("usageWindowFrom", () => { test("reads utilization + ISO reset", () => { expect(usageWindowFrom({ utilization: 12, resets_at: "2026-09-12T08:20:00.076068+00:00", })).toEqual({ usedPct: 12, resetsAt: Date.parse("2026-09-12T08:20:00.076068+00:00"), }); }); test("clamps percent and treats 0 as a real reading, not unknown", () => { expect(usageWindowFrom({ percent: 0, resets_at: "2026-09-12T00:00:00Z" })?.usedPct).toBe(0); expect(usageWindowFrom({ utilization: 140 })?.usedPct).toBe(100); }); test("returns null on garbage rather than a fabricated zero", () => { expect(usageWindowFrom(null)).toBeNull(); expect(usageWindowFrom("nope")).toBeNull(); expect(usageWindowFrom({})).toBeNull(); }); }); describe("parseOAuthUsage", () => { test("prefers the named five_hour / seven_day fields", () => { const u = parseOAuthUsage({ five_hour: { utilization: 12, resets_at: "2026-09-12T08:20:00Z" }, seven_day: { utilization: 52, resets_at: "2026-09-13T14:00:00Z" }, seven_day_sonnet: null, seven_day_opus: { utilization: 3, resets_at: "2026-09-13T14:00:00Z" }, limits: [{ kind: "session", percent: 99, resets_at: "2026-01-01T00:00:00Z" }], }); expect(u.fiveHour?.usedPct).toBe(12); expect(u.sevenDay?.usedPct).toBe(52); expect(u.sevenDaySonnet).toBeNull(); expect(u.sevenDayOpus?.usedPct).toBe(3); }); test("falls back to limits[] when named windows are missing", () => { const u = parseOAuthUsage({ limits: [ { kind: "session", percent: 8, resets_at: "2026-09-12T08:20:00Z" }, { kind: "weekly_all", percent: 40, resets_at: "2026-09-13T14:00:00Z" }, { kind: "weekly_scoped", percent: 5, resets_at: "2026-09-13T14:00:00Z", scope: { model: { display_name: "Sonnet" } } }, { kind: "weekly_scoped", percent: 1, resets_at: "2026-09-13T14:00:00Z", scope: { model: { display_name: "Opus" } } }, ], }); expect(u.fiveHour?.usedPct).toBe(8); expect(u.sevenDay?.usedPct).toBe(40); expect(u.sevenDaySonnet?.usedPct).toBe(5); expect(u.sevenDayOpus?.usedPct).toBe(1); }); test("still reads limits[] when the named windows are present", () => { // Returning as soon as five_hour and seven_day parsed left the per-model // weekly windows null on every node in the fleet. const u = parseOAuthUsage({ five_hour: { utilization: 12, resets_at: "2026-09-12T08:20:00Z" }, seven_day: { utilization: 52, resets_at: "2026-09-13T14:00:00Z" }, limits: [ { kind: "weekly_scoped", percent: 5, resets_at: "2026-09-13T14:00:00Z", scope: { model: { display_name: "Sonnet" } } }, { kind: "weekly_scoped", percent: 1, resets_at: "2026-09-13T14:00:00Z", scope: { model: { display_name: "Opus" } } }, ], }); expect(u.fiveHour?.usedPct).toBe(12); expect(u.sevenDaySonnet?.usedPct).toBe(5); expect(u.sevenDayOpus?.usedPct).toBe(1); }); test("a named window with holes loses to a complete one in limits[]", () => { // Production shape: `five_hour` came back as utilization 0 with no reset // while limits[] held the real session window, and the panel showed 0%. const u = parseOAuthUsage({ five_hour: { utilization: 0 }, seven_day: { utilization: 27, resets_at: "2026-09-13T14:00:00Z" }, limits: [{ kind: "session", percent: 44, resets_at: "2026-09-12T08:20:00Z" }], }); expect(u.fiveHour).toEqual({ usedPct: 44, resetsAt: Date.parse("2026-09-12T08:20:00Z") }); expect(u.sevenDay?.usedPct).toBe(27); }); }); describe("parseStatsCache", () => { test("sums per-model token totals, prices by family, and keeps the date span", () => { const { tokens, asOf, startedAt, apiCostUsd } = parseStatsCache({ lastComputedDate: "2026-09-11", firstSessionDate: "2026-06-12T10:00:00.000Z", modelUsage: { "claude-sonnet-4-5": { inputTokens: 1_000_000, outputTokens: 0, cacheReadInputTokens: 0, cacheCreationInputTokens: 0, }, "claude-opus-4-6": { inputTokens: 1_000_000, outputTokens: 0, cacheReadInputTokens: 0, cacheCreationInputTokens: 0, }, }, }); expect(asOf).toBe("2026-09-11"); expect(startedAt).toBe(Date.parse("2026-06-12T10:00:00.000Z")); expect(tokens).toEqual({ input: 2_000_000, output: 0, cacheRead: 0, cacheCreation: 0 }); // 1M sonnet input ($3) + 1M opus input ($15) expect(apiCostUsd).toBeCloseTo(18, 6); }); test("missing modelUsage is unknown, not zero", () => { expect(parseStatsCache({ lastComputedDate: "2026-09-11" })).toEqual({ tokens: null, asOf: "2026-09-11", startedAt: null, apiCostUsd: null, }); expect(parseStatsCache(null).tokens).toBeNull(); }); }); describe("session jsonl", () => { test("afterCacheDay is the UTC day after lastComputedDate", () => { const day = Date.parse("2026-07-21T12:00:00Z"); expect(afterCacheDay(day, "2026-07-21")).toBe(false); expect(afterCacheDay(Date.parse("2026-07-22T00:00:00Z"), "2026-07-21")).toBe(true); expect(afterCacheDay(day, null)).toBe(true); }); test("sums assistant turns after the cache day and keeps the full date span", () => { const root = mkdtempSync(join(tmpdir(), "claude-sess-")); const dir = join(root, "proj"); mkdirSync(dir); writeFileSync(join(dir, "old.jsonl"), [ JSON.stringify({ type: "assistant", timestamp: "2026-07-20T10:00:00.000Z", message: { model: "claude-sonnet-4-5", usage: { input_tokens: 100, output_tokens: 0, cache_read_input_tokens: 0, cache_creation_input_tokens: 0 } }, }), JSON.stringify({ type: "assistant", timestamp: "2026-07-22T10:00:00.000Z", message: { model: "claude-opus-4-6", usage: { input_tokens: 1_000_000, output_tokens: 0, cache_read_input_tokens: 0, cache_creation_input_tokens: 0 } }, }), ].join("\n")); const scan = scanProjectSessions(dir, "2026-07-21", Date.parse("2026-07-22T23:00:00Z")); expect(scan.firstAt).toBe(Date.parse("2026-07-20T10:00:00.000Z")); expect(scan.lastAt).toBe(Date.parse("2026-07-22T10:00:00.000Z")); expect(scan.tokens).toEqual({ input: 1_000_000, output: 0, cacheRead: 0, cacheCreation: 0 }); expect(scan.apiCostUsd).toBeCloseTo(15, 6); rmSync(root, { recursive: true, force: true }); }); test("the daily series buckets by UTC day and ignores the stats-cache cutoff", () => { const root = mkdtempSync(join(tmpdir(), "claude-daily-")); const dir = join(root, "proj"); mkdirSync(dir); const turn = (ts: string, input: number) => JSON.stringify({ type: "assistant", timestamp: ts, message: { model: "claude-sonnet-4-5", usage: { input_tokens: input, output_tokens: 0, cache_read_input_tokens: 0, cache_creation_input_tokens: 0 } }, }); writeFileSync(join(dir, "a.jsonl"), [ turn("2026-07-20T01:00:00.000Z", 100), turn("2026-07-20T23:30:00.000Z", 50), turn("2026-07-22T10:00:00.000Z", 7), // Older than keepDays: kept out of the series, still counted in firstAt. turn("2026-01-01T10:00:00.000Z", 999), ].join("\n")); const scan = scanProjectSessions(dir, "2026-07-21", Date.parse("2026-07-22T23:00:00Z"), 35); // 2026-07-20 predates the cache cutoff, so it is absent from the lifetime // sum but present here — the series is built from the logs alone. expect(scan.tokens!.input).toBe(7); expect(scan.daily!.map(({ bySource: _s, ...d }) => d)).toEqual([ { date: "2026-07-20", tokens: { input: 150, output: 0, cacheRead: 0, cacheCreation: 0 }, apiCostUsd: 150 * 3 / 1e6 }, { date: "2026-07-22", tokens: { input: 7, output: 0, cacheRead: 0, cacheCreation: 0 }, apiCostUsd: 7 * 3 / 1e6 }, ]); expect(scan.firstAt).toBe(Date.parse("2026-01-01T10:00:00.000Z")); rmSync(root, { recursive: true, force: true }); }); test("a readable but quiet month is an empty series, not unknown", () => { const root = mkdtempSync(join(tmpdir(), "claude-quiet-")); mkdirSync(join(root, "proj")); expect(scanProjectSessions(join(root, "proj")).daily).toEqual([]); expect(scanProjectSessions(join(root, "missing")).daily).toBeNull(); rmSync(root, { recursive: true, force: true }); }); }); describe("usage by source", () => { const NOW = Date.parse("2026-07-22T23:00:00Z"); const user = (text: string, cwd = "/Users/fei/ai_workspace") => JSON.stringify({ type: "user", timestamp: "2026-07-22T09:00:00.000Z", cwd, message: { role: "user", content: text }, }); const toolResult = () => JSON.stringify({ type: "user", timestamp: "2026-07-22T09:00:01.000Z", message: { role: "user", content: [{ type: "tool_result", tool_use_id: "x", content: "ok" }] }, }); const turn = (id: string, input: number, ts = "2026-07-22T10:00:00.000Z") => JSON.stringify({ type: "assistant", timestamp: ts, requestId: `req-${id}`, message: { id, model: "claude-sonnet-4-5", usage: { input_tokens: input, output_tokens: 0, cache_read_input_tokens: 0, cache_creation_input_tokens: 0 } }, }); const sources = (scan: ReturnType) => Object.fromEntries(scan.daily!.flatMap((d) => d.bySource ?? []).map((s) => [s.source, s.tokens])); test("one API response written as several content-block lines is counted once", () => { const root = mkdtempSync(join(tmpdir(), "claude-dedupe-")); const dir = join(root, "-Users-fei-ai-workspace"); mkdirSync(dir); writeFileSync(join(dir, "s.jsonl"), [ user("你是 TikTok 项目的自主 Bug 修复 Agent。本次任务的工作区在 /x"), turn("msg_1", 100), turn("msg_1", 100), turn("msg_1", 100), turn("msg_2", 5), ].join("\n")); const scan = scanProjectSessions(dir, null, NOW); expect(scan.tokens!.input).toBe(105); expect(scan.daily![0].tokens.input).toBe(105); expect(sources(scan)).toEqual({ bugfix: 105 }); rmSync(root, { recursive: true, force: true }); }); test("sessions bill to the pipeline their first prompt names; subagents to their parent", () => { const root = mkdtempSync(join(tmpdir(), "claude-src-")); const dir = join(root, "-Users-fei-ai-workspace"); mkdirSync(join(dir, "feat-1", "subagents"), { recursive: true }); writeFileSync(join(dir, "feat-1.jsonl"), [ user("你是 TikTok 项目的自主 Feature 实现 Agent。本次任务的工作区在 /x"), turn("f1", 10), ].join("\n")); writeFileSync(join(dir, "feat-1", "subagents", "agent-a.jsonl"), [ user("Find where the comment panel is created"), turn("f2", 3), ].join("\n")); writeFileSync(join(dir, "eval.jsonl"), [ user("【基线】本单是**特性验收 bug**,基线是分支 `x`\n\n请使用中文生成 TestSpec 以及 REASON 行的内容"), turn("e1", 20), ].join("\n")); writeFileSync(join(dir, "mystery.jsonl"), [user("do something odd"), turn("u1", 7)].join("\n")); const scan = scanProjectSessions(dir, null, NOW); expect(sources(scan)).toEqual({ feature: 13, eval: 20, unknown: 7 }); expect(scan.unknownSamples).toEqual([{ prefix: "do something odd", cwd: "ai_workspace", sessions: 1, tokens: 7 }]); rmSync(root, { recursive: true, force: true }); }); test("Asking turns split into ask vs edit by the code-mode wrap-up block", () => { const root = mkdtempSync(join(tmpdir(), "claude-asking-")); const dir = join(root, "-Users-fei-ai-workspace"); mkdirSync(dir); writeFileSync(join(dir, "ask.jsonl"), [ user("你是 TikTok 项目「人工复核」阶段的复核协作 Agent(代号 Asking)。\n## 复核人消息\n为什么这样改"), turn("a1", 10), user("把按钮改成蓝色\n\n[代码模式·收尾要求] 本回合改完代码"), turn("a2", 30), toolResult(), turn("a3", 40), user("现在可以了吗"), turn("a4", 2), ].join("\n")); const scan = scanProjectSessions(dir, null, NOW); expect(sources(scan)).toEqual({ asking: 12, asking_edit: 70 }); rmSync(root, { recursive: true, force: true }); }); test("未识别 is only for sessions that are certainly the platform's", () => { const root = mkdtempSync(join(tmpdir(), "claude-human-")); const dir = join(root, "-Users-b-ai-workspace"); mkdirSync(dir); const mode = (m: string) => JSON.stringify({ type: "permission-mode", permissionMode: m }); // Someone at the keyboard in the AI workspace: a `!` command an hour in. writeFileSync(join(dir, "figma.jsonl"), [ mode("auto"), user("https://www.figma.com/design/xyz 看下这个能不能读"), turn("h1", 30), user("kinit someone"), turn("h2", 5), ].join("\n")); // Same workspace, no markers, but not in bypass mode: still a person. writeFileSync(join(dir, "rebase.jsonl"), [mode("auto"), user("帮我把这个分支 rebase 到 develop"), turn("h3", 7)].join("\n")); // A platform-shaped session the rules do not know: this is what 未识别 is for. writeFileSync(join(dir, "new-pipeline.jsonl"), [mode("bypassPermissions"), user("You are a brand new pipeline"), turn("p1", 11)].join("\n")); const scan = scanProjectSessions(dir, null, NOW); expect(sources(scan)).toEqual({ human: 42, unknown: 11 }); expect(scan.unknownSamples.map((u) => u.prefix)).toEqual(["You are a brand new pipeline"]); rmSync(root, { recursive: true, force: true }); }); test("classifier: unknown only inside automation dirs; elsewhere it is someone at the keyboard", () => { expect(classifyClaudeSession("你是 TikTok 项目的 **真机走查 Agent**。", "/x/ai_workspace")).toBe("verify"); expect(classifyClaudeSession("## 接力 —— 这是本轮的第 3 段会话", "/x/ai_workspace")).toBe("verify"); expect(classifyClaudeSession("You transform verbose engineering Markdown into", "/x/ai_workspace")).toBe("report"); expect(classifyClaudeSession("\n你是MR评估Agent。", "/Users/b/bug_workspace/security-1")).toBe("eval"); expect(classifyClaudeSession("你在审计 coding agent 的一段历史执行记录。", "/private/tmp")).toBe("rsi"); expect(classifyClaudeSession("只回答 OK", "/Users/b/streamline")).toBe("probe"); expect(classifyClaudeSession("Caveat", "/Users/b/ai_workspace")).toBe("human"); expect(classifyClaudeSession("帮我看下构建为什么慢", "/Users/fei/BBBBBBug")).toBe("human"); expect(classifyClaudeSession("帮我看下构建为什么慢", "-Users-fei-ai-workspace")).toBe("unknown"); // Test sessions a person's Claude spawned in its scratchpad — the path // carries the parent's ai-workspace cwd, but it is still not the platform. expect(classifyClaudeSession("This is a harness test, follow exactly.", "/private/tmp/claude-501/-Users-b-do-not-scan-ai-workspace-70ee/scratchpad/ptytest-2-1-287-bash", { permissionModes: ["bypassPermissions"] })).toBe("human"); // A bugfix prompt that quotes another pipeline's phrase deep in its body // still bills to bugfix: execution identities are matched at the head. expect(classifyClaudeSession("你是 TikTok 项目的自主 Bug 修复 Agent。……You transform verbose engineering Markdown", "/x/ai_workspace")).toBe("bugfix"); }); }); describe("fleet source breakdown", () => { const now = Date.parse("2026-07-30T12:00:00Z"); const day = 86_400_000; const base = (over: Partial): ClaudeUsage => ({ at: now, ms: 1, error: null, email: "a@x.com", plan: "max", machineId: "m1", fiveHour: null, sevenDay: null, sevenDaySonnet: null, sevenDayOpus: null, quotaAt: null, tokens: null, tokensAsOf: null, startedAt: null, apiCostUsd: null, daily: [], dailyFromAt: now - 29 * day, ...over, }); const d = (ago: number, bySource: [string, number, number][]) => { const total = bySource.reduce((a, s) => a + s[1], 0); return { date: new Date(now - ago * day).toISOString().slice(0, 10), tokens: { input: total, output: 0, cacheRead: 0, cacheCreation: 0 }, apiCostUsd: bySource.reduce((a, s) => a + s[2], 0), bySource: bySource.map(([source, tokens, apiCostUsd]) => ({ source: source as never, tokens, apiCostUsd, sessions: 1 })), }; }; test("two satellites on one machine count once; old satellites stay out of the slices", () => { const shared = base({ daily: [d(0, [["bugfix", 100, 1], ["unknown", 10, 0.5]]), d(10, [["eval", 50, 2]])] }); const legacy = base({ machineId: "m2", daily: [{ date: new Date(now).toISOString().slice(0, 10), tokens: { input: 70, output: 0, cacheRead: 0, cacheCreation: 0 }, apiCostUsd: 1 }], }); const b = claudeSourceBreakdown([ { nodeId: "ios", usage: shared }, { nodeId: "android", usage: { ...shared, at: now - 1000 } }, { nodeId: "old", usage: legacy }, ], now, 30); expect(b.machines).toBe(1); expect(b.legacyMachines).toBe(1); expect(b.legacyTokens).toBe(70); expect(b.tokens).toBe(160); expect(b.sources.map((s) => [s.source, s.tokens])).toEqual([["bugfix", 100], ["eval", 50], ["unknown", 10]]); expect(b.days).toHaveLength(30); expect(b.days[29].tokens).toEqual({ bugfix: 100, unknown: 10 }); expect(b.days[19].usd).toEqual({ eval: 2 }); }); test("the node:status push carries no per-source detail", () => { const u = base({ daily: [d(0, [["bugfix", 1, 1]])], unknownSamples: [{ prefix: "x", cwd: "y", sessions: 1, tokens: 1 }] }); const light = claudeUsageForBroadcast(u)!; expect(light.unknownSamples).toBeUndefined(); expect(light.daily![0].bySource).toBeUndefined(); expect(light.daily![0].tokens.input).toBe(1); expect(u.daily![0].bySource).toHaveLength(1); }); }); describe("credentials / account", () => { test("reads claudeAiOauth without inventing a token", () => { expect(accessTokenFromCredentials({})).toBeNull(); expect(accessTokenFromCredentials({ claudeAiOauth: { accessToken: "" } })).toBeNull(); expect(accessTokenFromCredentials({ claudeAiOauth: { accessToken: "tok", expiresAt: 1_700_000_000_000, subscriptionType: "max" }, })).toEqual({ token: "tok", expiresAt: 1_700_000_000_000, plan: "max" }); }); test("promotes unix-seconds expiry to milliseconds", () => { expect(accessTokenFromCredentials({ claudeAiOauth: { accessToken: "tok", expiresAt: 1_789_000_000 }, })?.expiresAt).toBe(1_789_000_000_000); }); test("reads email from ~/.claude.json oauthAccount", () => { expect(parseClaudeAccount({ oauthAccount: { emailAddress: "dev@bytedance.com", organizationType: "claude_max" }, })).toEqual({ email: "dev@bytedance.com", plan: "claude_max" }); }); }); describe("probeClaudeUsage", () => { const files: Record = { "/tmp/claude-test/stats-cache.json": JSON.stringify({ lastComputedDate: "2026-09-11", firstSessionDate: "2026-08-12T00:00:00.000Z", modelUsage: { sonnet: { inputTokens: 10, outputTokens: 2, cacheReadInputTokens: 0, cacheCreationInputTokens: 0 } }, }), "/tmp/home/.claude.json": JSON.stringify({ oauthAccount: { emailAddress: "n@x.com", organizationType: "claude_max" }, }), }; test("publishes local tokens even when OAuth credentials are missing", async () => { const snap = await probeClaudeUsage({ now: () => 1_700_000_000_000, homedir: () => "/tmp/home", configDir: "/tmp/claude-test", fileExists: (p) => p in files, readFile: (p) => files[p]!, readKeychain: async () => null, }); expect(snap.tokens).toEqual({ input: 10, output: 2, cacheRead: 0, cacheCreation: 0 }); expect(snap.startedAt).toBe(Date.parse("2026-08-12T00:00:00.000Z")); expect(snap.apiCostUsd).toBeCloseTo((10 * 3 + 2 * 15) / 1_000_000, 10); expect(snap.email).toBe("n@x.com"); expect(snap.fiveHour).toBeNull(); expect(snap.error).toContain("credentials unavailable"); }); test("still calls the usage endpoint when the stored expiry is in the past", async () => { let fetched = 0; const snap = await probeClaudeUsage({ now: () => 1_700_000_000_000, homedir: () => "/tmp/home", configDir: "/tmp/claude-test", fileExists: (p) => p in files, readFile: (p) => files[p]!, readKeychain: async () => JSON.stringify({ claudeAiOauth: { accessToken: "tok", expiresAt: 1_600_000_000_000, subscriptionType: "max" }, }), fetchImpl: (async () => { fetched++; return new Response(JSON.stringify({ five_hour: { utilization: 4, resets_at: "2026-09-12T08:20:00Z" }, seven_day: { utilization: 10, resets_at: "2026-09-13T14:00:00Z" }, })); }) as typeof fetch, }); expect(fetched).toBe(1); expect(snap.error).toBeNull(); expect(snap.fiveHour?.usedPct).toBe(4); expect(snap.plan).toBe("max"); expect(snap.tokens?.input).toBe(10); }); test("fills quota windows from a successful OAuth response", async () => { const snap = await probeClaudeUsage({ now: () => 1_700_000_000_000, homedir: () => "/tmp/home", configDir: "/tmp/claude-test", fileExists: (p) => p in files, readFile: (p) => files[p]!, readKeychain: async () => JSON.stringify({ claudeAiOauth: { accessToken: "tok", expiresAt: 1_800_000_000_000, subscriptionType: "max" }, }), fetchImpl: (async () => new Response(JSON.stringify({ five_hour: { utilization: 12, resets_at: "2026-09-12T08:20:00Z" }, seven_day: { utilization: 52, resets_at: "2026-09-13T14:00:00Z" }, }))) as typeof fetch, }); expect(snap.error).toBeNull(); expect(snap.fiveHour?.usedPct).toBe(12); expect(snap.sevenDay?.usedPct).toBe(52); expect(snap.tokens?.input).toBe(10); }); test("keeps local tokens when the OAuth call fails", async () => { const snap = await probeClaudeUsage({ now: () => 1_700_000_000_000, homedir: () => "/tmp/home", configDir: "/tmp/claude-test", fileExists: (p) => p in files, readFile: (p) => files[p]!, readKeychain: async () => JSON.stringify({ claudeAiOauth: { accessToken: "tok", expiresAt: 1_800_000_000_000 }, }), fetchImpl: (async () => new Response("nope", { status: 429 })) as typeof fetch, }); expect(snap.error).toBe("oauth usage HTTP 429"); expect(snap.fiveHour).toBeNull(); expect(snap.tokens?.input).toBe(10); }); }); describe("machine fingerprint", () => { test("same machine + same Claude home is one id; a different home is not", async () => { resetMachineFingerprintCache(); const read = async () => "UUID-A"; const a = await machineFingerprint("/Users/x/.claude", read); const again = await machineFingerprint("/Users/x/.claude", read); const otherHome = await machineFingerprint("/Users/y/.claude", read); expect(a).toBe(again!); expect(a).not.toBe(otherHome!); // A raw hardware UUID must not ride the heartbeat. expect(a).not.toContain("UUID-A"); resetMachineFingerprintCache(); }); test("falls back to hostname rather than giving up on identity", async () => { resetMachineFingerprintCache(); expect(await machineFingerprint("/Users/x/.claude", async () => null)).toBeTruthy(); resetMachineFingerprintCache(); }); }); describe("fleet aggregation", () => { const day = 86_400_000; const now = Date.parse("2026-09-22T06:00:00Z"); function tokens(n: number): ClaudeTokenTotals { return { input: n, output: 0, cacheRead: 0, cacheCreation: 0 }; } /** `perDay[i]` is i days before today; 0 means an idle day. */ function daily(perDay: number[]): ClaudeDailyUsage[] { return perDay.map((n, back) => ({ date: new Date(now - back * day).toISOString().slice(0, 10), tokens: tokens(n), apiCostUsd: n / 1_000_000, })).reverse(); } function usage(over: Partial = {}): ClaudeUsage { return { at: now, ms: 100, error: null, email: null, plan: "max", machineId: null, fiveHour: null, sevenDay: null, sevenDaySonnet: null, sevenDayOpus: null, quotaAt: null, tokens: tokens(999), tokensAsOf: "2026-09-22", startedAt: now - 300 * day, apiCostUsd: 1, daily: daily([1, 2, 3]), dailyFromAt: now - 2 * day, ...over, }; } test("window sums only days inside it, and idle days are covered but not active", () => { const w = claudeWindowUsage(usage({ daily: daily([5, 0, 7]), dailyFromAt: now - 9 * day, }), now, 30)!; expect(w.tokens.input).toBe(12); expect(w.activeDays).toBe(2); expect(w.coveredDays).toBe(10); }); test("days older than the window are dropped, not folded in", () => { const old = { date: "2026-01-01", tokens: tokens(1_000_000), apiCostUsd: 3 }; const w = claudeWindowUsage(usage({ daily: [old, ...daily([4])] }), now, 30)!; expect(w.tokens.input).toBe(4); }); test("coverage caps at the window, so an old box is not billed for 300 days", () => { const w = claudeWindowUsage(usage({ dailyFromAt: now - 300 * day }), now, 30)!; expect(w.coveredDays).toBe(30); expect(claudeMaxPlanCostUsd(w.coveredDays)).toBe(200); }); test("a satellite with no daily series is unknown, not zero", () => { expect(claudeWindowUsage(usage({ daily: null }), now, 30)).toBeNull(); }); test("machine key prefers machineId, then account + first session, then the node", () => { expect(claudeMachineKey("n1", usage({ machineId: "abc" }))).toBe( claudeMachineKey("n2", usage({ machineId: "abc" })), ); // Deploy-gap fallback: no machineId yet, but same login and same first session. expect(claudeMachineKey("n1", usage({ email: "a@b.c" }))).toBe( claudeMachineKey("n2", usage({ email: "a@b.c" })), ); expect(claudeMachineKey("n1", usage())).not.toBe(claudeMachineKey("n2", usage())); }); test("two satellites on one Mac count once; two Macs on one login count twice", () => { // The exact production shape: lemon-{android,ios} share a box, fei-{android,ios} // share only a Claude login. Summing per node inflated the fleet by 39%. const sameBox = usage({ machineId: "mach-lemon", email: "lemon@x.com", daily: daily([10]) }); const feiA = usage({ machineId: "mach-fei-a", email: "fei@x.com", daily: daily([10]) }); const feiI = usage({ machineId: "mach-fei-i", email: "fei@x.com", daily: daily([10]) }); const fleet = claudeFleetUsage([ { nodeId: "lemon-android-01", usage: sameBox }, { nodeId: "lemon-ios-01", usage: sameBox }, { nodeId: "fei-android-01", usage: feiA }, { nodeId: "fei-ios-01", usage: feiI }, ], now, 30); expect(fleet.machines.length).toBe(3); expect(claudeTokenTotal(fleet.tokens)).toBe(30); expect(fleet.windowMachines).toBe(3); expect(fleet.machines.find((m) => m.key === "m:mach-lemon")!.nodeIds) .toEqual(["lemon-android-01", "lemon-ios-01"]); }); test("a machine's row keeps the later of the two satellites' reads", () => { const early = usage({ machineId: "m", at: now - 60_000, daily: daily([1]) }); const late = usage({ machineId: "m", at: now, daily: daily([9]) }); const fleet = claudeFleetUsage([ { nodeId: "a", usage: early }, { nodeId: "b", usage: late }, ], now, 30); expect(claudeTokenTotal(fleet.tokens)).toBe(9); }); test("machines too old to send a series are excluded and counted, not zeroed", () => { const fleet = claudeFleetUsage([ { nodeId: "new", usage: usage({ machineId: "m1", daily: daily([6]) }) }, { nodeId: "old", usage: usage({ machineId: "m2", daily: null }) }, ], now, 30); expect(claudeTokenTotal(fleet.tokens)).toBe(6); expect(fleet.windowMachines).toBe(1); expect(fleet.legacyMachines).toBe(1); }); test("a rate-limited node borrows quota from a sibling on the same account", () => { const good = usage({ email: "fei@x.com", machineId: "m1", fiveHour: { usedPct: 28, resetsAt: now + 3600_000 }, sevenDay: { usedPct: 19, resetsAt: now + 5 * day }, quotaAt: now, }); const rateLimited = usage({ email: "fei@x.com", machineId: "m2", error: "oauth usage HTTP 429" }); const entries = [ { nodeId: "fei-ios-01", usage: good }, { nodeId: "fei-android-01", usage: rateLimited }, ]; const byAccount = claudeQuotaByAccount(entries); const borrowed = claudeQuotaFor("fei-android-01", rateLimited, byAccount); expect(borrowed.fiveHour?.usedPct).toBe(28); expect(borrowed.fromNodeId).toBe("fei-ios-01"); // The node that fetched it owns the reading — no "borrowed from" label. expect(claudeQuotaFor("fei-ios-01", good, byAccount).fromNodeId).toBeNull(); }); test("two healthy satellites on one login both own their reading", () => { // Both fetch fine and report the same percentages; calling the one whose // read is six seconds older 借用 would be noise, not information. const win = { fiveHour: { usedPct: 91, resetsAt: now + 3600_000 }, sevenDay: { usedPct: 67, resetsAt: now + day } }; const a = usage({ email: "lemon@x.com", ...win, quotaAt: now }); const b = usage({ email: "lemon@x.com", ...win, quotaAt: now - 6_000 }); const byAccount = claudeQuotaByAccount([{ nodeId: "a", usage: a }, { nodeId: "b", usage: b }]); expect(claudeQuotaFor("a", a, byAccount).fromNodeId).toBeNull(); expect(claudeQuotaFor("b", b, byAccount).fromNodeId).toBeNull(); }); test("accounts roll up once per login, with each machine's tokens counted once", () => { // lemon: one Mac, two satellites. fei: one login, two Macs. const lemon = usage({ machineId: "mach-lemon", email: "lemon@x.com", daily: daily([10]) }); const feiA = usage({ machineId: "mach-fei-a", email: "fei@x.com", daily: daily([3]) }); const feiI = usage({ machineId: "mach-fei-i", email: "fei@x.com", daily: daily([4]) }); const rows = claudeAccounts([ { nodeId: "lemon-android-01", usage: lemon }, { nodeId: "lemon-ios-01", usage: lemon }, { nodeId: "fei-android-01", usage: feiA }, { nodeId: "fei-ios-01", usage: feiI }, ], now, 30); expect(rows.map((r) => r.email).sort()).toEqual(["fei@x.com", "lemon@x.com"]); const lemonRow = rows.find((r) => r.email === "lemon@x.com")!; expect(lemonRow.machines.length).toBe(1); expect(lemonRow.nodeIds).toEqual(["lemon-android-01", "lemon-ios-01"]); expect(claudeTokenTotal(lemonRow.tokens)).toBe(10); const feiRow = rows.find((r) => r.email === "fei@x.com")!; expect(feiRow.machines.length).toBe(2); expect(claudeTokenTotal(feiRow.tokens)).toBe(7); }); test("one node's 429 is not an account outage, but the node is still named", () => { const good = usage({ email: "fei@x.com", machineId: "m1", fiveHour: { usedPct: 28, resetsAt: now + 3600_000 }, quotaAt: now, }); const dead = usage({ email: "fei@x.com", machineId: "m2", error: "oauth usage HTTP 429" }); const [row] = claudeAccounts([ { nodeId: "a", usage: good }, { nodeId: "b", usage: dead }, ], now, 30); expect(row.quota.fiveHour?.usedPct).toBe(28); expect(row.error).toBeNull(); // Pooling the numbers would otherwise hide that one box cannot read them. expect(row.degradedNodeIds).toEqual(["b"]); }); test("an account with no windowed machine reads unknown, never zero", () => { const legacy = usage({ email: "old@x.com", machineId: "m1", daily: null }); const [row] = claudeAccounts([{ nodeId: "old", usage: legacy }], now, 30); expect(row.windowMachines).toBe(0); expect(claudeTokenTotal(row.tokens)).toBe(0); expect(row.machines.length).toBe(1); }); test("an account nobody could read surfaces the probe's own reason", () => { const dead = usage({ email: "solo@x.com", error: "claude oauth credentials unavailable" }); const [row] = claudeAccounts([{ nodeId: "solo", usage: dead }], now, 30); expect(row.quota.fiveHour).toBeNull(); expect(row.error).toBe("claude oauth credentials unavailable"); }); test("nodes with no readable account each stand alone rather than merging into one blank row", () => { const anon = usage({ email: null, machineId: null, startedAt: null }); const rows = claudeAccounts([ { nodeId: "pns-android-01", usage: anon }, { nodeId: "fei-android-03", usage: anon }, ], now, 30); expect(rows.length).toBe(2); }); test("a node that never got quota is dated null, not by its probe time", () => { const never = usage({ email: "solo@x.com", at: now, quotaAt: null }); const byAccount = claudeQuotaByAccount([{ nodeId: "solo", usage: never }]); expect(claudeQuotaFor("solo", never, byAccount).at).toBeNull(); }); test("a complete reading beats a degenerate one on the same account", () => { const degenerate = usage({ email: "a@b.c", fiveHour: { usedPct: 0, resetsAt: null }, quotaAt: now }); const complete = usage({ email: "a@b.c", fiveHour: { usedPct: 41, resetsAt: now + 3600_000 }, sevenDay: { usedPct: 30, resetsAt: now + day }, quotaAt: now - 60_000, }); const byAccount = claudeQuotaByAccount([ { nodeId: "thin", usage: degenerate }, { nodeId: "full", usage: complete }, ]); expect(claudeQuotaFor("thin", degenerate, byAccount).fiveHour?.usedPct).toBe(41); }); test("accounts do not leak into each other", () => { const a = usage({ email: "a@x.com", fiveHour: { usedPct: 80, resetsAt: now }, quotaAt: now }); const b = usage({ email: "b@x.com" }); const byAccount = claudeQuotaByAccount([{ nodeId: "a", usage: a }, { nodeId: "b", usage: b }]); expect(claudeQuotaFor("b", b, byAccount).fiveHour).toBeNull(); }); }); describe("withLastGoodQuota", () => { const base: ClaudeUsage = { at: 2_000, ms: 10, error: null, email: null, plan: null, machineId: null, fiveHour: null, sevenDay: null, sevenDaySonnet: null, sevenDayOpus: null, quotaAt: null, tokens: null, tokensAsOf: null, startedAt: null, apiCostUsd: null, daily: null, dailyFromAt: null, }; const good: ClaudeUsage = { ...base, at: 1_000, quotaAt: 1_000, fiveHour: { usedPct: 30, resetsAt: 9_000 }, sevenDay: { usedPct: 12, resetsAt: 9_000 }, }; test("a failed probe republishes the previous windows, marked stale", () => { const merged = withLastGoodQuota(good, { ...base, error: "oauth usage HTTP 429" }); expect(merged.fiveHour?.usedPct).toBe(30); // `at` moves, `quotaAt` does not — that gap is how a reader dates them. expect(merged.at).toBe(2_000); expect(merged.quotaAt).toBe(1_000); expect(merged.error).toBe("oauth usage HTTP 429"); }); test("a successful probe wins outright, including a drop back to unknown", () => { const fresh = { ...base, quotaAt: 2_000, fiveHour: { usedPct: 5, resetsAt: null } }; expect(withLastGoodQuota(good, fresh).fiveHour?.usedPct).toBe(5); expect(withLastGoodQuota(good, fresh).quotaAt).toBe(2_000); }); test("nothing to carry forward leaves the failure as-is", () => { expect(withLastGoodQuota(null, base).fiveHour).toBeNull(); expect(withLastGoodQuota(base, base).fiveHour).toBeNull(); }); }); describe("Max plan vs API cost helpers", () => { test("30 days of Max is exactly $200, and a partial window prorates", () => { expect(claudeMaxPlanCostUsd(30)).toBe(200); expect(claudeMaxPlanCostUsd(15)).toBe(100); expect(claudeMaxPlanCostUsd(9)).toBe(60); }); });