Skip to content

Commit e4a1717

Browse files
authored
test(amazonq): add tests for inline utils files (#2385)
1 parent 35f0795 commit e4a1717

File tree

7 files changed

+441
-223
lines changed

7 files changed

+441
-223
lines changed

server/aws-lsp-codewhisperer/src/language-server/inline-completion/handler/editCompletionHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { CodeWhispererSession, SessionManager } from '../session/sessionManager'
2222
import { CursorTracker } from '../tracker/cursorTracker'
2323
import { CodewhispererLanguage, getSupportedLanguageId } from '../../../shared/languageDetection'
2424
import { WorkspaceFolderManager } from '../../workspaceContext/workspaceFolderManager'
25-
import { shouldTriggerEdits } from '../trigger'
25+
import { shouldTriggerEdits } from '../utils/triggerUtils'
2626
import {
2727
emitEmptyUserTriggerDecisionTelemetry,
2828
emitServiceInvocationFailure,
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import * as assert from 'assert'
2+
import { getAddedAndDeletedLines, getCharacterDifferences, generateDiffContexts } from './diffUtils'
3+
4+
describe('diffUtils', () => {
5+
describe('getAddedAndDeletedLines', () => {
6+
const SAMPLE_UNIFIED_DIFF = `--- a/file.txt
7+
+++ b/file.txt
8+
@@ -1,3 +1,3 @@
9+
line1
10+
-old line
11+
+new line
12+
line3`
13+
it('should extract added and deleted lines from unified diff', () => {
14+
const result = getAddedAndDeletedLines(SAMPLE_UNIFIED_DIFF)
15+
16+
assert.deepEqual(result.addedLines, ['new line'])
17+
assert.deepEqual(result.deletedLines, ['old line'])
18+
})
19+
20+
it('should handle empty diff', () => {
21+
const result = getAddedAndDeletedLines('')
22+
assert.deepEqual(result.addedLines, [])
23+
assert.deepEqual(result.deletedLines, [])
24+
})
25+
})
26+
27+
describe('getCharacterDifferences', () => {
28+
const ADDED_LINES = ['hello world']
29+
const DELETED_LINES = ['hello there']
30+
it('should calculate character differences using LCS', () => {
31+
const result = getCharacterDifferences(ADDED_LINES, DELETED_LINES)
32+
33+
assert.equal(result.charactersAdded, 4)
34+
assert.equal(result.charactersRemoved, 4)
35+
})
36+
37+
it('should handle empty added lines', () => {
38+
const result = getCharacterDifferences([], DELETED_LINES)
39+
40+
assert.equal(result.charactersAdded, 0)
41+
assert.equal(result.charactersRemoved, 11) // 'hello there' = 11 chars
42+
})
43+
44+
it('should handle empty deleted lines', () => {
45+
const result = getCharacterDifferences(ADDED_LINES, [])
46+
47+
assert.equal(result.charactersAdded, 11) // 'hello world' = 11 chars
48+
assert.equal(result.charactersRemoved, 0)
49+
})
50+
})
51+
52+
describe('generateDiffContexts', () => {
53+
const TEST_FILE_PATH = '/test/file.ts'
54+
const CURRENT_CONTENT = 'current content'
55+
const OLD_CONTENT = 'old content'
56+
const MAX_CONTEXTS = 5
57+
const SNAPSHOT_CONTENTS = [
58+
{
59+
filePath: TEST_FILE_PATH,
60+
content: OLD_CONTENT,
61+
timestamp: Date.now() - 1000,
62+
},
63+
]
64+
it('should generate diff contexts from snapshots', () => {
65+
const result = generateDiffContexts(TEST_FILE_PATH, CURRENT_CONTENT, SNAPSHOT_CONTENTS, MAX_CONTEXTS)
66+
67+
assert.equal(result.isUtg, false)
68+
assert.equal(result.isProcessTimeout, false)
69+
assert.equal(result.strategy, 'recentEdits')
70+
assert.equal(typeof result.latency, 'number')
71+
assert.equal(typeof result.contentsLength, 'number')
72+
})
73+
74+
it('should return empty context for no snapshots', () => {
75+
const result = generateDiffContexts(TEST_FILE_PATH, 'content', [], MAX_CONTEXTS)
76+
77+
assert.equal(result.isUtg, false)
78+
assert.equal(result.isProcessTimeout, false)
79+
assert.equal(result.supplementalContextItems.length, 0)
80+
assert.equal(result.contentsLength, 0)
81+
assert.equal(result.latency, 0)
82+
assert.equal(result.strategy, 'recentEdits')
83+
})
84+
})
85+
})

server/aws-lsp-codewhisperer/src/language-server/inline-completion/utils/diffUtils.ts

Lines changed: 0 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -131,88 +131,6 @@ export function generateDiffContexts(
131131
}
132132
}
133133

134-
/** src: https://github.com/aws/aws-toolkit-vscode/blob/3921457b0a2094b831beea0d66cc2cbd2a833890/packages/amazonq/src/app/inline/EditRendering/diffUtils.ts#L18
135-
* Apply a unified diff to original code to generate modified code
136-
* @param originalCode The original code as a string
137-
* @param unifiedDiff The unified diff content
138-
* @returns The modified code after applying the diff
139-
*/
140-
export function applyUnifiedDiff(docText: string, unifiedDiff: string): string {
141-
try {
142-
// First try the standard diff package
143-
try {
144-
const result = diff.applyPatch(docText, unifiedDiff)
145-
if (result !== false) {
146-
return result
147-
}
148-
} catch (error) {}
149-
150-
// Parse the unified diff to extract the changes
151-
const diffLines = unifiedDiff.split('\n')
152-
let result = docText
153-
154-
// Find all hunks in the diff
155-
const hunkStarts = diffLines
156-
.map((line, index) => (line.startsWith('@@ ') ? index : -1))
157-
.filter(index => index !== -1)
158-
159-
// Process each hunk
160-
for (const hunkStart of hunkStarts) {
161-
// Parse the hunk header
162-
const hunkHeader = diffLines[hunkStart]
163-
const match = hunkHeader.match(/@@ -(\d+),(\d+) \+(\d+),(\d+) @@/)
164-
165-
if (!match) {
166-
continue
167-
}
168-
169-
const oldStart = parseInt(match[1])
170-
const oldLines = parseInt(match[2])
171-
172-
// Extract the content lines for this hunk
173-
let i = hunkStart + 1
174-
const contentLines = []
175-
while (i < diffLines.length && !diffLines[i].startsWith('@@')) {
176-
contentLines.push(diffLines[i])
177-
i++
178-
}
179-
180-
// Build the old and new text
181-
let oldText = ''
182-
let newText = ''
183-
184-
for (const line of contentLines) {
185-
if (line.startsWith('-')) {
186-
oldText += line.substring(1) + '\n'
187-
} else if (line.startsWith('+')) {
188-
newText += line.substring(1) + '\n'
189-
} else if (line.startsWith(' ')) {
190-
oldText += line.substring(1) + '\n'
191-
newText += line.substring(1) + '\n'
192-
}
193-
}
194-
195-
// Remove trailing newline if it was added
196-
oldText = oldText.replace(/\n$/, '')
197-
newText = newText.replace(/\n$/, '')
198-
199-
// Find the text to replace in the document
200-
const docLines = docText.split('\n')
201-
const startLine = oldStart - 1 // Convert to 0-based
202-
const endLine = startLine + oldLines
203-
204-
// Extract the text that should be replaced
205-
const textToReplace = docLines.slice(startLine, endLine).join('\n')
206-
207-
// Replace the text
208-
result = result.replace(textToReplace, newText)
209-
}
210-
return result
211-
} catch (error) {
212-
return docText // Return original text if all methods fail
213-
}
214-
}
215-
216134
export function getAddedAndDeletedLines(unifiedDiff: string): { addedLines: string[]; deletedLines: string[] } {
217135
const lines = unifiedDiff.split('\n')
218136
const addedLines = lines.filter(line => line.startsWith('+') && !line.startsWith('+++')).map(line => line.slice(1))
@@ -225,48 +143,6 @@ export function getAddedAndDeletedLines(unifiedDiff: string): { addedLines: stri
225143
}
226144
}
227145

228-
// src https://github.com/aws/aws-toolkit-vscode/blob/3921457b0a2094b831beea0d66cc2cbd2a833890/packages/amazonq/src/app/inline/EditRendering/diffUtils.ts#L147
229-
export function getAddedAndDeletedChars(unifiedDiff: string): {
230-
addedCharacters: string
231-
deletedCharacters: string
232-
} {
233-
let addedCharacters = ''
234-
let deletedCharacters = ''
235-
const lines = unifiedDiff.split('\n')
236-
for (let i = 0; i < lines.length; i++) {
237-
const line = lines[i]
238-
if (line.startsWith('+') && !line.startsWith('+++')) {
239-
addedCharacters += line.slice(1)
240-
} else if (line.startsWith('-') && !line.startsWith('---')) {
241-
const removedLine = line.slice(1)
242-
243-
// Check if this is a modified line rather than a pure deletion
244-
const nextLine = lines[i + 1]
245-
if (nextLine && nextLine.startsWith('+') && !nextLine.startsWith('+++')) {
246-
// This is a modified line, not a pure deletion
247-
// We've already counted the deletion, so we'll just increment i to skip the next line
248-
// since we'll process the addition on the next iteration
249-
const addedLine = nextLine.slice(1)
250-
const changes = diff.diffChars(removedLine, addedLine)
251-
for (const part of changes) {
252-
if (part.removed) {
253-
deletedCharacters += part.value
254-
} else if (part.added) {
255-
addedCharacters += part.value
256-
}
257-
}
258-
i += 1
259-
} else {
260-
deletedCharacters += removedLine
261-
}
262-
}
263-
}
264-
return {
265-
addedCharacters,
266-
deletedCharacters,
267-
}
268-
}
269-
270146
/**
271147
* Calculate character differences between added and deleted text blocks using LCS
272148
*/

0 commit comments

Comments
 (0)