Module: Deja::Helpers
- Defined in:
- lib/deja/rspec.rb
Overview
The test-facing DSL, mixed into every example by the RSpec.configure block below. Require “deja/rspec” from your spec setup to install it.
Instance Method Summary collapse
-
#cached_llm_value(id, *path) ⇒ Object
Read a value from a recorded cache YAML file by walking ‘path`.
-
#expect_llm_called ⇒ Object
Assert exactly one LLM call happened (across all providers) and return its kwargs.
-
#forbid_llm_calls ⇒ Object
Assert the code path under test never reaches the LLM.
-
#use_llm_cache(id) ⇒ Object
Call from the top of an ‘it` block (per-test — the id should be distinct for each test) to install the caching client and set the cache id used for this example.
Instance Method Details
#cached_llm_value(id, *path) ⇒ Object
Read a value from a recorded cache YAML file by walking ‘path`. Each segment is a string key (for hashes) or an integer index (for arrays). Raises with the path traversed so far if any segment is missing — so a renamed key or shifted index fails loud rather than returning nil.
cached_llm_value("2026-04-30_17-03",
"calls", 0, "response", "tool_uses", 0, "input", "session_instructions")
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/deja/rspec.rb', line 43 def cached_llm_value(id, *path) file = Deja::Cache.cache_dir.join(Deja::Cache.test_suite, "#{id}.yaml") rel = Deja::Cache.display_path(file) raise "No cached LLM file at #{rel}" unless file.exist? current = YAML.safe_load(file.read) path.each_with_index do |segment, i| crumb = i.zero? ? "<root>" : path[0...i].map(&:inspect).join("/") current = case current when Hash unless current.key?(segment) raise "No key #{segment.inspect} at #{crumb} in #{rel}; available: #{current.keys.inspect}" end current[segment] when Array unless segment.is_a?(Integer) raise "Expected integer index at #{crumb} in #{rel}, got #{segment.inspect}" end unless segment < current.size raise "Index #{segment} out of range at #{crumb} (size #{current.size}) in #{rel}" end current[segment] else raise "Cannot traverse into #{current.class} at #{crumb} in #{rel}" end end current end |
#expect_llm_called ⇒ Object
Assert exactly one LLM call happened (across all providers) and return its kwargs.
32 33 34 |
# File 'lib/deja/rspec.rb', line 32 def expect_llm_called Deja::Session.expect_called end |
#forbid_llm_calls ⇒ Object
Assert the code path under test never reaches the LLM. Call from a ‘before` block or the top of an example.
26 27 28 |
# File 'lib/deja/rspec.rb', line 26 def forbid_llm_calls Deja::Session.forbid end |
#use_llm_cache(id) ⇒ Object
Call from the top of an ‘it` block (per-test — the id should be distinct for each test) to install the caching client and set the cache id used for this example.
19 20 21 22 |
# File 'lib/deja/rspec.rb', line 19 def use_llm_cache(id) RSpec.current_example.[:llm_cache_id] = id Deja::Session.enable end |