@@ -259,23 +259,46 @@ describe("MessageEnhancer", () => {
259259 describe ( "captureTelemetry" , ( ) => {
260260 it ( "should capture telemetry when TelemetryService is available" , ( ) => {
261261 const mockTaskId = "task-123"
262- MessageEnhancer . captureTelemetry ( mockTaskId )
262+ const mockCaptureEvent = vi . fn ( )
263+ vi . mocked ( TelemetryService . instance ) . captureEvent = mockCaptureEvent
264+
265+ MessageEnhancer . captureTelemetry ( mockTaskId , true )
263266
264267 expect ( TelemetryService . hasInstance ) . toHaveBeenCalled ( )
265- expect ( TelemetryService . instance . capturePromptEnhanced ) . toHaveBeenCalledWith ( mockTaskId )
268+ expect ( mockCaptureEvent ) . toHaveBeenCalledWith ( expect . any ( String ) , {
269+ taskId : mockTaskId ,
270+ includeTaskHistory : true ,
271+ } )
266272 } )
267273
268274 it ( "should handle missing TelemetryService gracefully" , ( ) => {
269275 vi . mocked ( TelemetryService ) . hasInstance = vi . fn ( ) . mockReturnValue ( false )
270276
271277 // Should not throw
272- expect ( ( ) => MessageEnhancer . captureTelemetry ( "task-123" ) ) . not . toThrow ( )
278+ expect ( ( ) => MessageEnhancer . captureTelemetry ( "task-123" , true ) ) . not . toThrow ( )
273279 } )
274280
275281 it ( "should work without task ID" , ( ) => {
276- MessageEnhancer . captureTelemetry ( )
282+ const mockCaptureEvent = vi . fn ( )
283+ vi . mocked ( TelemetryService . instance ) . captureEvent = mockCaptureEvent
277284
278- expect ( TelemetryService . instance . capturePromptEnhanced ) . toHaveBeenCalledWith ( undefined )
285+ MessageEnhancer . captureTelemetry ( undefined , false )
286+
287+ expect ( mockCaptureEvent ) . toHaveBeenCalledWith ( expect . any ( String ) , {
288+ includeTaskHistory : false ,
289+ } )
290+ } )
291+
292+ it ( "should default includeTaskHistory to false when not provided" , ( ) => {
293+ const mockCaptureEvent = vi . fn ( )
294+ vi . mocked ( TelemetryService . instance ) . captureEvent = mockCaptureEvent
295+
296+ MessageEnhancer . captureTelemetry ( "task-123" )
297+
298+ expect ( mockCaptureEvent ) . toHaveBeenCalledWith ( expect . any ( String ) , {
299+ taskId : "task-123" ,
300+ includeTaskHistory : false ,
301+ } )
279302 } )
280303 } )
281304
@@ -299,5 +322,44 @@ describe("MessageEnhancer", () => {
299322 expect ( history ) . not . toContain ( "Tool use" )
300323 expect ( history . split ( "\n" ) . length ) . toBe ( 3 ) // Only 3 valid messages
301324 } )
325+
326+ it ( "should handle malformed messages gracefully" , ( ) => {
327+ const consoleSpy = vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } )
328+
329+ // Create messages that will cause errors when accessed
330+ const malformedMessages = [
331+ null ,
332+ undefined ,
333+ { type : "ask" } , // Missing required properties
334+ "not an object" ,
335+ ] as any
336+
337+ // Access private method through any type assertion for testing
338+ const history = ( MessageEnhancer as any ) . extractTaskHistory ( malformedMessages )
339+
340+ // Should return empty string and log error
341+ expect ( history ) . toBe ( "" )
342+ expect ( consoleSpy ) . toHaveBeenCalledWith ( "Failed to extract task history:" , expect . any ( Error ) )
343+
344+ consoleSpy . mockRestore ( )
345+ } )
346+
347+ it ( "should handle messages with circular references" , ( ) => {
348+ const consoleSpy = vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } )
349+
350+ // Create a message with circular reference
351+ const circularMessage : any = { type : "ask" , text : "Test" }
352+ circularMessage . self = circularMessage
353+
354+ const messages = [ circularMessage ] as ClineMessage [ ]
355+
356+ // Access private method through any type assertion for testing
357+ const history = ( MessageEnhancer as any ) . extractTaskHistory ( messages )
358+
359+ // Should handle gracefully
360+ expect ( history ) . toBe ( "User: Test" )
361+
362+ consoleSpy . mockRestore ( )
363+ } )
302364 } )
303365} )
0 commit comments