|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import * as vscode from 'vscode' |
| 7 | +import * as sinon from 'sinon' |
| 8 | +import assert from 'assert' |
| 9 | +// Remove static import - we'll use dynamic import instead |
| 10 | +// import { showEdits } from '../../../../../src/app/inline/EditRendering/imageRenderer' |
| 11 | +import { SvgGenerationService } from '../../../../../src/app/inline/EditRendering/svgGenerator' |
| 12 | +import { InlineCompletionItemWithReferences } from '@aws/language-server-runtimes/protocol' |
| 13 | + |
| 14 | +describe('showEdits', function () { |
| 15 | + let sandbox: sinon.SinonSandbox |
| 16 | + let editorStub: sinon.SinonStubbedInstance<vscode.TextEditor> |
| 17 | + let documentStub: sinon.SinonStubbedInstance<vscode.TextDocument> |
| 18 | + let svgGenerationServiceStub: sinon.SinonStubbedInstance<SvgGenerationService> |
| 19 | + let displaySvgDecorationStub: sinon.SinonStub |
| 20 | + let loggerStub: sinon.SinonStubbedInstance<any> |
| 21 | + let getLoggerStub: sinon.SinonStub |
| 22 | + let showEdits: any // Will be dynamically imported |
| 23 | + let languageClientStub: any |
| 24 | + let sessionStub: any |
| 25 | + let itemStub: InlineCompletionItemWithReferences |
| 26 | + |
| 27 | + beforeEach(function () { |
| 28 | + sandbox = sinon.createSandbox() |
| 29 | + |
| 30 | + // Create logger stub |
| 31 | + loggerStub = { |
| 32 | + error: sandbox.stub(), |
| 33 | + info: sandbox.stub(), |
| 34 | + debug: sandbox.stub(), |
| 35 | + warn: sandbox.stub(), |
| 36 | + } |
| 37 | + |
| 38 | + // Clear all relevant module caches |
| 39 | + const moduleId = require.resolve('../../../../../src/app/inline/EditRendering/imageRenderer') |
| 40 | + const sharedModuleId = require.resolve('aws-core-vscode/shared') |
| 41 | + delete require.cache[moduleId] |
| 42 | + delete require.cache[sharedModuleId] |
| 43 | + |
| 44 | + // Create getLogger stub and store reference for test verification |
| 45 | + getLoggerStub = sandbox.stub().returns(loggerStub) |
| 46 | + |
| 47 | + // Create a mock shared module with stubbed getLogger |
| 48 | + const mockSharedModule = { |
| 49 | + getLogger: getLoggerStub, |
| 50 | + } |
| 51 | + |
| 52 | + // Override the require cache with our mock |
| 53 | + require.cache[sharedModuleId] = { |
| 54 | + id: sharedModuleId, |
| 55 | + filename: sharedModuleId, |
| 56 | + loaded: true, |
| 57 | + parent: null, |
| 58 | + children: [], |
| 59 | + exports: mockSharedModule, |
| 60 | + paths: [], |
| 61 | + } as any |
| 62 | + |
| 63 | + // Now require the module - it should use our mocked getLogger |
| 64 | + const imageRendererModule = require('../../../../../src/app/inline/EditRendering/imageRenderer') |
| 65 | + showEdits = imageRendererModule.showEdits |
| 66 | + |
| 67 | + // Create document stub |
| 68 | + documentStub = { |
| 69 | + uri: { |
| 70 | + fsPath: '/path/to/test/file.ts', |
| 71 | + }, |
| 72 | + getText: sandbox.stub().returns('Original code content'), |
| 73 | + lineCount: 5, |
| 74 | + } as unknown as sinon.SinonStubbedInstance<vscode.TextDocument> |
| 75 | + |
| 76 | + // Create editor stub |
| 77 | + editorStub = { |
| 78 | + document: documentStub, |
| 79 | + setDecorations: sandbox.stub(), |
| 80 | + edit: sandbox.stub().resolves(true), |
| 81 | + } as unknown as sinon.SinonStubbedInstance<vscode.TextEditor> |
| 82 | + |
| 83 | + // Create SVG generation service stub |
| 84 | + svgGenerationServiceStub = { |
| 85 | + generateDiffSvg: sandbox.stub(), |
| 86 | + } as unknown as sinon.SinonStubbedInstance<SvgGenerationService> |
| 87 | + |
| 88 | + // Stub the SvgGenerationService constructor |
| 89 | + sandbox |
| 90 | + .stub(SvgGenerationService.prototype, 'generateDiffSvg') |
| 91 | + .callsFake(svgGenerationServiceStub.generateDiffSvg) |
| 92 | + |
| 93 | + // Create display SVG decoration stub |
| 94 | + displaySvgDecorationStub = sandbox.stub() |
| 95 | + sandbox.replace( |
| 96 | + require('../../../../../src/app/inline/EditRendering/displayImage'), |
| 97 | + 'displaySvgDecoration', |
| 98 | + displaySvgDecorationStub |
| 99 | + ) |
| 100 | + |
| 101 | + // Create language client stub |
| 102 | + languageClientStub = {} as any |
| 103 | + |
| 104 | + // Create session stub |
| 105 | + sessionStub = { |
| 106 | + sessionId: 'test-session-id', |
| 107 | + suggestions: [], |
| 108 | + isRequestInProgress: false, |
| 109 | + requestStartTime: Date.now(), |
| 110 | + startPosition: new vscode.Position(0, 0), |
| 111 | + } as any |
| 112 | + |
| 113 | + // Create item stub |
| 114 | + itemStub = { |
| 115 | + insertText: 'console.log("Hello World");', |
| 116 | + range: new vscode.Range(0, 0, 0, 0), |
| 117 | + itemId: 'test-item-id', |
| 118 | + } as any |
| 119 | + }) |
| 120 | + |
| 121 | + afterEach(function () { |
| 122 | + sandbox.restore() |
| 123 | + }) |
| 124 | + |
| 125 | + it('should return early when editor is undefined', async function () { |
| 126 | + await showEdits(itemStub, undefined, sessionStub, languageClientStub) |
| 127 | + |
| 128 | + // Verify that no SVG generation or display methods were called |
| 129 | + sinon.assert.notCalled(svgGenerationServiceStub.generateDiffSvg) |
| 130 | + sinon.assert.notCalled(displaySvgDecorationStub) |
| 131 | + sinon.assert.notCalled(loggerStub.error) |
| 132 | + }) |
| 133 | + |
| 134 | + it('should successfully generate and display SVG when all parameters are valid', async function () { |
| 135 | + // Setup successful SVG generation |
| 136 | + const mockSvgResult = { |
| 137 | + svgImage: vscode.Uri.file('/path/to/generated.svg'), |
| 138 | + startLine: 5, |
| 139 | + newCode: 'console.log("Hello World");', |
| 140 | + origionalCodeHighlightRange: [{ line: 5, start: 0, end: 10 }], |
| 141 | + addedCharacterCount: 25, |
| 142 | + deletedCharacterCount: 0, |
| 143 | + } |
| 144 | + svgGenerationServiceStub.generateDiffSvg.resolves(mockSvgResult) |
| 145 | + |
| 146 | + await showEdits(itemStub, editorStub as unknown as vscode.TextEditor, sessionStub, languageClientStub) |
| 147 | + |
| 148 | + // Verify SVG generation was called with correct parameters |
| 149 | + sinon.assert.calledOnce(svgGenerationServiceStub.generateDiffSvg) |
| 150 | + sinon.assert.calledWith( |
| 151 | + svgGenerationServiceStub.generateDiffSvg, |
| 152 | + '/path/to/test/file.ts', |
| 153 | + 'console.log("Hello World");' |
| 154 | + ) |
| 155 | + |
| 156 | + // Verify display decoration was called with correct parameters |
| 157 | + sinon.assert.calledOnce(displaySvgDecorationStub) |
| 158 | + sinon.assert.calledWith( |
| 159 | + displaySvgDecorationStub, |
| 160 | + editorStub, |
| 161 | + mockSvgResult.svgImage, |
| 162 | + mockSvgResult.startLine, |
| 163 | + mockSvgResult.newCode, |
| 164 | + mockSvgResult.origionalCodeHighlightRange, |
| 165 | + sessionStub, |
| 166 | + languageClientStub, |
| 167 | + itemStub, |
| 168 | + mockSvgResult.addedCharacterCount, |
| 169 | + mockSvgResult.deletedCharacterCount |
| 170 | + ) |
| 171 | + |
| 172 | + // Verify no errors were logged |
| 173 | + sinon.assert.notCalled(loggerStub.error) |
| 174 | + }) |
| 175 | + |
| 176 | + it('should log error when SVG generation returns empty result', async function () { |
| 177 | + // Setup SVG generation to return undefined svgImage |
| 178 | + const mockSvgResult = { |
| 179 | + svgImage: undefined as any, |
| 180 | + startLine: 5, |
| 181 | + newCode: 'console.log("Hello World");', |
| 182 | + origionalCodeHighlightRange: [{ line: 5, start: 0, end: 10 }], |
| 183 | + addedCharacterCount: 25, |
| 184 | + deletedCharacterCount: 0, |
| 185 | + } |
| 186 | + svgGenerationServiceStub.generateDiffSvg.resolves(mockSvgResult) |
| 187 | + |
| 188 | + await showEdits(itemStub, editorStub as unknown as vscode.TextEditor, sessionStub, languageClientStub) |
| 189 | + |
| 190 | + // Verify SVG generation was called |
| 191 | + sinon.assert.calledOnce(svgGenerationServiceStub.generateDiffSvg) |
| 192 | + |
| 193 | + // Verify display decoration was NOT called |
| 194 | + sinon.assert.notCalled(displaySvgDecorationStub) |
| 195 | + |
| 196 | + // Verify error was logged |
| 197 | + sinon.assert.calledOnce(loggerStub.error) |
| 198 | + sinon.assert.calledWith(loggerStub.error, 'SVG image generation returned an empty result.') |
| 199 | + }) |
| 200 | + |
| 201 | + it('should catch and log error when SVG generation throws exception', async function () { |
| 202 | + // Setup SVG generation to throw an error |
| 203 | + const testError = new Error('SVG generation failed') |
| 204 | + svgGenerationServiceStub.generateDiffSvg.rejects(testError) |
| 205 | + |
| 206 | + await showEdits(itemStub, editorStub as unknown as vscode.TextEditor, sessionStub, languageClientStub) |
| 207 | + |
| 208 | + // Verify SVG generation was called |
| 209 | + sinon.assert.calledOnce(svgGenerationServiceStub.generateDiffSvg) |
| 210 | + |
| 211 | + // Verify display decoration was NOT called |
| 212 | + sinon.assert.notCalled(displaySvgDecorationStub) |
| 213 | + |
| 214 | + // Verify error was logged with correct message |
| 215 | + sinon.assert.calledOnce(loggerStub.error) |
| 216 | + const errorCall = loggerStub.error.getCall(0) |
| 217 | + assert.strictEqual(errorCall.args[0], `Error generating SVG image: ${testError}`) |
| 218 | + }) |
| 219 | + |
| 220 | + it('should catch and log error when displaySvgDecoration throws exception', async function () { |
| 221 | + // Setup successful SVG generation |
| 222 | + const mockSvgResult = { |
| 223 | + svgImage: vscode.Uri.file('/path/to/generated.svg'), |
| 224 | + startLine: 5, |
| 225 | + newCode: 'console.log("Hello World");', |
| 226 | + origionalCodeHighlightRange: [{ line: 5, start: 0, end: 10 }], |
| 227 | + addedCharacterCount: 25, |
| 228 | + deletedCharacterCount: 0, |
| 229 | + } |
| 230 | + svgGenerationServiceStub.generateDiffSvg.resolves(mockSvgResult) |
| 231 | + |
| 232 | + // Setup displaySvgDecoration to throw an error |
| 233 | + const testError = new Error('Display decoration failed') |
| 234 | + displaySvgDecorationStub.rejects(testError) |
| 235 | + |
| 236 | + await showEdits(itemStub, editorStub as unknown as vscode.TextEditor, sessionStub, languageClientStub) |
| 237 | + |
| 238 | + // Verify SVG generation was called |
| 239 | + sinon.assert.calledOnce(svgGenerationServiceStub.generateDiffSvg) |
| 240 | + |
| 241 | + // Verify display decoration was called |
| 242 | + sinon.assert.calledOnce(displaySvgDecorationStub) |
| 243 | + |
| 244 | + // Verify error was logged with correct message |
| 245 | + sinon.assert.calledOnce(loggerStub.error) |
| 246 | + const errorCall = loggerStub.error.getCall(0) |
| 247 | + assert.strictEqual(errorCall.args[0], `Error generating SVG image: ${testError}`) |
| 248 | + }) |
| 249 | + |
| 250 | + it('should use correct logger name', async function () { |
| 251 | + await showEdits(itemStub, editorStub as unknown as vscode.TextEditor, sessionStub, languageClientStub) |
| 252 | + |
| 253 | + // Verify getLogger was called with correct name |
| 254 | + sinon.assert.calledWith(getLoggerStub, 'nextEditPrediction') |
| 255 | + }) |
| 256 | + |
| 257 | + it('should handle item with undefined insertText', async function () { |
| 258 | + // Create item with undefined insertText |
| 259 | + const itemWithUndefinedText = { |
| 260 | + ...itemStub, |
| 261 | + insertText: undefined, |
| 262 | + } as any |
| 263 | + |
| 264 | + // Setup successful SVG generation |
| 265 | + const mockSvgResult = { |
| 266 | + svgImage: vscode.Uri.file('/path/to/generated.svg'), |
| 267 | + startLine: 5, |
| 268 | + newCode: 'console.log("Hello World");', |
| 269 | + origionalCodeHighlightRange: [{ line: 5, start: 0, end: 10 }], |
| 270 | + addedCharacterCount: 25, |
| 271 | + deletedCharacterCount: 0, |
| 272 | + } |
| 273 | + svgGenerationServiceStub.generateDiffSvg.resolves(mockSvgResult) |
| 274 | + |
| 275 | + await showEdits( |
| 276 | + itemWithUndefinedText, |
| 277 | + editorStub as unknown as vscode.TextEditor, |
| 278 | + sessionStub, |
| 279 | + languageClientStub |
| 280 | + ) |
| 281 | + |
| 282 | + // Verify SVG generation was called with undefined as string |
| 283 | + sinon.assert.calledOnce(svgGenerationServiceStub.generateDiffSvg) |
| 284 | + sinon.assert.calledWith(svgGenerationServiceStub.generateDiffSvg, '/path/to/test/file.ts', undefined) |
| 285 | + }) |
| 286 | +}) |
0 commit comments