@@ -17,6 +17,7 @@ import { FakeExtensionContext } from '../../fakeExtensionContext'
1717import { createMockDocument } from '../testUtil'
1818import * as diffGenerator from '../../../codewhisperer/nextEditPrediction/diffContextGenerator'
1919import globals from '../../../shared/extensionGlobals'
20+ import { charactersLimit , supplementalContextMaxTotalLength } from '../../../codewhisperer/models/constants'
2021
2122describe ( 'PredictionTracker' , function ( ) {
2223 let sandbox : sinon . SinonSandbox
@@ -273,4 +274,71 @@ describe('PredictionTracker', function () {
273274 // Mock the path from vscode uri
274275 return path . sep + path . join ( ...segments )
275276 }
277+
278+ describe ( 'trimSupplementalContexts' , function ( ) {
279+ it ( 'should filter out contexts that exceed individual character limit' , function ( ) {
280+ const smallContext = {
281+ filePath : 'file.js' ,
282+ content : 'small content' ,
283+ type : 'PreviousEditorState' ,
284+ }
285+
286+ // Create a context that exceeds the characters limit
287+ const largeContent = 'a' . repeat ( charactersLimit + 100 )
288+ const largeContext = {
289+ filePath : 'file.js' ,
290+ content : largeContent ,
291+ type : 'PreviousEditorState' ,
292+ }
293+
294+ const contexts = [ smallContext , largeContext ]
295+ const result = diffGenerator . trimSupplementalContexts ( contexts , 10 )
296+
297+ assert . strictEqual ( result . length , 1 )
298+ assert . deepStrictEqual ( result [ 0 ] , smallContext )
299+ } )
300+
301+ it ( 'should limit the number of contexts to maxContexts' , function ( ) {
302+ const contexts = [
303+ { filePath : 'file1.js' , content : 'content 1' , type : 'PreviousEditorState' } ,
304+ { filePath : 'file2.js' , content : 'content 2' , type : 'PreviousEditorState' } ,
305+ { filePath : 'file3.js' , content : 'content 3' , type : 'PreviousEditorState' } ,
306+ { filePath : 'file4.js' , content : 'content 4' , type : 'PreviousEditorState' } ,
307+ { filePath : 'file5.js' , content : 'content 5' , type : 'PreviousEditorState' } ,
308+ ]
309+
310+ const maxContexts = 3
311+ const result = diffGenerator . trimSupplementalContexts ( contexts , maxContexts )
312+
313+ assert . strictEqual ( result . length , maxContexts )
314+ } )
315+
316+ it ( 'should enforce total character length limit across all contexts' , function ( ) {
317+ // Create contexts where total size exceeds the limit
318+ const contentSize = Math . floor ( supplementalContextMaxTotalLength / 2.5 )
319+ const contexts = [
320+ {
321+ filePath : 'file1.js' ,
322+ content : 'a' . repeat ( contentSize ) ,
323+ type : 'PreviousEditorState' ,
324+ } ,
325+ {
326+ filePath : 'file2.js' ,
327+ content : 'b' . repeat ( contentSize ) ,
328+ type : 'PreviousEditorState' ,
329+ } ,
330+ {
331+ filePath : 'file3.js' ,
332+ content : 'c' . repeat ( contentSize ) ,
333+ type : 'PreviousEditorState' ,
334+ } ,
335+ ]
336+
337+ const result = diffGenerator . trimSupplementalContexts ( contexts , 10 )
338+
339+ // Only the first two contexts should be included since the third would exceed the total limit
340+ assert . strictEqual ( result . length , 2 )
341+ assert . deepStrictEqual ( result , contexts . slice ( 0 , 2 ) )
342+ } )
343+ } )
276344} )
0 commit comments