From edfcb0a509722876e11d1d78b8c49259926b4096 Mon Sep 17 00:00:00 2001 From: Henry Mercer Date: Tue, 10 Mar 2026 12:49:58 +0000 Subject: [PATCH] Update tests --- src/database-upload.test.ts | 100 ++++++++++++++++++++++++++---------- 1 file changed, 72 insertions(+), 28 deletions(-) diff --git a/src/database-upload.test.ts b/src/database-upload.test.ts index 3d8433d8b..8bd22091c 100644 --- a/src/database-upload.test.ts +++ b/src/database-upload.test.ts @@ -214,37 +214,81 @@ test.serial( }, ); -test.serial("Don't crash if uploading a database fails", async (t) => { - await withTmpDir(async (tmpDir) => { - setupActionsVars(tmpDir, tmpDir); - sinon - .stub(actionsUtil, "getRequiredInput") - .withArgs("upload-database") - .returns("true"); - sinon.stub(gitUtils, "isAnalyzingDefaultBranch").resolves(true); +test.serial( + "Don't crash if uploading a database fails with a non-retryable error", + async (t) => { + await withTmpDir(async (tmpDir) => { + setupActionsVars(tmpDir, tmpDir); + sinon + .stub(actionsUtil, "getRequiredInput") + .withArgs("upload-database") + .returns("true"); + sinon.stub(gitUtils, "isAnalyzingDefaultBranch").resolves(true); - await mockHttpRequests(500); + await mockHttpRequests(422); - const loggedMessages = [] as LoggedMessage[]; - await cleanupAndUploadDatabases( - testRepoName, - getCodeQL(), - getTestConfig(tmpDir), - testApiDetails, - createFeatures([]), - getRecordingLogger(loggedMessages), - ); + const loggedMessages = [] as LoggedMessage[]; + await cleanupAndUploadDatabases( + testRepoName, + getCodeQL(), + getTestConfig(tmpDir), + testApiDetails, + createFeatures([]), + getRecordingLogger(loggedMessages), + ); - t.assert( - loggedMessages.find( - (v) => - v.type === "warning" && - v.message === - "Failed to upload database for javascript: some error message", - ) !== undefined, - ); - }); -}); + t.assert( + loggedMessages.find( + (v) => + v.type === "warning" && + v.message === + "Failed to upload database for javascript: some error message", + ) !== undefined, + ); + }); + }, +); + +test.serial( + "Don't crash if uploading a database fails with a retryable error", + async (t) => { + await withTmpDir(async (tmpDir) => { + setupActionsVars(tmpDir, tmpDir); + sinon + .stub(actionsUtil, "getRequiredInput") + .withArgs("upload-database") + .returns("true"); + sinon.stub(gitUtils, "isAnalyzingDefaultBranch").resolves(true); + + await mockHttpRequests(500); + + // Stub setTimeout to fire immediately to avoid real delays from retry backoff. + const originalSetTimeout = global.setTimeout; + sinon + .stub(global, "setTimeout") + .callsFake((fn: () => void) => originalSetTimeout(fn, 0)); + + const loggedMessages = [] as LoggedMessage[]; + await cleanupAndUploadDatabases( + testRepoName, + getCodeQL(), + getTestConfig(tmpDir), + testApiDetails, + createFeatures([]), + getRecordingLogger(loggedMessages), + ); + + t.assert( + loggedMessages.find( + (v) => + v.type === "warning" && + v.message === + "Failed to upload database for javascript: some error message", + ) !== undefined, + ); + }); + }, +); test.serial("Successfully uploading a database to github.com", async (t) => { await withTmpDir(async (tmpDir) => {