@@ -244,55 +244,121 @@ func TestCloseEventDataMarshaling(t *testing.T) {
244244 }
245245}
246246
247- func TestFormatJSONRPCEvent (t * testing.T ) {
248- // Test data
249- eventType := "test_event"
250- eventID := "test-request-123"
251- eventData := map [string ]string {
252- "key1" : "value1" ,
253- "key2" : "value2" ,
247+ func TestFormatJSONRPCEventBatch (t * testing.T ) {
248+ // Test data - multiple events
249+ events := []EventBatch {
250+ {
251+ EventType : "status_update" ,
252+ ID : "test-request-123" ,
253+ Data : map [string ]string {
254+ "status" : "working" ,
255+ "task_id" : "task-1" ,
256+ },
257+ },
258+ {
259+ EventType : "artifact_update" ,
260+ ID : "test-request-123" ,
261+ Data : map [string ]string {
262+ "artifact_id" : "artifact-1" ,
263+ "content" : "test content" ,
264+ },
265+ },
266+ {
267+ EventType : "status_update" ,
268+ ID : "test-request-123" ,
269+ Data : map [string ]string {
270+ "status" : "completed" ,
271+ "task_id" : "task-1" ,
272+ },
273+ },
254274 }
255275
256276 // Buffer to capture the output
257277 var buf bytes.Buffer
258278
259- // Format the event
260- err := FormatJSONRPCEvent (& buf , eventType , eventID , eventData )
261- assert .NoError (t , err , "FormatJSONRPCEvent should not return an error" )
279+ // Format the batch
280+ err := FormatJSONRPCEventBatch (& buf , events )
281+ assert .NoError (t , err , "FormatJSONRPCEventBatch should not return an error" )
262282
263283 // Get the formatted output
264284 output := buf .String ()
265285
266- // Verify the SSE format structure
267- assert .Contains (t , output , "event: test_event " , "Output should contain the event type" )
268- assert .Contains (t , output , "data: { " , "Output should contain JSON data " )
286+ // Verify the SSE format structure for all events
287+ assert .Contains (t , output , "event: status_update " , "Output should contain status_update event type" )
288+ assert .Contains (t , output , "event: artifact_update " , "Output should contain artifact_update event type " )
269289 assert .Contains (t , output , "\" jsonrpc\" :\" 2.0\" " , "Output should contain JSON-RPC version" )
270290 assert .Contains (t , output , "\" id\" :\" test-request-123\" " , "Output should contain the request ID" )
271291
272- // Verify the content can be parsed back
292+ // Verify we have the expected number of events (3 events = 3 event: lines)
293+ eventLines := strings .Count (output , "event: " )
294+ assert .Equal (t , 3 , eventLines , "Should have 3 event lines" )
295+
296+ // Verify we have the expected number of data lines (3 events = 3 data: lines)
297+ dataLines := strings .Count (output , "data: " )
298+ assert .Equal (t , 3 , dataLines , "Should have 3 data lines" )
299+
300+ // Parse and verify each event
273301 scanner := bufio .NewScanner (strings .NewReader (output ))
274- var dataLine string
302+ var currentEventType string
303+ var eventCount int
304+
275305 for scanner .Scan () {
276306 line := scanner .Text ()
277- if strings .HasPrefix (line , "data: " ) {
278- dataLine = strings .TrimPrefix (line , "data: " )
279- break
307+ if strings .HasPrefix (line , "event: " ) {
308+ currentEventType = strings .TrimPrefix (line , "event: " )
309+ } else if strings .HasPrefix (line , "data: " ) {
310+ dataLine := strings .TrimPrefix (line , "data: " )
311+
312+ // Parse the JSON-RPC response
313+ var response jsonrpc.Response
314+ err = json .Unmarshal ([]byte (dataLine ), & response )
315+ assert .NoError (t , err , "Should be able to unmarshal the JSON-RPC response" )
316+
317+ // Check response structure
318+ assert .Equal (t , "2.0" , response .JSONRPC , "JSONRPC version should be 2.0" )
319+ assert .Equal (t , "test-request-123" , response .ID , "ID should match the provided request ID" )
320+
321+ // Verify result based on event type
322+ resultMap , ok := response .Result .(map [string ]interface {})
323+ assert .True (t , ok , "Result should be a map" )
324+
325+ switch currentEventType {
326+ case "status_update" :
327+ if resultMap ["status" ] == "working" {
328+ assert .Equal (t , "working" , resultMap ["status" ], "Working status should match" )
329+ assert .Equal (t , "task-1" , resultMap ["task_id" ], "Task ID should match" )
330+ } else if resultMap ["status" ] == "completed" {
331+ assert .Equal (t , "completed" , resultMap ["status" ], "Completed status should match" )
332+ assert .Equal (t , "task-1" , resultMap ["task_id" ], "Task ID should match" )
333+ }
334+ case "artifact_update" :
335+ assert .Equal (t , "artifact-1" , resultMap ["artifact_id" ], "Artifact ID should match" )
336+ assert .Equal (t , "test content" , resultMap ["content" ], "Content should match" )
337+ }
338+
339+ eventCount ++
280340 }
281341 }
282342
283- // Parse the JSON-RPC response
284- var response jsonrpc.Response
285- err = json .Unmarshal ([]byte (dataLine ), & response )
286- assert .NoError (t , err , "Should be able to unmarshal the JSON-RPC response" )
287-
288- // Check response structure
289- assert .Equal (t , "2.0" , response .JSONRPC , "JSONRPC version should be 2.0" )
290- assert .Equal (t , eventID , response .ID , "ID should match the provided request ID" )
291-
292- // Verify result contains the same key-value pairs
293- // JSON unmarshaling creates map[string]interface{}, so we can't use direct equality
294- resultMap , ok := response .Result .(map [string ]interface {})
295- assert .True (t , ok , "Result should be a map" )
296- assert .Equal (t , "value1" , resultMap ["key1" ], "Value for key1 should match" )
297- assert .Equal (t , "value2" , resultMap ["key2" ], "Value for key2 should match" )
343+ assert .Equal (t , 3 , eventCount , "Should have processed 3 events" )
344+ }
345+
346+ func TestFormatJSONRPCEventBatch_EmptySlice (t * testing.T ) {
347+ // Test with empty events slice
348+ var buf bytes.Buffer
349+ err := FormatJSONRPCEventBatch (& buf , []EventBatch {})
350+ assert .NoError (t , err , "FormatJSONRPCEventBatch with empty slice should not return an error" )
351+
352+ output := buf .String ()
353+ assert .Empty (t , output , "Output should be empty for empty events slice" )
354+ }
355+
356+ func TestFormatJSONRPCEventBatch_NilSlice (t * testing.T ) {
357+ // Test with nil events slice
358+ var buf bytes.Buffer
359+ err := FormatJSONRPCEventBatch (& buf , nil )
360+ assert .NoError (t , err , "FormatJSONRPCEventBatch with nil slice should not return an error" )
361+
362+ output := buf .String ()
363+ assert .Empty (t , output , "Output should be empty for nil events slice" )
298364}
0 commit comments