Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit b272d4a

Browse files
committed
tests
1 parent b6ea353 commit b272d4a

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# frozen_string_literal: true
2+
3+
require "rails_helper"
4+
require "webmock/rspec"
5+
6+
RSpec.describe DiscourseAi::Inference::CloudflareWorkersAi do
7+
let(:account_id) { "test_account_id" }
8+
let(:api_token) { "test_api_token" }
9+
let(:model) { "test_model" }
10+
let(:content) { "test content" }
11+
let(:endpoint) do
12+
"https://api.cloudflare.com/client/v4/accounts/#{account_id}/ai/run/@cf/#{model}"
13+
end
14+
let(:headers) do
15+
{
16+
"Referer" => Discourse.base_url,
17+
"Content-Type" => "application/json",
18+
"Authorization" => "Bearer #{api_token}",
19+
}
20+
end
21+
let(:payload) { { text: [content] }.to_json }
22+
23+
subject { described_class.new(account_id, api_token, model) }
24+
25+
before do
26+
stub_request(:post, endpoint).with(body: payload, headers: headers).to_return(
27+
status: response_status,
28+
body: response_body,
29+
)
30+
end
31+
32+
describe "#perform!" do
33+
context "when the response status is 200" do
34+
let(:response_status) { 200 }
35+
let(:response_body) { { result: { data: ["embedding_result"] } }.to_json }
36+
37+
it "returns the embedding result" do
38+
result = subject.perform!(content)
39+
expect(result).to eq("embedding_result")
40+
end
41+
end
42+
43+
context "when the response status is 429" do
44+
let(:response_status) { 429 }
45+
let(:response_body) { "" }
46+
47+
it "doesn't raises a Net::HTTPBadResponse error" do
48+
expect { subject.perform!(content) }.not_to raise_error
49+
end
50+
end
51+
52+
context "when the response status is not 200 or 429" do
53+
let(:response_status) { 500 }
54+
let(:response_body) { "Internal Server Error" }
55+
56+
it "raises a Net::HTTPBadResponse error" do
57+
expect(Rails.logger).to receive(:warn).with(
58+
"Cloudflare Workers AI Embeddings failed with status: #{response_status} body: #{response_body}",
59+
)
60+
expect { subject.perform!(content) }.to raise_error(Net::HTTPBadResponse)
61+
end
62+
end
63+
end
64+
end

0 commit comments

Comments
 (0)