Class: Mana::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/mana/mock.rb

Overview

Test double for LLM calls. Stubs are matched against prompt text:

Regexp patterns use match?, String patterns use include?.

Usage:

Mana.mock do
prompt(/average/, result: 3.0)
~"compute average of <numbers> and store in <result>"
end

Thread-local — safe for parallel test execution.

Defined Under Namespace

Classes: Stub

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMock

Initialize with an empty stub list



20
21
22
# File 'lib/mana/mock.rb', line 20

def initialize
  @stubs = []
end

Instance Attribute Details

#stubsObject (readonly)

Returns the value of attribute stubs.



17
18
19
# File 'lib/mana/mock.rb', line 17

def stubs
  @stubs
end

Instance Method Details

#match(prompt_text) ⇒ Object

Find the first stub that matches the given prompt text. Regexp patterns use match?, String patterns use include?.



31
32
33
34
35
36
37
38
39
40
# File 'lib/mana/mock.rb', line 31

def match(prompt_text)
  @stubs.find do |stub|
    case stub.pattern
    # Regex: full pattern match
    when Regexp then prompt_text.match?(stub.pattern)
    # String: substring match
    when String then prompt_text.include?(stub.pattern)
    end
  end
end

#prompt(pattern, **values, &block) ⇒ Object

Register a stub: when a prompt matches pattern, return values or call block



25
26
27
# File 'lib/mana/mock.rb', line 25

def prompt(pattern, **values, &block)
  @stubs << Stub.new(pattern: pattern, values: values, block: block)
end