Skip to content

Commit 2a2e128

Browse files
committed
add tests for computeDiff
1 parent 9fb4603 commit 2a2e128

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

packages/core/src/test/amazonq/common/diff.test.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ import {
1919
openDeletedDiff,
2020
openDiff,
2121
openFile,
22+
computeDiff,
2223
} from '../../../amazonq'
2324
import { FileSystem } from '../../../shared/fs/fs'
25+
import { TextDocument } from 'vscode'
2426

2527
describe('diff', () => {
2628
const filePath = path.join('/', 'foo', 'fi')
@@ -136,4 +138,83 @@ describe('diff', () => {
136138
assert.ok(executeCommandSpy.notCalled)
137139
})
138140
})
141+
142+
describe('computeDiff', () => {
143+
const mockLeftDocument = {
144+
getText: () => 'line1\nline2',
145+
uri: vscode.Uri.file('test.txt'),
146+
fileName: 'test.txt',
147+
}
148+
149+
const mockRightDocument = {
150+
getText: () => 'line1\nline3\nline4\nline5',
151+
uri: vscode.Uri.file('test.txt'),
152+
fileName: 'test.txt',
153+
}
154+
155+
it('returns 0 added or removed chars and lines for the same file', async () => {
156+
sandbox.stub(FileSystem.prototype, 'exists').resolves(true)
157+
sandbox.stub(vscode.workspace, 'openTextDocument').resolves(mockLeftDocument as unknown as TextDocument)
158+
159+
const { changes, charsAdded, linesAdded, charsRemoved, linesRemoved } = await computeDiff(
160+
filePath,
161+
filePath,
162+
tabId
163+
)
164+
165+
const expectedChanges = [
166+
{
167+
count: 2,
168+
value: 'line1\nline2',
169+
},
170+
]
171+
172+
assert.deepEqual(changes, expectedChanges)
173+
assert.equal(charsAdded, 0)
174+
assert.equal(linesAdded, 0)
175+
assert.equal(charsRemoved, 0)
176+
assert.equal(linesRemoved, 0)
177+
})
178+
179+
it('returns expected added or removed chars and lines for different files', async () => {
180+
sandbox.stub(FileSystem.prototype, 'exists').resolves(true)
181+
sandbox
182+
.stub(vscode.workspace, 'openTextDocument')
183+
.onFirstCall()
184+
.resolves(mockLeftDocument as unknown as TextDocument)
185+
.onSecondCall()
186+
.resolves(mockRightDocument as unknown as TextDocument)
187+
188+
const { changes, charsAdded, linesAdded, charsRemoved, linesRemoved } = await computeDiff(
189+
filePath,
190+
rightPath,
191+
tabId
192+
)
193+
194+
const expectedChanges = [
195+
{
196+
count: 1,
197+
value: 'line1\n',
198+
},
199+
{
200+
count: 1,
201+
added: undefined,
202+
removed: true,
203+
value: 'line2',
204+
},
205+
{
206+
count: 3,
207+
added: true,
208+
removed: undefined,
209+
value: 'line3\nline4\nline5',
210+
},
211+
]
212+
213+
assert.deepEqual(changes, expectedChanges)
214+
assert.equal(charsAdded, 15)
215+
assert.equal(linesAdded, 3)
216+
assert.equal(charsRemoved, 5)
217+
assert.equal(linesRemoved, 1)
218+
})
219+
})
139220
})

0 commit comments

Comments
 (0)