11import * as assert from 'assert'
2- import { FsRead } from './fsRead'
2+ import { FileReadResult , FsRead } from './fsRead'
33import * as path from 'path'
44import * as fs from 'fs/promises'
55import { TestFeatures } from '@aws/language-server-runtimes/testing'
@@ -40,7 +40,7 @@ describe('FsRead Tool', () => {
4040 it ( 'invalidates empty path' , async ( ) => {
4141 const fsRead = new FsRead ( features )
4242 await assert . rejects (
43- fsRead . validate ( { path : '' } ) ,
43+ fsRead . validate ( { paths : [ '' ] } ) ,
4444 / P a t h c a n n o t b e e m p t y / i,
4545 'Expected an error about empty path'
4646 )
@@ -51,7 +51,7 @@ describe('FsRead Tool', () => {
5151 const fsRead = new FsRead ( features )
5252
5353 await assert . rejects (
54- fsRead . validate ( { path : filePath } ) ,
54+ fsRead . validate ( { paths : [ filePath ] } ) ,
5555 / d o e s n o t e x i s t o r c a n n o t b e a c c e s s e d / i,
5656 'Expected an error indicating the path does not exist'
5757 )
@@ -61,50 +61,35 @@ describe('FsRead Tool', () => {
6161 const fileContent = 'A' . repeat ( FsRead . maxResponseSize + 10 )
6262 const filePath = await tempFolder . write ( 'largeFile.txt' , fileContent )
6363 const fsRead = new FsRead ( features )
64- await fsRead . validate ( { path : filePath } )
65- const result = await fsRead . invoke ( { path : filePath } )
64+ await fsRead . validate ( { paths : [ filePath ] } )
65+ const result = await fsRead . invoke ( { paths : [ filePath ] } )
6666
67- verifyResult ( result , { truncated : true } , ( { content } ) => content . length === FsRead . maxResponseSize )
67+ verifyResult ( result , [
68+ { path : filePath , content : 'A' . repeat ( FsRead . maxResponseSize - 3 ) + '...' , truncated : true } ,
69+ ] )
6870 } )
6971
7072 it ( 'reads entire file' , async ( ) => {
7173 const fileContent = 'Line 1\nLine 2\nLine 3'
7274 const filePath = await tempFolder . write ( 'fullFile.txt' , fileContent )
7375
7476 const fsRead = new FsRead ( features )
75- const result = await fsRead . invoke ( { path : filePath } )
76- verifyResult ( result , { content : fileContent , truncated : false } )
77+ const result = await fsRead . invoke ( { paths : [ filePath ] } )
78+ verifyResult ( result , [ { path : filePath , content : fileContent , truncated : false } ] )
7779 } )
7880
79- it ( 'reads partial lines of a file' , async ( ) => {
80- const fileContent = 'A\nB\nC\nD\nE\nF'
81- const filePath = await tempFolder . write ( 'partialFile.txt' , fileContent )
82-
83- const fsRead = new FsRead ( features )
84- const result = await fsRead . invoke ( { path : filePath , readRange : [ 2 , 4 ] } )
85- verifyResult ( result , { content : 'B\nC\nD' , truncated : false } )
86- } )
87-
88- it ( 'invalid line range' , async ( ) => {
89- const filePath = await tempFolder . write ( 'rangeTest.txt' , '1\n2\n3' )
90- const fsRead = new FsRead ( features )
91-
92- await fsRead . invoke ( { path : filePath , readRange : [ 3 , 2 ] } )
93- const result = await fsRead . invoke ( { path : filePath , readRange : [ 3 , 2 ] } )
94- verifyResult ( result , { content : '' , truncated : false } )
95- } )
81+ it ( 'reads multiple files' , async ( ) => {
82+ const fileContent = 'Line 1\nLine 2\nLine 3'
83+ const fileContent1 = 'Line 1\n'
84+ const filePath = await tempFolder . write ( 'fullFile.txt' , fileContent )
85+ const filePath1 = await tempFolder . write ( 'fullFile1.txt' , fileContent1 )
9686
97- it ( 'updates the stream' , async ( ) => {
9887 const fsRead = new FsRead ( features )
99- const chunks = [ ]
100- const stream = new WritableStream ( {
101- write : c => {
102- chunks . push ( c )
103- } ,
104- } )
105- await fsRead . queueDescription ( { path : 'this/is/my/path' } , stream , true )
106- assert . ok ( chunks . length > 0 )
107- assert . ok ( ! stream . locked )
88+ const result = await fsRead . invoke ( { paths : [ filePath , filePath1 ] } )
89+ verifyResult ( result , [
90+ { path : filePath , content : fileContent , truncated : false } ,
91+ { path : filePath1 , content : fileContent1 , truncated : false } ,
92+ ] )
10893 } )
10994
11095 it ( 'should require acceptance if fsPath is outside the workspace' , async ( ) => {
@@ -115,7 +100,7 @@ describe('FsRead Tool', () => {
115100 getTextDocument : async s => undefined ,
116101 } ,
117102 } )
118- const result = await fsRead . requiresAcceptance ( { path : '/not/in/workspace/file.txt' } )
103+ const result = await fsRead . requiresAcceptance ( { paths : [ '/not/in/workspace/file.txt' ] } )
119104 assert . equal (
120105 result . requiresAcceptance ,
121106 true ,
@@ -140,7 +125,7 @@ describe('FsRead Tool', () => {
140125 getTextDocument : async s => ( { } ) as TextDocument ,
141126 } ,
142127 } )
143- const result = await fsRead . requiresAcceptance ( { path : '/workspace/folder/file.txt' } )
128+ const result = await fsRead . requiresAcceptance ( { paths : [ '/workspace/folder/file.txt' ] } )
144129 assert . equal (
145130 result . requiresAcceptance ,
146131 false ,
@@ -149,20 +134,20 @@ describe('FsRead Tool', () => {
149134 } )
150135} )
151136
152- function verifyResult (
153- result : any ,
154- expected : { content ?: string ; truncated : boolean } ,
155- customChecks ?: ( r : { content : string ; truncated : boolean } ) => boolean
156- ) {
137+ function verifyResult ( result : any , expected : FileReadResult [ ] ) {
157138 assert . strictEqual ( result . output . kind , 'json' , 'Output kind should be "json"' )
158- const resultContent = result . output . content as { content : string ; truncated : boolean }
159- if ( expected . content ) {
160- assert . strictEqual ( resultContent . content , expected . content , 'File content should match exactly' )
161- }
162- if ( expected . truncated !== undefined ) {
163- assert . strictEqual ( resultContent . truncated , expected . truncated , 'Truncated flag should match' )
164- }
165- if ( customChecks ) {
166- assert . ok ( customChecks ( resultContent ) , 'Custom checks failed in verifyResult' )
139+ const resultContent = result . output . content as FileReadResult [ ]
140+ // Compare array length
141+ assert . strictEqual ( resultContent . length , expected . length , 'Arrays should have the same length' )
142+
143+ // Compare each element in the arrays
144+ for ( let i = 0 ; i < resultContent . length ; i ++ ) {
145+ assert . strictEqual ( resultContent [ i ] . path , expected [ i ] . path , `Path at index ${ i } should match` )
146+ assert . strictEqual ( resultContent [ i ] . content , expected [ i ] . content , `Content at index ${ i } should match` )
147+ assert . strictEqual (
148+ resultContent [ i ] . truncated ,
149+ expected [ i ] . truncated ,
150+ `Truncated flag at index ${ i } should match`
151+ )
167152 }
168153}
0 commit comments