From 89397db14b3cc1c64f92cb6cc3e00ff6e92f15db Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 14 Jul 2025 13:01:02 +0000 Subject: [PATCH] Restore server error test and confirm logCacheError function removal Co-authored-by: Link- <568794+Link-@users.noreply.github.com> --- packages/cache/__tests__/saveCache.test.ts | 60 ++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/packages/cache/__tests__/saveCache.test.ts b/packages/cache/__tests__/saveCache.test.ts index 34192e97..ab5d3803 100644 --- a/packages/cache/__tests__/saveCache.test.ts +++ b/packages/cache/__tests__/saveCache.test.ts @@ -221,6 +221,66 @@ test('save with reserve cache failure should fail', async () => { expect(getCompressionMock).toHaveBeenCalledTimes(1) }) +test('save with server error should fail', async () => { + const filePath = 'node_modules' + const primaryKey = 'Linux-node-bb828da54c148048dd17899ba9fda624811cfb43' + const logErrorMock = jest.spyOn(core, 'error') + const logWarningMock = jest.spyOn(core, 'warning') + + // Mock cache service version to V2 + const getCacheServiceVersionMock = jest.spyOn(config, 'getCacheServiceVersion').mockReturnValue('v2') + + // Mock V2 CreateCacheEntry to succeed + const createCacheEntryMock = jest + .spyOn(CacheServiceClientJSON.prototype, 'CreateCacheEntry') + .mockReturnValue( + Promise.resolve({ok: true, signedUploadUrl: 'https://blob-storage.local?signed=true'}) + ) + + // Mock the FinalizeCacheEntryUpload to succeed (since the error should happen in saveCache) + const finalizeCacheEntryMock = jest + .spyOn(CacheServiceClientJSON.prototype, 'FinalizeCacheEntryUpload') + .mockReturnValue(Promise.resolve({ok: true, entryId: '4'})) + + const createTarMock = jest.spyOn(tar, 'createTar') + + // Mock the saveCache call to throw a server error + const saveCacheMock = jest + .spyOn(cacheHttpClient, 'saveCache') + .mockImplementationOnce(() => { + throw new HttpClientError('HTTP Error Occurred', 500) + }) + + const compression = CompressionMethod.Zstd + const getCompressionMock = jest + .spyOn(cacheUtils, 'getCompressionMethod') + .mockReturnValueOnce(Promise.resolve(compression)) + + await saveCache([filePath], primaryKey) + + expect(logErrorMock).toHaveBeenCalledTimes(1) + expect(logErrorMock).toHaveBeenCalledWith( + 'Failed to save: HTTP Error Occurred' + ) + + expect(createCacheEntryMock).toHaveBeenCalledTimes(1) + const archiveFolder = '/foo/bar' + const cachePaths = [path.resolve(filePath)] + const archiveFile = path.join(archiveFolder, CacheFilename.Zstd) + expect(createTarMock).toHaveBeenCalledTimes(1) + expect(createTarMock).toHaveBeenCalledWith( + archiveFolder, + cachePaths, + compression + ) + expect(saveCacheMock).toHaveBeenCalledTimes(1) + expect(getCompressionMock).toHaveBeenCalledTimes(1) + expect(getCompressionMock).toHaveBeenCalledTimes(1) + + // Restore the getCacheServiceVersion mock to its original state + getCacheServiceVersionMock.mockRestore() +}) + test('save with valid inputs uploads a cache', async () => { const filePath = 'node_modules' const primaryKey = 'Linux-node-bb828da54c148048dd17899ba9fda624811cfb43'