summaryrefslogtreecommitdiffhomepage
path: root/spec/dispatch/adapter/claude/usage_report_spec.rb
blob: 2a5e430f6916e715272748ca4ff44c6d850b8bef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# frozen_string_literal: true

require "webmock/rspec"

RSpec.describe Dispatch::Adapter::Claude, "#usage_report" do
  let(:api_key)   { "sk-ant-api03-test" }
  let(:oauth_key) { "sk-ant-oat-test-token" }
  let(:base_url)  { "https://api.anthropic.com" }
  let(:usage_url) { "#{base_url}/api/oauth/usage" }
  let(:profile_url) { "#{base_url}/api/oauth/profile" }

  before { WebMock.disable_net_connect! }
  after  { WebMock.reset! }

  # ── Helpers ───────────────────────────────────────────────────────────────

  def make_adapter(key: api_key, oauth: false)
    described_class.new(
      model: "claude-sonnet-4-6",
      api_key: key,
      base_url: base_url,
      is_oauth: oauth
    )
  end

  def stub_usage(body, status: 200)
    stub_request(:get, usage_url)
      .to_return(
        status: status,
        body: JSON.generate(body),
        headers: { "Content-Type" => "application/json" }
      )
  end

  # A realistic usage payload with all four buckets
  let(:full_payload) do
    {
      "five_hour" => { "utilization" => 45.0,  "resets_at" => "2025-01-01T12:00:00Z" },
      "seven_day" => { "utilization" => 92.0,  "resets_at" => "2025-01-07T00:00:00Z" },
      "seven_day_opus" => { "utilization" => 95.5, "resets_at" => "2025-01-07T00:00:00Z" },
      "seven_day_sonnet" => { "utilization" => 100.0, "resets_at" => "2025-01-07T00:00:00Z" }
    }
  end

  # ── API-key mode returns nil immediately ──────────────────────────────────

  describe "API-key (non-OAuth) mode" do
    subject(:adapter) { make_adapter(key: api_key, oauth: false) }

    it "returns nil without making any HTTP call" do
      result = adapter.usage_report
      expect(result).to be_nil
      expect(WebMock).not_to have_requested(:get, usage_url)
    end
  end

  # ── OAuth mode — successful response ─────────────────────────────────────

  describe "OAuth mode — full payload" do
    subject(:adapter) { make_adapter(key: oauth_key, oauth: true) }

    before do
      stub_usage(full_payload)
      stub_request(:get, profile_url).to_return(
        status: 200,
        body: JSON.generate({ "email" => "[email protected]", "account_id" => "acct_123" }),
        headers: { "Content-Type" => "application/json" }
      )
    end

    it "returns a UsageReport" do
      expect(adapter.usage_report).to be_a(Dispatch::Adapter::UsageReport)
    end

    it "sets provider to 'Anthropic (Claude)'" do
      expect(adapter.usage_report.provider).to eq("Anthropic (Claude)")
    end

    it "returns 4 limits (one per bucket)" do
      expect(adapter.usage_report.limits.size).to eq(4)
    end

    it "all limits are UsageLimitEntry objects" do
      expect(adapter.usage_report.limits).to all(be_a(Dispatch::Adapter::UsageLimitEntry))
    end

    it "five_hour entry has :ok status (45%)" do
      entry = adapter.usage_report.limits.find { |e| e.id == "anthropic:5h" }
      expect(entry).not_to be_nil
      expect(entry.status).to eq(:ok)
    end

    it "seven_day entry has :warning status (92%)" do
      entry = adapter.usage_report.limits.find { |e| e.id == "anthropic:7d" }
      expect(entry).not_to be_nil
      expect(entry.status).to eq(:warning)
    end

    it "seven_day_opus entry has :warning status (95.5%)" do
      entry = adapter.usage_report.limits.find { |e| e.id == "anthropic:7d:opus" }
      expect(entry).not_to be_nil
      expect(entry.status).to eq(:warning)
    end

    it "seven_day_sonnet entry has :exhausted status (100%)" do
      entry = adapter.usage_report.limits.find { |e| e.id == "anthropic:7d:sonnet" }
      expect(entry).not_to be_nil
      expect(entry.status).to eq(:exhausted)
    end

    it "amount.used reflects utilization" do
      entry = adapter.usage_report.limits.find { |e| e.id == "anthropic:5h" }
      expect(entry.amount.used).to be_within(0.001).of(45.0)
    end

    it "amount.remaining = 100 - used" do
      entry = adapter.usage_report.limits.find { |e| e.id == "anthropic:5h" }
      expect(entry.amount.remaining).to be_within(0.001).of(55.0)
    end

    it "amount.unit is :percent" do
      adapter.usage_report.limits.each do |entry|
        expect(entry.amount.unit).to eq(:percent)
      end
    end

    it "amount.limit is 100" do
      adapter.usage_report.limits.each do |entry|
        expect(entry.amount.limit).to eq(100)
      end
    end

    it "window duration_ms for 5h is 18_000_000" do
      entry = adapter.usage_report.limits.find { |e| e.id == "anthropic:5h" }
      expect(entry.window.duration_ms).to eq(18_000_000)
    end

    it "window duration_ms for 7d is 604_800_000" do
      entry = adapter.usage_report.limits.find { |e| e.id == "anthropic:7d" }
      expect(entry.window.duration_ms).to eq(604_800_000)
    end

    it "window.resets_at is a Time for five_hour" do
      entry = adapter.usage_report.limits.find { |e| e.id == "anthropic:5h" }
      expect(entry.window.resets_at).to be_a(Time)
    end

    it "five_hour scope has shared: true" do
      entry = adapter.usage_report.limits.find { |e| e.id == "anthropic:5h" }
      expect(entry.scope[:shared]).to be true
    end

    it "seven_day_opus scope has shared: false" do
      entry = adapter.usage_report.limits.find { |e| e.id == "anthropic:7d:opus" }
      expect(entry.scope[:shared]).to be false
    end

    it "seven_day_opus scope has tier: 'opus'" do
      entry = adapter.usage_report.limits.find { |e| e.id == "anthropic:7d:opus" }
      expect(entry.scope[:tier]).to eq("opus")
    end

    it "five_hour label is 'Claude 5 Hour'" do
      entry = adapter.usage_report.limits.find { |e| e.id == "anthropic:5h" }
      expect(entry.label).to eq("Claude 5 Hour")
    end

    it "seven_day_sonnet label is 'Claude 7 Day (Sonnet)'" do
      entry = adapter.usage_report.limits.find { |e| e.id == "anthropic:7d:sonnet" }
      expect(entry.label).to eq("Claude 7 Day (Sonnet)")
    end
  end

  # ── Status thresholds ─────────────────────────────────────────────────────

  describe "status thresholds" do
    subject(:adapter) { make_adapter(key: oauth_key, oauth: true) }

    before do
      stub_request(:get, profile_url).to_return(
        status: 200,
        body: JSON.generate({ "email" => "[email protected]", "account_id" => "acct_123" }),
        headers: { "Content-Type" => "application/json" }
      )
    end

    [
      [89.9, :ok],
      [90.0, :warning],
      [99.9, :warning],
      [100.0, :exhausted],
      [105.0, :exhausted]
    ].each do |utilization, expected_status|
      it "#{utilization}% → #{expected_status}" do
        stub_usage({ "five_hour" => { "utilization" => utilization, "resets_at" => nil } })
        entry = adapter.usage_report.limits.first
        expect(entry.status).to eq(expected_status)
      end
    end
  end

  # ── Empty payload returns nil ─────────────────────────────────────────────

  describe "empty payload (no recognised buckets)" do
    subject(:adapter) { make_adapter(key: oauth_key, oauth: true) }

    before { stub_usage({ "other_field" => "value" }) }

    it "returns nil when no recognised buckets are present" do
      expect(adapter.usage_report).to be_nil
    end
  end

  # ── Profile fetch for missing metadata ────────────────────────────────────

  describe "profile fetch for email/account_id" do
    subject(:adapter) { make_adapter(key: oauth_key, oauth: true) }

    before do
      stub_usage({ "five_hour" => { "utilization" => 50.0 } })
      stub_request(:get, profile_url)
        .to_return(
          status: 200,
          body: JSON.generate({ "email" => "[email protected]",
                                "account_id" => "acct_123" }),
          headers: { "Content-Type" => "application/json" }
        )
    end

    it "fetches profile to populate email" do
      report = adapter.usage_report
      expect(report.metadata[:email]).to eq("[email protected]")
    end

    it "fetches profile to populate account_id" do
      report = adapter.usage_report
      expect(report.metadata[:account_id]).to eq("acct_123")
    end
  end

  describe "email/account_id already in usage payload" do
    subject(:adapter) { make_adapter(key: oauth_key, oauth: true) }

    before do
      payload = full_payload.merge("email" => "[email protected]",
                                   "account_id" => "acct_456")
      stub_usage(payload)
    end

    it "uses email from usage payload without calling profile" do
      report = adapter.usage_report
      expect(report.metadata[:email]).to eq("[email protected]")
      expect(WebMock).not_to have_requested(:get, profile_url)
    end
  end

  # ── Failure returns nil ───────────────────────────────────────────────────

  describe "network failure returns nil" do
    subject(:adapter) { make_adapter(key: oauth_key, oauth: true) }

    before do
      stub_request(:get, usage_url).to_raise(Errno::ECONNREFUSED)
    end

    it "returns nil without raising" do
      expect(adapter.usage_report).to be_nil
    end
  end

  describe "HTTP 500 returns nil after retries" do
    subject(:adapter) { make_adapter(key: oauth_key, oauth: true) }

    before do
      # Suppress sleep in UsageClient retry loop (it's a module_function,
      # sleep is called via Kernel; stub at the UsageClient module level)
      allow(Dispatch::Adapter::Claude::UsageClient).to receive(:sleep)
      stub_request(:get, usage_url)
        .to_return(
          status: 500,
          body: JSON.generate({ "error" => { "message" => "oops" } }),
          headers: { "Content-Type" => "application/json" }
        )
    end

    it "returns nil after exhausting retries" do
      expect(adapter.usage_report).to be_nil
    end
  end

  describe "HTTP 401 returns nil immediately" do
    subject(:adapter) { make_adapter(key: oauth_key, oauth: true) }

    before do
      stub_request(:get, usage_url)
        .to_return(
          status: 401,
          body: JSON.generate({ "error" => { "message" => "Unauthorized" } }),
          headers: { "Content-Type" => "application/json" }
        )
    end

    it "returns nil on auth error without raising" do
      expect(adapter.usage_report).to be_nil
    end
  end
end