summaryrefslogtreecommitdiffhomepage
path: root/spec/dispatch/tools/registry_spec.rb
blob: 9574f198b528382cbf32d520e35c42495bf51c8f (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
# frozen_string_literal: true

RSpec.describe Dispatch::Tools::Registry do
  let(:read_file_tool) do
    Dispatch::Tools::Definition.new(
      name: "read_file",
      description: "Read the contents of a file",
      parameters: {
        type: "object",
        properties: {
          path: { type: "string" }
        },
        required: [ "path" ]
      }
    ) { |params, _context| Dispatch::Tools::Result.success(output: "contents of #{params[:path]}") }
  end

  let(:write_file_tool) do
    Dispatch::Tools::Definition.new(
      name: "write_file",
      description: "Write contents to a file",
      parameters: {
        type: "object",
        properties: {
          path: { type: "string" },
          content: { type: "string" }
        },
        required: %w[path content]
      }
    ) { |_params, _context| Dispatch::Tools::Result.success(output: "written") }
  end

  let(:delete_file_tool) do
    Dispatch::Tools::Definition.new(
      name: "delete_file",
      description: "Delete a file",
      parameters: {
        type: "object",
        properties: {
          path: { type: "string" }
        },
        required: [ "path" ]
      }
    ) { |_params, _context| Dispatch::Tools::Result.success(output: "deleted") }
  end

  let(:registry) { described_class.new }

  describe "#register" do
    it "adds a tool definition to the registry" do
      registry.register(read_file_tool)

      expect(registry.has?("read_file")).to be true
    end

    it "returns self for chaining" do
      result = registry.register(read_file_tool)

      expect(result).to be(registry)
    end

    it "supports chaining multiple registrations" do
      registry.register(read_file_tool).register(write_file_tool)

      expect(registry.has?("read_file")).to be true
      expect(registry.has?("write_file")).to be true
    end

    it "raises DuplicateToolError when registering a tool with the same name" do
      registry.register(read_file_tool)

      duplicate_tool = Dispatch::Tools::Definition.new(
        name: "read_file",
        description: "Another read file",
        parameters: { type: "object", properties: {}, required: [] }
      ) { |_params, _context| Dispatch::Tools::Result.success(output: "dupe") }

      expect { registry.register(duplicate_tool) }.to raise_error(Dispatch::Tools::DuplicateToolError)
    end
  end

  describe "#get" do
    before { registry.register(read_file_tool) }

    it "returns the tool definition by name" do
      tool = registry.get("read_file")

      expect(tool).to be(read_file_tool)
    end

    it "returns nil for unknown tool name" do
      expect(registry.get("nonexistent")).to be_nil
    end
  end

  describe "#has?" do
    it "returns true when the tool is registered" do
      registry.register(read_file_tool)

      expect(registry.has?("read_file")).to be true
    end

    it "returns false when the tool is not registered" do
      expect(registry.has?("read_file")).to be false
    end
  end

  describe "#tools" do
    it "returns an empty array when no tools are registered" do
      expect(registry.tools).to eq([])
    end

    it "returns all registered tool definitions" do
      registry.register(read_file_tool).register(write_file_tool)

      expect(registry.tools).to contain_exactly(read_file_tool, write_file_tool)
    end
  end

  describe "#tool_names" do
    it "returns an empty array when no tools are registered" do
      expect(registry.tool_names).to eq([])
    end

    it "returns all registered tool names as strings" do
      registry.register(read_file_tool).register(write_file_tool)

      expect(registry.tool_names).to contain_exactly("read_file", "write_file")
    end
  end

  describe "#to_a" do
    it "returns an empty array when no tools are registered" do
      expect(registry.to_a).to eq([])
    end

    it "returns an array of hashes with name, description, and parameters" do
      registry.register(read_file_tool)

      result = registry.to_a

      expect(result).to be_an(Array)
      expect(result.size).to eq(1)
      expect(result.first).to eq({
                                   name: "read_file",
                                   description: "Read the contents of a file",
                                   parameters: {
                                     type: "object",
                                     properties: {
                                       path: { type: "string" }
                                     },
                                     required: [ "path" ]
                                   }
                                 })
    end

    it "returns plain hashes, not structs" do
      registry.register(read_file_tool)

      registry.to_a.each do |entry|
        expect(entry).to be_a(Hash)
      end
    end

    it "includes all registered tools" do
      registry.register(read_file_tool).register(write_file_tool)

      names = registry.to_a.map { |h| h[:name] }

      expect(names).to contain_exactly("read_file", "write_file")
    end
  end

  describe "#subset" do
    before do
      registry.register(read_file_tool).register(write_file_tool).register(delete_file_tool)
    end

    it "returns a new Registry containing only the specified tools" do
      sub = registry.subset("read_file", "write_file")

      expect(sub).to be_a(described_class)
      expect(sub.tool_names).to contain_exactly("read_file", "write_file")
    end

    it "does not include tools not specified" do
      sub = registry.subset("read_file")

      expect(sub.has?("write_file")).to be false
      expect(sub.has?("delete_file")).to be false
    end

    it "returns a different registry instance" do
      sub = registry.subset("read_file")

      expect(sub).not_to be(registry)
    end

    it "raises ToolNotFoundError when a requested name is not found" do
      expect { registry.subset("read_file", "nonexistent") }.to raise_error(Dispatch::Tools::ToolNotFoundError)
    end
  end

  describe "#size" do
    it "returns 0 for an empty registry" do
      expect(registry.size).to eq(0)
    end

    it "returns the number of registered tools" do
      registry.register(read_file_tool).register(write_file_tool)

      expect(registry.size).to eq(2)
    end
  end

  describe "#empty?" do
    it "returns true when no tools are registered" do
      expect(registry.empty?).to be true
    end

    it "returns false when tools are registered" do
      registry.register(read_file_tool)

      expect(registry.empty?).to be false
    end
  end
end