@@ -367,5 +367,126 @@ const isEmptyTextContent = (block: AssistantMessageContent) =>
367367 expect ( tool . params . path ) . toBe ( "demo.txt" )
368368 } )
369369 } )
370+
371+ describe ( "code block edge cases" , ( ) => {
372+ it ( "handles nested code blocks with tool tags" , ( ) => {
373+ const message = [
374+ "Here's a nested code block:" ,
375+ "> ```" ,
376+ "> <read_file><path>test.txt</path></read_file>" ,
377+ "> ```" ,
378+ ] . join ( "\n" )
379+
380+ const result = parser ( message )
381+ expect ( result ) . toHaveLength ( 1 )
382+ expect ( result [ 0 ] . type ) . toBe ( "text" )
383+ } )
384+
385+ it ( "handles escaped backticks" , ( ) => {
386+ const message = "This has an escaped \\`<read_file>\\` tag"
387+
388+ const result = parser ( message )
389+ expect ( result ) . toHaveLength ( 1 )
390+ expect ( result [ 0 ] . type ) . toBe ( "text" )
391+ } )
392+
393+ it ( "handles incomplete code blocks" , ( ) => {
394+ const message = [
395+ "```javascript" ,
396+ "<read_file><path>test.txt</path></read_file>" ,
397+ // Missing closing backticks
398+ ] . join ( "\n" )
399+
400+ const result = parser ( message )
401+ expect ( result ) . toHaveLength ( 1 )
402+ expect ( result [ 0 ] . type ) . toBe ( "text" )
403+ } )
404+ } )
405+
406+ describe ( "tool tag edge cases" , ( ) => {
407+ it ( "handles HTML comments containing tool tags" , ( ) => {
408+ const message = "<!-- <read_file><path>test.txt</path></read_file> -->"
409+
410+ // Note: Currently both parsers treat tool tags in HTML comments as actual tool invocations
411+ // This test documents the current behavior rather than the ideal behavior
412+ const result = parser ( message )
413+ expect ( result ) . toHaveLength ( 3 )
414+ expect ( result [ 0 ] . type ) . toBe ( "text" )
415+ expect ( ( result [ 0 ] as TextContent ) . content ) . toBe ( "<!--" )
416+ expect ( result [ 1 ] . type ) . toBe ( "tool_use" )
417+ expect ( ( result [ 1 ] as ToolUse ) . name ) . toBe ( "read_file" )
418+ expect ( result [ 2 ] . type ) . toBe ( "text" )
419+ expect ( ( result [ 2 ] as TextContent ) . content ) . toBe ( "-->" )
420+ } )
421+
422+ it ( "handles tool tags with HTML-like attributes" , ( ) => {
423+ const message = '<read_file class="example"><path>test.txt</path></read_file>'
424+
425+ // This should be treated as plain text since it's not a valid tool format
426+ const result = parser ( message )
427+ expect ( result ) . toHaveLength ( 1 )
428+ expect ( result [ 0 ] . type ) . toBe ( "text" )
429+ } )
430+
431+ it ( "handles malformed tool tags with extra spaces" , ( ) => {
432+ const message = "< read_file >< path >test.txt< /path >< /read_file >"
433+
434+ // This should be treated as plain text since it's not a valid tool format
435+ const result = parser ( message )
436+ expect ( result ) . toHaveLength ( 1 )
437+ expect ( result [ 0 ] . type ) . toBe ( "text" )
438+ } )
439+ } )
440+
441+ describe ( "mixed formatting edge cases" , ( ) => {
442+ it ( "handles tool tags with markdown formatting" , ( ) => {
443+ const message = "**<read_file>**<path>test.txt</path></read_file>"
444+
445+ // The opening tag is malformed due to the bold formatting
446+ const result = parser ( message )
447+ expect ( result ) . toHaveLength ( 1 )
448+ expect ( result [ 0 ] . type ) . toBe ( "text" )
449+ } )
450+
451+ it ( "handles tool tags in markdown links" , ( ) => {
452+ const message = "[Example](<read_file><path>test.txt</path></read_file>)"
453+
454+ // Note: Currently both parsers treat tool tags in markdown links as actual tool invocations
455+ // This test documents the current behavior rather than the ideal behavior
456+ const result = parser ( message )
457+ expect ( result ) . toHaveLength ( 3 )
458+ expect ( result [ 0 ] . type ) . toBe ( "text" )
459+ expect ( ( result [ 0 ] as TextContent ) . content ) . toBe ( "[Example](" )
460+ expect ( result [ 1 ] . type ) . toBe ( "tool_use" )
461+ expect ( ( result [ 1 ] as ToolUse ) . name ) . toBe ( "read_file" )
462+ expect ( result [ 2 ] . type ) . toBe ( "text" )
463+ expect ( ( result [ 2 ] as TextContent ) . content ) . toBe ( ")" )
464+ } )
465+ } )
466+
467+ describe ( "performance edge cases" , ( ) => {
468+ it ( "handles very long text with no tool uses" , ( ) => {
469+ const message = "A" . repeat ( 10000 )
470+
471+ const result = parser ( message )
472+ expect ( result ) . toHaveLength ( 1 )
473+ expect ( result [ 0 ] . type ) . toBe ( "text" )
474+ expect ( ( result [ 0 ] as TextContent ) . content . length ) . toBe ( 10000 )
475+ } )
476+
477+ it ( "handles deeply nested tool parameters" , ( ) => {
478+ // Create a message with a tool that has many nested parameters
479+ let message = "<execute_command><command>"
480+ for ( let i = 0 ; i < 10 ; i ++ ) {
481+ message += `echo "Level ${ i } " && `
482+ }
483+ message += 'echo "Done"</command></execute_command>'
484+
485+ const result = parser ( message ) . filter ( ( block ) => ! isEmptyTextContent ( block ) )
486+ expect ( result ) . toHaveLength ( 1 )
487+ expect ( result [ 0 ] . type ) . toBe ( "tool_use" )
488+ expect ( ( result [ 0 ] as ToolUse ) . name ) . toBe ( "execute_command" )
489+ } )
490+ } )
370491 } )
371492} )
0 commit comments