Module: Deja::Session

Defined in:
lib/deja/session.rb

Overview

The per-example runtime. Installs every registered adapter's caching stub (so a suite can mix providers — each test exercises whichever it actually calls), and aggregates the captured calls across adapters.

Class Method Summary collapse

Class Method Details

.enable ⇒ Object

Install all registered adapters' stubs and reset the captured call log.



11
12
13
14
15
16
17
18
19
# File 'lib/deja/session.rb', line 11

def enable
  Deja.reset_calls!
  adapters = Deja.adapters
  if adapters.empty?
    raise Deja::Error, "No providers registered. Call `c.register :anthropic, ...` inside Deja.configure."
  end

  adapters.each {|adapter| install(adapter, adapter.build_mock_client) }
end

.example_instance! ⇒ Object



41
42
43
44
# File 'lib/deja/session.rb', line 41

def example_instance!
  RSpec.current_example&.example_group_instance or
    raise Deja::Error, "Deja must be used inside an RSpec example"
end

.expect_called ⇒ Object

Assert exactly one call was captured across all adapters; return its kwargs.



33
34
35
36
37
38
39
# File 'lib/deja/session.rb', line 33

def expect_called
  instance = example_instance!
  instance.instance_exec do
    expect(Deja.calls.size).to eq(1)
  end
  Deja.calls.first[:kwargs]
end

.forbid ⇒ Object

Install a poison client for every adapter so any LLM access raises.



22
23
24
# File 'lib/deja/session.rb', line 22

def forbid
  Deja.adapters.each {|adapter| install(adapter, poison_client) }
end

.install(adapter, client) ⇒ Object

Runs an adapter's install block in the current example's context (so RSpec's allow is available), handing it the client to return.



28
29
30
# File 'lib/deja/session.rb', line 28

def install(adapter, client)
  example_instance!.instance_exec(client, &adapter.install_block)
end

.poison_client ⇒ Object



46
47
48
49
50
51
# File 'lib/deja/session.rb', line 46

def poison_client
  poison = Object.new
  def poison.method_missing(*) = raise("LLM should not be called (deja forbid_llm_calls)")
  def poison.respond_to_missing?(*) = true
  poison
end