|
6 | 6 | "fmt" |
7 | 7 | "net/http" |
8 | 8 | "net/http/httptest" |
9 | | - "strings" |
10 | 9 | "testing" |
11 | 10 | ) |
12 | 11 |
|
@@ -91,124 +90,3 @@ func TestQueryCommandStream_ErrorResponseBody(t *testing.T) { |
91 | 90 | }) |
92 | 91 | } |
93 | 92 | } |
94 | | - |
95 | | -func TestQueryCommandStream_SSEParsing(t *testing.T) { |
96 | | - tests := []struct { |
97 | | - name string |
98 | | - body string |
99 | | - wantErr bool |
100 | | - wantErrSubstr string |
101 | | - wantTokens []string |
102 | | - }{ |
103 | | - { |
104 | | - name: "data with space and [DONE] terminates cleanly", |
105 | | - body: "data: [DONE]\n\n", |
106 | | - wantTokens: nil, |
107 | | - }, |
108 | | - { |
109 | | - name: "data without space and [DONE] terminates cleanly", |
110 | | - body: "data:[DONE]\n\n", |
111 | | - wantTokens: nil, |
112 | | - }, |
113 | | - { |
114 | | - name: "single data token with leading space is stripped", |
115 | | - body: "data: hello\n\ndata: [DONE]\n\n", |
116 | | - wantTokens: []string{"hello"}, |
117 | | - }, |
118 | | - { |
119 | | - name: "single data token without leading space passes through", |
120 | | - body: "data:hello\n\ndata:[DONE]\n\n", |
121 | | - wantTokens: []string{"hello"}, |
122 | | - }, |
123 | | - { |
124 | | - name: "multi-token stream concatenates without spurious spaces", |
125 | | - body: "data: ls\n\ndata: -la\n\ndata: [DONE]\n\n", |
126 | | - wantTokens: []string{"ls", " -la"}, |
127 | | - }, |
128 | | - { |
129 | | - name: "event error with space", |
130 | | - body: "event: error\ndata: boom\n\n", |
131 | | - wantErr: true, |
132 | | - wantErrSubstr: "boom", |
133 | | - }, |
134 | | - { |
135 | | - name: "event error without space", |
136 | | - body: "event:error\ndata:boom\n\n", |
137 | | - wantErr: true, |
138 | | - wantErrSubstr: "boom", |
139 | | - }, |
140 | | - { |
141 | | - name: "blank line resets error state between events", |
142 | | - body: "event: error\n\ndata: hello\n\ndata: [DONE]\n\n", |
143 | | - wantTokens: []string{"hello"}, |
144 | | - }, |
145 | | - } |
146 | | - |
147 | | - for _, tt := range tests { |
148 | | - t.Run(tt.name, func(t *testing.T) { |
149 | | - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
150 | | - w.Header().Set("Content-Type", "text/event-stream") |
151 | | - w.WriteHeader(http.StatusOK) |
152 | | - _, _ = w.Write([]byte(tt.body)) |
153 | | - })) |
154 | | - defer server.Close() |
155 | | - |
156 | | - var got []string |
157 | | - svc := NewAIService() |
158 | | - err := svc.QueryCommandStream( |
159 | | - context.Background(), |
160 | | - CommandSuggestVariables{Shell: "bash", Os: "linux", Query: "test"}, |
161 | | - Endpoint{APIEndpoint: server.URL, Token: "test-token"}, |
162 | | - func(token string) { got = append(got, token) }, |
163 | | - ) |
164 | | - |
165 | | - if tt.wantErr { |
166 | | - if err == nil { |
167 | | - t.Fatalf("expected error, got nil (tokens=%v)", got) |
168 | | - } |
169 | | - if !strings.Contains(err.Error(), tt.wantErrSubstr) { |
170 | | - t.Fatalf("expected error to contain %q, got %q", tt.wantErrSubstr, err.Error()) |
171 | | - } |
172 | | - return |
173 | | - } |
174 | | - |
175 | | - if err != nil { |
176 | | - t.Fatalf("unexpected error: %v", err) |
177 | | - } |
178 | | - if len(got) != len(tt.wantTokens) { |
179 | | - t.Fatalf("token count mismatch: want %d %v, got %d %v", len(tt.wantTokens), tt.wantTokens, len(got), got) |
180 | | - } |
181 | | - for i, tok := range tt.wantTokens { |
182 | | - if got[i] != tok { |
183 | | - t.Errorf("token[%d] = %q, want %q", i, got[i], tok) |
184 | | - } |
185 | | - } |
186 | | - }) |
187 | | - } |
188 | | -} |
189 | | - |
190 | | -func TestStripSSEField(t *testing.T) { |
191 | | - tests := []struct { |
192 | | - name string |
193 | | - line string |
194 | | - prefix string |
195 | | - wantVal string |
196 | | - wantOk bool |
197 | | - }{ |
198 | | - {"no match", "foo:bar", "data:", "", false}, |
199 | | - {"match no space", "data:hello", "data:", "hello", true}, |
200 | | - {"match one space stripped", "data: hello", "data:", "hello", true}, |
201 | | - {"match two spaces preserves second", "data: hello", "data:", " hello", true}, |
202 | | - {"empty value no space", "data:", "data:", "", true}, |
203 | | - {"empty value one space", "data: ", "data:", "", true}, |
204 | | - {"event error with space", "event: error", "event:", "error", true}, |
205 | | - } |
206 | | - for _, tt := range tests { |
207 | | - t.Run(tt.name, func(t *testing.T) { |
208 | | - v, ok := stripSSEField(tt.line, tt.prefix) |
209 | | - if ok != tt.wantOk || v != tt.wantVal { |
210 | | - t.Errorf("stripSSEField(%q, %q) = (%q, %v), want (%q, %v)", tt.line, tt.prefix, v, ok, tt.wantVal, tt.wantOk) |
211 | | - } |
212 | | - }) |
213 | | - } |
214 | | -} |
0 commit comments