-
-
Notifications
You must be signed in to change notification settings - Fork 360
/
Copy pathclient_spec.rb
128 lines (113 loc) · 4.73 KB
/
client_spec.rb
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
RSpec.describe OpenAI::Client do
context "with clients with different access tokens" do
before do
OpenAI.configure do |config|
config.organization_id = "organization_id0"
config.extra_headers = { "test" => "X-Default" }
end
end
after do
# Necessary otherwise the dummy organization_id bleeds into other specs
# that actually hit the API and causes them to fail.
OpenAI.configure do |config|
config.organization_id = nil
config.extra_headers = {}
end
end
let!(:c0) { OpenAI::Client.new }
let!(:c1) do
OpenAI::Client.new(
api_type: "azure",
access_token: "access_token1",
organization_id: "organization_id1",
request_timeout: 60,
uri_base: "https://oai.hconeai.com/",
extra_headers: { "test" => "X-Test" }
)
end
let!(:c2) do
OpenAI::Client.new(
access_token: "access_token2",
organization_id: nil,
request_timeout: 1,
uri_base: "https://example.com/"
)
end
it "does not confuse the clients" do
expect(c0.azure?).to eq(false)
expect(c0.access_token).to eq(ENV.fetch("OPENAI_ACCESS_TOKEN", "dummy-token"))
expect(c0.organization_id).to eq("organization_id0")
expect(c0.request_timeout).to eq(OpenAI::Configuration::DEFAULT_REQUEST_TIMEOUT)
expect(c0.uri_base).to eq(OpenAI::Configuration::DEFAULT_URI_BASE)
expect(c0.send(:headers).values).to include("Bearer #{c0.access_token}")
expect(c0.send(:headers).values).to include(c0.organization_id)
expect(c0.send(:connection).options.timeout).to eq(
OpenAI::Configuration::DEFAULT_REQUEST_TIMEOUT
)
expect(c0.send(:uri, path: "")).to include(OpenAI::Configuration::DEFAULT_URI_BASE)
expect(c0.send(:headers).values).to include("X-Default")
expect(c0.send(:headers).values).not_to include("X-Test")
expect(c1.azure?).to eq(true)
expect(c1.access_token).to eq("access_token1")
expect(c1.organization_id).to eq("organization_id1")
expect(c1.request_timeout).to eq(60)
expect(c1.uri_base).to eq("https://oai.hconeai.com/")
expect(c1.send(:headers).values).to include(c1.access_token)
expect(c1.send(:connection).options.timeout).to eq(60)
expect(c1.send(:uri, path: "")).to include("https://oai.hconeai.com/")
expect(c1.send(:headers).values).not_to include("X-Default")
expect(c1.send(:headers).values).to include("X-Test")
expect(c2.azure?).to eq(false)
expect(c2.access_token).to eq("access_token2")
expect(c2.organization_id).to eq("organization_id0") # Fall back to default.
expect(c2.request_timeout).to eq(1)
expect(c2.uri_base).to eq("https://example.com/")
expect(c2.send(:headers).values).to include("Bearer #{c2.access_token}")
expect(c2.send(:headers).values).to include(c2.organization_id)
expect(c2.send(:connection).options.timeout).to eq(1)
expect(c2.send(:uri, path: "")).to include("https://example.com/")
expect(c2.send(:headers).values).to include("X-Default")
expect(c2.send(:headers).values).not_to include("X-Test")
end
context "hitting other classes" do
after do
c0.files.list
c1.files.list
c2.files.list
c0.finetunes.list
c1.finetunes.list
c2.finetunes.list
c0.images.generate
c1.images.generate
c2.images.generate
c0.models.list
c1.models.list
c2.models.list
end
it "does not confuse the clients" do
expect(c0).to receive(:get).with(path: "/files").once
expect(c1).to receive(:get).with(path: "/files").once
expect(c2).to receive(:get).with(path: "/files").once
expect(c0).to receive(:get).with(path: "/fine_tuning/jobs").once
expect(c1).to receive(:get).with(path: "/fine_tuning/jobs").once
expect(c2).to receive(:get).with(path: "/fine_tuning/jobs").once
expect(c0).to receive(:json_post).with(path: "/images/generations", parameters: {}).once
expect(c1).to receive(:json_post).with(path: "/images/generations", parameters: {}).once
expect(c2).to receive(:json_post).with(path: "/images/generations", parameters: {}).once
expect(c0).to receive(:get).with(path: "/models").once
expect(c1).to receive(:get).with(path: "/models").once
expect(c2).to receive(:get).with(path: "/models").once
end
end
end
context "with a block" do
let(:client) do
OpenAI::Client.new do |client|
client.response :logger, Logger.new($stdout), bodies: true
end
end
it "sets the logger" do
expect(client.send(:connection).builder.handlers).to include Faraday::Response::Logger
end
end
end