|
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { createProviderUsageFetch, makeResponse } from "../test-utils/provider-usage-fetch.js"; |
| 3 | +import { fetchClaudeUsage } from "./provider-usage.fetch.claude.js"; |
| 4 | + |
| 5 | +const MISSING_SCOPE_MESSAGE = "missing scope requirement user:profile"; |
| 6 | + |
| 7 | +function makeMissingScopeResponse() { |
| 8 | + return makeResponse(403, { |
| 9 | + error: { message: MISSING_SCOPE_MESSAGE }, |
| 10 | + }); |
| 11 | +} |
| 12 | + |
| 13 | +function expectMissingScopeError(result: Awaited<ReturnType<typeof fetchClaudeUsage>>) { |
| 14 | + expect(result.error).toBe(`HTTP 403: ${MISSING_SCOPE_MESSAGE}`); |
| 15 | + expect(result.windows).toHaveLength(0); |
| 16 | +} |
| 17 | + |
| 18 | +function createScopeFallbackFetch(handler: (url: string) => Promise<Response> | Response) { |
| 19 | + return createProviderUsageFetch(async (url) => { |
| 20 | + if (url.includes("/api/oauth/usage")) { |
| 21 | + return makeMissingScopeResponse(); |
| 22 | + } |
| 23 | + return handler(url); |
| 24 | + }); |
| 25 | +} |
| 26 | + |
| 27 | +type ScopeFallbackFetch = ReturnType<typeof createScopeFallbackFetch>; |
| 28 | + |
| 29 | +async function expectMissingScopeWithoutFallback(mockFetch: ScopeFallbackFetch) { |
| 30 | + const result = await fetchClaudeUsage("token", 5000, mockFetch); |
| 31 | + expectMissingScopeError(result); |
| 32 | + expect(mockFetch).toHaveBeenCalledTimes(1); |
| 33 | +} |
| 34 | + |
| 35 | +function makeOrgAResponse() { |
| 36 | + return makeResponse(200, [{ uuid: "org-a" }]); |
| 37 | +} |
| 38 | + |
| 39 | +describe("fetchClaudeUsage", () => { |
| 40 | + afterEach(() => { |
| 41 | + vi.unstubAllEnvs(); |
| 42 | + }); |
| 43 | + |
| 44 | + it("parses oauth usage windows", async () => { |
| 45 | + const fiveHourReset = "2026-01-08T00:00:00Z"; |
| 46 | + const weekReset = "2026-01-12T00:00:00Z"; |
| 47 | + const mockFetch = createProviderUsageFetch(async (_url, init) => { |
| 48 | + const headers = (init?.headers as Record<string, string> | undefined) ?? {}; |
| 49 | + expect(headers.Authorization).toBe("Bearer token"); |
| 50 | + expect(headers["anthropic-beta"]).toBe("oauth-2025-04-20"); |
| 51 | + |
| 52 | + return makeResponse(200, { |
| 53 | + five_hour: { utilization: 18, resets_at: fiveHourReset }, |
| 54 | + seven_day: { utilization: 54, resets_at: weekReset }, |
| 55 | + seven_day_sonnet: { utilization: 67 }, |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + const result = await fetchClaudeUsage("token", 5000, mockFetch); |
| 60 | + |
| 61 | + expect(result.windows).toEqual([ |
| 62 | + { label: "5h", usedPercent: 18, resetAt: new Date(fiveHourReset).getTime() }, |
| 63 | + { label: "Week", usedPercent: 54, resetAt: new Date(weekReset).getTime() }, |
| 64 | + { label: "Sonnet", usedPercent: 67 }, |
| 65 | + ]); |
| 66 | + }); |
| 67 | + |
| 68 | + it("returns HTTP errors with provider message suffix", async () => { |
| 69 | + const mockFetch = createProviderUsageFetch(async () => |
| 70 | + makeResponse(403, { |
| 71 | + error: { message: "scope not granted" }, |
| 72 | + }), |
| 73 | + ); |
| 74 | + |
| 75 | + const result = await fetchClaudeUsage("token", 5000, mockFetch); |
| 76 | + expect(result.error).toBe("HTTP 403: scope not granted"); |
| 77 | + expect(result.windows).toHaveLength(0); |
| 78 | + }); |
| 79 | + |
| 80 | + it("falls back to claude web usage when oauth scope is missing", async () => { |
| 81 | + vi.stubEnv("CLAUDE_AI_SESSION_KEY", "sk-ant-session-key"); |
| 82 | + |
| 83 | + const mockFetch = createProviderUsageFetch(async (url, init) => { |
| 84 | + if (url.includes("/api/oauth/usage")) { |
| 85 | + return makeMissingScopeResponse(); |
| 86 | + } |
| 87 | + |
| 88 | + const headers = (init?.headers as Record<string, string> | undefined) ?? {}; |
| 89 | + expect(headers.Cookie).toBe("sessionKey=sk-ant-session-key"); |
| 90 | + |
| 91 | + if (url.endsWith("/api/organizations")) { |
| 92 | + return makeResponse(200, [{ uuid: "org-123" }]); |
| 93 | + } |
| 94 | + |
| 95 | + if (url.endsWith("/api/organizations/org-123/usage")) { |
| 96 | + return makeResponse(200, { |
| 97 | + five_hour: { utilization: 12 }, |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + return makeResponse(404, "not found"); |
| 102 | + }); |
| 103 | + |
| 104 | + const result = await fetchClaudeUsage("token", 5000, mockFetch); |
| 105 | + |
| 106 | + expect(result.error).toBeUndefined(); |
| 107 | + expect(result.windows).toEqual([{ label: "5h", usedPercent: 12, resetAt: undefined }]); |
| 108 | + }); |
| 109 | + |
| 110 | + it("keeps oauth error when cookie header cannot be parsed into a session key", async () => { |
| 111 | + vi.stubEnv("CLAUDE_WEB_COOKIE", "sessionKey=sk-ant-cookie-session"); |
| 112 | + |
| 113 | + const mockFetch = createScopeFallbackFetch(async (url) => { |
| 114 | + if (url.endsWith("/api/organizations")) { |
| 115 | + return makeResponse(200, [{ uuid: "org-cookie" }]); |
| 116 | + } |
| 117 | + if (url.endsWith("/api/organizations/org-cookie/usage")) { |
| 118 | + return makeResponse(200, { seven_day_opus: { utilization: 44 } }); |
| 119 | + } |
| 120 | + return makeResponse(404, "not found"); |
| 121 | + }); |
| 122 | + |
| 123 | + await expectMissingScopeWithoutFallback(mockFetch); |
| 124 | + }); |
| 125 | + |
| 126 | + it("keeps oauth error when fallback session key is unavailable", async () => { |
| 127 | + const mockFetch = createScopeFallbackFetch(async (url) => { |
| 128 | + if (url.endsWith("/api/organizations")) { |
| 129 | + return makeResponse(200, [{ uuid: "org-missing-session" }]); |
| 130 | + } |
| 131 | + return makeResponse(404, "not found"); |
| 132 | + }); |
| 133 | + |
| 134 | + await expectMissingScopeWithoutFallback(mockFetch); |
| 135 | + }); |
| 136 | + |
| 137 | + it.each([ |
| 138 | + { |
| 139 | + name: "org list request fails", |
| 140 | + orgResponse: () => makeResponse(500, "boom"), |
| 141 | + usageResponse: () => makeResponse(200, {}), |
| 142 | + }, |
| 143 | + { |
| 144 | + name: "org list has no id", |
| 145 | + orgResponse: () => makeResponse(200, [{}]), |
| 146 | + usageResponse: () => makeResponse(200, {}), |
| 147 | + }, |
| 148 | + { |
| 149 | + name: "usage request fails", |
| 150 | + orgResponse: makeOrgAResponse, |
| 151 | + usageResponse: () => makeResponse(503, "down"), |
| 152 | + }, |
| 153 | + { |
| 154 | + name: "usage request has no windows", |
| 155 | + orgResponse: makeOrgAResponse, |
| 156 | + usageResponse: () => makeResponse(200, {}), |
| 157 | + }, |
| 158 | + ])( |
| 159 | + "returns oauth error when web fallback is unavailable: $name", |
| 160 | + async ({ orgResponse, usageResponse }) => { |
| 161 | + vi.stubEnv("CLAUDE_AI_SESSION_KEY", "sk-ant-fallback"); |
| 162 | + |
| 163 | + const mockFetch = createScopeFallbackFetch(async (url) => { |
| 164 | + if (url.endsWith("/api/organizations")) { |
| 165 | + return orgResponse(); |
| 166 | + } |
| 167 | + if (url.endsWith("/api/organizations/org-a/usage")) { |
| 168 | + return usageResponse(); |
| 169 | + } |
| 170 | + return makeResponse(404, "not found"); |
| 171 | + }); |
| 172 | + |
| 173 | + const result = await fetchClaudeUsage("token", 5000, mockFetch); |
| 174 | + expectMissingScopeError(result); |
| 175 | + }, |
| 176 | + ); |
| 177 | +}); |
0 commit comments