diff --git a/src/__snapshots__/return-dispatch.spec.ts.snap b/src/__snapshots__/return-dispatch.spec.ts.snap index fa08ee09..e85d65b7 100644 --- a/src/__snapshots__/return-dispatch.spec.ts.snap +++ b/src/__snapshots__/return-dispatch.spec.ts.snap @@ -1,27 +1,27 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`return-dispatch > handleAction > getRunIdAndUrl > called fetchWorkflowRunIds with the provided workflowId and branch 1`] = `"Attempting to get step names for Run IDs: [0]"`; +exports[`return-dispatch > getRunIdAndUrl > called fetchWorkflowRunIds with the provided workflowId and branch 1`] = `"Attempting to get step names for Run IDs: [0]"`; -exports[`return-dispatch > handleAction > getRunIdAndUrl > should call retryOrTimeout with the larger workflowTimeoutMs timeout value 1`] = `"Attempting to get step names for Run IDs: [0]"`; +exports[`return-dispatch > getRunIdAndUrl > should call retryOrTimeout with the larger workflowTimeoutMs timeout value 1`] = `"Attempting to get step names for Run IDs: [0]"`; -exports[`return-dispatch > handleAction > getRunIdAndUrl > should retry until an ID is found 1`] = `"No Run IDs found for workflow, attempt 1..."`; +exports[`return-dispatch > getRunIdAndUrl > should retry until an ID is found 1`] = `"No Run IDs found for workflow, attempt 1..."`; -exports[`return-dispatch > handleAction > getRunIdAndUrl > should retry until an ID is found 2`] = `"No Run IDs found for workflow, attempt 2..."`; +exports[`return-dispatch > getRunIdAndUrl > should retry until an ID is found 2`] = `"No Run IDs found for workflow, attempt 2..."`; -exports[`return-dispatch > handleAction > getRunIdAndUrl > should retry until an ID is found 3`] = `"Attempting to get step names for Run IDs: [0]"`; +exports[`return-dispatch > getRunIdAndUrl > should retry until an ID is found 3`] = `"Attempting to get step names for Run IDs: [0]"`; -exports[`return-dispatch > handleAction > getRunIdAndUrl > should return the ID when found 1`] = `"Attempting to get step names for Run IDs: [0]"`; +exports[`return-dispatch > getRunIdAndUrl > should return the ID when found 1`] = `"Attempting to get step names for Run IDs: [0]"`; -exports[`return-dispatch > handleAction > getRunIdAndUrl > should return the ID when found 2`] = `undefined`; +exports[`return-dispatch > getRunIdAndUrl > should return the ID when found 2`] = `undefined`; -exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable to find over time 1`] = `"Exhausted searching IDs in known runs, attempt 1..."`; +exports[`return-dispatch > getRunIdAndUrl > should timeout when unable to find over time 1`] = `"Exhausted searching IDs in known runs, attempt 1..."`; -exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable to find over time 2`] = `"Attempting to get step names for Run IDs: [0]"`; +exports[`return-dispatch > getRunIdAndUrl > should timeout when unable to find over time 2`] = `"Attempting to get step names for Run IDs: [0]"`; -exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable to find over time 3`] = `"Exhausted searching IDs in known runs, attempt 2..."`; +exports[`return-dispatch > getRunIdAndUrl > should timeout when unable to find over time 3`] = `"Exhausted searching IDs in known runs, attempt 2..."`; -exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable to find over time 4`] = `"Attempting to get step names for Run IDs: [0]"`; +exports[`return-dispatch > getRunIdAndUrl > should timeout when unable to find over time 4`] = `"Attempting to get step names for Run IDs: [0]"`; -exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable to find over time 5`] = `"Exhausted searching IDs in known runs, attempt 3..."`; +exports[`return-dispatch > getRunIdAndUrl > should timeout when unable to find over time 5`] = `"Exhausted searching IDs in known runs, attempt 3..."`; -exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable to find over time 6`] = `"Attempting to get step names for Run IDs: [0]"`; +exports[`return-dispatch > getRunIdAndUrl > should timeout when unable to find over time 6`] = `"Attempting to get step names for Run IDs: [0]"`; diff --git a/src/return-dispatch.spec.ts b/src/return-dispatch.spec.ts index f4e2417a..23cb3b14 100644 --- a/src/return-dispatch.spec.ts +++ b/src/return-dispatch.spec.ts @@ -462,390 +462,386 @@ describe("return-dispatch", () => { ); }); }); + }); - describe("getRunIdAndUrl", () => { - const distinctId = crypto.randomUUID(); - const distinctIdRegex = new RegExp(distinctId); - const workflowId = 123; - const branch: utils.BranchNameResult = Object.freeze({ - isTag: false, - ref: "/refs/heads/main", - branchName: "main", - }); - const defaultOpts: GetRunIdAndUrlOpts = { - startTime: Date.now(), - branch: branch, - distinctIdRegex: distinctIdRegex, - workflowId: workflowId, - workflowTimeoutMs: 100, - }; - - let apiFetchWorkflowRunIdsMock: MockInstance< - typeof api.fetchWorkflowRunIds - >; - let apiFetchWorkflowRunJobStepsMock: MockInstance< - typeof api.fetchWorkflowRunJobSteps - >; - let apiFetchWorkflowRunUrlMock: MockInstance< - typeof api.fetchWorkflowRunUrl - >; - let apiRetryOrTimeoutMock: MockInstance; - let utilSleepMock: MockInstance; + describe("getRunIdAndUrl", () => { + const distinctId = crypto.randomUUID(); + const distinctIdRegex = new RegExp(distinctId); + const workflowId = 123; + const branch: utils.BranchNameResult = Object.freeze({ + isTag: false, + ref: "/refs/heads/main", + branchName: "main", + }); + const defaultOpts: GetRunIdAndUrlOpts = { + startTime: Date.now(), + branch: branch, + distinctIdRegex: distinctIdRegex, + workflowId: workflowId, + workflowTimeoutMs: 100, + }; + + let apiFetchWorkflowRunIdsMock: MockInstance< + typeof api.fetchWorkflowRunIds + >; + let apiFetchWorkflowRunJobStepsMock: MockInstance< + typeof api.fetchWorkflowRunJobSteps + >; + let apiFetchWorkflowRunUrlMock: MockInstance< + typeof api.fetchWorkflowRunUrl + >; + let apiRetryOrTimeoutMock: MockInstance; + let utilSleepMock: MockInstance; - beforeEach(() => { - vi.useFakeTimers(); + beforeEach(() => { + vi.useFakeTimers(); + + apiFetchWorkflowRunIdsMock = vi.spyOn(api, "fetchWorkflowRunIds"); + apiFetchWorkflowRunJobStepsMock = vi.spyOn( + api, + "fetchWorkflowRunJobSteps", + ); + apiFetchWorkflowRunUrlMock = vi.spyOn(api, "fetchWorkflowRunUrl"); + apiRetryOrTimeoutMock = vi.spyOn(api, "retryOrTimeout"); - apiFetchWorkflowRunIdsMock = vi.spyOn(api, "fetchWorkflowRunIds"); - apiFetchWorkflowRunJobStepsMock = vi.spyOn( - api, - "fetchWorkflowRunJobSteps", + utilSleepMock = vi + .spyOn(utils, "sleep") + .mockImplementation( + (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)), ); - apiFetchWorkflowRunUrlMock = vi.spyOn(api, "fetchWorkflowRunUrl"); - apiRetryOrTimeoutMock = vi.spyOn(api, "retryOrTimeout"); - - utilSleepMock = vi - .spyOn(utils, "sleep") - .mockImplementation( - (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)), - ); - }); + }); - afterEach(() => { - vi.useRealTimers(); + afterEach(() => { + vi.useRealTimers(); + + vi.resetAllMocks(); + }); - vi.resetAllMocks(); + it("should return the ID when found", async () => { + const runId = 0; + const runUrl = "test-url"; + apiRetryOrTimeoutMock.mockResolvedValue({ + success: true, + value: [runId], }); + apiFetchWorkflowRunJobStepsMock.mockResolvedValue([distinctId]); + apiFetchWorkflowRunUrlMock.mockResolvedValue(runUrl); - it("should return the ID when found", async () => { - const runId = 0; - const runUrl = "test-url"; - apiRetryOrTimeoutMock.mockResolvedValue({ - success: true, - value: [runId], - }); - apiFetchWorkflowRunJobStepsMock.mockResolvedValue([distinctId]); - apiFetchWorkflowRunUrlMock.mockResolvedValue(runUrl); + const run = await getRunIdAndUrl({ + ...defaultOpts, + workflowTimeoutMs: 1000, + }); - const run = await getRunIdAndUrl({ - ...defaultOpts, - workflowTimeoutMs: 1000, - }); + if (!run.success) { + expect.fail("expected call to succeed"); + } - if (!run.success) { - expect.fail("expected call to succeed"); - } + // Behaviour + expect(run.value.id).toStrictEqual(runId); + expect(run.value.url).toStrictEqual(runUrl); - // Behaviour - expect(run.value.id).toStrictEqual(runId); - expect(run.value.url).toStrictEqual(runUrl); + expect(apiRetryOrTimeoutMock).toHaveBeenCalledOnce(); + expect(apiFetchWorkflowRunJobStepsMock).toHaveBeenCalledOnce(); + expect(apiFetchWorkflowRunIdsMock).not.toHaveBeenCalled(); + expect(utilSleepMock).not.toHaveBeenCalled(); - expect(apiRetryOrTimeoutMock).toHaveBeenCalledOnce(); - expect(apiFetchWorkflowRunJobStepsMock).toHaveBeenCalledOnce(); - expect(apiFetchWorkflowRunIdsMock).not.toHaveBeenCalled(); - expect(utilSleepMock).not.toHaveBeenCalled(); + // Logging + assertOnlyCalled(coreDebugLogMock); + expect(coreDebugLogMock).toHaveBeenCalledOnce(); + expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchSnapshot(); + expect(coreDebugLogMock.mock.calls[1]?.[0]).toMatchSnapshot(); + }); - // Logging - assertOnlyCalled(coreDebugLogMock); - expect(coreDebugLogMock).toHaveBeenCalledOnce(); - expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchSnapshot(); - expect(coreDebugLogMock.mock.calls[1]?.[0]).toMatchSnapshot(); + it("should call retryOrTimeout with the larger WORKFLOW_FETCH_TIMEOUT_MS timeout value", async () => { + const workflowFetchTimeoutMs = 1000; + const workflowTimeoutMs = 100; + apiRetryOrTimeoutMock.mockResolvedValue({ + success: true, + value: [0], }); + apiFetchWorkflowRunJobStepsMock.mockResolvedValue([distinctId]); + vi.spyOn(constants, "WORKFLOW_FETCH_TIMEOUT_MS", "get").mockReturnValue( + workflowFetchTimeoutMs, + ); - it("should call retryOrTimeout with the larger WORKFLOW_FETCH_TIMEOUT_MS timeout value", async () => { - const workflowFetchTimeoutMs = 1000; - const workflowTimeoutMs = 100; - apiRetryOrTimeoutMock.mockResolvedValue({ - success: true, - value: [0], - }); - apiFetchWorkflowRunJobStepsMock.mockResolvedValue([distinctId]); - vi.spyOn(constants, "WORKFLOW_FETCH_TIMEOUT_MS", "get").mockReturnValue( - workflowFetchTimeoutMs, - ); + await getRunIdAndUrl({ + ...defaultOpts, + workflowTimeoutMs: workflowTimeoutMs, + }); - await getRunIdAndUrl({ - ...defaultOpts, - workflowTimeoutMs: workflowTimeoutMs, - }); + // Behaviour + expect(apiRetryOrTimeoutMock).toHaveBeenCalledOnce(); + expect(apiRetryOrTimeoutMock.mock.calls[0]?.[1]).toStrictEqual( + workflowFetchTimeoutMs, + ); + expect(apiFetchWorkflowRunJobStepsMock).toHaveBeenCalledOnce(); + expect(apiFetchWorkflowRunIdsMock).not.toHaveBeenCalled(); + expect(utilSleepMock).not.toHaveBeenCalled(); - // Behaviour - expect(apiRetryOrTimeoutMock).toHaveBeenCalledOnce(); - expect(apiRetryOrTimeoutMock.mock.calls[0]?.[1]).toStrictEqual( - workflowFetchTimeoutMs, - ); - expect(apiFetchWorkflowRunJobStepsMock).toHaveBeenCalledOnce(); - expect(apiFetchWorkflowRunIdsMock).not.toHaveBeenCalled(); - expect(utilSleepMock).not.toHaveBeenCalled(); + // Logging + assertOnlyCalled(coreDebugLogMock); + expect(coreDebugLogMock).toHaveBeenCalledOnce(); + expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchInlineSnapshot( + `"Attempting to get step names for Run IDs: [0]"`, + ); + }); - // Logging - assertOnlyCalled(coreDebugLogMock); - expect(coreDebugLogMock).toHaveBeenCalledOnce(); - expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchInlineSnapshot( - `"Attempting to get step names for Run IDs: [0]"`, - ); + it("should call retryOrTimeout with the larger workflowTimeoutMs timeout value", async () => { + const workflowFetchTimeoutMs = 100; + const workflowTimeoutMs = 1000; + apiRetryOrTimeoutMock.mockResolvedValue({ + success: true, + value: [0], }); + apiFetchWorkflowRunJobStepsMock.mockResolvedValue([distinctId]); + vi.spyOn(constants, "WORKFLOW_FETCH_TIMEOUT_MS", "get").mockReturnValue( + workflowFetchTimeoutMs, + ); - it("should call retryOrTimeout with the larger workflowTimeoutMs timeout value", async () => { - const workflowFetchTimeoutMs = 100; - const workflowTimeoutMs = 1000; - apiRetryOrTimeoutMock.mockResolvedValue({ - success: true, - value: [0], - }); - apiFetchWorkflowRunJobStepsMock.mockResolvedValue([distinctId]); - vi.spyOn(constants, "WORKFLOW_FETCH_TIMEOUT_MS", "get").mockReturnValue( - workflowFetchTimeoutMs, - ); + await getRunIdAndUrl({ + ...defaultOpts, + workflowTimeoutMs: workflowTimeoutMs, + }); - await getRunIdAndUrl({ - ...defaultOpts, - workflowTimeoutMs: workflowTimeoutMs, - }); + // Behaviour + expect(apiRetryOrTimeoutMock).toHaveBeenCalledOnce(); + expect(apiRetryOrTimeoutMock.mock.calls[0]?.[1]).toStrictEqual( + workflowTimeoutMs, + ); + expect(apiFetchWorkflowRunJobStepsMock).toHaveBeenCalledOnce(); + expect(apiFetchWorkflowRunIdsMock).not.toHaveBeenCalled(); + expect(utilSleepMock).not.toHaveBeenCalled(); - // Behaviour - expect(apiRetryOrTimeoutMock).toHaveBeenCalledOnce(); - expect(apiRetryOrTimeoutMock.mock.calls[0]?.[1]).toStrictEqual( - workflowTimeoutMs, - ); - expect(apiFetchWorkflowRunJobStepsMock).toHaveBeenCalledOnce(); - expect(apiFetchWorkflowRunIdsMock).not.toHaveBeenCalled(); - expect(utilSleepMock).not.toHaveBeenCalled(); + // Logging + assertOnlyCalled(coreDebugLogMock); + expect(coreDebugLogMock).toHaveBeenCalledOnce(); + expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchSnapshot(); + }); - // Logging - assertOnlyCalled(coreDebugLogMock); - expect(coreDebugLogMock).toHaveBeenCalledOnce(); - expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchSnapshot(); + it("called fetchWorkflowRunIds with the provided workflowId and branch", async () => { + apiRetryOrTimeoutMock.mockImplementation(async (retryFunc) => { + await retryFunc(); + return { + success: true, + value: [0], + }; }); + apiFetchWorkflowRunJobStepsMock.mockResolvedValue([distinctId]); + apiFetchWorkflowRunUrlMock.mockResolvedValue("test-url"); + + await getRunIdAndUrl(defaultOpts); - it("called fetchWorkflowRunIds with the provided workflowId and branch", async () => { - apiRetryOrTimeoutMock.mockImplementation(async (retryFunc) => { - await retryFunc(); - return { - success: true, - value: [0], - }; - }); - apiFetchWorkflowRunJobStepsMock.mockResolvedValue([distinctId]); - apiFetchWorkflowRunUrlMock.mockResolvedValue("test-url"); + // Behaviour + expect(apiRetryOrTimeoutMock).toHaveBeenCalledOnce(); + expect(apiFetchWorkflowRunJobStepsMock).toHaveBeenCalledOnce(); - await getRunIdAndUrl(defaultOpts); + expect(apiFetchWorkflowRunIdsMock).toHaveBeenCalledOnce(); + expect(apiFetchWorkflowRunIdsMock.mock.lastCall?.[0]).toStrictEqual( + workflowId, + ); + expect(apiFetchWorkflowRunIdsMock.mock.lastCall?.[1]).toStrictEqual( + branch, + ); - // Behaviour - expect(apiRetryOrTimeoutMock).toHaveBeenCalledOnce(); - expect(apiFetchWorkflowRunJobStepsMock).toHaveBeenCalledOnce(); + // Logging + assertOnlyCalled(coreDebugLogMock); + expect(coreDebugLogMock).toHaveBeenCalledOnce(); + expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchSnapshot(); + }); - expect(apiFetchWorkflowRunIdsMock).toHaveBeenCalledOnce(); - expect(apiFetchWorkflowRunIdsMock.mock.lastCall?.[0]).toStrictEqual( - workflowId, - ); - expect(apiFetchWorkflowRunIdsMock.mock.lastCall?.[1]).toStrictEqual( - branch, - ); + it("should retry until an ID is found", async () => { + const runId = 0; + const runUrl = "test-url"; + apiRetryOrTimeoutMock + .mockResolvedValue({ + success: true, + value: [runId], + }) + .mockResolvedValueOnce({ success: true, value: [] }) + .mockResolvedValueOnce({ success: true, value: [] }); + apiFetchWorkflowRunJobStepsMock.mockResolvedValue([distinctId]); + apiFetchWorkflowRunUrlMock.mockResolvedValue(runUrl); + vi.spyOn(constants, "WORKFLOW_JOB_STEPS_RETRY_MS", "get").mockReturnValue( + 5000, + ); - // Logging - assertOnlyCalled(coreDebugLogMock); - expect(coreDebugLogMock).toHaveBeenCalledOnce(); - expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchSnapshot(); + const getRunIdAndUrlPromise = getRunIdAndUrl({ + ...defaultOpts, + workflowTimeoutMs: 60 * 60 * 1000, }); - it("should retry until an ID is found", async () => { - const runId = 0; - const runUrl = "test-url"; - apiRetryOrTimeoutMock - .mockResolvedValue({ - success: true, - value: [runId], - }) - .mockResolvedValueOnce({ success: true, value: [] }) - .mockResolvedValueOnce({ success: true, value: [] }); - apiFetchWorkflowRunJobStepsMock.mockResolvedValue([distinctId]); - apiFetchWorkflowRunUrlMock.mockResolvedValue(runUrl); - vi.spyOn( - constants, - "WORKFLOW_JOB_STEPS_RETRY_MS", - "get", - ).mockReturnValue(5000); - - const getRunIdAndUrlPromise = getRunIdAndUrl({ - ...defaultOpts, - workflowTimeoutMs: 60 * 60 * 1000, - }); + // First attempt + expect(apiRetryOrTimeoutMock).toHaveBeenCalledOnce(); + await vi.advanceTimersByTimeAsync(1); // deplete queue - // First attempt - expect(apiRetryOrTimeoutMock).toHaveBeenCalledOnce(); - await vi.advanceTimersByTimeAsync(1); // deplete queue + assertOnlyCalled(coreDebugLogMock, coreInfoLogMock); - assertOnlyCalled(coreDebugLogMock, coreInfoLogMock); + expect(coreInfoLogMock).toHaveBeenCalledOnce(); + expect(coreInfoLogMock.mock.calls[0]?.[0]).toMatchSnapshot(); - expect(coreInfoLogMock).toHaveBeenCalledOnce(); - expect(coreInfoLogMock.mock.calls[0]?.[0]).toMatchSnapshot(); + expect(utilSleepMock).toHaveBeenCalledOnce(); + expect(utilSleepMock).toHaveBeenCalledWith(5000); - expect(utilSleepMock).toHaveBeenCalledOnce(); - expect(utilSleepMock).toHaveBeenCalledWith(5000); + resetLogMocks(); + await vi.advanceTimersByTimeAsync(5000); - resetLogMocks(); - await vi.advanceTimersByTimeAsync(5000); + // Second attempt + expect(apiRetryOrTimeoutMock).toHaveBeenCalledTimes(2); - // Second attempt - expect(apiRetryOrTimeoutMock).toHaveBeenCalledTimes(2); + assertOnlyCalled(coreInfoLogMock); - assertOnlyCalled(coreInfoLogMock); + expect(coreInfoLogMock).toHaveBeenCalledOnce(); + expect(coreInfoLogMock.mock.calls[0]?.[0]).toMatchSnapshot(); - expect(coreInfoLogMock).toHaveBeenCalledOnce(); - expect(coreInfoLogMock.mock.calls[0]?.[0]).toMatchSnapshot(); + expect(utilSleepMock).toHaveBeenCalledTimes(2); + expect(utilSleepMock).toHaveBeenCalledWith(5000); - expect(utilSleepMock).toHaveBeenCalledTimes(2); - expect(utilSleepMock).toHaveBeenCalledWith(5000); + resetLogMocks(); + await vi.advanceTimersByTimeAsync(5000); - resetLogMocks(); - await vi.advanceTimersByTimeAsync(5000); + // Third attempt + expect(apiRetryOrTimeoutMock).toHaveBeenCalledTimes(3); + expect(apiFetchWorkflowRunJobStepsMock).toHaveBeenCalledOnce(); + expect(apiFetchWorkflowRunUrlMock).toHaveBeenCalledOnce(); - // Third attempt - expect(apiRetryOrTimeoutMock).toHaveBeenCalledTimes(3); - expect(apiFetchWorkflowRunJobStepsMock).toHaveBeenCalledOnce(); - expect(apiFetchWorkflowRunUrlMock).toHaveBeenCalledOnce(); + assertOnlyCalled(coreDebugLogMock); - assertOnlyCalled(coreDebugLogMock); + expect(coreDebugLogMock).toHaveBeenCalledOnce(); + expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchSnapshot(); - expect(coreDebugLogMock).toHaveBeenCalledOnce(); - expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchSnapshot(); + expect(utilSleepMock).toHaveBeenCalledTimes(2); + resetLogMocks(); - expect(utilSleepMock).toHaveBeenCalledTimes(2); - resetLogMocks(); + // Result + const run = await getRunIdAndUrlPromise; + if (!run.success) { + expect.fail("expected call to succeed"); + } + expect(run.value.id).toStrictEqual(runId); + expect(run.value.url).toStrictEqual(runUrl); + expect(apiRetryOrTimeoutMock).toHaveBeenCalledTimes(3); + expect(apiFetchWorkflowRunJobStepsMock).toHaveBeenCalledOnce(); + expect(apiFetchWorkflowRunIdsMock).not.toHaveBeenCalled(); + expect(apiFetchWorkflowRunUrlMock).toHaveBeenCalledOnce(); + assertNoneCalled(); + }); - // Result - const run = await getRunIdAndUrlPromise; - if (!run.success) { - expect.fail("expected call to succeed"); - } - expect(run.value.id).toStrictEqual(runId); - expect(run.value.url).toStrictEqual(runUrl); - expect(apiRetryOrTimeoutMock).toHaveBeenCalledTimes(3); - expect(apiFetchWorkflowRunJobStepsMock).toHaveBeenCalledOnce(); - expect(apiFetchWorkflowRunIdsMock).not.toHaveBeenCalled(); - expect(apiFetchWorkflowRunUrlMock).toHaveBeenCalledOnce(); - assertNoneCalled(); + it("should timeout when unable failing to get the run IDs", async () => { + apiRetryOrTimeoutMock.mockResolvedValue({ + success: false, + reason: "timeout", }); - it("should timeout when unable failing to get the run IDs", async () => { - apiRetryOrTimeoutMock.mockResolvedValue({ - success: false, - reason: "timeout", - }); + // Behaviour + const getRunIdAndUrlPromise = getRunIdAndUrl({ + ...defaultOpts, + }); + await vi.advanceTimersByTimeAsync(1000); - // Behaviour - const getRunIdAndUrlPromise = getRunIdAndUrl({ - ...defaultOpts, - }); - await vi.advanceTimersByTimeAsync(1000); + const run = await getRunIdAndUrlPromise; - const run = await getRunIdAndUrlPromise; + if (run.success) { + expect.fail("expected call to fail"); + } - if (run.success) { - expect.fail("expected call to fail"); - } + // Behaviour + expect(run.reason).toStrictEqual("timeout"); - // Behaviour - expect(run.reason).toStrictEqual("timeout"); + expect(apiRetryOrTimeoutMock).toHaveBeenCalledOnce(); + expect(apiFetchWorkflowRunJobStepsMock).not.toHaveBeenCalled(); + expect(apiFetchWorkflowRunIdsMock).not.toHaveBeenCalled(); + expect(apiFetchWorkflowRunUrlMock).not.toHaveBeenCalled(); + expect(utilSleepMock).not.toHaveBeenCalled(); - expect(apiRetryOrTimeoutMock).toHaveBeenCalledOnce(); - expect(apiFetchWorkflowRunJobStepsMock).not.toHaveBeenCalled(); - expect(apiFetchWorkflowRunIdsMock).not.toHaveBeenCalled(); - expect(apiFetchWorkflowRunUrlMock).not.toHaveBeenCalled(); - expect(utilSleepMock).not.toHaveBeenCalled(); + // Logging + assertOnlyCalled(coreDebugLogMock); + expect(coreDebugLogMock).toHaveBeenCalledOnce(); + expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatch( + /Timed out while attempting to fetch Workflow Run IDs, waited [0-9]+ms/, + ); + }); - // Logging - assertOnlyCalled(coreDebugLogMock); - expect(coreDebugLogMock).toHaveBeenCalledOnce(); - expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatch( - /Timed out while attempting to fetch Workflow Run IDs, waited [0-9]+ms/, - ); + it("should timeout when unable to find over time", async () => { + const runId = 0; + const runUrl = "test-url"; + apiRetryOrTimeoutMock.mockResolvedValue({ + success: true, + value: [runId], }); + apiFetchWorkflowRunJobStepsMock.mockResolvedValue([]); + apiFetchWorkflowRunUrlMock.mockResolvedValue(runUrl); + vi.spyOn(constants, "WORKFLOW_JOB_STEPS_RETRY_MS", "get").mockReturnValue( + 5000, + ); - it("should timeout when unable to find over time", async () => { - const runId = 0; - const runUrl = "test-url"; - apiRetryOrTimeoutMock.mockResolvedValue({ - success: true, - value: [runId], - }); - apiFetchWorkflowRunJobStepsMock.mockResolvedValue([]); - apiFetchWorkflowRunUrlMock.mockResolvedValue(runUrl); - vi.spyOn( - constants, - "WORKFLOW_JOB_STEPS_RETRY_MS", - "get", - ).mockReturnValue(5000); - - const getRunIdAndUrlPromise = getRunIdAndUrl({ - ...defaultOpts, - workflowTimeoutMs: 15 * 1000, - }); + const getRunIdAndUrlPromise = getRunIdAndUrl({ + ...defaultOpts, + workflowTimeoutMs: 15 * 1000, + }); - // First attempt - expect(apiRetryOrTimeoutMock).toHaveBeenCalledOnce(); - await vi.advanceTimersByTimeAsync(1); // deplete queue - expect(apiFetchWorkflowRunJobStepsMock).toHaveBeenCalledOnce(); - assertOnlyCalled(coreDebugLogMock, coreInfoLogMock); + // First attempt + expect(apiRetryOrTimeoutMock).toHaveBeenCalledOnce(); + await vi.advanceTimersByTimeAsync(1); // deplete queue + expect(apiFetchWorkflowRunJobStepsMock).toHaveBeenCalledOnce(); + assertOnlyCalled(coreDebugLogMock, coreInfoLogMock); - expect(coreInfoLogMock).toHaveBeenCalledOnce(); - expect(coreInfoLogMock.mock.calls[0]?.[0]).toMatchSnapshot(); + expect(coreInfoLogMock).toHaveBeenCalledOnce(); + expect(coreInfoLogMock.mock.calls[0]?.[0]).toMatchSnapshot(); - expect(coreDebugLogMock).toHaveBeenCalledOnce(); - expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchSnapshot(); + expect(coreDebugLogMock).toHaveBeenCalledOnce(); + expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchSnapshot(); - expect(utilSleepMock).toHaveBeenCalledOnce(); - expect(utilSleepMock).toHaveBeenCalledWith(5000); + expect(utilSleepMock).toHaveBeenCalledOnce(); + expect(utilSleepMock).toHaveBeenCalledWith(5000); - resetLogMocks(); - await vi.advanceTimersByTimeAsync(5000); + resetLogMocks(); + await vi.advanceTimersByTimeAsync(5000); - // Second attempt - expect(apiRetryOrTimeoutMock).toHaveBeenCalledTimes(2); - expect(apiFetchWorkflowRunJobStepsMock).toHaveBeenCalledTimes(2); - assertOnlyCalled(coreDebugLogMock, coreInfoLogMock); + // Second attempt + expect(apiRetryOrTimeoutMock).toHaveBeenCalledTimes(2); + expect(apiFetchWorkflowRunJobStepsMock).toHaveBeenCalledTimes(2); + assertOnlyCalled(coreDebugLogMock, coreInfoLogMock); - expect(coreInfoLogMock).toHaveBeenCalledOnce(); - expect(coreInfoLogMock.mock.calls[0]?.[0]).toMatchSnapshot(); + expect(coreInfoLogMock).toHaveBeenCalledOnce(); + expect(coreInfoLogMock.mock.calls[0]?.[0]).toMatchSnapshot(); - expect(coreDebugLogMock).toHaveBeenCalledOnce(); - expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchSnapshot(); + expect(coreDebugLogMock).toHaveBeenCalledOnce(); + expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchSnapshot(); - expect(utilSleepMock).toHaveBeenCalledTimes(2); - expect(utilSleepMock).toHaveBeenCalledWith(5000); + expect(utilSleepMock).toHaveBeenCalledTimes(2); + expect(utilSleepMock).toHaveBeenCalledWith(5000); - resetLogMocks(); - await vi.advanceTimersByTimeAsync(5000); + resetLogMocks(); + await vi.advanceTimersByTimeAsync(5000); - // Timeout attempt - expect(apiRetryOrTimeoutMock).toHaveBeenCalledTimes(3); - expect(apiFetchWorkflowRunJobStepsMock).toHaveBeenCalledTimes(3); - assertOnlyCalled(coreDebugLogMock, coreInfoLogMock); + // Timeout attempt + expect(apiRetryOrTimeoutMock).toHaveBeenCalledTimes(3); + expect(apiFetchWorkflowRunJobStepsMock).toHaveBeenCalledTimes(3); + assertOnlyCalled(coreDebugLogMock, coreInfoLogMock); - expect(coreInfoLogMock).toHaveBeenCalledOnce(); - expect(coreInfoLogMock.mock.calls[0]?.[0]).toMatchSnapshot(); + expect(coreInfoLogMock).toHaveBeenCalledOnce(); + expect(coreInfoLogMock.mock.calls[0]?.[0]).toMatchSnapshot(); - expect(coreDebugLogMock).toHaveBeenCalledOnce(); - expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchSnapshot(); + expect(coreDebugLogMock).toHaveBeenCalledOnce(); + expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchSnapshot(); - expect(utilSleepMock).toHaveBeenCalledTimes(3); - expect(utilSleepMock).toHaveBeenCalledWith(5000); + expect(utilSleepMock).toHaveBeenCalledTimes(3); + expect(utilSleepMock).toHaveBeenCalledWith(5000); - resetLogMocks(); - await vi.advanceTimersByTimeAsync(5000); + resetLogMocks(); + await vi.advanceTimersByTimeAsync(5000); - // Result - const run = await getRunIdAndUrlPromise; - if (run.success) { - expect.fail("expected call to fail"); - } - expect(run.reason).toStrictEqual("timeout"); - expect(apiRetryOrTimeoutMock).toHaveBeenCalledTimes(3); - expect(apiFetchWorkflowRunJobStepsMock).toHaveBeenCalledTimes(3); - expect(apiFetchWorkflowRunIdsMock).not.toHaveBeenCalled(); - expect(apiFetchWorkflowRunUrlMock).not.toHaveBeenCalled(); - assertNoneCalled(); - }); + // Result + const run = await getRunIdAndUrlPromise; + if (run.success) { + expect.fail("expected call to fail"); + } + expect(run.reason).toStrictEqual("timeout"); + expect(apiRetryOrTimeoutMock).toHaveBeenCalledTimes(3); + expect(apiFetchWorkflowRunJobStepsMock).toHaveBeenCalledTimes(3); + expect(apiFetchWorkflowRunIdsMock).not.toHaveBeenCalled(); + expect(apiFetchWorkflowRunUrlMock).not.toHaveBeenCalled(); + assertNoneCalled(); }); }); });