summaryrefslogtreecommitdiffhomepage
path: root/lib/dispatch/adapter/tester/playbooks/claude.rb
blob: 0e3a1fdd86def720fb9aaa472e26f77b2c7f2f81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# frozen_string_literal: true

module Dispatch
  module Adapter
    module Tester
      module Playbooks
        # Pre-built playbook scripts for smoke-testing against the Claude adapter.
        #
        # This module provides scripted step sequences that mimic realistic Claude
        # API responses for six common scenarios. Each method returns a plain Ruby
        # Array of step hashes suitable for passing directly to
        # `Dispatch::Adapter::Tester::Playbook.new(steps_json: ...)`.
        #
        # ## Usage (recorded / CI mode)
        #
        # Use any playbook method to create a deterministic Playbook adapter:
        #
        #   steps   = Dispatch::Adapter::Tester::Playbooks::Claude.smoke_text
        #   adapter = Dispatch::Adapter::Tester::Playbook.new(steps_json: steps)
        #   msgs    = [Dispatch::Adapter::Message.new(role: "user",
        #                content: [Dispatch::Adapter::TextBlock.new(text: "Say hi")])]
        #   resp = adapter.chat(msgs)
        #   raise "unexpected stop_reason" unless resp.stop_reason == :end_turn
        #   raise "empty content" if resp.content.to_s.empty?
        #
        # ## Usage (live mode against the real Claude API)
        #
        # To run the same scenarios against the real Claude adapter, substitute
        # `Dispatch::Adapter::Tester::Playbook` with `Dispatch::Adapter::Claude`:
        #
        #   require "dispatch/adapter/claude"
        #   require "dispatch/adapter/tester/playbooks/claude"
        #
        #   adapter = Dispatch::Adapter::Claude.new(
        #     model:   "claude-sonnet-4-5-20250929",
        #     api_key: ENV.fetch("ANTHROPIC_API_KEY"),
        #     min_request_interval: 1.0
        #   )
        #
        #   # smoke_text
        #   msgs = [Dispatch::Adapter::Message.new(
        #     role: "user",
        #     content: [Dispatch::Adapter::TextBlock.new(text: "Say hi")]
        #   )]
        #   resp = adapter.chat(msgs)
        #   raise unless resp.stop_reason == :end_turn
        #   raise if resp.content.to_s.empty?
        #
        #   # smoke_tool_use
        #   add_tool = Dispatch::Adapter::ToolDefinition.new(
        #     name: "add",
        #     description: "Returns the sum of two numbers",
        #     parameters: { type: "object",
        #                   properties: { a: { type: "integer" }, b: { type: "integer" } },
        #                   required: %w[a b] }
        #   )
        #   msgs = [Dispatch::Adapter::Message.new(
        #     role: "user",
        #     content: [Dispatch::Adapter::TextBlock.new(text: "What is 2+3?")]
        #   )]
        #   resp = adapter.chat(msgs, tools: [add_tool])
        #   raise unless resp.stop_reason == :tool_use
        #   tc = resp.tool_calls.find { |t| t.name == "add" }
        #   raise "expected add tool call" unless tc
        #   raise unless tc.arguments["a"] == 2 && tc.arguments["b"] == 3
        #
        #   # smoke_thinking — requires Opus 4.7+ with thinking enabled
        #   msgs = [Dispatch::Adapter::Message.new(
        #     role: "user",
        #     content: [Dispatch::Adapter::TextBlock.new(text: "Think carefully about 42")]
        #   )]
        #   resp = adapter.chat(msgs, thinking: "high",
        #                       max_tokens: 16_000,
        #                       # Use Opus 4.7 for adaptive thinking:
        #                       # Dispatch::Adapter::Claude.new(model: "claude-opus-4-7", ...)
        #                       )
        #   raise unless resp.content.any? { |c| c.is_a?(Dispatch::Adapter::ThinkingBlock) }
        #
        #   # smoke_usage — OAuth only
        #   # adapter = Dispatch::Adapter::Claude.new(token_path: "~/.config/dispatch/claude_oauth.json")
        #   # report = adapter.usage_report
        #   # raise unless report.limits.any? { |e| e.id == "anthropic:5h" }
        #
        #   # smoke_pricing
        #   msgs = [Dispatch::Adapter::Message.new(
        #     role: "user",
        #     content: [Dispatch::Adapter::TextBlock.new(text: "Hello")]
        #   )]
        #   resp = adapter.chat(msgs)
        #   raise unless resp.usage.cost.total > 0
        #
        #   # smoke_cache — run the same request twice within 5 minutes
        #   resp2 = adapter.chat(msgs, cache_retention: :short)
        #   raise if resp2.usage.cache_read_tokens.to_i == 0
        #
        module Claude
          # Scripted response for a simple "Say hi" text request.
          #
          # Expected adapter behaviour:
          #   - stop_reason == :end_turn
          #   - content (string) is non-empty
          #
          # @return [Array<Hash>] steps array for Playbook
          def self.smoke_text
            [
              {
                "step" => 1,
                "type" => "message",
                "content" => "Hi there! How can I help you today?"
              }
            ]
          end

          # Scripted response for tool-use with an `add` function.
          #
          # Expected adapter behaviour:
          #   - stop_reason == :tool_use
          #   - tool_calls contains one entry with name "add"
          #   - arguments["a"] == 2, arguments["b"] == 3
          #
          # @return [Array<Hash>] steps array for Playbook
          def self.smoke_tool_use
            [
              {
                "step" => 1,
                "type" => "tool_calls",
                "content" => nil,
                "tool_calls" => [
                  {
                    "id" => "toolu_smoke_add_01",
                    "name" => "add",
                    "arguments" => { "a" => 2, "b" => 3 }
                  }
                ]
              }
            ]
          end

          # Scripted response for a thinking-enabled request.
          #
          # In the recorded (tester) mode, the "ThinkingBlock" is represented
          # as a synthetic message confirming thinking was requested.
          # In live mode, use claude-opus-4-7 with thinking: "high".
          #
          # Expected adapter behaviour (tester mode):
          #   - stop_reason == :end_turn
          #   - content is non-empty
          #
          # Expected adapter behaviour (live mode, Opus 4.7+ with thinking):
          #   - stop_reason == :end_turn
          #   - Response.content includes at least one ThinkingBlock
          #
          # @return [Array<Hash>] steps array for Playbook
          def self.smoke_thinking
            [
              {
                "step" => 1,
                "type" => "message",
                "content" => "[thinking] The number 42 is the answer to life, the universe, and everything."
              }
            ]
          end

          # Scripted response for usage_report (OAuth mode).
          #
          # In tester mode, usage_report is not driven by the Playbook (it is a
          # separate API call). This step collection is a placeholder that confirms
          # a text response is returned; live tests must use an OAuth-authenticated
          # Claude adapter and call adapter.usage_report directly.
          #
          # Expected adapter behaviour (live OAuth mode):
          #   - usage_report returns a UsageReport
          #   - limits.any? { |e| e.id == "anthropic:5h" }
          #
          # @return [Array<Hash>] steps array for Playbook
          def self.smoke_usage
            [
              {
                "step" => 1,
                "type" => "message",
                "content" => "Usage report smoke test placeholder."
              }
            ]
          end

          # Scripted response for verifying that pricing is calculated.
          #
          # In tester mode the Usage struct has 0 tokens and nil cost.
          # Live mode asserts response.usage.cost.total > 0.
          #
          # @return [Array<Hash>] steps array for Playbook
          def self.smoke_pricing
            [
              {
                "step" => 1,
                "type" => "message",
                "content" => "Hello!"
              }
            ]
          end

          # Scripted response for cache smoke test.
          #
          # Two identical calls with cache_retention: :short should result in
          # cache_read_tokens > 0 on the second call when using the real adapter.
          # In tester mode, both calls are separate steps with identical content.
          #
          # @return [Array<Hash>] steps array for Playbook (two identical steps)
          def self.smoke_cache
            [
              {
                "step" => 1,
                "type" => "message",
                "content" => "Hello! (first call)"
              },
              {
                "step" => 2,
                "type" => "message",
                "content" => "Hello! (second call — cache hit expected in live mode)"
              }
            ]
          end

          # Returns all six scenarios as a Hash keyed by scenario name.
          #
          # Useful for iterating over all smoke scenarios:
          #
          #   Dispatch::Adapter::Tester::Playbooks::Claude.all.each do |name, steps|
          #     adapter = Dispatch::Adapter::Tester::Playbook.new(steps_json: steps)
          #     # run scenario named `name`
          #   end
          #
          # @return [Hash<Symbol, Array<Hash>>]
          def self.all
            {
              smoke_text: smoke_text,
              smoke_tool_use: smoke_tool_use,
              smoke_thinking: smoke_thinking,
              smoke_usage: smoke_usage,
              smoke_pricing: smoke_pricing,
              smoke_cache: smoke_cache
            }
          end
        end
      end
    end
  end
end