Module: Omen::Stubs

Extended by:
ActiveSupport::Concern
Defined in:
lib/omen/stubs.rb

Overview

Answers the Anthropic API with canned turns, so no test of a host reaches the network.

Constant Summary collapse

MESSAGES_URL =

Where a message is asked for.

'https://api.anthropic.com/v1/messages'

Instance Method Summary collapse

Instance Method Details

#claude_answers(sql: '', note: '', combine: []) ⇒ Hash

Carries a stray caller, so a test notices if a reply is ever replayed whole.

Parameters:

  • sql (String) (defaults to: '')

    the statement Claude answers with, or '' to ask something instead.

  • note (String) (defaults to: '')

    what Claude says about it.

  • combine (Array<Hash>) (defaults to: [])

    the columns it asks to be drawn as their parts joined.

Returns:

  • (Hash)

    a turn where Claude answers in the shape the output schema demands.



18
19
20
21
22
# File 'lib/omen/stubs.rb', line 18

def claude_answers(sql: '', note: '', combine: [])
  answered = { sql: sql, note: note, combine: combine }
  claude_turn 'end_turn', [ { type: 'text', text: answered.to_json,
                              caller: { type: 'assistant' }, } ]
end

#claude_says(text) ⇒ Hash

A reply cut short at max_tokens is not JSON, whatever the output schema demanded.

Parameters:

  • text (String)

    what Claude got as far as saying.

Returns:

  • (Hash)

    a turn the API stopped mid-sentence.



27
28
29
# File 'lib/omen/stubs.rb', line 27

def claude_says(text)
  claude_turn 'max_tokens', [ { type: 'text', text: text } ]
end

#stub_claude(*turns) ⇒ Object

Answers each request with the next turn, so a question and its refinement are scripted apart.

Parameters:

  • turns (Array<Hash>)

    what Claude says, in order.



8
9
10
11
# File 'lib/omen/stubs.rb', line 8

def stub_claude(*turns)
  stub_request(:post, MESSAGES_URL).
    to_return(*turns.map { |turn| { body: turn.to_json, headers: json_headers } })
end