From 3a03667a44b512ea361f0d6696352031fa9657d3 Mon Sep 17 00:00:00 2001 From: tiefseetauchner Date: Thu, 15 Aug 2024 20:39:57 +0200 Subject: [PATCH] fix tests --- cli/src/commands/asset.spec.ts | 11 ++++------- cli/src/commands/asset.ts | 31 ++++++++++++++++++++----------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/cli/src/commands/asset.spec.ts b/cli/src/commands/asset.spec.ts index f820a0e5af21d..f4c969195f7a4 100644 --- a/cli/src/commands/asset.spec.ts +++ b/cli/src/commands/asset.spec.ts @@ -190,14 +190,11 @@ describe('checkForDuplicates', () => { }); }); - // TODO: this shouldn't return empty arrays, this should return an error - // Failed duplicate checks should be a reason for panic instead of ignoring - it('returns results when check duplicates retry is failed', async () => { + it('throws error when check duplicates retry is failed', async () => { vi.mocked(checkBulkUpload).mockRejectedValue(new Error('Network error')); - await expect(checkForDuplicates([testFilePath], { concurrency: 1 })).resolves.toEqual({ - duplicates: [], - newFiles: [], - }); + await expect(checkForDuplicates([testFilePath], { concurrency: 1 })).rejects.toThrowError( + 'An error occurred while checking for duplicates: Network error', + ); }); }); diff --git a/cli/src/commands/asset.ts b/cli/src/commands/asset.ts index f0ca3b7dab582..496a0c79e730f 100644 --- a/cli/src/commands/asset.ts +++ b/cli/src/commands/asset.ts @@ -122,20 +122,29 @@ export const checkForDuplicates = async (files: string[], { concurrency, skipHas const newFiles: string[] = []; const duplicates: Asset[] = []; - // TODO: Retry 3 times if there is an error - try { - const response = await checkBulkUpload({ assetBulkUploadCheckDto: { assets: results } }); + let retries = 0; + const maxRetries = 3; - for (const { id: filepath, assetId, action } of response.results) { - if (action === Action.Accept) { - newFiles.push(filepath); - } else { - // rejects are always duplicates - duplicates.push({ id: assetId as string, filepath }); + while (retries < maxRetries) { + try { + const response = await checkBulkUpload({ assetBulkUploadCheckDto: { assets: results } }); + + for (const { id: filepath, assetId, action } of response.results) { + if (action === Action.Accept) { + newFiles.push(filepath); + } else { + // rejects are always duplicates + duplicates.push({ id: assetId as string, filepath }); + } + } + + break; + } catch (error: any) { + retries++; + if (retries >= maxRetries) { + throw new Error(`An error occurred while checking for duplicates: ${error.message}`); } } - } catch (error: any) { - throw new Error(`An error occurred while checking for duplicates: ${error.message}`); } progressBar.stop();