summaryrefslogtreecommitdiffhomepage
path: root/spec/dispatch/adapter/tester/playbook_spec.rb
blob: fc3ecd9e2ef31ef2395e01e68e0be965b54da317 (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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# frozen_string_literal: true

RSpec.describe Dispatch::Adapter::Tester::Playbook do
  let(:message_step) do
    { "step" => 1, "type" => "message", "content" => "Hello from the AI" }
  end

  let(:tool_call_step) do
    {
      "step" => 2,
      "type" => "tool_calls",
      "content" => nil,
      "tool_calls" => [
        {
          "id" => "tc_read_001",
          "name" => "read_file",
          "arguments" => { "path" => "/some/file.rb" }
        }
      ]
    }
  end

  let(:tool_call_with_content_step) do
    {
      "step" => 3,
      "type" => "tool_calls",
      "content" => "I will read the file for you.",
      "tool_calls" => [
        {
          "id" => "tc_read_002",
          "name" => "read_file",
          "arguments" => { "path" => "/other/file.rb" }
        }
      ]
    }
  end

  let(:multi_tool_step) do
    {
      "step" => 4,
      "type" => "tool_calls",
      "content" => nil,
      "tool_calls" => [
        {
          "id" => "tc_write_001",
          "name" => "write_file",
          "arguments" => { "path" => "/a.rb", "content" => "hello" }
        },
        {
          "id" => "tc_write_002",
          "name" => "write_file",
          "arguments" => { "path" => "/b.rb", "content" => "world" }
        }
      ]
    }
  end

  def build_adapter(steps, **kwargs)
    described_class.new(steps_json: JSON.generate(steps), **kwargs)
  end

  describe "#initialize" do
    it "parses a valid JSON string of steps" do
      adapter = build_adapter([message_step])
      expect(adapter.steps.length).to eq(1)
      expect(adapter.steps.first.step_id).to eq(1)
    end

    it "accepts an array directly instead of a JSON string" do
      adapter = described_class.new(steps_json: [message_step])
      expect(adapter.steps.length).to eq(1)
    end

    it "accepts empty steps" do
      adapter = build_adapter([])
      expect(adapter.steps).to be_empty
    end

    it "absorbs extra keyword arguments for drop-in compat" do
      expect do
        described_class.new(
          steps_json: "[]",
          model: "gpt-4",
          max_tokens: 50_000,
          min_request_interval: 3.0,
          rate_limit: nil
        )
      end.not_to raise_error
    end

    it "raises on invalid JSON" do
      expect do
        described_class.new(steps_json: "not json")
      end.to raise_error(Dispatch::Adapter::Tester::InvalidPlaybookError, /Failed to parse/)
    end

    it "raises when JSON is not an array" do
      expect do
        described_class.new(steps_json: '{"step": 1}')
      end.to raise_error(Dispatch::Adapter::Tester::InvalidPlaybookError, /must be an array/)
    end

    it "raises on duplicate step IDs" do
      steps = [
        { "step" => 1, "type" => "message", "content" => "a" },
        { "step" => 1, "type" => "message", "content" => "b" }
      ]
      expect do
        build_adapter(steps)
      end.to raise_error(Dispatch::Adapter::Tester::InvalidPlaybookError, /Duplicate step IDs/)
    end
  end

  describe "#chat" do
    context "with a message step" do
      it "returns a Response with content and end_turn stop_reason" do
        adapter = build_adapter([message_step])
        response = adapter.chat([])

        expect(response).to be_a(Dispatch::Adapter::Response)
        expect(response.content).to eq("Hello from the AI")
        expect(response.stop_reason).to eq(:end_turn)
        expect(response.tool_calls).to be_empty
      end

      it "reports zero token usage" do
        adapter = build_adapter([message_step])
        response = adapter.chat([])

        expect(response.usage.input_tokens).to eq(0)
        expect(response.usage.output_tokens).to eq(0)
      end
    end

    context "with a tool_calls step" do
      it "returns a Response with tool_calls and tool_use stop_reason" do
        adapter = build_adapter([tool_call_step])
        response = adapter.chat([])

        expect(response.stop_reason).to eq(:tool_use)
        expect(response.tool_calls.length).to eq(1)

        tc = response.tool_calls.first
        expect(tc).to be_a(Dispatch::Adapter::ToolUseBlock)
        expect(tc.id).to eq("tc_read_001")
        expect(tc.name).to eq("read_file")
        expect(tc.arguments).to eq({ "path" => "/some/file.rb" })
      end

      it "returns nil content when content is nil" do
        adapter = build_adapter([tool_call_step])
        response = adapter.chat([])

        expect(response.content).to be_nil
      end
    end

    context "with a tool_calls step that has content" do
      it "returns both content and tool_calls" do
        adapter = build_adapter([tool_call_with_content_step])
        response = adapter.chat([])

        expect(response.content).to eq("I will read the file for you.")
        expect(response.tool_calls.length).to eq(1)
        expect(response.stop_reason).to eq(:tool_use)
      end
    end

    context "with multiple tool calls in a single step" do
      it "returns all tool calls" do
        adapter = build_adapter([multi_tool_step])
        response = adapter.chat([])

        expect(response.tool_calls.length).to eq(2)
        expect(response.tool_calls.map(&:id)).to eq(%w[tc_write_001 tc_write_002])
        expect(response.tool_calls.map(&:name)).to eq(%w[write_file write_file])
      end
    end

    context "with a multi-step playbook" do
      it "consumes steps sequentially" do
        adapter = build_adapter([message_step, tool_call_step])

        r1 = adapter.chat([])
        expect(r1.content).to eq("Hello from the AI")
        expect(r1.stop_reason).to eq(:end_turn)

        r2 = adapter.chat([])
        expect(r2.stop_reason).to eq(:tool_use)
        expect(r2.tool_calls.first.id).to eq("tc_read_001")
      end

      it "advances the current_index" do
        adapter = build_adapter([message_step, tool_call_step])
        expect(adapter.current_index).to eq(0)

        adapter.chat([])
        expect(adapter.current_index).to eq(1)

        adapter.chat([])
        expect(adapter.current_index).to eq(2)
      end
    end

    context "when the playbook is exhausted" do
      it "raises PlaybookExhaustedError with step details" do
        adapter = build_adapter([message_step])
        adapter.chat([])

        expect do
          adapter.chat([])
        end.to raise_error(Dispatch::Adapter::Tester::PlaybookExhaustedError) do |error|
          expect(error.total_steps).to eq(1)
          expect(error.calls_made).to eq(2)
          expect(error.message).to include("all 1 steps")
          expect(error.message).to include("2nd time")
        end
      end
    end

    it "records each call in the call_log" do
      adapter = build_adapter([message_step])
      messages = [Dispatch::Adapter::Message.new(role: "user", content: "hi")]
      tools = [{ name: "test", description: "test", parameters: {} }]

      adapter.chat(messages, system: "You are helpful", tools: tools)

      expect(adapter.call_log.length).to eq(1)
      log_entry = adapter.call_log.first
      expect(log_entry[:system]).to eq("You are helpful")
      expect(log_entry[:tools]).to eq(tools)
      expect(log_entry[:step].step_id).to eq(1)
    end

    it "reports the configured model name in responses" do
      adapter = build_adapter([message_step], model: "custom-model")
      response = adapter.chat([])

      expect(response.model).to eq("custom-model")
    end
  end

  describe "#model_name" do
    it "returns the default model name" do
      adapter = build_adapter([])
      expect(adapter.model_name).to eq("tester-playbook")
    end

    it "returns a custom model name" do
      adapter = build_adapter([], model: "gpt-4")
      expect(adapter.model_name).to eq("gpt-4")
    end
  end

  describe "#provider_name" do
    it "returns 'Tester Playbook'" do
      adapter = build_adapter([])
      expect(adapter.provider_name).to eq("Tester Playbook")
    end
  end

  describe "#max_context_tokens" do
    it "returns a large context window" do
      adapter = build_adapter([])
      expect(adapter.max_context_tokens).to eq(1_000_000)
    end
  end

  describe "#list_models" do
    it "returns a single ModelInfo entry" do
      adapter = build_adapter([])
      models = adapter.list_models

      expect(models.length).to eq(1)
      expect(models.first).to be_a(Dispatch::Adapter::ModelInfo)
      expect(models.first.id).to eq("tester-playbook")
      expect(models.first.supports_tool_use).to be true
    end
  end

  describe "#finished?" do
    it "returns false when steps remain" do
      adapter = build_adapter([message_step])
      expect(adapter.finished?).to be false
    end

    it "returns true when all steps consumed" do
      adapter = build_adapter([message_step])
      adapter.chat([])
      expect(adapter.finished?).to be true
    end

    it "returns true for empty playbook" do
      adapter = build_adapter([])
      expect(adapter.finished?).to be true
    end
  end

  describe "#remaining_steps" do
    it "tracks remaining step count" do
      adapter = build_adapter([message_step, tool_call_step])
      expect(adapter.remaining_steps).to eq(2)

      adapter.chat([])
      expect(adapter.remaining_steps).to eq(1)

      adapter.chat([])
      expect(adapter.remaining_steps).to eq(0)
    end
  end

  describe "#verify_all_consumed!" do
    it "does not raise when all steps consumed" do
      adapter = build_adapter([message_step])
      adapter.chat([])

      expect { adapter.verify_all_consumed! }.not_to raise_error
    end

    it "raises UnconsumedStepsError with remaining step IDs" do
      adapter = build_adapter([message_step, tool_call_step])
      adapter.chat([])

      expect do
        adapter.verify_all_consumed!
      end.to raise_error(Dispatch::Adapter::Tester::UnconsumedStepsError) do |error|
        expect(error.total_steps).to eq(2)
        expect(error.consumed).to eq(1)
        expect(error.remaining_step_ids).to eq([2])
      end
    end

    it "does not raise for empty playbook" do
      adapter = build_adapter([])
      expect { adapter.verify_all_consumed! }.not_to raise_error
    end
  end

  describe "#reset!" do
    it "resets the index and call_log" do
      adapter = build_adapter([message_step])
      adapter.chat([])

      adapter.reset!

      expect(adapter.current_index).to eq(0)
      expect(adapter.call_log).to be_empty
      expect(adapter.finished?).to be false
    end

    it "allows replaying the playbook" do
      adapter = build_adapter([message_step])
      r1 = adapter.chat([])
      adapter.reset!
      r2 = adapter.chat([])

      expect(r1.content).to eq(r2.content)
    end
  end
end