@@ -407,6 +407,91 @@ describe("truncateOutput", () => {
407407
408408 expect ( result ) . toBe ( expected )
409409 } )
410+
411+ describe ( "edge cases with very small character limits" , ( ) => {
412+ it ( "handles character limit of 1" , ( ) => {
413+ const content = "abcdefghijklmnopqrstuvwxyz"
414+ const result = truncateOutput ( content , undefined , 1 )
415+
416+ // 20% of 1 = 0.2 (floor = 0), so beforeLimit = 0
417+ // afterLimit = 1 - 0 = 1
418+ // Should keep 0 chars from start and 1 char from end
419+ const expected = "\n[...25 characters omitted...]\nz"
420+ expect ( result ) . toBe ( expected )
421+ } )
422+
423+ it ( "handles character limit of 2" , ( ) => {
424+ const content = "abcdefghijklmnopqrstuvwxyz"
425+ const result = truncateOutput ( content , undefined , 2 )
426+
427+ // 20% of 2 = 0.4 (floor = 0), so beforeLimit = 0
428+ // afterLimit = 2 - 0 = 2
429+ // Should keep 0 chars from start and 2 chars from end
430+ const expected = "\n[...24 characters omitted...]\nyz"
431+ expect ( result ) . toBe ( expected )
432+ } )
433+
434+ it ( "handles character limit of 5" , ( ) => {
435+ const content = "abcdefghijklmnopqrstuvwxyz"
436+ const result = truncateOutput ( content , undefined , 5 )
437+
438+ // 20% of 5 = 1, so beforeLimit = 1
439+ // afterLimit = 5 - 1 = 4
440+ // Should keep 1 char from start and 4 chars from end
441+ const expected = "a\n[...21 characters omitted...]\nwxyz"
442+ expect ( result ) . toBe ( expected )
443+ } )
444+
445+ it ( "handles character limit with multi-byte characters" , ( ) => {
446+ const content = "🚀🎉🔥💻🌟🎨🎯🎪🎭🎬" // 10 emojis, each is multi-byte
447+ const result = truncateOutput ( content , undefined , 10 )
448+
449+ // Character limit works on string length, not byte count
450+ // 20% of 10 = 2, 80% of 10 = 8
451+ const expected = "🚀🎉\n[...10 characters omitted...]\n🎨🎯🎪🎭🎬"
452+ expect ( result ) . toBe ( expected )
453+ } )
454+
455+ it ( "handles character limit with newlines in content" , ( ) => {
456+ const content = "line1\nline2\nline3\nline4\nline5"
457+ const result = truncateOutput ( content , undefined , 15 )
458+
459+ // Total length is 29 chars (including newlines)
460+ // 20% of 15 = 3, 80% of 15 = 12
461+ const expected = "lin\n[...14 characters omitted...]\ne3\nline4\nline5"
462+ expect ( result ) . toBe ( expected )
463+ } )
464+
465+ it ( "handles character limit exactly matching content with omission message" , ( ) => {
466+ // Edge case: when the omission message would make output longer than original
467+ const content = "short"
468+ const result = truncateOutput ( content , undefined , 10 )
469+
470+ // Content is 5 chars, limit is 10, so no truncation needed
471+ expect ( result ) . toBe ( content )
472+ } )
473+
474+ it ( "handles character limit smaller than omission message" , ( ) => {
475+ const content = "a" . repeat ( 100 )
476+ const result = truncateOutput ( content , undefined , 3 )
477+
478+ // 20% of 3 = 0.6 (floor = 0), so beforeLimit = 0
479+ // afterLimit = 3 - 0 = 3
480+ const expected = "\n[...97 characters omitted...]\naaa"
481+ expect ( result ) . toBe ( expected )
482+ } )
483+
484+ it ( "prioritizes character limit even with very high line limit" , ( ) => {
485+ const content = "a" . repeat ( 1000 )
486+ const result = truncateOutput ( content , 999999 , 50 )
487+
488+ // Character limit should still apply despite high line limit
489+ const expectedStart = "a" . repeat ( 10 ) // 20% of 50
490+ const expectedEnd = "a" . repeat ( 40 ) // 80% of 50
491+ const expected = expectedStart + "\n[...950 characters omitted...]\n" + expectedEnd
492+ expect ( result ) . toBe ( expected )
493+ } )
494+ } )
410495 } )
411496} )
412497
0 commit comments