ruby_llm-test 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6320355012e11036b37b8a13d6a7bd5782e573a1e7ab1a94601d67836e3e02ab
4
+ data.tar.gz: 566e774f1e9ac7fb636b5f05eeef52fc27565eb2d9fb9997f0f14b6630ba67c0
5
+ SHA512:
6
+ metadata.gz: 4a5b97108962cfa24230b4733e4357f2042ed6240dbe934d466567c102aadfee07224d0762b0105121c4092879d8ae5ef8f98ef7e76146170e366057aa09f15d
7
+ data.tar.gz: e30334a2adf81c0faeb482daf76a96dda06efa7664f567c2778a6aafd1e02c934686f33f9302f6533eae184f390c7f7bba7355e5b0fd09dc2408ff79276282eb
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Todd Kummer
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,115 @@
1
+ # RubyLLM::Test
2
+
3
+ This gem provides testing utilities for RubyLLM, a Ruby library for working with large language models (LLMs). It enables calls to LLM's to be stubbed so that the surrounding application logic can be tested without making actual calls to the LLM. This is particularly useful for testing code that interacts with LLMs, as it allows developers to simulate responses from the LLM without incurring the cost, latency, or randomness of real API calls.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ruby_llm-test'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ruby_llm-test
20
+
21
+ ## Usage
22
+
23
+ Add the following lines to your `spec/spec_helper.rb` or `test/test_helper.rb`:
24
+
25
+ ```ruby
26
+ require 'ruby_llm/test'
27
+
28
+ RubyLLM::Models.singleton_class.prepend(RubyLLM::Test::ResolveWithTestProvider)
29
+ ```
30
+
31
+ Then, in your tests, you can use the `stub_response` method to stub responses from the LLM. For example:
32
+
33
+ ```ruby
34
+ it 'returns a stubbed response' do
35
+ RubyLLM::Test.stub_response('Hello, world!')
36
+
37
+ response = MyLLMClient.call('Hello?')
38
+ expect(response).to eq('Hello, world!')
39
+ end
40
+ ```
41
+
42
+ If you make multiple calls to the LLM, you can call `stub_response` more than once or use method `stub_responses` to stub multiple responses at once. For example:
43
+
44
+ ```ruby
45
+ it 'returns multiple stubbed responses' do
46
+ RubyLLM::Test.stub_responses('Hello, world!', 'How are you?')
47
+ response1 = MyLLMClient.call('Hello?')
48
+ response2 = MyLLMClient.call('How are you?')
49
+
50
+ expect(response1).to eq('Hello, world!')
51
+ expect(response2).to eq('How are you?')
52
+ end
53
+ ```
54
+
55
+ ### Stubbing with a Message
56
+
57
+ If you stub with a string, it will be wrapped in a `RubyLLM::Message` with the role of `:assistant`. If you want more control over the message, you can stub with a `RubyLLM::Message` directly. For example:
58
+
59
+ ```ruby
60
+ it 'returns a stubbed message' do
61
+ message = RubyLLM::Message.new(role: :assistant, content: 'Hello, world!')
62
+ RubyLLM::Test.stub_response(message)
63
+
64
+ response = MyLLMClient.call('Hello?')
65
+ expect(response).to eq(message)
66
+ end
67
+ ```
68
+
69
+ ### Stubbing with a Hash Returns JSON
70
+
71
+ If you stub with a hash, it will be converted to a `RubyLLM::Message` with the content set to the JSON representation of the hash. For example:
72
+
73
+ ```ruby
74
+ it 'returns a stubbed JSON message' do
75
+ hash = { key: 'value' }
76
+ RubyLLM::Test.stub_response(hash)
77
+
78
+ response = MyLLMClient.call('Hello?')
79
+ expect(response.content).to eq(hash.to_json)
80
+ end
81
+ ```
82
+
83
+ ### Resetting Stubs
84
+
85
+ Make sure to reset stubs after each test to avoid interference before or between tests.
86
+
87
+ ```ruby
88
+ RubyLLM::Test.reset
89
+ ```
90
+
91
+ ### Stubbing with a Block
92
+
93
+ You can also stub responses in a block, which handles the setup and teardown of stubs automatically. For example:
94
+
95
+ ```ruby
96
+
97
+ RubyLLM::Test.with_responses('Hello, world!') do
98
+ response = MyLLMClient.call('Hello?')
99
+ expect(response).to eq('Hello, world!')
100
+ end
101
+ ```
102
+
103
+ ## Development
104
+
105
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
106
+
107
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
108
+
109
+ ## Contributing
110
+
111
+ Bug reports and pull requests are welcome on GitHub at https://github.com/RockSolt/ruby_llm-test.
112
+
113
+ ## License
114
+
115
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLLM
4
+ module Test
5
+ # This class encapsulates all the parameters that are passed to the `complete` method of the `Test` class. It
6
+ # serves as a structured way to manage and access these parameters throughout the testing process.
7
+ class CompleteParameters
8
+ attr_reader :messages, :tools, :temperature, :model, :params, :headers, :schema, :thinking, :tool_prefs, :block
9
+
10
+ def initialize(messages:, tools:, temperature:, model:, params:, # rubocop:disable Metrics/ParameterLists
11
+ headers:, schema:, thinking:, tool_prefs:, block:)
12
+ @messages = messages
13
+ @tools = tools
14
+ @temperature = temperature
15
+ @model = model
16
+ @params = params
17
+ @headers = headers
18
+ @schema = schema
19
+ @thinking = thinking
20
+ @tool_prefs = tool_prefs
21
+ @block = block
22
+ end
23
+
24
+ def block_received?
25
+ !@block.nil?
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLLM
4
+ module Test
5
+ module Errors
6
+ # # No Response Provided Error
7
+ #
8
+ # If the test setup does not provide a response for a test chat completion,
9
+ # this error is raised to indicate that the test is incomplete. The final chat message,
10
+ # generated by the `ask` method, is included in the error message for context.
11
+ class NoResponseProvidedError < StandardError
12
+ attr_reader :messages
13
+
14
+ def initialize(messages)
15
+ @messages = messages
16
+ super("No test response provided for the following request:\n#{messages.last&.content}")
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLLM
4
+ module Test
5
+ # Extend the RubyLLM::Models class to inject the Test Provider in test environments.
6
+ #
7
+ # Add the following to your test setup (e.g. in spec_helper.rb or test_helper.rb):
8
+ #
9
+ # require_relative "ruby_llm/test/resolve_with_test_provider"
10
+ #
11
+ # RubyLLM::Models.singleton_class.prepend(RubyLLM::Test::ResolveWithTestProvider)
12
+ module ResolveWithTestProvider
13
+ def resolve(...)
14
+ model, provider_instance = super
15
+ [ model, Test::TestProvider.new(provider_instance, RubyLLM::Test) ]
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLLM
4
+ module Test
5
+ # This class serves as a test double for the provider used in the `RubyLLM::Test` class. It captures all calls to
6
+ # the `complete` method, allowing tests to assert that the correct parameters were passed and to simulate
7
+ # responses from the provider.
8
+ class TestProvider < SimpleDelegator
9
+ extend Forwardable
10
+
11
+ attr_reader :complete_calls
12
+
13
+ def_delegators :last_call, :messages, :tools, :temperature, :model, :params, :headers, :schema, :thinking,
14
+ :tool_prefs, :block_received?
15
+
16
+ def initialize(provider, test_harness = RubyLLM::Test)
17
+ super(provider)
18
+ @test_harness = test_harness
19
+ @complete_calls = []
20
+ end
21
+
22
+ def complete(messages, tools:, temperature:, model:, params: {}, # rubocop:disable Metrics/ParameterLists
23
+ headers: {}, schema: nil, thinking: nil, tool_prefs: nil, &block)
24
+ @complete_calls << CompleteParameters.new(messages:, tools:, temperature:, model:, params:, headers:, schema:,
25
+ thinking:, tool_prefs:, block:)
26
+ raise Errors::NoResponseProvidedError, messages if @test_harness.responses_empty?
27
+
28
+ response = @test_harness.next_response
29
+ return response if response.is_a?(Message)
30
+
31
+ Message.new(
32
+ role: :assistant,
33
+ content: response.is_a?(Hash) ? response.to_json : response
34
+ )
35
+ end
36
+
37
+ def last_call
38
+ @complete_calls.last
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLLM
4
+ module Test
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ruby_llm"
4
+ require "zeitwerk"
5
+
6
+ loader = Zeitwerk::Loader.for_gem_extension(RubyLLM)
7
+ loader.setup
8
+
9
+ module RubyLLM
10
+ # The Test module provides a simple way to stub responses from an LLM for testing purposes. You can use it to set up
11
+ # predetermined responses that your tests can rely on, allowing you to test your code's behavior without making
12
+ # actual calls to an LLM.
13
+ module Test
14
+ class << self
15
+ def reset
16
+ @responses = nil
17
+ end
18
+
19
+ # Pass in a RubyLLM::Message to have full control; strings and hashes will be wrapped in a message
20
+ def stub_response(body)
21
+ responses << body
22
+ end
23
+
24
+ def stub_responses(*bodies)
25
+ responses.concat(bodies)
26
+ end
27
+
28
+ def with_responses(*bodies)
29
+ previous_responses = responses.dup
30
+ @responses = []
31
+ stub_responses(*bodies)
32
+ yield
33
+ ensure
34
+ @responses = previous_responses
35
+ end
36
+
37
+ def next_response
38
+ responses.shift
39
+ end
40
+
41
+ def responses_empty?
42
+ responses.empty?
43
+ end
44
+
45
+ def responses
46
+ @responses ||= []
47
+ end
48
+ end
49
+ end
50
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_llm-test
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Todd Kummer
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 2026-04-22 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: ruby_llm
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: 1.14.0
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: 1.14.0
26
+ description: Provides a RubyLLM::Provider that allows you to stub responses for testing
27
+ purposes. You can stub individual responses or a sequence of responses, and you
28
+ can also temporarily stub responses within a block.
29
+ email:
30
+ - todd@rockridgesolutions.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - LICENSE.txt
36
+ - README.md
37
+ - lib/ruby_llm/test.rb
38
+ - lib/ruby_llm/test/complete_parameters.rb
39
+ - lib/ruby_llm/test/errors/no_response_provided_error.rb
40
+ - lib/ruby_llm/test/resolve_with_test_provider.rb
41
+ - lib/ruby_llm/test/test_provider.rb
42
+ - lib/ruby_llm/test/version.rb
43
+ homepage: https://github.com/RockSolt/ruby_llm-test
44
+ licenses:
45
+ - MIT
46
+ metadata: {}
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '3.3'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubygems_version: 4.0.10
62
+ specification_version: 4
63
+ summary: Test application logic by stubbing responses from a RubyLLM::Provider.
64
+ test_files: []