|
| 1 | +package ssestream |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "net/http" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +type mockReadCloser struct { |
| 10 | + *bytes.Reader |
| 11 | +} |
| 12 | + |
| 13 | +func (m mockReadCloser) Close() error { |
| 14 | + return nil |
| 15 | +} |
| 16 | + |
| 17 | +// TestStream_EmptyEvents tests that the stream correctly handles empty SSE events |
| 18 | +// (e.g., from retry: directives or comment lines) without crashing on JSON unmarshal |
| 19 | +func TestStream_EmptyEvents(t *testing.T) { |
| 20 | + // Simulate SSE stream with retry directive that creates empty event |
| 21 | + sseData := `retry: 3000 |
| 22 | +
|
| 23 | +data: {"id":"msg_01ABC","type":"content_block_delta","delta":{"type":"text","text":"Hello"}} |
| 24 | +
|
| 25 | +data: [DONE] |
| 26 | +
|
| 27 | +` |
| 28 | + |
| 29 | + resp := &http.Response{ |
| 30 | + StatusCode: 200, |
| 31 | + Header: http.Header{"Content-Type": []string{"text/event-stream"}}, |
| 32 | + Body: mockReadCloser{bytes.NewReader([]byte(sseData))}, |
| 33 | + } |
| 34 | + |
| 35 | + decoder := NewDecoder(resp) |
| 36 | + if decoder == nil { |
| 37 | + t.Fatal("Expected decoder to be created, got nil") |
| 38 | + } |
| 39 | + |
| 40 | + type testMsg struct { |
| 41 | + ID string `json:"id"` |
| 42 | + Type string `json:"type"` |
| 43 | + Delta struct { |
| 44 | + Type string `json:"type"` |
| 45 | + Text string `json:"text"` |
| 46 | + } `json:"delta"` |
| 47 | + } |
| 48 | + |
| 49 | + stream := NewStream[testMsg](decoder, nil) |
| 50 | + |
| 51 | + // Should successfully iterate without crashing on empty event |
| 52 | + var receivedMessages int |
| 53 | + for stream.Next() { |
| 54 | + msg := stream.Current() |
| 55 | + receivedMessages++ |
| 56 | + |
| 57 | + if msg.ID != "msg_01ABC" { |
| 58 | + t.Errorf("Expected ID 'msg_01ABC', got '%s'", msg.ID) |
| 59 | + } |
| 60 | + if msg.Delta.Text != "Hello" { |
| 61 | + t.Errorf("Expected text 'Hello', got '%s'", msg.Delta.Text) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if err := stream.Err(); err != nil { |
| 66 | + t.Errorf("Expected no error, got: %v", err) |
| 67 | + } |
| 68 | + |
| 69 | + if receivedMessages != 1 { |
| 70 | + t.Errorf("Expected 1 message, got %d", receivedMessages) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// TestStream_OnlyRetryDirective tests stream with only retry directive (no data events) |
| 75 | +func TestStream_OnlyRetryDirective(t *testing.T) { |
| 76 | + sseData := `retry: 3000 |
| 77 | +
|
| 78 | +` |
| 79 | + |
| 80 | + resp := &http.Response{ |
| 81 | + StatusCode: 200, |
| 82 | + Header: http.Header{"Content-Type": []string{"text/event-stream"}}, |
| 83 | + Body: mockReadCloser{bytes.NewReader([]byte(sseData))}, |
| 84 | + } |
| 85 | + |
| 86 | + decoder := NewDecoder(resp) |
| 87 | + type testMsg struct { |
| 88 | + ID string `json:"id"` |
| 89 | + } |
| 90 | + stream := NewStream[testMsg](decoder, nil) |
| 91 | + |
| 92 | + // Should handle gracefully without any messages |
| 93 | + var count int |
| 94 | + for stream.Next() { |
| 95 | + count++ |
| 96 | + } |
| 97 | + |
| 98 | + if err := stream.Err(); err != nil { |
| 99 | + t.Errorf("Expected no error, got: %v", err) |
| 100 | + } |
| 101 | + |
| 102 | + if count != 0 { |
| 103 | + t.Errorf("Expected 0 messages, got %d", count) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +// TestStream_MultipleEmptyEvents tests handling of multiple empty events |
| 108 | +func TestStream_MultipleEmptyEvents(t *testing.T) { |
| 109 | + sseData := `retry: 3000 |
| 110 | +
|
| 111 | +: comment line |
| 112 | +
|
| 113 | +data: {"id":"1","text":"first"} |
| 114 | +
|
| 115 | +retry: 5000 |
| 116 | +
|
| 117 | +data: {"id":"2","text":"second"} |
| 118 | +
|
| 119 | +` |
| 120 | + |
| 121 | + resp := &http.Response{ |
| 122 | + StatusCode: 200, |
| 123 | + Header: http.Header{"Content-Type": []string{"text/event-stream"}}, |
| 124 | + Body: mockReadCloser{bytes.NewReader([]byte(sseData))}, |
| 125 | + } |
| 126 | + |
| 127 | + decoder := NewDecoder(resp) |
| 128 | + type testMsg struct { |
| 129 | + ID string `json:"id"` |
| 130 | + Text string `json:"text"` |
| 131 | + } |
| 132 | + stream := NewStream[testMsg](decoder, nil) |
| 133 | + |
| 134 | + messages := []testMsg{} |
| 135 | + for stream.Next() { |
| 136 | + messages = append(messages, stream.Current()) |
| 137 | + } |
| 138 | + |
| 139 | + if err := stream.Err(); err != nil { |
| 140 | + t.Errorf("Expected no error, got: %v", err) |
| 141 | + } |
| 142 | + |
| 143 | + if len(messages) != 2 { |
| 144 | + t.Fatalf("Expected 2 messages, got %d", len(messages)) |
| 145 | + } |
| 146 | + |
| 147 | + if messages[0].ID != "1" || messages[0].Text != "first" { |
| 148 | + t.Errorf("First message incorrect: %+v", messages[0]) |
| 149 | + } |
| 150 | + |
| 151 | + if messages[1].ID != "2" || messages[1].Text != "second" { |
| 152 | + t.Errorf("Second message incorrect: %+v", messages[1]) |
| 153 | + } |
| 154 | +} |
0 commit comments