@@ -357,12 +357,67 @@ describe("read_text_file and read_file", () => {
357357 expect ( textOf ( result ) ) . toBe ( "one\ntwo" ) ;
358358 } ) ;
359359
360- it ( "returns the last N lines with tail, counting the trailing newline as a line" , async ( ) => {
360+ it ( "returns the last N lines with tail, not counting the trailing newline as a line (#5187) " , async ( ) => {
361361 const result = await call ( client , "read_text_file" , {
362362 path : file ,
363363 tail : 2 ,
364364 } ) ;
365- expect ( textOf ( result ) ) . toBe ( "four\n" ) ;
365+ expect ( textOf ( result ) ) . toBe ( "three\nfour" ) ;
366+ } ) ;
367+
368+ describe ( "tail at the end of the file (#5187)" , ( ) => {
369+ async function tail ( content : string , n : number ) : Promise < string > {
370+ const target = path . join ( dir , "tail.txt" ) ;
371+ await fs . writeFile ( target , content ) ;
372+ return textOf (
373+ await call ( client , "read_text_file" , { path : target , tail : n } ) ,
374+ ) ;
375+ }
376+
377+ it ( "returns the last line of an LF file that ends with a newline" , async ( ) => {
378+ expect ( await tail ( "a\nb\nc\n" , 1 ) ) . toBe ( "c" ) ;
379+ } ) ;
380+
381+ it ( "returns the last lines of a CRLF file that ends with a newline" , async ( ) => {
382+ expect ( await tail ( "a\r\nb\r\nc\r\n" , 2 ) ) . toBe ( "b\nc" ) ;
383+ } ) ;
384+
385+ it ( "returns the last lines of a file with no final newline unchanged" , async ( ) => {
386+ expect ( await tail ( "a\nb\nc" , 2 ) ) . toBe ( "b\nc" ) ;
387+ } ) ;
388+
389+ it ( "counts a blank last line as a line" , async ( ) => {
390+ expect ( await tail ( "a\nb\n\n" , 2 ) ) . toBe ( "b\n" ) ;
391+ } ) ;
392+
393+ it ( "returns the whole file when tail exceeds its line count" , async ( ) => {
394+ expect ( await tail ( "a\nb\n" , 5 ) ) . toBe ( "a\nb" ) ;
395+ } ) ;
396+
397+ // tailFile reads 1 KB chunks from the end. Here the last chunk holds both
398+ // remaining newlines but starts partway through the first wanted line, so
399+ // the read loop must not count the final newline as a line break it needs.
400+ it ( "reads back past a chunk boundary for the first wanted line (LF)" , async ( ) => {
401+ const content =
402+ "head\n" + "A" . repeat ( 611 ) + "\n" + "B" . repeat ( 511 ) + "\n" ;
403+ expect ( await tail ( content , 2 ) ) . toBe (
404+ "A" . repeat ( 611 ) + "\n" + "B" . repeat ( 511 ) ,
405+ ) ;
406+ } ) ;
407+
408+ it ( "reads back past a chunk boundary for the first wanted line (CRLF)" , async ( ) => {
409+ const content =
410+ "head\r\n" + "A" . repeat ( 611 ) + "\r\n" + "B" . repeat ( 510 ) + "\r\n" ;
411+ expect ( await tail ( content , 2 ) ) . toBe (
412+ "A" . repeat ( 611 ) + "\n" + "B" . repeat ( 510 ) ,
413+ ) ;
414+ } ) ;
415+
416+ it ( "joins a CRLF pair split across a chunk boundary" , async ( ) => {
417+ // The last 1024 bytes start with the "\n" of the "\r\n" after "x".
418+ const content = "x\r\n" + "C" . repeat ( 1021 ) + "\r\n" ;
419+ expect ( await tail ( content , 2 ) ) . toBe ( "x\n" + "C" . repeat ( 1021 ) ) ;
420+ } ) ;
366421 } ) ;
367422
368423 it ( "treats head: 0 as absent and returns the whole file" , async ( ) => {
0 commit comments