Skip to content

Commit

Permalink
Tests for chat
Browse files Browse the repository at this point in the history
  • Loading branch information
ksylvest committed Jun 17, 2024
1 parent 927b61b commit 8c4977f
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 3 deletions.
14 changes: 13 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,17 @@ RSpec/SpecFilePathFormat:
CustomTransform:
OmniAI: omniai

Layout/FirstHashElementIndentation:
EnforcedStyle: consistent

Layout/FirstArrayElementIndentation:
EnforcedStyle: consistent

Metrics/ParameterLists:
Max: 8
Enabled: false

Style/TrailingCommaInArrayLiteral:
EnforcedStyleForMultiline: consistent_comma

Style/TrailingCommaInHashLiteral:
EnforcedStyleForMultiline: consistent_comma
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ gem 'rubocop'
gem 'rubocop-rake'
gem 'rubocop-rspec'
gem 'simplecov'
gem 'webmock'
10 changes: 10 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ GEM
public_suffix (>= 2.0.2, < 6.0)
ast (2.4.2)
base64 (0.2.0)
bigdecimal (3.1.8)
crack (1.0.0)
bigdecimal
rexml
diff-lcs (1.5.1)
docile (1.4.0)
domain_name (0.6.20240107)
Expand All @@ -31,6 +35,7 @@ GEM
ffi-compiler (1.3.2)
ffi (>= 1.15.5)
rake
hashdiff (1.1.0)
http (5.2.0)
addressable (~> 2.8)
base64 (~> 0.1)
Expand Down Expand Up @@ -97,6 +102,10 @@ GEM
simplecov_json_formatter (0.1.4)
strscan (3.1.0)
unicode-display_width (2.5.0)
webmock (3.23.1)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
zeitwerk (2.6.16)

PLATFORMS
Expand All @@ -121,6 +130,7 @@ DEPENDENCIES
rubocop-rake
rubocop-rspec
simplecov
webmock

BUNDLED WITH
2.5.11
4 changes: 2 additions & 2 deletions spec/omniai/chat/usage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
let(:data) do
{
'input_tokens' => 2,
'output_tokens' => 3
'output_tokens' => 3,
}
end

Expand All @@ -24,7 +24,7 @@
{
'prompt_tokens' => 2,
'completion_tokens' => 3,
'total_tokens' => 5
'total_tokens' => 5,
}
end

Expand Down
89 changes: 89 additions & 0 deletions spec/omniai/chat_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# frozen_string_literal: true

class FakeClient < OmniAI::Client
def connection
HTTP.persistent('http://localhost:8080')
end
end

class FakeChat < OmniAI::Chat
module Model
FAKE = 'fake'
end

def path
'/chat'
end

def payload
{ messages:, model: @model }
end
end

RSpec.describe OmniAI::Chat do
subject(:chat) { described_class.new(messages, model:, client:) }

let(:model) { '...' }
let(:client) { OmniAI::Client.new(api_key: '...') }
let(:messages) do
[
{ role: described_class::Role::SYSTEM, content: 'You are a helpful assistant.' },
'What is the name of the dummer for the Beatles?',
]
end

describe '#path' do
it { expect { chat.send(:path) }.to raise_error(NotImplementedError) }
end

describe '#payload' do
it { expect { chat.send(:payload) }.to raise_error(NotImplementedError) }
end

describe '.process!' do
subject(:process!) { FakeChat.process!(messages, model:, client:) }

let(:client) { FakeClient.new(api_key: '...') }
let(:model) { FakeChat::Model::FAKE }

context 'when OK' do
before do
stub_request(:post, 'http://localhost:8080/chat')
.with(body: {
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'What is the name of the dummer for the Beatles?' },
],
model:,
})
.to_return_json(status: 200, body: {
choices: [{
index: 0,
message: {
role: 'system',
content: '{ "name": "Ringo" }',
},
}],
})
end

it { expect(process!).to be_a(OmniAI::Chat::Completion) }
end

context 'when UNPROCESSABLE' do
before do
stub_request(:post, 'http://localhost:8080/chat')
.with(body: {
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'What is the name of the dummer for the Beatles?' },
],
model:,
})
.to_return_json(status: 422, body: 'An unknown error occurred.')
end

it { expect { process! }.to raise_error(OmniAI::HTTPError) }
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'simplecov'
require 'webmock/rspec'

SimpleCov.start do
enable_coverage :branch
Expand Down

0 comments on commit 8c4977f

Please sign in to comment.