Skip to content

Commit 4250b65

Browse files
committed
add tests for diff context generation
1 parent 1a9b305 commit 4250b65

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

packages/core/src/codewhisperer/models/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ export const testGenExcludePatterns = [
933933
]
934934

935935
export const predictionTrackerDefaultConfig = {
936-
maxStorageSizeKb: 10000,
936+
maxStorageSizeKb: 5000,
937937
debounceIntervalMs: 2000,
938938
maxAgeMs: 30000,
939939
maxSupplementalContext: 15,

packages/core/src/codewhisperer/nextEditPrediction/diffContextGenerator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export function generateDiffContexts(
115115
* @param maxContexts - Maximum number of supplemental contexts allowed
116116
* @returns Trimmed array of SupplementalContext objects
117117
*/
118-
function trimSupplementalContexts(
118+
export function trimSupplementalContexts(
119119
supplementalContexts: codewhispererClient.SupplementalContext[],
120120
maxContexts: number
121121
): codewhispererClient.SupplementalContext[] {

packages/core/src/test/codewhisperer/nextEditPrediction/PredictionTracker.test.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { FakeExtensionContext } from '../../fakeExtensionContext'
1717
import { createMockDocument } from '../testUtil'
1818
import * as diffGenerator from '../../../codewhisperer/nextEditPrediction/diffContextGenerator'
1919
import globals from '../../../shared/extensionGlobals'
20+
import { charactersLimit, supplementalContextMaxTotalLength } from '../../../codewhisperer/models/constants'
2021

2122
describe('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

Comments
 (0)