summaryrefslogtreecommitdiffhomepage
path: root/spec/dispatch/adapter/copilot_rate_limiting_spec.rb
blob: a51ed2b22b0305c9751743454f9c33bbde5f63bf (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
# frozen_string_literal: true

require "webmock/rspec"
require "fileutils"
require "tmpdir"

RSpec.describe Dispatch::Adapter::Copilot, "rate limiting" do
  let(:copilot_token) { "cop_test_token_abc" }
  let(:github_token) { "gho_test_github_token" }
  let(:tmpdir) { Dir.mktmpdir("copilot_rate_limit_test") }
  let(:token_path) { File.join(tmpdir, "copilot_github_token") }

  let(:chat_response_body) do
    JSON.generate({
                    "choices" => [{ "message" => { "content" => "ok" }, "finish_reason" => "stop" }],
                    "usage" => { "prompt_tokens" => 5, "completion_tokens" => 1 }
                  })
  end

  let(:messages) { [Dispatch::Adapter::Message.new(role: "user", content: "Hi")] }

  before do
    stub_request(:get, "https://api.github.com/copilot_internal/v2/token")
      .with(headers: { "Authorization" => "token #{github_token}" })
      .to_return(
        status: 200,
        body: JSON.generate({
                              "token" => copilot_token,
                              "expires_at" => (Time.now.to_i + 3600)
                            }),
        headers: { "Content-Type" => "application/json" }
      )

    stub_request(:post, "https://api.githubcopilot.com/chat/completions")
      .to_return(
        status: 200,
        body: chat_response_body,
        headers: { "Content-Type" => "application/json" }
      )
  end

  after { FileUtils.rm_rf(tmpdir) }

  describe "constructor rate limit parameters" do
    it "accepts default rate limit parameters" do
      adapter = described_class.new(
        model: "gpt-4.1",
        github_token: github_token,
        token_path: token_path
      )
      expect(adapter).to be_a(described_class)
    end

    it "accepts custom min_request_interval" do
      adapter = described_class.new(
        model: "gpt-4.1",
        github_token: github_token,
        token_path: token_path,
        min_request_interval: 5.0
      )
      expect(adapter).to be_a(described_class)
    end

    it "accepts nil min_request_interval to disable cooldown" do
      adapter = described_class.new(
        model: "gpt-4.1",
        github_token: github_token,
        token_path: token_path,
        min_request_interval: nil
      )
      expect(adapter).to be_a(described_class)
    end

    it "accepts rate_limit hash for sliding window" do
      adapter = described_class.new(
        model: "gpt-4.1",
        github_token: github_token,
        token_path: token_path,
        rate_limit: { requests: 10, period: 60 }
      )
      expect(adapter).to be_a(described_class)
    end

    it "raises ArgumentError for invalid min_request_interval" do
      expect do
        described_class.new(
          model: "gpt-4.1",
          github_token: github_token,
          token_path: token_path,
          min_request_interval: -1
        )
      end.to raise_error(ArgumentError)
    end

    it "raises ArgumentError for invalid rate_limit hash" do
      expect do
        described_class.new(
          model: "gpt-4.1",
          github_token: github_token,
          token_path: token_path,
          rate_limit: { requests: 0, period: 60 }
        )
      end.to raise_error(ArgumentError)
    end
  end

  describe "#chat with rate limiting" do
    context "with default 3s cooldown" do
      let(:adapter) do
        described_class.new(
          model: "gpt-4.1",
          github_token: github_token,
          token_path: token_path
        )
      end

      it "does not sleep on the first request" do
        rate_limiter = instance_double(Dispatch::Adapter::RateLimiter)
        allow(Dispatch::Adapter::RateLimiter).to receive(:new).and_return(rate_limiter)
        allow(rate_limiter).to receive(:wait!)

        fresh_adapter = described_class.new(
          model: "gpt-4.1",
          github_token: github_token,
          token_path: token_path
        )
        fresh_adapter.chat(messages)

        expect(rate_limiter).to have_received(:wait!).once
      end

      it "calls wait! before every chat request" do
        rate_limiter = instance_double(Dispatch::Adapter::RateLimiter)
        allow(Dispatch::Adapter::RateLimiter).to receive(:new).and_return(rate_limiter)
        allow(rate_limiter).to receive(:wait!)

        fresh_adapter = described_class.new(
          model: "gpt-4.1",
          github_token: github_token,
          token_path: token_path
        )
        fresh_adapter.chat(messages)
        fresh_adapter.chat(messages)
        fresh_adapter.chat(messages)

        expect(rate_limiter).to have_received(:wait!).exactly(3).times
      end
    end

    context "with rate limiting disabled" do
      let(:adapter) do
        described_class.new(
          model: "gpt-4.1",
          github_token: github_token,
          token_path: token_path,
          min_request_interval: nil,
          rate_limit: nil
        )
      end

      it "does not sleep between rapid requests" do
        rate_limiter = instance_double(Dispatch::Adapter::RateLimiter)
        allow(Dispatch::Adapter::RateLimiter).to receive(:new).and_return(rate_limiter)
        allow(rate_limiter).to receive(:wait!)

        fresh_adapter = described_class.new(
          model: "gpt-4.1",
          github_token: github_token,
          token_path: token_path,
          min_request_interval: nil,
          rate_limit: nil
        )
        fresh_adapter.chat(messages)
        fresh_adapter.chat(messages)

        expect(rate_limiter).to have_received(:wait!).twice
      end
    end
  end

  describe "#chat streaming with rate limiting" do
    it "calls wait! before a streaming request" do
      sse_body = [
        "data: #{JSON.generate({ "choices" => [{ "delta" => { "content" => "hi" }, "index" => 0 }] })}\n\n",
        "data: #{JSON.generate({ "choices" => [{ "delta" => {}, "index" => 0, "finish_reason" => "stop" }],
                                 "usage" => { "prompt_tokens" => 5, "completion_tokens" => 1 } })}\n\n",
        "data: [DONE]\n\n"
      ].join

      stub_request(:post, "https://api.githubcopilot.com/chat/completions")
        .to_return(status: 200, body: sse_body, headers: { "Content-Type" => "text/event-stream" })

      rate_limiter = instance_double(Dispatch::Adapter::RateLimiter)
      allow(Dispatch::Adapter::RateLimiter).to receive(:new).and_return(rate_limiter)
      allow(rate_limiter).to receive(:wait!)

      adapter = described_class.new(
        model: "gpt-4.1",
        github_token: github_token,
        token_path: token_path
      )
      adapter.chat(messages, stream: true) { |_| nil }

      expect(rate_limiter).to have_received(:wait!).once
    end
  end

  describe "#list_models with rate limiting" do
    it "calls wait! before list_models request" do
      stub_request(:get, "https://api.githubcopilot.com/models")
        .to_return(
          status: 200,
          body: JSON.generate({
                                "data" => [{
                                  "id" => "gpt-4.1",
                                  "name" => "GPT 4.1",
                                  "model_picker_enabled" => true,
                                  "capabilities" => { "type" => "chat", "supports" => {} }
                                }]
                              }),
          headers: { "Content-Type" => "application/json" }
        )

      rate_limiter = instance_double(Dispatch::Adapter::RateLimiter)
      allow(Dispatch::Adapter::RateLimiter).to receive(:new).and_return(rate_limiter)
      allow(rate_limiter).to receive(:wait!)

      adapter = described_class.new(
        model: "gpt-4.1",
        github_token: github_token,
        token_path: token_path
      )
      adapter.list_models

      expect(rate_limiter).to have_received(:wait!).once
    end
  end

  describe "rate limit file location" do
    it "stores the rate limit file in the same directory as the token file" do
      adapter = described_class.new(
        model: "gpt-4.1",
        github_token: github_token,
        token_path: token_path
      )
      adapter.chat(messages)

      rate_limit_path = File.join(tmpdir, "copilot_rate_limit")
      expect(File.exist?(rate_limit_path)).to be(true)
    end
  end
end