summaryrefslogtreecommitdiffhomepage
path: root/spec/dispatch/adapter/tester/step_spec.rb
blob: df5cb1e33552e4181d5ef0e79859bcad59f53f62 (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
# frozen_string_literal: true

RSpec.describe Dispatch::Adapter::Tester::Step do
  describe "validation" do
    it "parses a valid message step" do
      step = described_class.new("step" => 1, "type" => "message", "content" => "Hello")

      expect(step.step_id).to eq(1)
      expect(step.type).to eq("message")
      expect(step.content).to eq("Hello")
      expect(step).to be_message
      expect(step).not_to be_tool_calls
      expect(step.tool_calls).to be_empty
    end

    it "parses a valid tool_calls step" do
      step = described_class.new(
        "step" => 2,
        "type" => "tool_calls",
        "tool_calls" => [
          { "id" => "tc_001", "name" => "read_file", "arguments" => { "path" => "/a.rb" } }
        ]
      )

      expect(step.step_id).to eq(2)
      expect(step).to be_tool_calls
      expect(step.tool_calls.length).to eq(1)
      expect(step.tool_calls.first["id"]).to eq("tc_001")
    end

    it "allows content on tool_calls steps" do
      step = described_class.new(
        "step" => 3,
        "type" => "tool_calls",
        "content" => "I will do something",
        "tool_calls" => [
          { "id" => "tc_001", "name" => "test", "arguments" => {} }
        ]
      )

      expect(step.content).to eq("I will do something")
    end

    it "is frozen after initialization" do
      step = described_class.new("step" => 1, "type" => "message", "content" => "hi")
      expect(step).to be_frozen
    end

    it "has a useful to_s" do
      step = described_class.new("step" => 42, "type" => "message", "content" => "hi")
      expect(step.to_s).to eq("Step #42 (message)")
    end

    context "missing fields" do
      it "raises when step ID is missing" do
        expect do
          described_class.new("type" => "message", "content" => "hi")
        end.to raise_error(Dispatch::Adapter::Tester::InvalidPlaybookError, /missing required field 'step'/)
      end

      it "raises when step ID is not an integer" do
        expect do
          described_class.new("step" => "one", "type" => "message", "content" => "hi")
        end.to raise_error(Dispatch::Adapter::Tester::InvalidPlaybookError, /must be an integer/)
      end

      it "raises when type is missing" do
        expect do
          described_class.new("step" => 1, "content" => "hi")
        end.to raise_error(Dispatch::Adapter::Tester::InvalidPlaybookError, /missing required field 'type'/)
      end

      it "raises when type is invalid" do
        expect do
          described_class.new("step" => 1, "type" => "invalid", "content" => "hi")
        end.to raise_error(Dispatch::Adapter::Tester::InvalidPlaybookError, /invalid type/)
      end
    end

    context "message step validation" do
      it "raises when content is missing" do
        expect do
          described_class.new("step" => 1, "type" => "message")
        end.to raise_error(Dispatch::Adapter::Tester::InvalidPlaybookError, /requires a non-empty 'content'/)
      end

      it "raises when content is empty string" do
        expect do
          described_class.new("step" => 1, "type" => "message", "content" => "")
        end.to raise_error(Dispatch::Adapter::Tester::InvalidPlaybookError, /requires a non-empty 'content'/)
      end

      it "raises when content is not a string" do
        expect do
          described_class.new("step" => 1, "type" => "message", "content" => 123)
        end.to raise_error(Dispatch::Adapter::Tester::InvalidPlaybookError, /requires a non-empty 'content'/)
      end
    end

    context "tool_calls step validation" do
      it "raises when tool_calls array is missing" do
        expect do
          described_class.new("step" => 1, "type" => "tool_calls")
        end.to raise_error(Dispatch::Adapter::Tester::InvalidPlaybookError, /requires a non-empty 'tool_calls' array/)
      end

      it "raises when tool_calls array is empty" do
        expect do
          described_class.new("step" => 1, "type" => "tool_calls", "tool_calls" => [])
        end.to raise_error(Dispatch::Adapter::Tester::InvalidPlaybookError, /requires a non-empty 'tool_calls' array/)
      end

      it "raises when tool_call is missing 'id'" do
        expect do
          described_class.new(
            "step" => 1, "type" => "tool_calls",
            "tool_calls" => [{ "name" => "test", "arguments" => {} }]
          )
        end.to raise_error(Dispatch::Adapter::Tester::InvalidPlaybookError, /missing required field 'id'/)
      end

      it "raises when tool_call is missing 'name'" do
        expect do
          described_class.new(
            "step" => 1, "type" => "tool_calls",
            "tool_calls" => [{ "id" => "tc_001", "arguments" => {} }]
          )
        end.to raise_error(Dispatch::Adapter::Tester::InvalidPlaybookError, /missing required field 'name'/)
      end

      it "raises when tool_call is missing 'arguments'" do
        expect do
          described_class.new(
            "step" => 1, "type" => "tool_calls",
            "tool_calls" => [{ "id" => "tc_001", "name" => "test" }]
          )
        end.to raise_error(Dispatch::Adapter::Tester::InvalidPlaybookError, /missing required field 'arguments'/)
      end

      it "raises when tool_call 'id' is not a string" do
        expect do
          described_class.new(
            "step" => 1, "type" => "tool_calls",
            "tool_calls" => [{ "id" => 1, "name" => "test", "arguments" => {} }]
          )
        end.to raise_error(Dispatch::Adapter::Tester::InvalidPlaybookError, /'id' must be a string/)
      end

      it "raises when tool_call 'name' is not a string" do
        expect do
          described_class.new(
            "step" => 1, "type" => "tool_calls",
            "tool_calls" => [{ "id" => "tc_001", "name" => 123, "arguments" => {} }]
          )
        end.to raise_error(Dispatch::Adapter::Tester::InvalidPlaybookError, /'name' must be a string/)
      end

      it "raises when tool_call 'arguments' is not a hash" do
        expect do
          described_class.new(
            "step" => 1, "type" => "tool_calls",
            "tool_calls" => [{ "id" => "tc_001", "name" => "test", "arguments" => "bad" }]
          )
        end.to raise_error(Dispatch::Adapter::Tester::InvalidPlaybookError, /'arguments' must be a JSON object/)
      end

      it "raises when tool_call entry is not a hash" do
        expect do
          described_class.new(
            "step" => 1, "type" => "tool_calls",
            "tool_calls" => ["not a hash"]
          )
        end.to raise_error(Dispatch::Adapter::Tester::InvalidPlaybookError, /must be a JSON object/)
      end
    end

    context "non-hash input" do
      it "raises when step data is not a hash" do
        expect do
          described_class.new("just a string")
        end.to raise_error(Dispatch::Adapter::Tester::InvalidPlaybookError, /must be a JSON object/)
      end
    end
  end
end