From 7c628f992ecc15d742f2dc0cc363118fa23fab88 Mon Sep 17 00:00:00 2001 From: Dmytro Hryshyn Date: Wed, 31 Jan 2024 12:29:08 +0200 Subject: [PATCH] finalize --- package.json | 2 +- src/axios/index.ts | 2 +- src/components/bootstrapExecutablesService.ts | 21 ++-- src/components/downloadService.ts | 18 +-- src/data/index.ts | 40 +++--- src/extension.ts | 4 +- src/telemetry/telemetry.ts | 17 +-- test/dowloadService.test.ts | 116 +++++++++--------- 8 files changed, 111 insertions(+), 109 deletions(-) diff --git a/package.json b/package.json index 18695f94..6e7ceb31 100644 --- a/package.json +++ b/package.json @@ -308,4 +308,4 @@ "extensionDependencies": [ "vscode.git" ] -} \ No newline at end of file +} diff --git a/src/axios/index.ts b/src/axios/index.ts index 4b182a00..afff39bb 100644 --- a/src/axios/index.ts +++ b/src/axios/index.ts @@ -5,4 +5,4 @@ const client = axios.create(); axiosRetry(client); export const DEFAULT_RETRY_COUNT = 3; -export default client; \ No newline at end of file +export default client; diff --git a/src/components/bootstrapExecutablesService.ts b/src/components/bootstrapExecutablesService.ts index 2339bec6..8604b56f 100644 --- a/src/components/bootstrapExecutablesService.ts +++ b/src/components/bootstrapExecutablesService.ts @@ -10,7 +10,7 @@ export class BootstrapExecutablesService { private readonly __globalStorageUri: Uri, private readonly __fileSystem: FileSystem, private readonly __messageBus: MessageBus, - private readonly __telemetryService: Telemetry + private readonly __telemetryService: Telemetry, ) { __messageBus.subscribe(MessageKind.bootstrapEngine, () => this.__onBootstrapEngines(), @@ -25,17 +25,16 @@ export class BootstrapExecutablesService { const codemodEngineNodeExecutableUri = await this.__bootstrapCodemodEngineNodeExecutableUri(); - // Uri.file('/intuita/codemod-engine-rust/target/release/codemod-engine-rust'); - const codemodEngineRustExecutableUri = - await this.__bootstrapCodemodEngineRustExecutableUri(); + // Uri.file('/intuita/codemod-engine-rust/target/release/codemod-engine-rust'); + const codemodEngineRustExecutableUri = + await this.__bootstrapCodemodEngineRustExecutableUri(); - this.__messageBus.publish({ - kind: MessageKind.engineBootstrapped, - codemodEngineNodeExecutableUri, - codemodEngineRustExecutableUri, - }); - - } catch(e) { + this.__messageBus.publish({ + kind: MessageKind.engineBootstrapped, + codemodEngineNodeExecutableUri, + codemodEngineRustExecutableUri, + }); + } catch (e) { const message = e instanceof Error ? e.message : String(e); window.showErrorMessage(message); diff --git a/src/components/downloadService.ts b/src/components/downloadService.ts index 925b08d7..b67e4d9e 100644 --- a/src/components/downloadService.ts +++ b/src/components/downloadService.ts @@ -30,11 +30,11 @@ export class DownloadService { let response; try { - response = await axios.head(url, { - timeout: 15000, + response = await axios.head(url, { + timeout: 15000, 'axios-retry': { - retries: DEFAULT_RETRY_COUNT - } + retries: DEFAULT_RETRY_COUNT, + }, }); } catch (error) { if (localModificationTime > 0) { @@ -75,16 +75,16 @@ export class DownloadService { uri: Uri, chmod: Mode | null, ): Promise { - const response = await axios.get(url, { - responseType: 'arraybuffer', + const response = await axios.get(url, { + responseType: 'arraybuffer', 'axios-retry': { - retries: DEFAULT_RETRY_COUNT - } + retries: DEFAULT_RETRY_COUNT, + }, }); const content = new Uint8Array(response.data); await this.#fileSystem.writeFile(uri, content); - + if (chmod !== null) { await this.#fileSystemUtilities.setChmod(uri, chmod); } diff --git a/src/data/index.ts b/src/data/index.ts index e32e6d2d..3857a032 100644 --- a/src/data/index.ts +++ b/src/data/index.ts @@ -39,28 +39,30 @@ const deserializeState = (serializedState: string) => { }; const getPreloadedState = async (storage: MementoStorage) => { -const initialState = - (await storage.getItem(`${PERSISTANCE_PREFIX}:${PERSISTANCE_KEY}`)); + const initialState = await storage.getItem( + `${PERSISTANCE_PREFIX}:${PERSISTANCE_KEY}`, + ); -if(initialState === null) { - return null; -} + if (!initialState) { + return null; + } -const deserializedState = deserializeState(initialState); + const deserializedState = deserializeState(initialState); + + if (!deserializedState) { + return null; + } -if(deserializeState === null) { - return null; -} + const decodedState = persistedStateCodecNew.decode(deserializedState); -const decodedState = persistedStateCodecNew.decode(deserializedState); + // should never happen because of codec fallback + if (decodedState._tag !== 'Right') { + return null; + } -// should never happen because of codec fallback -if (decodedState._tag !== 'Right') { - throw new Error('Invalid state'); -} + return decodedState.right; +}; -return decodedState.right; -} const buildStore = async (workspaceState: Memento) => { const storage = new MementoStorage(workspaceState); @@ -93,10 +95,10 @@ const buildStore = async (workspaceState: Memento) => { const preloadedState = await getPreloadedState(storage); - if(preloadedState === null) { - window.showWarningMessage('Unable to get preloaded state'); + if (preloadedState === null) { + window.showWarningMessage('Unable to get preloaded state.'); } - + const store = configureStore({ reducer: validatedReducer, ...(preloadedState !== null && { preloadedState }), diff --git a/src/extension.ts b/src/extension.ts index e6b1a061..41e33623 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -140,14 +140,12 @@ export async function activate(context: vscode.ExtensionContext) { context.globalStorageUri, vscode.workspace.fs, messageBus, - vscodeTelemetry + vscodeTelemetry, ); const intuitaTextDocumentContentProvider = new IntuitaTextDocumentContentProvider(); - - const mainViewProvider = new MainViewProvider( context, userService, diff --git a/src/telemetry/telemetry.ts b/src/telemetry/telemetry.ts index 5d80cbcb..7ea9b767 100644 --- a/src/telemetry/telemetry.ts +++ b/src/telemetry/telemetry.ts @@ -1,13 +1,14 @@ import type { CaseHash } from '../cases/types'; -export type ErrorEvent = Readonly<{ - kind: 'failedToExecuteCommand'; - commandName: string; -}> | -Readonly<{ - kind: 'failedToBootstrapEngines', - message: string; -}> +export type ErrorEvent = + | Readonly<{ + kind: 'failedToExecuteCommand'; + commandName: string; + }> + | Readonly<{ + kind: 'failedToBootstrapEngines'; + message: string; + }>; export type Event = | Readonly<{ diff --git a/test/dowloadService.test.ts b/test/dowloadService.test.ts index 54565e45..47c4e117 100644 --- a/test/dowloadService.test.ts +++ b/test/dowloadService.test.ts @@ -1,24 +1,24 @@ -import { describe, test, afterEach, vi, expect } from 'vitest'; +import { describe, test, afterEach, vi, expect } from 'vitest'; import { DownloadService } from '../src/components/downloadService'; import type { FileSystem } from 'vscode'; -import { AxiosError, AxiosInstance } from 'axios'; +import { AxiosError, AxiosInstance } from 'axios'; import nock from 'nock'; import axiosInstance from '../src/axios'; -const mockedFileSystemUtilities = { - getModificationTime: vi.fn(() => 1), - setChmod: vi.fn() +const mockedFileSystemUtilities = { + getModificationTime: vi.fn(() => 1), + setChmod: vi.fn(), }; - - const fs = { - writeFile: vi.fn(), - stat: vi.fn(() => ({ mtime: Date.now() })) - } as unknown as FileSystem; + +const fs = { + writeFile: vi.fn(), + stat: vi.fn(() => ({ mtime: Date.now() })), +} as unknown as FileSystem; const downloadService = new DownloadService( - fs, - // @ts-ignore - mockedFileSystemUtilities, + fs, + // @ts-ignore + mockedFileSystemUtilities, ); const NETWORK_ERROR = new AxiosError('Some connection error'); @@ -26,56 +26,58 @@ NETWORK_ERROR.code = 'ECONNRESET'; // 3 failed responses, then good response const responses = [ - () => nock('https://test.com').head('/test').replyWithError(NETWORK_ERROR), - () => nock('https://test.com').head('/test').replyWithError(NETWORK_ERROR), - () => nock('https://test.com').head('/test').replyWithError(NETWORK_ERROR), - () => nock('https://test.com').head('/test') - .reply(200, '', { 'last-modified': new Date(2).toISOString() }), - () => nock('https://test.com').get('/test').replyWithError(NETWORK_ERROR), - () => nock('https://test.com').get('/test').replyWithError(NETWORK_ERROR), - () => nock('https://test.com').get('/test').replyWithError(NETWORK_ERROR), - () => nock('https://test.com').get('/test') - .reply(200, 'Test'), + () => nock('https://test.com').head('/test').replyWithError(NETWORK_ERROR), + () => nock('https://test.com').head('/test').replyWithError(NETWORK_ERROR), + () => nock('https://test.com').head('/test').replyWithError(NETWORK_ERROR), + () => + nock('https://test.com') + .head('/test') + .reply(200, '', { 'last-modified': new Date(2).toISOString() }), + () => nock('https://test.com').get('/test').replyWithError(NETWORK_ERROR), + () => nock('https://test.com').get('/test').replyWithError(NETWORK_ERROR), + () => nock('https://test.com').get('/test').replyWithError(NETWORK_ERROR), + () => nock('https://test.com').get('/test').reply(200, 'Test'), ]; const setupResponses = (client: AxiosInstance, responses: Array) => { + const configureResponse = () => { + const response = responses.shift(); + if (response) { + response(); + } + }; - const configureResponse = () => { - const response = responses.shift(); - if (response) { - response(); - } - }; - - client.interceptors.request.use( - (config) => { - configureResponse(); - return config; - }, - (error) => { - configureResponse(); - return Promise.reject(error); - }, - ); -} + client.interceptors.request.use( + (config) => { + configureResponse(); + return config; + }, + (error) => { + configureResponse(); + return Promise.reject(error); + }, + ); +}; describe('DownloadService', () => { - afterEach(() => { - nock.cleanAll(); - nock.enableNetConnect(); - }); + afterEach(() => { + nock.cleanAll(); + nock.enableNetConnect(); + }); - test('Should retry 3 times if request fails', async () => { + test('Should retry 3 times if request fails', async () => { + setupResponses(axiosInstance, responses); - setupResponses(axiosInstance, responses); - - await downloadService.downloadFileIfNeeded( - `https://test.com/test`, - // @ts-expect-error passing a string instead of URI, because URI cannot be imported from vscode - '/', - '755', - ); + await downloadService.downloadFileIfNeeded( + `https://test.com/test`, + // @ts-expect-error passing a string instead of URI, because URI cannot be imported from vscode + '/', + '755', + ); - expect(fs.writeFile).toBeCalledWith('/', new Uint8Array( [84, 101, 115, 116])) - }); -}) \ No newline at end of file + expect(fs.writeFile).toBeCalledWith( + '/', + new Uint8Array([84, 101, 115, 116]), + ); + }); +});