|
| 1 | +package acp |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/coder/acp-go-sdk" |
| 9 | +) |
| 10 | + |
| 11 | +// TestToolProgressAccumulation tests that tool progress is properly accumulated |
| 12 | +// and included in the final tool result |
| 13 | +func TestToolProgressAccumulation(t *testing.T) { |
| 14 | + agent := &ScriptschnellAIAgent{} |
| 15 | + |
| 16 | + session := &statcodeSession{ |
| 17 | + sessionID: "test-progress-accumulation", |
| 18 | + promptCtx: context.Background(), |
| 19 | + toolLocations: make(map[string][]acp.ToolCallLocation), |
| 20 | + toolParams: make(map[string]map[string]interface{}), |
| 21 | + toolProgress: make(map[string]*strings.Builder), |
| 22 | + } |
| 23 | + |
| 24 | + toolID := "test-tool-123" |
| 25 | + |
| 26 | + // Send multiple progress messages |
| 27 | + progress1 := "Starting operation...\n" |
| 28 | + progress2 := "Processing step 1...\n" |
| 29 | + progress3 := "Processing step 2...\n" |
| 30 | + |
| 31 | + // Accumulate progress messages |
| 32 | + err := agent.sendToolCallProgress(session, toolID, progress1) |
| 33 | + if err != nil { |
| 34 | + t.Fatalf("Failed to send first progress: %v", err) |
| 35 | + } |
| 36 | + |
| 37 | + err = agent.sendToolCallProgress(session, toolID, progress2) |
| 38 | + if err != nil { |
| 39 | + t.Fatalf("Failed to send second progress: %v", err) |
| 40 | + } |
| 41 | + |
| 42 | + err = agent.sendToolCallProgress(session, toolID, progress3) |
| 43 | + if err != nil { |
| 44 | + t.Fatalf("Failed to send third progress: %v", err) |
| 45 | + } |
| 46 | + |
| 47 | + // Verify progress was accumulated |
| 48 | + session.mu.Lock() |
| 49 | + progressBuilder, exists := session.toolProgress[toolID] |
| 50 | + if !exists { |
| 51 | + t.Fatal("No progress was accumulated") |
| 52 | + } |
| 53 | + accumulated := progressBuilder.String() |
| 54 | + session.mu.Unlock() |
| 55 | + |
| 56 | + expectedAccumulated := progress1 + progress2 + progress3 |
| 57 | + if accumulated != expectedAccumulated { |
| 58 | + t.Errorf("Expected accumulated progress: %q, got: %q", expectedAccumulated, accumulated) |
| 59 | + } |
| 60 | + |
| 61 | + // Test that popToolContext returns the accumulated progress |
| 62 | + params, locations, progressText := agent.popToolContext(session, toolID) |
| 63 | + |
| 64 | + if params != nil || len(locations) != 0 { |
| 65 | + t.Error("Expected empty params and locations") |
| 66 | + } |
| 67 | + |
| 68 | + if progressText != expectedAccumulated { |
| 69 | + t.Errorf("Expected progress text: %q, got: %q", expectedAccumulated, progressText) |
| 70 | + } |
| 71 | + |
| 72 | + // Verify the progress was cleaned up |
| 73 | + session.mu.Lock() |
| 74 | + _, exists = session.toolProgress[toolID] |
| 75 | + if exists { |
| 76 | + t.Error("Progress was not cleaned up after popToolContext") |
| 77 | + } |
| 78 | + session.mu.Unlock() |
| 79 | +} |
| 80 | + |
| 81 | +// TestToolProgressWithFinalResult tests that accumulated progress is combined |
| 82 | +// with the final tool result correctly |
| 83 | +func TestToolProgressWithFinalResult(t *testing.T) { |
| 84 | + agent := &ScriptschnellAIAgent{} |
| 85 | + |
| 86 | + session := &statcodeSession{ |
| 87 | + sessionID: "test-progress-with-result", |
| 88 | + promptCtx: context.Background(), |
| 89 | + toolLocations: make(map[string][]acp.ToolCallLocation), |
| 90 | + toolParams: make(map[string]map[string]interface{}), |
| 91 | + toolProgress: make(map[string]*strings.Builder), |
| 92 | + } |
| 93 | + |
| 94 | + toolID := "test-tool-with-result-123" |
| 95 | + |
| 96 | + // Store some mock tool parameters |
| 97 | + agent.rememberToolContext(session, toolID, map[string]interface{}{ |
| 98 | + "command": "echo hello", |
| 99 | + }, []acp.ToolCallLocation{}) |
| 100 | + |
| 101 | + // Send progress messages |
| 102 | + progressMsg := "Executing command...\nOutput: hello\n" |
| 103 | + err := agent.sendToolCallProgress(session, toolID, progressMsg) |
| 104 | + if err != nil { |
| 105 | + t.Fatalf("Failed to send progress: %v", err) |
| 106 | + } |
| 107 | + |
| 108 | + // Test the combination logic by manually calling popToolContext |
| 109 | + // with the final result to simulate what handleToolCallResult does |
| 110 | + params, locations, progressText := agent.popToolContext(session, toolID) |
| 111 | + finalResult := "\nCommand completed successfully." |
| 112 | + |
| 113 | + // Verify progress was accumulated correctly |
| 114 | + if progressText != progressMsg { |
| 115 | + t.Errorf("Expected progress: %q, got: %q", progressMsg, progressText) |
| 116 | + } |
| 117 | + |
| 118 | + // Simulate the combined result logic from handleToolCallResult |
| 119 | + combinedResult := finalResult |
| 120 | + if progressText != "" { |
| 121 | + if combinedResult != "" { |
| 122 | + combinedResult = progressText + combinedResult |
| 123 | + } else { |
| 124 | + combinedResult = progressText |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + expectedCombined := progressMsg + finalResult |
| 129 | + if combinedResult != expectedCombined { |
| 130 | + t.Errorf("Expected combined result: %q, got: %q", expectedCombined, combinedResult) |
| 131 | + } |
| 132 | + |
| 133 | + // Verify params and locations are also retrieved correctly |
| 134 | + if params == nil { |
| 135 | + t.Error("Expected params to be retrieved") |
| 136 | + } |
| 137 | + if len(locations) != 0 { |
| 138 | + t.Error("Expected empty locations") |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +// TestMultipleToolProgresses tests that progress accumulation works correctly |
| 143 | +// for multiple tools running concurrently |
| 144 | +func TestMultipleToolProgresses(t *testing.T) { |
| 145 | + agent := &ScriptschnellAIAgent{} |
| 146 | + |
| 147 | + session := &statcodeSession{ |
| 148 | + sessionID: "test-multiple-progresses", |
| 149 | + promptCtx: context.Background(), |
| 150 | + toolLocations: make(map[string][]acp.ToolCallLocation), |
| 151 | + toolParams: make(map[string]map[string]interface{}), |
| 152 | + toolProgress: make(map[string]*strings.Builder), |
| 153 | + } |
| 154 | + |
| 155 | + toolID1 := "tool-1" |
| 156 | + toolID2 := "tool-2" |
| 157 | + |
| 158 | + // Send progress to both tools |
| 159 | + err := agent.sendToolCallProgress(session, toolID1, "Tool 1 progress A\n") |
| 160 | + if err != nil { |
| 161 | + t.Fatalf("Failed to send progress to tool 1: %v", err) |
| 162 | + } |
| 163 | + |
| 164 | + err = agent.sendToolCallProgress(session, toolID2, "Tool 2 progress X\n") |
| 165 | + if err != nil { |
| 166 | + t.Fatalf("Failed to send progress to tool 2: %v", err) |
| 167 | + } |
| 168 | + |
| 169 | + err = agent.sendToolCallProgress(session, toolID1, "Tool 1 progress B\n") |
| 170 | + if err != nil { |
| 171 | + t.Fatalf("Failed to send more progress to tool 1: %v", err) |
| 172 | + } |
| 173 | + |
| 174 | + // Verify each tool has its own accumulated progress |
| 175 | + _, _, progress1 := agent.popToolContext(session, toolID1) |
| 176 | + if progress1 != "Tool 1 progress A\nTool 1 progress B\n" { |
| 177 | + t.Errorf("Invalid progress for tool 1: %q", progress1) |
| 178 | + } |
| 179 | + |
| 180 | + _, _, progress2 := agent.popToolContext(session, toolID2) |
| 181 | + if progress2 != "Tool 2 progress X\n" { |
| 182 | + t.Errorf("Invalid progress for tool 2: %q", progress2) |
| 183 | + } |
| 184 | +} |
0 commit comments