|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +import assert from 'assert' |
| 6 | +import sinon from 'sinon' |
| 7 | +import { Content, LspController } from 'aws-core-vscode/amazonq' |
| 8 | +import { createTestFile } from 'aws-core-vscode/test' |
| 9 | +import { fs } from 'aws-core-vscode/shared' |
| 10 | + |
| 11 | +describe('Amazon Q LSP controller', function () { |
| 12 | + it('Download mechanism checks against hash, when hash matches', async function () { |
| 13 | + const content = { |
| 14 | + filename: 'qserver-linux-x64.zip', |
| 15 | + url: 'https://x/0.0.6/qserver-linux-x64.zip', |
| 16 | + hashes: [ |
| 17 | + 'sha384:768412320f7b0aa5812fce428dc4706b3cae50e02a64caa16a782249bfe8efc4b7ef1ccb126255d196047dfedf17a0a9', |
| 18 | + ], |
| 19 | + bytes: 512, |
| 20 | + } as Content |
| 21 | + const lspController = new LspController() |
| 22 | + sinon.stub(lspController, '_download') |
| 23 | + const mockFileName = 'test_case_1.zip' |
| 24 | + const mockDownloadFile = await createTestFile(mockFileName) |
| 25 | + await fs.writeFile(mockDownloadFile.fsPath, 'test') |
| 26 | + const result = await lspController.downloadAndCheckHash(mockDownloadFile.fsPath, content) |
| 27 | + assert.strictEqual(result, true) |
| 28 | + }) |
| 29 | + |
| 30 | + it('Download mechanism checks against hash, when hash does not match', async function () { |
| 31 | + const content = { |
| 32 | + filename: 'qserver-linux-x64.zip', |
| 33 | + url: 'https://x/0.0.6/qserver-linux-x64.zip', |
| 34 | + hashes: [ |
| 35 | + 'sha384:38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b', |
| 36 | + ], |
| 37 | + bytes: 512, |
| 38 | + } as Content |
| 39 | + const lspController = new LspController() |
| 40 | + sinon.stub(lspController, '_download') |
| 41 | + const mockFileName = 'test_case_2.zip' |
| 42 | + const mockDownloadFile = await createTestFile(mockFileName) |
| 43 | + await fs.writeFile(mockDownloadFile.fsPath, 'file_content') |
| 44 | + const result = await lspController.downloadAndCheckHash(mockDownloadFile.fsPath, content) |
| 45 | + assert.strictEqual(result, false) |
| 46 | + }) |
| 47 | + |
| 48 | + afterEach(() => { |
| 49 | + sinon.restore() |
| 50 | + }) |
| 51 | +}) |
0 commit comments