-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_basic.rb
More file actions
255 lines (225 loc) · 5.46 KB
/
test_basic.rb
File metadata and controls
255 lines (225 loc) · 5.46 KB
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
#!/usr/bin/env ruby
# frozen_string_literal: true
# Simple test to verify SDK loads and basic functionality works
# This tests core functionality without full RSpec dependencies
puts "Testing Z.ai Ruby SDK with Ruby #{RUBY_VERSION}"
puts "=" * 70
tests_passed = 0
tests_failed = 0
def test(name)
print "Testing #{name}... "
begin
result = yield
puts "✓ PASS"
return true
rescue => e
puts "✗ FAIL: #{e.message}"
puts " #{e.backtrace[0]}" if ENV['DEBUG']
return false
end
end
# Test 1: Load SDK
if test("SDK loads") do
require_relative 'lib/z/ai'
true
end
tests_passed += 1
else
tests_failed += 1
puts "\nCannot continue - SDK failed to load"
exit 1
end
# Test 2: Version constant
if test("Version constant exists") do
raise "No VERSION" unless defined?(Z::AI::VERSION)
raise "Empty VERSION" if Z::AI::VERSION.empty?
true
end
tests_passed += 1
else
tests_failed += 1
end
# Test 3: Configuration
if test("Configuration works") do
config = Z::AI::Configuration.new
config.api_key = 'test_key.12345'
config.validate!
true
end
tests_passed += 1
else
tests_failed += 1
end
# Test 4: Client initialization
if test("Client initializes") do
client = Z::AI::Client.new(api_key: 'test_key.12345')
raise "No client" unless client
true
end
tests_passed += 1
else
tests_failed += 1
end
# Test 5: API resources exist
if test("Chat API resource") do
client = Z::AI::Client.new(api_key: 'test_key.12345')
raise "No chat" unless client.respond_to?(:chat)
chat = client.chat
raise "Wrong type" unless chat.is_a?(Z::AI::Resources::Chat::Completions)
true
end
tests_passed += 1
else
tests_failed += 1
end
if test("Embeddings API resource") do
client = Z::AI::Client.new(api_key: 'test_key.12345')
raise "No embeddings" unless client.respond_to?(:embeddings)
embeddings = client.embeddings
raise "Wrong type" unless embeddings.is_a?(Z::AI::Resources::Embeddings)
true
end
tests_passed += 1
else
tests_failed += 1
end
if test("Images API resource") do
client = Z::AI::Client.new(api_key: 'test_key.12345')
raise "No images" unless client.respond_to?(:images)
images = client.images
raise "Wrong type" unless images.is_a?(Z::AI::Resources::Images)
true
end
tests_passed += 1
else
tests_failed += 1
end
if test("Files API resource") do
client = Z::AI::Client.new(api_key: 'test_key.12345')
raise "No files" unless client.respond_to?(:files)
files = client.files
raise "Wrong type" unless files.is_a?(Z::AI::Resources::Files)
true
end
tests_passed += 1
else
tests_failed += 1
end
# Test 6: Error classes
if test("Error classes work") do
error = Z::AI::Error.new(message: 'Test error')
raise "Wrong message" unless error.message == 'Test error'
api_error = Z::AI::APIAuthenticationError.new(
message: 'Auth failed',
http_status: 401
)
raise "Wrong status" unless api_error.http_status == 401
true
end
tests_passed += 1
else
tests_failed += 1
end
# Test 7: JWT Token generation
if test("JWT token generation") do
token = Z::AI::Auth::JWTToken.generate_token('id.secret')
raise "Empty token" if token.empty?
raise "Not JWT format" unless token.split('.').length == 3
true
end
tests_passed += 1
else
tests_failed += 1
end
# Test 8: JWT caching
if test("JWT token caching") do
Z::AI::Auth::JWTToken.clear_cache
token1 = Z::AI::Auth::JWTToken.generate_token('test_id.test_secret')
token2 = Z::AI::Auth::JWTToken.generate_token('test_id.test_secret')
raise "Tokens not cached" unless token1 == token2
stats = Z::AI::Auth::JWTToken.cache_stats
raise "Cache stats wrong" unless stats[:size] >= 1
true
end
tests_passed += 1
else
tests_failed += 1
end
# Test 9: Configuration validation
if test("Configuration validation") do
config = Z::AI::Configuration.new
# Should fail without API key
begin
config.validate!
raise "Should have failed"
rescue Z::AI::ConfigurationError
# Expected
end
# Should succeed with API key
config.api_key = 'valid_key'
config.validate!
true
end
tests_passed += 1
else
tests_failed += 1
end
# Test 10: Global configuration
if test("Global configuration") do
Z::AI.configure do |config|
config.api_key = 'global_test_key'
config.timeout = 60
end
raise "Config not set" unless Z::AI.configuration.api_key == 'global_test_key'
raise "Timeout wrong" unless Z::AI.configuration.timeout == 60
Z::AI.reset!
true
end
tests_passed += 1
else
tests_failed += 1
end
# Test 11: Model instantiation
if test("Chat models instantiate") do
message = Z::AI::Models::Chat::Message.new(
role: 'user',
content: 'Hello'
)
raise "Wrong role" unless message.role == 'user'
raise "Wrong content" unless message.content == 'Hello'
true
end
tests_passed += 1
else
tests_failed += 1
end
# Test 12: Client with custom options
if test("Client with custom options") do
client = Z::AI::Client.new(
api_key: 'test_key',
timeout: 45,
max_retries: 5
)
raise "Timeout wrong" unless client.config.timeout == 45
raise "Retries wrong" unless client.config.max_retries == 5
true
end
tests_passed += 1
else
tests_failed += 1
end
# Summary
puts "\n" + "=" * 70
puts "Test Summary"
puts "=" * 70
puts "Passed: #{tests_passed}"
puts "Failed: #{tests_failed}"
puts "Total: #{tests_passed + tests_failed}"
if tests_failed == 0
puts "\n✅ All basic tests passed!"
puts "\nThe SDK core functionality works correctly with Ruby #{RUBY_VERSION}"
exit 0
else
puts "\n❌ Some tests failed"
exit 1
end