|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { fetchAnthropicAdminUsage, resolveAnthropicUsageAuth } from "./usage.js"; |
| 3 | + |
| 4 | +function requestUrl(input: string | URL | Request): URL { |
| 5 | + return new URL(input instanceof Request ? input.url : input); |
| 6 | +} |
| 7 | + |
| 8 | +describe("Anthropic provider usage", () => { |
| 9 | + it("aggregates provider-reported costs, cache tokens, models, and categories", async () => { |
| 10 | + const fetchFn = vi.fn(async (input: string | URL | Request, _init?: RequestInit) => { |
| 11 | + const url = requestUrl(input); |
| 12 | + if (url.pathname.endsWith("/organizations/cost_report")) { |
| 13 | + return new Response( |
| 14 | + JSON.stringify({ |
| 15 | + data: [ |
| 16 | + { |
| 17 | + starting_at: "2026-07-06T00:00:00Z", |
| 18 | + ending_at: "2026-07-07T00:00:00Z", |
| 19 | + results: [{ amount: "1234", currency: "USD", description: "Claude API" }], |
| 20 | + }, |
| 21 | + ], |
| 22 | + has_more: false, |
| 23 | + }), |
| 24 | + { status: 200 }, |
| 25 | + ); |
| 26 | + } |
| 27 | + return new Response( |
| 28 | + JSON.stringify({ |
| 29 | + data: [ |
| 30 | + { |
| 31 | + starting_at: "2026-07-06T00:00:00Z", |
| 32 | + ending_at: "2026-07-07T00:00:00Z", |
| 33 | + results: [ |
| 34 | + { |
| 35 | + uncached_input_tokens: 1_000, |
| 36 | + cache_creation: { |
| 37 | + ephemeral_1h_input_tokens: 100, |
| 38 | + ephemeral_5m_input_tokens: 50, |
| 39 | + }, |
| 40 | + cache_read_input_tokens: 300, |
| 41 | + output_tokens: 250, |
| 42 | + model: "claude-opus-4-8", |
| 43 | + }, |
| 44 | + ], |
| 45 | + }, |
| 46 | + ], |
| 47 | + has_more: false, |
| 48 | + }), |
| 49 | + { status: 200 }, |
| 50 | + ); |
| 51 | + }); |
| 52 | + |
| 53 | + const result = await fetchAnthropicAdminUsage({ |
| 54 | + apiKey: "sk-ant-admin-test", |
| 55 | + timeoutMs: 5_000, |
| 56 | + fetchFn: fetchFn as typeof fetch, |
| 57 | + now: Date.parse("2026-07-06T12:00:00Z"), |
| 58 | + periodDays: 2, |
| 59 | + }); |
| 60 | + |
| 61 | + expect(result).toMatchObject({ |
| 62 | + provider: "anthropic", |
| 63 | + plan: "Admin API", |
| 64 | + billing: [{ type: "spend", amount: 12.34, unit: "USD", period: "2d" }], |
| 65 | + costHistory: { |
| 66 | + unit: "USD", |
| 67 | + periodDays: 2, |
| 68 | + daily: [ |
| 69 | + { |
| 70 | + date: "2026-07-06", |
| 71 | + amount: 12.34, |
| 72 | + inputTokens: 1_000, |
| 73 | + cacheWriteTokens: 150, |
| 74 | + cacheReadTokens: 300, |
| 75 | + outputTokens: 250, |
| 76 | + totalTokens: 1_700, |
| 77 | + }, |
| 78 | + ], |
| 79 | + models: [{ name: "claude-opus-4-8", totalTokens: 1_700 }], |
| 80 | + categories: [{ name: "Claude API", amount: 12.34 }], |
| 81 | + }, |
| 82 | + }); |
| 83 | + for (const [input, init] of fetchFn.mock.calls) { |
| 84 | + const url = requestUrl(input); |
| 85 | + expect(url.searchParams.get("bucket_width")).toBe("1d"); |
| 86 | + expect((init as RequestInit).headers).toMatchObject({ |
| 87 | + "anthropic-version": "2023-06-01", |
| 88 | + "x-api-key": "sk-ant-admin-test", |
| 89 | + }); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + it("uses explicit Admin API credentials before Claude OAuth", async () => { |
| 94 | + const result = await resolveAnthropicUsageAuth({ |
| 95 | + config: {}, |
| 96 | + env: { ANTHROPIC_ADMIN_API_KEY: "sk-ant-admin-explicit" }, |
| 97 | + provider: "anthropic", |
| 98 | + resolveApiKeyFromConfigAndStore: () => "sk-ant-oat01-fallback", |
| 99 | + resolveOAuthToken: async () => ({ token: "oauth-token" }), |
| 100 | + }); |
| 101 | + expect(result).toEqual({ |
| 102 | + token: 'openclaw:anthropic-admin:v1:{"token":"sk-ant-admin-explicit"}', |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + it("auto-detects an Admin API key stored in the Anthropic provider profile", async () => { |
| 107 | + const result = await resolveAnthropicUsageAuth({ |
| 108 | + config: {}, |
| 109 | + env: {}, |
| 110 | + provider: "anthropic", |
| 111 | + resolveApiKeyFromConfigAndStore: () => "sk-ant-admin-profile", |
| 112 | + resolveOAuthToken: async () => null, |
| 113 | + }); |
| 114 | + expect(result).toEqual({ |
| 115 | + token: 'openclaw:anthropic-admin:v1:{"token":"sk-ant-admin-profile"}', |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + it("prefers a stored Admin API key when normal API and OAuth credentials coexist", async () => { |
| 120 | + const result = await resolveAnthropicUsageAuth({ |
| 121 | + config: {}, |
| 122 | + env: {}, |
| 123 | + provider: "anthropic", |
| 124 | + resolveApiKeyFromConfigAndStore: () => "sk-ant-api03-inference", |
| 125 | + resolveApiKeyCandidatesFromConfigAndStore: async () => [ |
| 126 | + "sk-ant-api03-inference", |
| 127 | + "sk-ant-admin-billing", |
| 128 | + ], |
| 129 | + resolveOAuthToken: async () => ({ token: "oauth-token" }), |
| 130 | + }); |
| 131 | + expect(result).toEqual({ |
| 132 | + token: 'openclaw:anthropic-admin:v1:{"token":"sk-ant-admin-billing"}', |
| 133 | + }); |
| 134 | + }); |
| 135 | +}); |
0 commit comments