|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +/** |
| 7 | + * TODO: Move this file to amazonq/test/unit when we can figure out how to spy on vscode imports from amazonq. |
| 8 | + * |
| 9 | + * See TESTPLAN.md#Stubbing VSCode outside of core |
| 10 | + */ |
| 11 | +import assert from 'assert' |
| 12 | +import * as path from 'path' |
| 13 | +import * as vscode from 'vscode' |
| 14 | +import fs from 'fs' |
| 15 | +import sinon from 'sinon' |
| 16 | +import { createAmazonQUri, getFileDiffUris, getOriginalFileUri, openDeletedDiff, openDiff } from '../../../amazonq' |
| 17 | + |
| 18 | +describe('diff', () => { |
| 19 | + const filePath = path.join('/', 'foo', 'fi') |
| 20 | + const rightPath = path.join('foo', 'fee') |
| 21 | + const tabId = '0' |
| 22 | + |
| 23 | + let sandbox: sinon.SinonSandbox |
| 24 | + let executeCommandSpy: sinon.SinonSpy |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + sandbox = sinon.createSandbox() |
| 28 | + executeCommandSpy = sandbox.spy(vscode.commands, 'executeCommand') |
| 29 | + }) |
| 30 | + |
| 31 | + afterEach(() => { |
| 32 | + executeCommandSpy.restore() |
| 33 | + sandbox.restore() |
| 34 | + }) |
| 35 | + |
| 36 | + describe('openDiff', () => { |
| 37 | + it('file exists locally', async () => { |
| 38 | + sandbox.stub(fs, 'existsSync').returns(true) |
| 39 | + await openDiff(filePath, rightPath, tabId) |
| 40 | + |
| 41 | + const leftExpected = vscode.Uri.file(filePath) |
| 42 | + const rightExpected = createAmazonQUri(rightPath, tabId) |
| 43 | + assert.ok(executeCommandSpy.calledWith('vscode.diff', leftExpected, rightExpected)) |
| 44 | + }) |
| 45 | + |
| 46 | + it('file does not exists locally', async () => { |
| 47 | + sandbox.stub(fs, 'existsSync').returns(false) |
| 48 | + await openDiff(filePath, rightPath, tabId) |
| 49 | + |
| 50 | + const leftExpected = getOriginalFileUri(filePath, tabId) |
| 51 | + const rightExpected = createAmazonQUri(rightPath, tabId) |
| 52 | + assert.ok(executeCommandSpy.calledWith('vscode.diff', leftExpected, rightExpected)) |
| 53 | + }) |
| 54 | + }) |
| 55 | + |
| 56 | + describe('openDeletedDiff', () => { |
| 57 | + const name = 'foo' |
| 58 | + |
| 59 | + it('file exists locally', async () => { |
| 60 | + sandbox.stub(fs, 'existsSync').returns(true) |
| 61 | + await openDeletedDiff(filePath, name, tabId) |
| 62 | + |
| 63 | + const expectedPath = vscode.Uri.file(filePath) |
| 64 | + assert.ok(executeCommandSpy.calledWith('vscode.open', expectedPath, {}, `${name} (Deleted)`)) |
| 65 | + }) |
| 66 | + |
| 67 | + it('file does not exists locally', async () => { |
| 68 | + sandbox.stub(fs, 'existsSync').returns(false) |
| 69 | + await openDeletedDiff(filePath, name, tabId) |
| 70 | + |
| 71 | + const expectedPath = createAmazonQUri('empty', tabId) |
| 72 | + assert.ok(executeCommandSpy.calledWith('vscode.open', expectedPath, {}, `${name} (Deleted)`)) |
| 73 | + }) |
| 74 | + }) |
| 75 | + |
| 76 | + describe('getOriginalFileUri', () => { |
| 77 | + it('file exists locally', () => { |
| 78 | + sandbox.stub(fs, 'existsSync').returns(true) |
| 79 | + assert.deepStrictEqual(getOriginalFileUri(filePath, tabId).fsPath, filePath) |
| 80 | + }) |
| 81 | + |
| 82 | + it('file does not exists locally', () => { |
| 83 | + sandbox.stub(fs, 'existsSync').returns(false) |
| 84 | + const expected = createAmazonQUri('empty', tabId) |
| 85 | + assert.deepStrictEqual(getOriginalFileUri(filePath, tabId), expected) |
| 86 | + }) |
| 87 | + }) |
| 88 | + |
| 89 | + describe('getFileDiffUris', () => { |
| 90 | + it('file exists locally', () => { |
| 91 | + sandbox.stub(fs, 'existsSync').returns(true) |
| 92 | + |
| 93 | + const { left, right } = getFileDiffUris(filePath, rightPath, tabId) |
| 94 | + |
| 95 | + const leftExpected = vscode.Uri.file(filePath) |
| 96 | + assert.deepStrictEqual(left, leftExpected) |
| 97 | + |
| 98 | + const rightExpected = createAmazonQUri(rightPath, tabId) |
| 99 | + assert.deepStrictEqual(right, rightExpected) |
| 100 | + }) |
| 101 | + |
| 102 | + it('file does not exists locally', () => { |
| 103 | + sandbox.stub(fs, 'existsSync').returns(false) |
| 104 | + const { left, right } = getFileDiffUris(filePath, rightPath, tabId) |
| 105 | + |
| 106 | + const leftExpected = getOriginalFileUri(filePath, tabId) |
| 107 | + assert.deepStrictEqual(left, leftExpected) |
| 108 | + |
| 109 | + const rightExpected = createAmazonQUri(rightPath, tabId) |
| 110 | + assert.deepStrictEqual(right, rightExpected) |
| 111 | + }) |
| 112 | + }) |
| 113 | +}) |
0 commit comments