|
5 | 5 | "net/http" |
6 | 6 | "net/http/httptest" |
7 | 7 | "strings" |
| 8 | + "sync/atomic" |
8 | 9 | "testing" |
9 | 10 | "time" |
10 | 11 |
|
@@ -91,3 +92,271 @@ func TestEventsTailUnsupportedRouteMessage(t *testing.T) { |
91 | 92 | t.Fatalf("unexpected error: %v", err) |
92 | 93 | } |
93 | 94 | } |
| 95 | + |
| 96 | +func TestEventsTailSkipsControlMessagesByDefault(t *testing.T) { |
| 97 | + t.Setenv("BEEPER_TOKEN", "test-token") |
| 98 | + t.Setenv("BEEPER_ACCESS_TOKEN", "") |
| 99 | + |
| 100 | + upgrader := websocket.Upgrader{ |
| 101 | + CheckOrigin: func(r *http.Request) bool { return true }, |
| 102 | + } |
| 103 | + |
| 104 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 105 | + if r.URL.Path != "/v1/ws" { |
| 106 | + http.NotFound(w, r) |
| 107 | + return |
| 108 | + } |
| 109 | + if got := r.Header.Get("Authorization"); got != "Bearer test-token" { |
| 110 | + http.Error(w, "unauthorized", http.StatusUnauthorized) |
| 111 | + return |
| 112 | + } |
| 113 | + |
| 114 | + conn, err := upgrader.Upgrade(w, r, nil) |
| 115 | + if err != nil { |
| 116 | + return |
| 117 | + } |
| 118 | + defer func() { _ = conn.Close() }() |
| 119 | + |
| 120 | + var sub map[string]any |
| 121 | + if err := conn.ReadJSON(&sub); err != nil { |
| 122 | + return |
| 123 | + } |
| 124 | + |
| 125 | + _ = conn.WriteJSON(map[string]any{ |
| 126 | + "type": "ready", |
| 127 | + "version": 1, |
| 128 | + "chatIDs": []string{"*"}, |
| 129 | + }) |
| 130 | + _ = conn.WriteJSON(map[string]any{ |
| 131 | + "type": "message.upserted", |
| 132 | + "seq": 2, |
| 133 | + "ts": 1739320000001, |
| 134 | + "chatID": "chat_a", |
| 135 | + "ids": []string{"m2"}, |
| 136 | + }) |
| 137 | + time.Sleep(100 * time.Millisecond) |
| 138 | + })) |
| 139 | + defer server.Close() |
| 140 | + |
| 141 | + ctx := outfmt.WithMode(context.Background(), outfmt.Mode{JSON: true}) |
| 142 | + cmd := EventsTailCmd{ |
| 143 | + All: true, |
| 144 | + Reconnect: false, |
| 145 | + StopAfter: 250 * time.Millisecond, |
| 146 | + } |
| 147 | + |
| 148 | + out, _ := captureOutput(t, func() { |
| 149 | + if err := cmd.Run(ctx, &RootFlags{BaseURL: server.URL, Timeout: 5}); err != nil { |
| 150 | + t.Fatalf("Run() error = %v", err) |
| 151 | + } |
| 152 | + }) |
| 153 | + |
| 154 | + if strings.Contains(out, `"type":"ready"`) { |
| 155 | + t.Fatalf("expected ready control message to be filtered, got: %s", out) |
| 156 | + } |
| 157 | + if !strings.Contains(out, `"type":"message.upserted"`) { |
| 158 | + t.Fatalf("expected message.upserted in output, got: %s", out) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +func TestEventsTailIncludesControlMessagesWhenFlagSet(t *testing.T) { |
| 163 | + t.Setenv("BEEPER_TOKEN", "test-token") |
| 164 | + t.Setenv("BEEPER_ACCESS_TOKEN", "") |
| 165 | + |
| 166 | + upgrader := websocket.Upgrader{ |
| 167 | + CheckOrigin: func(r *http.Request) bool { return true }, |
| 168 | + } |
| 169 | + |
| 170 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 171 | + if r.URL.Path != "/v1/ws" { |
| 172 | + http.NotFound(w, r) |
| 173 | + return |
| 174 | + } |
| 175 | + if got := r.Header.Get("Authorization"); got != "Bearer test-token" { |
| 176 | + http.Error(w, "unauthorized", http.StatusUnauthorized) |
| 177 | + return |
| 178 | + } |
| 179 | + |
| 180 | + conn, err := upgrader.Upgrade(w, r, nil) |
| 181 | + if err != nil { |
| 182 | + return |
| 183 | + } |
| 184 | + defer func() { _ = conn.Close() }() |
| 185 | + |
| 186 | + var sub map[string]any |
| 187 | + if err := conn.ReadJSON(&sub); err != nil { |
| 188 | + return |
| 189 | + } |
| 190 | + |
| 191 | + _ = conn.WriteJSON(map[string]any{ |
| 192 | + "type": "ready", |
| 193 | + "version": 1, |
| 194 | + "chatIDs": []string{"*"}, |
| 195 | + }) |
| 196 | + time.Sleep(100 * time.Millisecond) |
| 197 | + })) |
| 198 | + defer server.Close() |
| 199 | + |
| 200 | + ctx := outfmt.WithMode(context.Background(), outfmt.Mode{JSON: true}) |
| 201 | + cmd := EventsTailCmd{ |
| 202 | + All: true, |
| 203 | + IncludeControl: true, |
| 204 | + Reconnect: false, |
| 205 | + StopAfter: 250 * time.Millisecond, |
| 206 | + } |
| 207 | + |
| 208 | + out, _ := captureOutput(t, func() { |
| 209 | + if err := cmd.Run(ctx, &RootFlags{BaseURL: server.URL, Timeout: 5}); err != nil { |
| 210 | + t.Fatalf("Run() error = %v", err) |
| 211 | + } |
| 212 | + }) |
| 213 | + |
| 214 | + if !strings.Contains(out, `"type":"ready"`) { |
| 215 | + t.Fatalf("expected ready control message in output, got: %s", out) |
| 216 | + } |
| 217 | +} |
| 218 | + |
| 219 | +func TestEventsTailReconnectsAndResubscribesAfterDisconnect(t *testing.T) { |
| 220 | + t.Setenv("BEEPER_TOKEN", "test-token") |
| 221 | + t.Setenv("BEEPER_ACCESS_TOKEN", "") |
| 222 | + |
| 223 | + upgrader := websocket.Upgrader{ |
| 224 | + CheckOrigin: func(r *http.Request) bool { return true }, |
| 225 | + } |
| 226 | + |
| 227 | + var connectCount atomic.Int32 |
| 228 | + var subscriptionCount atomic.Int32 |
| 229 | + |
| 230 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 231 | + if r.URL.Path != "/v1/ws" { |
| 232 | + http.NotFound(w, r) |
| 233 | + return |
| 234 | + } |
| 235 | + if got := r.Header.Get("Authorization"); got != "Bearer test-token" { |
| 236 | + http.Error(w, "unauthorized", http.StatusUnauthorized) |
| 237 | + return |
| 238 | + } |
| 239 | + |
| 240 | + conn, err := upgrader.Upgrade(w, r, nil) |
| 241 | + if err != nil { |
| 242 | + return |
| 243 | + } |
| 244 | + defer func() { _ = conn.Close() }() |
| 245 | + |
| 246 | + attempt := connectCount.Add(1) |
| 247 | + |
| 248 | + var sub map[string]any |
| 249 | + if err := conn.ReadJSON(&sub); err != nil { |
| 250 | + return |
| 251 | + } |
| 252 | + subscriptionCount.Add(1) |
| 253 | + |
| 254 | + if attempt == 1 { |
| 255 | + // Force reconnect by closing before any domain event is emitted. |
| 256 | + return |
| 257 | + } |
| 258 | + |
| 259 | + _ = conn.WriteJSON(map[string]any{ |
| 260 | + "type": "message.upserted", |
| 261 | + "seq": 3, |
| 262 | + "ts": 1739320000002, |
| 263 | + "chatID": "chat_b", |
| 264 | + "ids": []string{"m3"}, |
| 265 | + }) |
| 266 | + time.Sleep(100 * time.Millisecond) |
| 267 | + })) |
| 268 | + defer server.Close() |
| 269 | + |
| 270 | + ctx := outfmt.WithMode(context.Background(), outfmt.Mode{JSON: true}) |
| 271 | + cmd := EventsTailCmd{ |
| 272 | + All: true, |
| 273 | + Reconnect: true, |
| 274 | + ReconnectDelay: 10 * time.Millisecond, |
| 275 | + StopAfter: 300 * time.Millisecond, |
| 276 | + } |
| 277 | + |
| 278 | + out, _ := captureOutput(t, func() { |
| 279 | + if err := cmd.Run(ctx, &RootFlags{BaseURL: server.URL, Timeout: 5}); err != nil { |
| 280 | + t.Fatalf("Run() error = %v", err) |
| 281 | + } |
| 282 | + }) |
| 283 | + |
| 284 | + if !strings.Contains(out, `"type":"message.upserted"`) { |
| 285 | + t.Fatalf("expected message.upserted in output, got: %s", out) |
| 286 | + } |
| 287 | + if got := connectCount.Load(); got < 2 { |
| 288 | + t.Fatalf("expected reconnect attempts >= 2, got %d", got) |
| 289 | + } |
| 290 | + if got := subscriptionCount.Load(); got < 2 { |
| 291 | + t.Fatalf("expected subscriptions to be re-sent after reconnect, got %d", got) |
| 292 | + } |
| 293 | +} |
| 294 | + |
| 295 | +func TestEventsTailReconnectsAfterTemporaryHandshakeFailure(t *testing.T) { |
| 296 | + t.Setenv("BEEPER_TOKEN", "test-token") |
| 297 | + t.Setenv("BEEPER_ACCESS_TOKEN", "") |
| 298 | + |
| 299 | + upgrader := websocket.Upgrader{ |
| 300 | + CheckOrigin: func(r *http.Request) bool { return true }, |
| 301 | + } |
| 302 | + |
| 303 | + var requestCount atomic.Int32 |
| 304 | + |
| 305 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 306 | + if r.URL.Path != "/v1/ws" { |
| 307 | + http.NotFound(w, r) |
| 308 | + return |
| 309 | + } |
| 310 | + if got := r.Header.Get("Authorization"); got != "Bearer test-token" { |
| 311 | + http.Error(w, "unauthorized", http.StatusUnauthorized) |
| 312 | + return |
| 313 | + } |
| 314 | + |
| 315 | + attempt := requestCount.Add(1) |
| 316 | + if attempt == 1 { |
| 317 | + http.Error(w, "temporary failure", http.StatusInternalServerError) |
| 318 | + return |
| 319 | + } |
| 320 | + |
| 321 | + conn, err := upgrader.Upgrade(w, r, nil) |
| 322 | + if err != nil { |
| 323 | + return |
| 324 | + } |
| 325 | + defer func() { _ = conn.Close() }() |
| 326 | + |
| 327 | + var sub map[string]any |
| 328 | + if err := conn.ReadJSON(&sub); err != nil { |
| 329 | + return |
| 330 | + } |
| 331 | + _ = conn.WriteJSON(map[string]any{ |
| 332 | + "type": "message.upserted", |
| 333 | + "seq": 4, |
| 334 | + "ts": 1739320000003, |
| 335 | + "chatID": "chat_c", |
| 336 | + "ids": []string{"m4"}, |
| 337 | + }) |
| 338 | + time.Sleep(100 * time.Millisecond) |
| 339 | + })) |
| 340 | + defer server.Close() |
| 341 | + |
| 342 | + ctx := outfmt.WithMode(context.Background(), outfmt.Mode{JSON: true}) |
| 343 | + cmd := EventsTailCmd{ |
| 344 | + All: true, |
| 345 | + Reconnect: true, |
| 346 | + ReconnectDelay: 10 * time.Millisecond, |
| 347 | + StopAfter: 350 * time.Millisecond, |
| 348 | + } |
| 349 | + |
| 350 | + out, _ := captureOutput(t, func() { |
| 351 | + if err := cmd.Run(ctx, &RootFlags{BaseURL: server.URL, Timeout: 5}); err != nil { |
| 352 | + t.Fatalf("Run() error = %v", err) |
| 353 | + } |
| 354 | + }) |
| 355 | + |
| 356 | + if !strings.Contains(out, `"type":"message.upserted"`) { |
| 357 | + t.Fatalf("expected message.upserted in output, got: %s", out) |
| 358 | + } |
| 359 | + if got := requestCount.Load(); got < 2 { |
| 360 | + t.Fatalf("expected at least two connection attempts, got %d", got) |
| 361 | + } |
| 362 | +} |
0 commit comments