@@ -306,6 +306,108 @@ describe("truncateOutput", () => {
306306 const expectedLines = [ "line1" , "" , "[...10 lines omitted...]" , "" , "line12" , "line13" , "line14" , "line15" ]
307307 expect ( resultLines ) . toEqual ( expectedLines )
308308 } )
309+
310+ describe ( "character limit functionality" , ( ) => {
311+ it ( "returns original content when no character limit provided" , ( ) => {
312+ const content = "a" . repeat ( 1000 )
313+ expect ( truncateOutput ( content , undefined , undefined ) ) . toBe ( content )
314+ } )
315+
316+ it ( "returns original content when characters are under limit" , ( ) => {
317+ const content = "a" . repeat ( 100 )
318+ expect ( truncateOutput ( content , undefined , 200 ) ) . toBe ( content )
319+ } )
320+
321+ it ( "truncates content by character limit with 20/80 split" , ( ) => {
322+ // Create content with 1000 characters
323+ const content = "a" . repeat ( 1000 )
324+
325+ // Set character limit to 100
326+ const result = truncateOutput ( content , undefined , 100 )
327+
328+ // Should keep:
329+ // - First 20 characters (20% of 100)
330+ // - Last 80 characters (80% of 100)
331+ // - Omission indicator in between
332+ const expectedStart = "a" . repeat ( 20 )
333+ const expectedEnd = "a" . repeat ( 80 )
334+ const expected = expectedStart + "\n[...900 characters omitted...]\n" + expectedEnd
335+
336+ expect ( result ) . toBe ( expected )
337+ } )
338+
339+ it ( "prioritizes character limit over line limit" , ( ) => {
340+ // Create content with few lines but many characters per line
341+ const longLine = "a" . repeat ( 500 )
342+ const content = `${ longLine } \n${ longLine } \n${ longLine } `
343+
344+ // Set both limits - character limit should take precedence
345+ const result = truncateOutput ( content , 10 , 100 )
346+
347+ // Should truncate by character limit, not line limit
348+ const expectedStart = "a" . repeat ( 20 )
349+ const expectedEnd = "a" . repeat ( 80 )
350+ // Total content: 1502 chars, limit: 100, so 1402 chars omitted
351+ const expected = expectedStart + "\n[...1402 characters omitted...]\n" + expectedEnd
352+
353+ expect ( result ) . toBe ( expected )
354+ } )
355+
356+ it ( "falls back to line limit when character limit is satisfied" , ( ) => {
357+ // Create content with many short lines
358+ const lines = Array . from ( { length : 25 } , ( _ , i ) => `line${ i + 1 } ` )
359+ const content = lines . join ( "\n" )
360+
361+ // Character limit is high enough, so line limit should apply
362+ const result = truncateOutput ( content , 10 , 10000 )
363+
364+ // Should truncate by line limit
365+ const expectedLines = [
366+ "line1" ,
367+ "line2" ,
368+ "" ,
369+ "[...15 lines omitted...]" ,
370+ "" ,
371+ "line18" ,
372+ "line19" ,
373+ "line20" ,
374+ "line21" ,
375+ "line22" ,
376+ "line23" ,
377+ "line24" ,
378+ "line25" ,
379+ ]
380+ expect ( result ) . toBe ( expectedLines . join ( "\n" ) )
381+ } )
382+
383+ it ( "handles edge case where character limit equals content length" , ( ) => {
384+ const content = "exactly100chars" . repeat ( 6 ) + "1234" // exactly 100 chars
385+ const result = truncateOutput ( content , undefined , 100 )
386+ expect ( result ) . toBe ( content )
387+ } )
388+
389+ it ( "handles very small character limits" , ( ) => {
390+ const content = "a" . repeat ( 1000 )
391+ const result = truncateOutput ( content , undefined , 10 )
392+
393+ // 20% of 10 = 2, 80% of 10 = 8
394+ const expected = "aa\n[...990 characters omitted...]\n" + "a" . repeat ( 8 )
395+ expect ( result ) . toBe ( expected )
396+ } )
397+
398+ it ( "handles character limit with mixed content" , ( ) => {
399+ const content = "Hello world! This is a test with mixed content including numbers 123 and symbols @#$%"
400+ const result = truncateOutput ( content , undefined , 50 )
401+
402+ // 20% of 50 = 10, 80% of 50 = 40
403+ const expectedStart = content . slice ( 0 , 10 ) // "Hello worl"
404+ const expectedEnd = content . slice ( - 40 ) // last 40 chars
405+ const omittedChars = content . length - 50
406+ const expected = expectedStart + `\n[...${ omittedChars } characters omitted...]\n` + expectedEnd
407+
408+ expect ( result ) . toBe ( expected )
409+ } )
410+ } )
309411} )
310412
311413describe ( "applyRunLengthEncoding" , ( ) => {
0 commit comments