|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/killme2008/devtap/internal/mcp" |
| 9 | + "github.com/killme2008/devtap/internal/store" |
| 10 | +) |
| 11 | + |
| 12 | +func TestTruncateMessagesNoTruncation(t *testing.T) { |
| 13 | + messages := []store.LogMessage{ |
| 14 | + {Tag: "a", Lines: []string{"line1", "line2"}}, |
| 15 | + {Tag: "b", Lines: []string{"line3"}}, |
| 16 | + } |
| 17 | + result := mcp.TruncateMessages(messages, 10) |
| 18 | + if len(result) != 2 { |
| 19 | + t.Fatalf("expected 2 messages, got %d", len(result)) |
| 20 | + } |
| 21 | + if len(result[0].Lines) != 2 || len(result[1].Lines) != 1 { |
| 22 | + t.Error("lines should be unchanged when under limit") |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func TestTruncateMessagesTruncates(t *testing.T) { |
| 27 | + // Create 2 messages with 60 lines each = 120 total, truncate to 20. |
| 28 | + lines1 := make([]string, 60) |
| 29 | + lines2 := make([]string, 60) |
| 30 | + for i := range lines1 { |
| 31 | + lines1[i] = "msg1-line" |
| 32 | + lines2[i] = "msg2-line" |
| 33 | + } |
| 34 | + |
| 35 | + messages := []store.LogMessage{ |
| 36 | + {Tag: "a", Lines: lines1}, |
| 37 | + {Tag: "b", Lines: lines2}, |
| 38 | + } |
| 39 | + result := mcp.TruncateMessages(messages, 20) |
| 40 | + |
| 41 | + // Both messages should be present with truncated lines. |
| 42 | + totalLines := 0 |
| 43 | + for _, msg := range result { |
| 44 | + totalLines += len(msg.Lines) |
| 45 | + } |
| 46 | + if totalLines > 20 { |
| 47 | + t.Errorf("total lines %d should not exceed max 20", totalLines) |
| 48 | + } |
| 49 | + |
| 50 | + // Both messages should have lines (proportional allocation). |
| 51 | + if len(result[0].Lines) == 0 { |
| 52 | + t.Error("first message should have lines after truncation") |
| 53 | + } |
| 54 | + if len(result[1].Lines) == 0 { |
| 55 | + t.Error("second message should have lines after truncation") |
| 56 | + } |
| 57 | + |
| 58 | + // Tags should be preserved. |
| 59 | + if result[0].Tag != "a" || result[1].Tag != "b" { |
| 60 | + t.Error("tags should be preserved") |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestTruncateMessagesPreservesEmptyLines(t *testing.T) { |
| 65 | + messages := []store.LogMessage{ |
| 66 | + {Tag: "a", Lines: []string{"line1"}}, |
| 67 | + {Tag: "b"}, |
| 68 | + {Tag: "c", Lines: []string{"line2"}}, |
| 69 | + } |
| 70 | + result := mcp.TruncateMessages(messages, 10) |
| 71 | + if len(result) != 3 { |
| 72 | + t.Fatalf("expected 3 messages, got %d", len(result)) |
| 73 | + } |
| 74 | + if result[1].Lines != nil { |
| 75 | + t.Error("empty message should remain empty") |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func TestTruncateMessagesZeroMaxLines(t *testing.T) { |
| 80 | + messages := []store.LogMessage{ |
| 81 | + {Tag: "a", Lines: []string{"line1"}}, |
| 82 | + } |
| 83 | + result := mcp.TruncateMessages(messages, 0) |
| 84 | + if len(result) != 1 || len(result[0].Lines) != 1 { |
| 85 | + t.Error("maxLines=0 should skip truncation") |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestHandleAutoLoopStopNoErrors(t *testing.T) { |
| 90 | + dir := t.TempDir() |
| 91 | + exitCode := 0 |
| 92 | + messages := []store.LogMessage{ |
| 93 | + {Tag: "build", ExitCode: &exitCode}, |
| 94 | + } |
| 95 | + |
| 96 | + // Capture stdout |
| 97 | + old := os.Stdout |
| 98 | + r, w, _ := os.Pipe() |
| 99 | + os.Stdout = w |
| 100 | + |
| 101 | + err := handleAutoLoopStop(dir, "test-session", messages, 5) |
| 102 | + |
| 103 | + _ = w.Close() |
| 104 | + os.Stdout = old |
| 105 | + |
| 106 | + if err != nil { |
| 107 | + t.Fatalf("handleAutoLoopStop: %v", err) |
| 108 | + } |
| 109 | + |
| 110 | + buf := make([]byte, 1024) |
| 111 | + n, _ := r.Read(buf) |
| 112 | + output := string(buf[:n]) |
| 113 | + if output != "{}" { |
| 114 | + t.Errorf("expected '{}' for no errors, got %q", output) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +func TestHandleAutoLoopStopWithErrors(t *testing.T) { |
| 119 | + dir := t.TempDir() |
| 120 | + _ = os.MkdirAll(filepath.Join(dir, "test-session"), 0o755) |
| 121 | + |
| 122 | + exitCode := 1 |
| 123 | + messages := []store.LogMessage{ |
| 124 | + {Tag: "build", Lines: []string{"error: something failed"}, ExitCode: &exitCode}, |
| 125 | + } |
| 126 | + |
| 127 | + old := os.Stdout |
| 128 | + r, w, _ := os.Pipe() |
| 129 | + os.Stdout = w |
| 130 | + |
| 131 | + err := handleAutoLoopStop(dir, "test-session", messages, 5) |
| 132 | + |
| 133 | + _ = w.Close() |
| 134 | + os.Stdout = old |
| 135 | + |
| 136 | + if err != nil { |
| 137 | + t.Fatalf("handleAutoLoopStop: %v", err) |
| 138 | + } |
| 139 | + |
| 140 | + buf := make([]byte, 4096) |
| 141 | + n, _ := r.Read(buf) |
| 142 | + output := string(buf[:n]) |
| 143 | + |
| 144 | + if output == "{}" { |
| 145 | + t.Error("should block when errors present") |
| 146 | + } |
| 147 | + // Should contain "block" decision |
| 148 | + if !contains(output, "block") { |
| 149 | + t.Errorf("output should contain 'block' decision, got %q", output) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +func TestHandleAutoLoopStopExceedsRetries(t *testing.T) { |
| 154 | + dir := t.TempDir() |
| 155 | + sessionID := "test-session" |
| 156 | + _ = os.MkdirAll(filepath.Join(dir, sessionID), 0o755) |
| 157 | + |
| 158 | + exitCode := 1 |
| 159 | + messages := []store.LogMessage{ |
| 160 | + {Tag: "build", Lines: []string{"error"}, ExitCode: &exitCode}, |
| 161 | + } |
| 162 | + |
| 163 | + // Exhaust retries. |
| 164 | + maxRetries := 2 |
| 165 | + for i := 0; i < maxRetries; i++ { |
| 166 | + old := os.Stdout |
| 167 | + _, w, _ := os.Pipe() |
| 168 | + os.Stdout = w |
| 169 | + _ = handleAutoLoopStop(dir, sessionID, messages, maxRetries) |
| 170 | + _ = w.Close() |
| 171 | + os.Stdout = old |
| 172 | + } |
| 173 | + |
| 174 | + // Next call should allow stop (return "{}"). |
| 175 | + old := os.Stdout |
| 176 | + r, w, _ := os.Pipe() |
| 177 | + os.Stdout = w |
| 178 | + |
| 179 | + err := handleAutoLoopStop(dir, sessionID, messages, maxRetries) |
| 180 | + |
| 181 | + _ = w.Close() |
| 182 | + os.Stdout = old |
| 183 | + |
| 184 | + if err != nil { |
| 185 | + t.Fatalf("handleAutoLoopStop: %v", err) |
| 186 | + } |
| 187 | + |
| 188 | + buf := make([]byte, 1024) |
| 189 | + n, _ := r.Read(buf) |
| 190 | + output := string(buf[:n]) |
| 191 | + if output != "{}" { |
| 192 | + t.Errorf("expected '{}' after exhausting retries, got %q", output) |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +func contains(s, substr string) bool { |
| 197 | + return len(s) >= len(substr) && (s == substr || len(s) > 0 && containsSubstr(s, substr)) |
| 198 | +} |
| 199 | + |
| 200 | +func containsSubstr(s, substr string) bool { |
| 201 | + for i := 0; i <= len(s)-len(substr); i++ { |
| 202 | + if s[i:i+len(substr)] == substr { |
| 203 | + return true |
| 204 | + } |
| 205 | + } |
| 206 | + return false |
| 207 | +} |
0 commit comments