|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | RSpec.describe Mars::Agent do |
4 | | - describe "#initialize" do |
5 | | - it "initializes with a name" do |
6 | | - agent = described_class.new(name: "TestAgent") |
7 | | - expect(agent.name).to eq("TestAgent") |
| 4 | + describe "#run" do |
| 5 | + let(:agent) { described_class.new(name: "TestAgent", options: { model: "test-model" }) } |
| 6 | + let(:mock_chat_instance) do |
| 7 | + instance_double("RubyLLM::Chat").tap do |mock| |
| 8 | + allow(mock).to receive_messages(with_tools: mock, with_schema: mock, ask: nil) |
| 9 | + end |
8 | 10 | end |
| 11 | + let(:mock_chat_class) { class_double("RubyLLM::Chat", new: mock_chat_instance) } |
9 | 12 |
|
10 | | - it "accepts options parameter" do |
11 | | - options = { model: "gpt-4", temperature: 0.7 } |
12 | | - agent = described_class.new(name: "TestAgent", options: options) |
13 | | - expect(agent.name).to eq("TestAgent") |
| 13 | + before do |
| 14 | + stub_const("RubyLLM::Chat", mock_chat_class) |
14 | 15 | end |
15 | 16 |
|
16 | | - it "accepts tools parameter as an array" do |
17 | | - tool1 = instance_double("Tool1") |
18 | | - tool2 = instance_double("Tool2") |
19 | | - tools = [tool1, tool2] |
20 | | - agent = described_class.new(name: "TestAgent", tools: tools) |
21 | | - expect(agent.name).to eq("TestAgent") |
22 | | - end |
| 17 | + it "initializes RubyLLM::Chat with provided options" do |
| 18 | + agent.run("test input") |
23 | 19 |
|
24 | | - it "accepts a single tool and converts it to an array" do |
25 | | - tool = instance_double("Tool") |
26 | | - agent = described_class.new(name: "TestAgent", tools: tool) |
27 | | - expect(agent.name).to eq("TestAgent") |
| 20 | + expect(mock_chat_class).to have_received(:new).with(model: "test-model") |
28 | 21 | end |
29 | 22 |
|
30 | | - it "accepts schema parameter" do |
31 | | - schema = { type: "object", properties: {} } |
32 | | - agent = described_class.new(name: "TestAgent", schema: schema) |
33 | | - expect(agent.name).to eq("TestAgent") |
34 | | - end |
| 23 | + it "configures chat with tools if provided" do |
| 24 | + tools = [proc { "tool" }] |
| 25 | + agent_with_tools = described_class.new(name: "TestAgent", tools: tools) |
| 26 | + agent_with_tools.run("test input") |
35 | 27 |
|
36 | | - it "accepts all parameters together" do |
37 | | - tool = instance_double("Tool") |
38 | | - agent = described_class.new( |
39 | | - name: "CompleteAgent", |
40 | | - options: { model: "gpt-4" }, |
41 | | - tools: [tool], |
42 | | - schema: { type: "object" } |
43 | | - ) |
44 | | - expect(agent.name).to eq("CompleteAgent") |
| 28 | + expect(mock_chat_instance).to have_received(:with_tools).with(tools) |
45 | 29 | end |
46 | | - end |
47 | | - |
48 | | - describe "#name" do |
49 | | - it "returns the agent name" do |
50 | | - agent = described_class.new(name: "MyAgent") |
51 | | - expect(agent.name).to eq("MyAgent") |
52 | | - end |
53 | | - end |
54 | | - |
55 | | - describe "#run" do |
56 | | - let(:agent) { described_class.new(name: "TestAgent") } |
57 | | - let(:mock_chat) { instance_double("Chat") } |
58 | 30 |
|
59 | | - it "delegates to chat.ask with the input" do |
60 | | - allow(agent).to receive(:chat).and_return(mock_chat) |
61 | | - allow(mock_chat).to receive(:ask).with("test input").and_return("response") |
62 | | - |
63 | | - result = agent.run("test input") |
64 | | - |
65 | | - expect(result).to eq("response") |
66 | | - expect(mock_chat).to have_received(:ask).with("test input") |
67 | | - end |
68 | | - |
69 | | - it "passes different inputs to chat.ask" do |
70 | | - allow(agent).to receive(:chat).and_return(mock_chat) |
71 | | - allow(mock_chat).to receive(:ask).and_return("response") |
72 | | - |
73 | | - agent.run("first input") |
74 | | - agent.run("second input") |
75 | | - |
76 | | - expect(mock_chat).to have_received(:ask).with("first input") |
77 | | - expect(mock_chat).to have_received(:ask).with("second input") |
78 | | - end |
79 | | - end |
| 31 | + it "configures chat with schema if provided" do |
| 32 | + schema = { type: "object" } |
| 33 | + agent_with_schema = described_class.new(name: "TestAgent", schema: schema) |
80 | 34 |
|
81 | | - describe "inheritance" do |
82 | | - it "inherits from Mars::Runnable" do |
83 | | - expect(described_class.ancestors).to include(Mars::Runnable) |
| 35 | + agent_with_schema.run("test input") |
| 36 | + expect(mock_chat_instance).to have_received(:with_schema).with(schema) |
84 | 37 | end |
85 | 38 | end |
86 | 39 | end |
0 commit comments