This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
96cd2f9
commit 7c628f9
Showing
8 changed files
with
111 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -308,4 +308,4 @@ | |
"extensionDependencies": [ | ||
"vscode.git" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,83 @@ | ||
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'); | ||
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<Function>) => { | ||
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])) | ||
}); | ||
}) | ||
expect(fs.writeFile).toBeCalledWith( | ||
'/', | ||
new Uint8Array([84, 101, 115, 116]), | ||
); | ||
}); | ||
}); |