| Philip Zeyliger | 25f6ff1 | 2025-05-02 04:24:10 +0000 | [diff] [blame] | 1 | package server_test |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "context" |
| Sean McCullough | 138ec24 | 2025-06-02 22:42:06 +0000 | [diff] [blame] | 6 | "encoding/json" |
| Philip Zeyliger | 25f6ff1 | 2025-05-02 04:24:10 +0000 | [diff] [blame] | 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "slices" |
| 10 | "strings" |
| 11 | "sync" |
| 12 | "testing" |
| 13 | "time" |
| 14 | |
| 15 | "sketch.dev/llm/conversation" |
| 16 | "sketch.dev/loop" |
| 17 | "sketch.dev/loop/server" |
| 18 | ) |
| 19 | |
| 20 | // mockAgent is a mock implementation of loop.CodingAgent for testing |
| 21 | type mockAgent struct { |
| Philip Zeyliger | eab12de | 2025-05-14 02:35:53 +0000 | [diff] [blame] | 22 | mu sync.RWMutex |
| 23 | messages []loop.AgentMessage |
| 24 | messageCount int |
| 25 | currentState string |
| 26 | subscribers []chan *loop.AgentMessage |
| 27 | stateTransitionListeners []chan loop.StateTransition |
| banksean | cad67b0 | 2025-06-27 21:57:05 +0000 | [diff] [blame] | 28 | gitUsername string |
| Philip Zeyliger | eab12de | 2025-05-14 02:35:53 +0000 | [diff] [blame] | 29 | initialCommit string |
| Philip Zeyliger | eab12de | 2025-05-14 02:35:53 +0000 | [diff] [blame] | 30 | branchName string |
| Philip Zeyliger | be7802a | 2025-06-04 20:15:25 +0000 | [diff] [blame] | 31 | branchPrefix string |
| Philip Zeyliger | d3ac112 | 2025-05-14 02:54:18 +0000 | [diff] [blame] | 32 | workingDir string |
| Josh Bleecher Snyder | 112b923 | 2025-05-23 11:26:33 -0700 | [diff] [blame] | 33 | sessionID string |
| Josh Bleecher Snyder | 19969a9 | 2025-06-05 14:34:02 -0700 | [diff] [blame] | 34 | slug string |
| 35 | retryNumber int |
| Philip Zeyliger | 0113be5 | 2025-06-07 23:53:41 +0000 | [diff] [blame] | 36 | skabandAddr string |
| Philip Zeyliger | 25f6ff1 | 2025-05-02 04:24:10 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | func (m *mockAgent) NewIterator(ctx context.Context, nextMessageIdx int) loop.MessageIterator { |
| 40 | m.mu.RLock() |
| 41 | // Send existing messages that should be available immediately |
| 42 | ch := make(chan *loop.AgentMessage, 100) |
| 43 | iter := &mockIterator{ |
| 44 | agent: m, |
| 45 | ctx: ctx, |
| 46 | nextMessageIdx: nextMessageIdx, |
| 47 | ch: ch, |
| 48 | } |
| 49 | m.mu.RUnlock() |
| 50 | return iter |
| 51 | } |
| 52 | |
| 53 | type mockIterator struct { |
| 54 | agent *mockAgent |
| 55 | ctx context.Context |
| 56 | nextMessageIdx int |
| 57 | ch chan *loop.AgentMessage |
| 58 | subscribed bool |
| 59 | } |
| 60 | |
| 61 | func (m *mockIterator) Next() *loop.AgentMessage { |
| 62 | if !m.subscribed { |
| 63 | m.agent.mu.Lock() |
| 64 | m.agent.subscribers = append(m.agent.subscribers, m.ch) |
| 65 | m.agent.mu.Unlock() |
| 66 | m.subscribed = true |
| 67 | } |
| 68 | |
| 69 | for { |
| 70 | select { |
| 71 | case <-m.ctx.Done(): |
| 72 | return nil |
| 73 | case msg := <-m.ch: |
| 74 | return msg |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | func (m *mockIterator) Close() { |
| 80 | // Remove from subscribers using slices.Delete |
| 81 | m.agent.mu.Lock() |
| 82 | for i, ch := range m.agent.subscribers { |
| 83 | if ch == m.ch { |
| 84 | m.agent.subscribers = slices.Delete(m.agent.subscribers, i, i+1) |
| 85 | break |
| 86 | } |
| 87 | } |
| 88 | m.agent.mu.Unlock() |
| 89 | close(m.ch) |
| 90 | } |
| 91 | |
| 92 | func (m *mockAgent) Messages(start int, end int) []loop.AgentMessage { |
| 93 | m.mu.RLock() |
| 94 | defer m.mu.RUnlock() |
| 95 | |
| 96 | if start >= len(m.messages) || end > len(m.messages) || start < 0 || end < 0 { |
| 97 | return []loop.AgentMessage{} |
| 98 | } |
| 99 | return slices.Clone(m.messages[start:end]) |
| 100 | } |
| 101 | |
| 102 | func (m *mockAgent) MessageCount() int { |
| 103 | m.mu.RLock() |
| 104 | defer m.mu.RUnlock() |
| 105 | return m.messageCount |
| 106 | } |
| 107 | |
| 108 | func (m *mockAgent) AddMessage(msg loop.AgentMessage) { |
| 109 | m.mu.Lock() |
| 110 | msg.Idx = m.messageCount |
| 111 | m.messages = append(m.messages, msg) |
| 112 | m.messageCount++ |
| 113 | |
| 114 | // Create a copy of subscribers to avoid holding the lock while sending |
| 115 | subscribers := make([]chan *loop.AgentMessage, len(m.subscribers)) |
| 116 | copy(subscribers, m.subscribers) |
| 117 | m.mu.Unlock() |
| 118 | |
| 119 | // Notify subscribers |
| 120 | msgCopy := msg // Create a copy to avoid race conditions |
| 121 | for _, ch := range subscribers { |
| 122 | ch <- &msgCopy |
| 123 | } |
| 124 | } |
| 125 | |
| Philip Zeyliger | eab12de | 2025-05-14 02:35:53 +0000 | [diff] [blame] | 126 | func (m *mockAgent) NewStateTransitionIterator(ctx context.Context) loop.StateTransitionIterator { |
| 127 | m.mu.Lock() |
| 128 | ch := make(chan loop.StateTransition, 10) |
| 129 | m.stateTransitionListeners = append(m.stateTransitionListeners, ch) |
| 130 | m.mu.Unlock() |
| 131 | |
| 132 | return &mockStateTransitionIterator{ |
| 133 | agent: m, |
| 134 | ctx: ctx, |
| 135 | ch: ch, |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | type mockStateTransitionIterator struct { |
| 140 | agent *mockAgent |
| 141 | ctx context.Context |
| 142 | ch chan loop.StateTransition |
| 143 | } |
| 144 | |
| 145 | func (m *mockStateTransitionIterator) Next() *loop.StateTransition { |
| 146 | select { |
| 147 | case <-m.ctx.Done(): |
| 148 | return nil |
| 149 | case transition, ok := <-m.ch: |
| 150 | if !ok { |
| 151 | return nil |
| 152 | } |
| 153 | transitionCopy := transition |
| 154 | return &transitionCopy |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | func (m *mockStateTransitionIterator) Close() { |
| 159 | m.agent.mu.Lock() |
| 160 | for i, ch := range m.agent.stateTransitionListeners { |
| 161 | if ch == m.ch { |
| 162 | m.agent.stateTransitionListeners = slices.Delete(m.agent.stateTransitionListeners, i, i+1) |
| 163 | break |
| 164 | } |
| 165 | } |
| 166 | m.agent.mu.Unlock() |
| 167 | close(m.ch) |
| 168 | } |
| 169 | |
| Philip Zeyliger | 25f6ff1 | 2025-05-02 04:24:10 +0000 | [diff] [blame] | 170 | func (m *mockAgent) CurrentStateName() string { |
| 171 | m.mu.RLock() |
| 172 | defer m.mu.RUnlock() |
| 173 | return m.currentState |
| 174 | } |
| 175 | |
| Philip Zeyliger | eab12de | 2025-05-14 02:35:53 +0000 | [diff] [blame] | 176 | func (m *mockAgent) TriggerStateTransition(from, to loop.State, event loop.TransitionEvent) { |
| 177 | m.mu.Lock() |
| 178 | m.currentState = to.String() |
| 179 | transition := loop.StateTransition{ |
| 180 | From: from, |
| 181 | To: to, |
| 182 | Event: event, |
| 183 | } |
| 184 | |
| 185 | // Create a copy of listeners to avoid holding the lock while sending |
| 186 | listeners := make([]chan loop.StateTransition, len(m.stateTransitionListeners)) |
| 187 | copy(listeners, m.stateTransitionListeners) |
| 188 | m.mu.Unlock() |
| 189 | |
| 190 | // Notify listeners |
| 191 | for _, ch := range listeners { |
| 192 | ch <- transition |
| 193 | } |
| 194 | } |
| 195 | |
| Philip Zeyliger | 25f6ff1 | 2025-05-02 04:24:10 +0000 | [diff] [blame] | 196 | func (m *mockAgent) InitialCommit() string { |
| 197 | m.mu.RLock() |
| 198 | defer m.mu.RUnlock() |
| 199 | return m.initialCommit |
| 200 | } |
| 201 | |
| Philip Zeyliger | 49edc92 | 2025-05-14 09:45:45 -0700 | [diff] [blame] | 202 | func (m *mockAgent) SketchGitBase() string { |
| 203 | m.mu.RLock() |
| 204 | defer m.mu.RUnlock() |
| 205 | return m.initialCommit |
| 206 | } |
| 207 | |
| Philip Zeyliger | d3ac112 | 2025-05-14 02:54:18 +0000 | [diff] [blame] | 208 | func (m *mockAgent) SketchGitBaseRef() string { |
| 209 | return "sketch-base-test-session" |
| 210 | } |
| 211 | |
| Philip Zeyliger | 25f6ff1 | 2025-05-02 04:24:10 +0000 | [diff] [blame] | 212 | func (m *mockAgent) BranchName() string { |
| 213 | m.mu.RLock() |
| 214 | defer m.mu.RUnlock() |
| 215 | return m.branchName |
| 216 | } |
| 217 | |
| 218 | // Other required methods of loop.CodingAgent with minimal implementation |
| 219 | func (m *mockAgent) Init(loop.AgentInit) error { return nil } |
| 220 | func (m *mockAgent) Ready() <-chan struct{} { ch := make(chan struct{}); close(ch); return ch } |
| 221 | func (m *mockAgent) URL() string { return "http://localhost:8080" } |
| 222 | func (m *mockAgent) UserMessage(ctx context.Context, msg string) {} |
| 223 | func (m *mockAgent) Loop(ctx context.Context) {} |
| 224 | func (m *mockAgent) CancelTurn(cause error) {} |
| 225 | func (m *mockAgent) CancelToolUse(id string, cause error) error { return nil } |
| 226 | func (m *mockAgent) TotalUsage() conversation.CumulativeUsage { return conversation.CumulativeUsage{} } |
| 227 | func (m *mockAgent) OriginalBudget() conversation.Budget { return conversation.Budget{} } |
| Philip Zeyliger | d3ac112 | 2025-05-14 02:54:18 +0000 | [diff] [blame] | 228 | func (m *mockAgent) WorkingDir() string { return m.workingDir } |
| Josh Bleecher Snyder | c5848f3 | 2025-05-28 18:50:58 +0000 | [diff] [blame] | 229 | func (m *mockAgent) RepoRoot() string { return m.workingDir } |
| Philip Zeyliger | 25f6ff1 | 2025-05-02 04:24:10 +0000 | [diff] [blame] | 230 | func (m *mockAgent) Diff(commit *string) (string, error) { return "", nil } |
| 231 | func (m *mockAgent) OS() string { return "linux" } |
| Josh Bleecher Snyder | 112b923 | 2025-05-23 11:26:33 -0700 | [diff] [blame] | 232 | func (m *mockAgent) SessionID() string { return m.sessionID } |
| philip.zeyliger | 8773e68 | 2025-06-11 21:36:21 -0700 | [diff] [blame] | 233 | func (m *mockAgent) SSHConnectionString() string { return "sketch-" + m.sessionID } |
| Philip Zeyliger | be7802a | 2025-06-04 20:15:25 +0000 | [diff] [blame] | 234 | func (m *mockAgent) BranchPrefix() string { return m.branchPrefix } |
| Josh Bleecher Snyder | 112b923 | 2025-05-23 11:26:33 -0700 | [diff] [blame] | 235 | func (m *mockAgent) CurrentTodoContent() string { return "" } // Mock returns empty for simplicity |
| Philip Zeyliger | 25f6ff1 | 2025-05-02 04:24:10 +0000 | [diff] [blame] | 236 | func (m *mockAgent) OutstandingLLMCallCount() int { return 0 } |
| 237 | func (m *mockAgent) OutstandingToolCalls() []string { return nil } |
| 238 | func (m *mockAgent) OutsideOS() string { return "linux" } |
| 239 | func (m *mockAgent) OutsideHostname() string { return "test-host" } |
| 240 | func (m *mockAgent) OutsideWorkingDir() string { return "/app" } |
| 241 | func (m *mockAgent) GitOrigin() string { return "" } |
| banksean | cad67b0 | 2025-06-27 21:57:05 +0000 | [diff] [blame] | 242 | func (m *mockAgent) GitUsername() string { return m.gitUsername } |
| Philip Zeyliger | 25f6ff1 | 2025-05-02 04:24:10 +0000 | [diff] [blame] | 243 | func (m *mockAgent) OpenBrowser(url string) {} |
| Philip Zeyliger | b8a8f35 | 2025-06-02 07:39:37 -0700 | [diff] [blame] | 244 | func (m *mockAgent) CompactConversation(ctx context.Context) error { |
| 245 | // Mock implementation - just return nil |
| Philip Zeyliger | 25f6ff1 | 2025-05-02 04:24:10 +0000 | [diff] [blame] | 246 | return nil |
| 247 | } |
| Philip Zeyliger | b8a8f35 | 2025-06-02 07:39:37 -0700 | [diff] [blame] | 248 | func (m *mockAgent) IsInContainer() bool { return false } |
| 249 | func (m *mockAgent) FirstMessageIndex() int { return 0 } |
| 250 | func (m *mockAgent) DetectGitChanges(ctx context.Context) error { return nil } |
| Philip Zeyliger | b573940 | 2025-06-02 07:04:34 -0700 | [diff] [blame] | 251 | |
| Josh Bleecher Snyder | 19969a9 | 2025-06-05 14:34:02 -0700 | [diff] [blame] | 252 | func (m *mockAgent) Slug() string { |
| 253 | m.mu.RLock() |
| 254 | defer m.mu.RUnlock() |
| 255 | return m.slug |
| 256 | } |
| 257 | |
| 258 | func (m *mockAgent) IncrementRetryNumber() { |
| 259 | m.mu.Lock() |
| 260 | defer m.mu.Unlock() |
| 261 | m.retryNumber++ |
| 262 | } |
| 263 | |
| Philip Zeyliger | 1609893 | 2025-06-04 11:02:55 -0700 | [diff] [blame] | 264 | func (m *mockAgent) GetPortMonitor() *loop.PortMonitor { return loop.NewPortMonitor() } |
| Philip Zeyliger | 0113be5 | 2025-06-07 23:53:41 +0000 | [diff] [blame] | 265 | func (m *mockAgent) SkabandAddr() string { return m.skabandAddr } |
| philip.zeyliger | 6d3de48 | 2025-06-10 19:38:14 -0700 | [diff] [blame] | 266 | func (m *mockAgent) LinkToGitHub() bool { return false } |
| Philip Zeyliger | 64f6046 | 2025-06-16 13:57:10 -0700 | [diff] [blame] | 267 | func (m *mockAgent) DiffStats() (int, int) { return 0, 0 } |
| Philip Zeyliger | b8a8f35 | 2025-06-02 07:39:37 -0700 | [diff] [blame] | 268 | |
| 269 | // TestSSEStream tests the SSE stream endpoint |
| Philip Zeyliger | 25f6ff1 | 2025-05-02 04:24:10 +0000 | [diff] [blame] | 270 | func TestSSEStream(t *testing.T) { |
| 271 | // Create a mock agent with initial messages |
| 272 | mockAgent := &mockAgent{ |
| Philip Zeyliger | eab12de | 2025-05-14 02:35:53 +0000 | [diff] [blame] | 273 | messages: []loop.AgentMessage{}, |
| 274 | messageCount: 0, |
| 275 | currentState: "Ready", |
| 276 | subscribers: []chan *loop.AgentMessage{}, |
| 277 | stateTransitionListeners: []chan loop.StateTransition{}, |
| 278 | initialCommit: "abcd1234", |
| Philip Zeyliger | eab12de | 2025-05-14 02:35:53 +0000 | [diff] [blame] | 279 | branchName: "sketch/test-branch", |
| Philip Zeyliger | be7802a | 2025-06-04 20:15:25 +0000 | [diff] [blame] | 280 | branchPrefix: "sketch/", |
| Josh Bleecher Snyder | 19969a9 | 2025-06-05 14:34:02 -0700 | [diff] [blame] | 281 | slug: "test-slug", |
| Philip Zeyliger | 25f6ff1 | 2025-05-02 04:24:10 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | // Add the initial messages before creating the server |
| 285 | // to ensure they're available in the Messages slice |
| 286 | msg1 := loop.AgentMessage{ |
| 287 | Type: loop.UserMessageType, |
| 288 | Content: "Hello, this is a test message", |
| 289 | Timestamp: time.Now(), |
| 290 | } |
| 291 | mockAgent.messages = append(mockAgent.messages, msg1) |
| 292 | msg1.Idx = mockAgent.messageCount |
| 293 | mockAgent.messageCount++ |
| 294 | |
| 295 | msg2 := loop.AgentMessage{ |
| 296 | Type: loop.AgentMessageType, |
| 297 | Content: "This is a response message", |
| 298 | Timestamp: time.Now(), |
| 299 | EndOfTurn: true, |
| 300 | } |
| 301 | mockAgent.messages = append(mockAgent.messages, msg2) |
| 302 | msg2.Idx = mockAgent.messageCount |
| 303 | mockAgent.messageCount++ |
| 304 | |
| 305 | // Create a server with the mock agent |
| 306 | srv, err := server.New(mockAgent, nil) |
| 307 | if err != nil { |
| 308 | t.Fatalf("Failed to create server: %v", err) |
| 309 | } |
| 310 | |
| 311 | // Create a test server |
| 312 | ts := httptest.NewServer(srv) |
| 313 | defer ts.Close() |
| 314 | |
| 315 | // Create a context with cancellation for the client request |
| 316 | ctx, cancel := context.WithCancel(context.Background()) |
| 317 | |
| 318 | // Create a request to the /stream endpoint |
| 319 | req, err := http.NewRequestWithContext(ctx, "GET", ts.URL+"/stream?from=0", nil) |
| 320 | if err != nil { |
| 321 | t.Fatalf("Failed to create request: %v", err) |
| 322 | } |
| 323 | |
| 324 | // Execute the request |
| 325 | res, err := http.DefaultClient.Do(req) |
| 326 | if err != nil { |
| 327 | t.Fatalf("Failed to execute request: %v", err) |
| 328 | } |
| 329 | defer res.Body.Close() |
| 330 | |
| 331 | // Check response status |
| 332 | if res.StatusCode != http.StatusOK { |
| 333 | t.Fatalf("Expected status OK, got %v", res.Status) |
| 334 | } |
| 335 | |
| 336 | // Check content type |
| 337 | if contentType := res.Header.Get("Content-Type"); contentType != "text/event-stream" { |
| 338 | t.Fatalf("Expected Content-Type text/event-stream, got %s", contentType) |
| 339 | } |
| 340 | |
| 341 | // Read response events using a scanner |
| 342 | scanner := bufio.NewScanner(res.Body) |
| 343 | |
| 344 | // Track events received |
| 345 | eventsReceived := map[string]int{ |
| 346 | "state": 0, |
| 347 | "message": 0, |
| 348 | "heartbeat": 0, |
| 349 | } |
| 350 | |
| 351 | // Read for a short time to capture initial state and messages |
| 352 | dataLines := []string{} |
| 353 | eventType := "" |
| 354 | |
| 355 | go func() { |
| 356 | // After reading for a while, add a new message to test real-time updates |
| 357 | time.Sleep(500 * time.Millisecond) |
| 358 | |
| 359 | mockAgent.AddMessage(loop.AgentMessage{ |
| 360 | Type: loop.ToolUseMessageType, |
| 361 | Content: "This is a new real-time message", |
| 362 | Timestamp: time.Now(), |
| 363 | ToolName: "test_tool", |
| 364 | }) |
| 365 | |
| Philip Zeyliger | eab12de | 2025-05-14 02:35:53 +0000 | [diff] [blame] | 366 | // Trigger a state transition to test state updates |
| 367 | time.Sleep(200 * time.Millisecond) |
| 368 | mockAgent.TriggerStateTransition(loop.StateReady, loop.StateSendingToLLM, loop.TransitionEvent{ |
| 369 | Description: "Agent started thinking", |
| 370 | Data: "start_thinking", |
| 371 | }) |
| 372 | |
| Philip Zeyliger | 25f6ff1 | 2025-05-02 04:24:10 +0000 | [diff] [blame] | 373 | // Let it process for longer |
| 374 | time.Sleep(1000 * time.Millisecond) |
| 375 | cancel() // Cancel to end the test |
| 376 | }() |
| 377 | |
| 378 | // Read events |
| 379 | for scanner.Scan() { |
| 380 | line := scanner.Text() |
| 381 | |
| 382 | if strings.HasPrefix(line, "event: ") { |
| 383 | eventType = strings.TrimPrefix(line, "event: ") |
| 384 | eventsReceived[eventType]++ |
| 385 | } else if strings.HasPrefix(line, "data: ") { |
| 386 | dataLines = append(dataLines, line) |
| 387 | } else if line == "" && eventType != "" { |
| 388 | // End of event |
| 389 | eventType = "" |
| 390 | } |
| 391 | |
| 392 | // Break if context is done |
| 393 | if ctx.Err() != nil { |
| 394 | break |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | if err := scanner.Err(); err != nil && ctx.Err() == nil { |
| 399 | t.Fatalf("Scanner error: %v", err) |
| 400 | } |
| 401 | |
| 402 | // Simplified validation - just make sure we received something |
| 403 | t.Logf("Events received: %v", eventsReceived) |
| 404 | t.Logf("Data lines received: %d", len(dataLines)) |
| 405 | |
| 406 | // Basic validation that we received at least some events |
| 407 | if eventsReceived["state"] == 0 && eventsReceived["message"] == 0 { |
| 408 | t.Errorf("Did not receive any events") |
| 409 | } |
| 410 | } |
| Philip Zeyliger | d3ac112 | 2025-05-14 02:54:18 +0000 | [diff] [blame] | 411 | |
| 412 | func TestGitRawDiffHandler(t *testing.T) { |
| 413 | // Create a mock agent |
| 414 | mockAgent := &mockAgent{ |
| Philip Zeyliger | be7802a | 2025-06-04 20:15:25 +0000 | [diff] [blame] | 415 | workingDir: t.TempDir(), // Use a temp directory |
| 416 | branchPrefix: "sketch/", |
| Philip Zeyliger | d3ac112 | 2025-05-14 02:54:18 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | // Create the server with the mock agent |
| 420 | server, err := server.New(mockAgent, nil) |
| 421 | if err != nil { |
| 422 | t.Fatalf("Failed to create server: %v", err) |
| 423 | } |
| 424 | |
| 425 | // Create a test HTTP server |
| 426 | testServer := httptest.NewServer(server) |
| 427 | defer testServer.Close() |
| 428 | |
| 429 | // Test missing parameters |
| 430 | resp, err := http.Get(testServer.URL + "/git/rawdiff") |
| 431 | if err != nil { |
| 432 | t.Fatalf("Failed to make HTTP request: %v", err) |
| 433 | } |
| 434 | if resp.StatusCode != http.StatusBadRequest { |
| 435 | t.Errorf("Expected status bad request, got: %d", resp.StatusCode) |
| 436 | } |
| 437 | |
| 438 | // Test with commit parameter (this will fail due to no git repo, but we're testing the API, not git) |
| 439 | resp, err = http.Get(testServer.URL + "/git/rawdiff?commit=HEAD") |
| 440 | if err != nil { |
| 441 | t.Fatalf("Failed to make HTTP request: %v", err) |
| 442 | } |
| 443 | // We expect an error since there's no git repository, but the request should be processed |
| 444 | if resp.StatusCode != http.StatusInternalServerError { |
| 445 | t.Errorf("Expected status 500, got: %d", resp.StatusCode) |
| 446 | } |
| 447 | |
| 448 | // Test with from/to parameters |
| 449 | resp, err = http.Get(testServer.URL + "/git/rawdiff?from=HEAD~1&to=HEAD") |
| 450 | if err != nil { |
| 451 | t.Fatalf("Failed to make HTTP request: %v", err) |
| 452 | } |
| 453 | // We expect an error since there's no git repository, but the request should be processed |
| 454 | if resp.StatusCode != http.StatusInternalServerError { |
| 455 | t.Errorf("Expected status 500, got: %d", resp.StatusCode) |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | func TestGitShowHandler(t *testing.T) { |
| 460 | // Create a mock agent |
| 461 | mockAgent := &mockAgent{ |
| Philip Zeyliger | be7802a | 2025-06-04 20:15:25 +0000 | [diff] [blame] | 462 | workingDir: t.TempDir(), // Use a temp directory |
| 463 | branchPrefix: "sketch/", |
| Philip Zeyliger | d3ac112 | 2025-05-14 02:54:18 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | // Create the server with the mock agent |
| 467 | server, err := server.New(mockAgent, nil) |
| 468 | if err != nil { |
| 469 | t.Fatalf("Failed to create server: %v", err) |
| 470 | } |
| 471 | |
| 472 | // Create a test HTTP server |
| 473 | testServer := httptest.NewServer(server) |
| 474 | defer testServer.Close() |
| 475 | |
| 476 | // Test missing parameter |
| 477 | resp, err := http.Get(testServer.URL + "/git/show") |
| 478 | if err != nil { |
| 479 | t.Fatalf("Failed to make HTTP request: %v", err) |
| 480 | } |
| 481 | if resp.StatusCode != http.StatusBadRequest { |
| 482 | t.Errorf("Expected status bad request, got: %d", resp.StatusCode) |
| 483 | } |
| 484 | |
| 485 | // Test with hash parameter (this will fail due to no git repo, but we're testing the API, not git) |
| 486 | resp, err = http.Get(testServer.URL + "/git/show?hash=HEAD") |
| 487 | if err != nil { |
| 488 | t.Fatalf("Failed to make HTTP request: %v", err) |
| 489 | } |
| 490 | // We expect an error since there's no git repository, but the request should be processed |
| 491 | if resp.StatusCode != http.StatusInternalServerError { |
| 492 | t.Errorf("Expected status 500, got: %d", resp.StatusCode) |
| 493 | } |
| 494 | } |
| 495 | |
| Philip Zeyliger | b8a8f35 | 2025-06-02 07:39:37 -0700 | [diff] [blame] | 496 | func TestCompactHandler(t *testing.T) { |
| 497 | // Test that mock CompactConversation works |
| 498 | mockAgent := &mockAgent{ |
| 499 | messages: []loop.AgentMessage{}, |
| 500 | messageCount: 0, |
| 501 | sessionID: "test-session", |
| Philip Zeyliger | be7802a | 2025-06-04 20:15:25 +0000 | [diff] [blame] | 502 | branchPrefix: "sketch/", |
| Philip Zeyliger | b8a8f35 | 2025-06-02 07:39:37 -0700 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | ctx := context.Background() |
| 506 | err := mockAgent.CompactConversation(ctx) |
| 507 | if err != nil { |
| 508 | t.Errorf("Mock CompactConversation failed: %v", err) |
| 509 | } |
| 510 | |
| 511 | // No HTTP endpoint to test anymore - compaction is done via /compact message |
| 512 | t.Log("Mock CompactConversation works correctly") |
| 513 | } |
| Sean McCullough | 138ec24 | 2025-06-02 22:42:06 +0000 | [diff] [blame] | 514 | |
| 515 | // TestPortEventsEndpoint tests the /port-events HTTP endpoint |
| 516 | func TestPortEventsEndpoint(t *testing.T) { |
| 517 | // Create a mock agent that implements the CodingAgent interface |
| Philip Zeyliger | be7802a | 2025-06-04 20:15:25 +0000 | [diff] [blame] | 518 | agent := &mockAgent{ |
| 519 | branchPrefix: "sketch/", |
| 520 | } |
| Sean McCullough | 138ec24 | 2025-06-02 22:42:06 +0000 | [diff] [blame] | 521 | |
| 522 | // Create a server with the mock agent |
| 523 | server, err := server.New(agent, nil) |
| 524 | if err != nil { |
| 525 | t.Fatalf("Failed to create server: %v", err) |
| 526 | } |
| 527 | |
| 528 | // Test GET /port-events |
| 529 | req, err := http.NewRequest("GET", "/port-events", nil) |
| 530 | if err != nil { |
| 531 | t.Fatalf("Failed to create request: %v", err) |
| 532 | } |
| 533 | |
| 534 | rr := httptest.NewRecorder() |
| 535 | server.ServeHTTP(rr, req) |
| 536 | |
| 537 | // Should return 200 OK |
| 538 | if status := rr.Code; status != http.StatusOK { |
| 539 | t.Errorf("Expected status code %d, got %d", http.StatusOK, status) |
| 540 | } |
| 541 | |
| 542 | // Should return JSON content type |
| 543 | contentType := rr.Header().Get("Content-Type") |
| 544 | if contentType != "application/json" { |
| 545 | t.Errorf("Expected Content-Type application/json, got %s", contentType) |
| 546 | } |
| 547 | |
| 548 | // Should return valid JSON (empty array since mock returns no events) |
| 549 | var events []any |
| 550 | if err := json.Unmarshal(rr.Body.Bytes(), &events); err != nil { |
| 551 | t.Errorf("Failed to parse JSON response: %v", err) |
| 552 | } |
| 553 | |
| 554 | // Should be empty array for mock agent |
| 555 | if len(events) != 0 { |
| 556 | t.Errorf("Expected empty events array, got %d events", len(events)) |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | // TestPortEventsEndpointMethodNotAllowed tests that non-GET requests are rejected |
| 561 | func TestPortEventsEndpointMethodNotAllowed(t *testing.T) { |
| Philip Zeyliger | be7802a | 2025-06-04 20:15:25 +0000 | [diff] [blame] | 562 | agent := &mockAgent{ |
| 563 | branchPrefix: "sketch/", |
| 564 | } |
| Sean McCullough | 138ec24 | 2025-06-02 22:42:06 +0000 | [diff] [blame] | 565 | server, err := server.New(agent, nil) |
| 566 | if err != nil { |
| 567 | t.Fatalf("Failed to create server: %v", err) |
| 568 | } |
| 569 | |
| 570 | // Test POST /port-events (should be rejected) |
| 571 | req, err := http.NewRequest("POST", "/port-events", nil) |
| 572 | if err != nil { |
| 573 | t.Fatalf("Failed to create request: %v", err) |
| 574 | } |
| 575 | |
| 576 | rr := httptest.NewRecorder() |
| 577 | server.ServeHTTP(rr, req) |
| 578 | |
| 579 | // Should return 405 Method Not Allowed |
| 580 | if status := rr.Code; status != http.StatusMethodNotAllowed { |
| 581 | t.Errorf("Expected status code %d, got %d", http.StatusMethodNotAllowed, status) |
| 582 | } |
| 583 | } |
| Philip Zeyliger | a9710d7 | 2025-07-02 02:50:14 +0000 | [diff] [blame^] | 584 | |
| 585 | func TestParsePortProxyHost(t *testing.T) { |
| 586 | tests := []struct { |
| 587 | name string |
| 588 | host string |
| 589 | wantPort string |
| 590 | }{ |
| 591 | { |
| 592 | name: "valid port proxy host", |
| 593 | host: "p8000.localhost", |
| 594 | wantPort: "8000", |
| 595 | }, |
| 596 | { |
| 597 | name: "valid port proxy host with port suffix", |
| 598 | host: "p8000.localhost:8080", |
| 599 | wantPort: "8000", |
| 600 | }, |
| 601 | { |
| 602 | name: "different port", |
| 603 | host: "p3000.localhost", |
| 604 | wantPort: "3000", |
| 605 | }, |
| 606 | { |
| 607 | name: "regular localhost", |
| 608 | host: "localhost", |
| 609 | wantPort: "", |
| 610 | }, |
| 611 | { |
| 612 | name: "different domain", |
| 613 | host: "p8000.example.com", |
| 614 | wantPort: "", |
| 615 | }, |
| 616 | { |
| 617 | name: "missing p prefix", |
| 618 | host: "8000.localhost", |
| 619 | wantPort: "", |
| 620 | }, |
| 621 | { |
| 622 | name: "invalid port", |
| 623 | host: "pabc.localhost", |
| 624 | wantPort: "", |
| 625 | }, |
| 626 | { |
| 627 | name: "just p prefix", |
| 628 | host: "p.localhost", |
| 629 | wantPort: "", |
| 630 | }, |
| 631 | { |
| 632 | name: "port too high", |
| 633 | host: "p99999.localhost", |
| 634 | wantPort: "", |
| 635 | }, |
| 636 | { |
| 637 | name: "port zero", |
| 638 | host: "p0.localhost", |
| 639 | wantPort: "", |
| 640 | }, |
| 641 | { |
| 642 | name: "negative port", |
| 643 | host: "p-1.localhost", |
| 644 | wantPort: "", |
| 645 | }, |
| 646 | } |
| 647 | |
| 648 | // Create a test server to access the method |
| 649 | s, err := server.New(nil, nil) |
| 650 | if err != nil { |
| 651 | t.Fatalf("Failed to create server: %v", err) |
| 652 | } |
| 653 | |
| 654 | for _, tt := range tests { |
| 655 | t.Run(tt.name, func(t *testing.T) { |
| 656 | gotPort := s.ParsePortProxyHost(tt.host) |
| 657 | if gotPort != tt.wantPort { |
| 658 | t.Errorf("parsePortProxyHost(%q) = %q, want %q", tt.host, gotPort, tt.wantPort) |
| 659 | } |
| 660 | }) |
| 661 | } |
| 662 | } |