Module: Soka::Rails::TestHelpers

Extended by:
ActiveSupport::Concern
Defined in:
lib/soka/rails/test_helpers.rb

Overview

Test helpers for RSpec tests with Soka agents and tools

Instance Method Summary collapse

Instance Method Details

#build_mock_response(attrs) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/soka/rails/test_helpers.rb', line 19

def build_mock_response(attrs)
  default_response = {
    final_answer: 'Mocked answer',
    status: :completed,
    iterations: 1,
    thought_process: []
  }
  default_response.merge(attrs)
end

#build_react_response(attrs) ⇒ Object

Build ReAct format response



44
45
46
47
48
49
50
51
# File 'lib/soka/rails/test_helpers.rb', line 44

def build_react_response(attrs)
  thoughts = attrs[:thought_process].presence ||
             ['Analyzing the request']

  response = thoughts.map { |t| "<Thought>#{t}</Thought>" }.join("\n")
  response += "\n<Final_Answer>#{attrs[:final_answer]}</Final_Answer>"
  response
end

#collect_agent_events(agent, input) ⇒ Object

Event collector



66
67
68
69
70
71
72
73
74
# File 'lib/soka/rails/test_helpers.rb', line 66

def collect_agent_events(agent, input)
  events = []

  agent.run(input) do |event|
    events << event
  end

  events
end

#mock_ai_response(response_attrs = {}) ⇒ Object

Mock AI response



14
15
16
17
# File 'lib/soka/rails/test_helpers.rb', line 14

def mock_ai_response(response_attrs = {})
  response = build_mock_response(response_attrs)
  setup_llm_mock(response)
end

#mock_tool_execution(tool_class, result) ⇒ Object

Mock tool execution



39
40
41
# File 'lib/soka/rails/test_helpers.rb', line 39

def mock_tool_execution(tool_class, result)
  allow_any_instance_of(tool_class).to receive(:call).and_return(result)
end

#run_agent(agent, input) ⇒ Object

Agent test helper methods



54
55
56
57
58
59
60
61
62
63
# File 'lib/soka/rails/test_helpers.rb', line 54

def run_agent(agent, input, &)
  result = if block_given?
             agent.run(input, &)
           else
             agent.run(input)
           end

  expect(result).to be_a(Struct)
  result
end

#setup_llm_mock(response) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/soka/rails/test_helpers.rb', line 29

def setup_llm_mock(response)
  return unless defined?(Soka::LLM)

  allow(Soka::LLM).to receive(:new).and_return(mock_llm)
  allow(mock_llm).to receive(:chat).and_return(
    Struct.new(:content).new(build_react_response(response))
  )
end

#successful_result(attrs = {}) ⇒ Object

Helper method: create successful result



92
93
94
95
96
97
98
99
100
# File 'lib/soka/rails/test_helpers.rb', line 92

def successful_result(attrs = {})
  default_attrs = {
    status: :completed,
    final_answer: 'Success',
    iterations: 1
  }

  Struct.new(*default_attrs.keys).new(*default_attrs.merge(attrs).values)
end

#with_test_configurationObject

Test configuration



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/soka/rails/test_helpers.rb', line 77

def with_test_configuration
  original_config = Soka::Rails.configuration.dup

  Soka::Rails.configure do |config|
    config.ai_provider = :mock
    config.max_iterations = 3
    yield config if block_given?
  end

  yield
ensure
  Soka::Rails.configuration = original_config
end