|
| 1 | +package agent |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "sync" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/nextlevelbuilder/goclaw/internal/pipeline" |
| 9 | + "github.com/nextlevelbuilder/goclaw/internal/providers" |
| 10 | + "github.com/nextlevelbuilder/goclaw/internal/tools" |
| 11 | + "github.com/nextlevelbuilder/goclaw/pkg/protocol" |
| 12 | +) |
| 13 | + |
| 14 | +// stubExecutor implements tools.ToolExecutor with a canned successful Result. |
| 15 | +// Used to isolate the tool-callback wrappers from real tool registry wiring. |
| 16 | +type stubExecutor struct{} |
| 17 | + |
| 18 | +func (s *stubExecutor) ExecuteWithContext(_ context.Context, _ string, _ map[string]any, _, _, _, _ string, _ tools.AsyncCallback) *tools.Result { |
| 19 | + return &tools.Result{ForLLM: "ok", IsError: false} |
| 20 | +} |
| 21 | +func (s *stubExecutor) TryActivateDeferred(string) bool { return false } |
| 22 | +func (s *stubExecutor) ProviderDefs() []providers.ToolDefinition { return nil } |
| 23 | +func (s *stubExecutor) Get(string) (tools.Tool, bool) { return nil, false } |
| 24 | +func (s *stubExecutor) List() []string { return nil } |
| 25 | +func (s *stubExecutor) Aliases() map[string]string { return nil } |
| 26 | + |
| 27 | +// eventCollector buffers AgentEvents for inspection in tests. |
| 28 | +// Safe for concurrent appends from parallel goroutines. |
| 29 | +type eventCollector struct { |
| 30 | + mu sync.Mutex |
| 31 | + events []AgentEvent |
| 32 | +} |
| 33 | + |
| 34 | +func (c *eventCollector) onEvent(e AgentEvent) { |
| 35 | + c.mu.Lock() |
| 36 | + c.events = append(c.events, e) |
| 37 | + c.mu.Unlock() |
| 38 | +} |
| 39 | + |
| 40 | +func (c *eventCollector) filter(typ string) []AgentEvent { |
| 41 | + c.mu.Lock() |
| 42 | + defer c.mu.Unlock() |
| 43 | + var out []AgentEvent |
| 44 | + for _, e := range c.events { |
| 45 | + if e.Type == typ { |
| 46 | + out = append(out, e) |
| 47 | + } |
| 48 | + } |
| 49 | + return out |
| 50 | +} |
| 51 | + |
| 52 | +// newTestLoopForToolCallbacks builds a minimal Loop instance sufficient to |
| 53 | +// exercise makeExecuteToolCall / makeExecuteToolRaw. All optional subsystems |
| 54 | +// (tracing, metrics, input guard) are left nil and hit early-return paths. |
| 55 | +func newTestLoopForToolCallbacks(onEvent func(AgentEvent)) *Loop { |
| 56 | + return &Loop{ |
| 57 | + id: "test-agent", |
| 58 | + tools: &stubExecutor{}, |
| 59 | + onEvent: onEvent, |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// TestMakeExecuteToolCall_EmitsToolCallEvent verifies the sequential wrapper |
| 64 | +// emits a tool.call event before running tool I/O. |
| 65 | +func TestMakeExecuteToolCall_EmitsToolCallEvent(t *testing.T) { |
| 66 | + t.Parallel() |
| 67 | + col := &eventCollector{} |
| 68 | + l := newTestLoopForToolCallbacks(col.onEvent) |
| 69 | + |
| 70 | + req := &RunRequest{ |
| 71 | + RunID: "run-1", |
| 72 | + SessionKey: "sess-A", |
| 73 | + UserID: "u-1", |
| 74 | + Channel: "ws", |
| 75 | + RunKind: "", |
| 76 | + } |
| 77 | + state := &pipeline.RunState{RunID: "run-1"} |
| 78 | + tc := providers.ToolCall{ID: "tc-1", Name: "read_file", Arguments: map[string]any{"path": "/tmp/x"}} |
| 79 | + |
| 80 | + _, err := l.makeExecuteToolCall(req, &runState{})(context.Background(), state, tc) |
| 81 | + if err != nil { |
| 82 | + t.Fatalf("makeExecuteToolCall returned error: %v", err) |
| 83 | + } |
| 84 | + |
| 85 | + calls := col.filter(protocol.AgentEventToolCall) |
| 86 | + if len(calls) != 1 { |
| 87 | + t.Fatalf("expected 1 tool.call event, got %d (all events: %+v)", len(calls), col.events) |
| 88 | + } |
| 89 | + assertToolCallPayload(t, calls[0], tc, req) |
| 90 | +} |
| 91 | + |
| 92 | +// TestMakeExecuteToolRaw_EmitsToolCallEvent is the PRIMARY regression guard. |
| 93 | +// The original bug: parallel path (makeExecuteToolRaw) did not emit tool.call, |
| 94 | +// so web UI and desktop UI silently dropped tool info during real-time streaming. |
| 95 | +// Mutation-verify: remove emitRun(...) from makeExecuteToolRaw — this test must fail. |
| 96 | +func TestMakeExecuteToolRaw_EmitsToolCallEvent(t *testing.T) { |
| 97 | + t.Parallel() |
| 98 | + col := &eventCollector{} |
| 99 | + l := newTestLoopForToolCallbacks(col.onEvent) |
| 100 | + |
| 101 | + req := &RunRequest{ |
| 102 | + RunID: "run-2", |
| 103 | + SessionKey: "sess-B", |
| 104 | + UserID: "u-2", |
| 105 | + Channel: "ws", |
| 106 | + RunKind: "", |
| 107 | + } |
| 108 | + tc := providers.ToolCall{ID: "tc-2", Name: "write_file", Arguments: map[string]any{"path": "/tmp/y"}} |
| 109 | + |
| 110 | + msg, raw, err := l.makeExecuteToolRaw(req)(context.Background(), tc) |
| 111 | + if err != nil { |
| 112 | + t.Fatalf("makeExecuteToolRaw returned error: %v", err) |
| 113 | + } |
| 114 | + if msg.Role != "tool" || msg.ToolCallID != tc.ID { |
| 115 | + t.Errorf("unexpected tool message: %+v", msg) |
| 116 | + } |
| 117 | + if raw == nil { |
| 118 | + t.Error("expected non-nil raw data (toolRawResult)") |
| 119 | + } |
| 120 | + |
| 121 | + calls := col.filter(protocol.AgentEventToolCall) |
| 122 | + if len(calls) != 1 { |
| 123 | + t.Fatalf("expected 1 tool.call event, got %d (all events: %+v)", len(calls), col.events) |
| 124 | + } |
| 125 | + assertToolCallPayload(t, calls[0], tc, req) |
| 126 | +} |
| 127 | + |
| 128 | +// TestMakeExecuteToolRaw_ConcurrentCallsEmitAllEvents confirms the parallel |
| 129 | +// wrapper is safe to invoke from multiple goroutines — mirrors the real |
| 130 | +// executeParallel dispatch in pipeline/tool_stage.go. |
| 131 | +func TestMakeExecuteToolRaw_ConcurrentCallsEmitAllEvents(t *testing.T) { |
| 132 | + t.Parallel() |
| 133 | + col := &eventCollector{} |
| 134 | + l := newTestLoopForToolCallbacks(col.onEvent) |
| 135 | + |
| 136 | + req := &RunRequest{RunID: "run-3", SessionKey: "sess-C", UserID: "u-3", Channel: "ws"} |
| 137 | + exec := l.makeExecuteToolRaw(req) |
| 138 | + |
| 139 | + const n = 5 |
| 140 | + var wg sync.WaitGroup |
| 141 | + for i := range n { |
| 142 | + wg.Add(1) |
| 143 | + go func(idx int) { |
| 144 | + defer wg.Done() |
| 145 | + tc := providers.ToolCall{ID: "tc-" + string(rune('a'+idx)), Name: "t", Arguments: nil} |
| 146 | + if _, _, err := exec(context.Background(), tc); err != nil { |
| 147 | + t.Errorf("goroutine %d: %v", idx, err) |
| 148 | + } |
| 149 | + }(i) |
| 150 | + } |
| 151 | + wg.Wait() |
| 152 | + |
| 153 | + calls := col.filter(protocol.AgentEventToolCall) |
| 154 | + if len(calls) != n { |
| 155 | + t.Fatalf("expected %d tool.call events, got %d", n, len(calls)) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +// assertToolCallPayload verifies the event carries the expected tc identity |
| 160 | +// and routing context from RunRequest. |
| 161 | +func assertToolCallPayload(t *testing.T, ev AgentEvent, tc providers.ToolCall, req *RunRequest) { |
| 162 | + t.Helper() |
| 163 | + if ev.AgentID != "test-agent" { |
| 164 | + t.Errorf("AgentID: got %q, want test-agent", ev.AgentID) |
| 165 | + } |
| 166 | + if ev.RunID != req.RunID { |
| 167 | + t.Errorf("RunID: got %q, want %q", ev.RunID, req.RunID) |
| 168 | + } |
| 169 | + if ev.SessionKey != req.SessionKey { |
| 170 | + t.Errorf("SessionKey: got %q, want %q", ev.SessionKey, req.SessionKey) |
| 171 | + } |
| 172 | + if ev.Channel != req.Channel { |
| 173 | + t.Errorf("Channel: got %q, want %q", ev.Channel, req.Channel) |
| 174 | + } |
| 175 | + if ev.UserID != req.UserID { |
| 176 | + t.Errorf("UserID: got %q, want %q", ev.UserID, req.UserID) |
| 177 | + } |
| 178 | + payload, ok := ev.Payload.(map[string]any) |
| 179 | + if !ok { |
| 180 | + t.Fatalf("Payload is not map[string]any: %T", ev.Payload) |
| 181 | + } |
| 182 | + if payload["id"] != tc.ID { |
| 183 | + t.Errorf("payload.id: got %v, want %q", payload["id"], tc.ID) |
| 184 | + } |
| 185 | + if payload["name"] != tc.Name { |
| 186 | + t.Errorf("payload.name: got %v, want %q", payload["name"], tc.Name) |
| 187 | + } |
| 188 | +} |
0 commit comments