Skip to content

Commit 89afa8f

Browse files
committed
test: finish testing remaining behaviour
1 parent 6a4b2bb commit 89afa8f

File tree

2 files changed

+129
-7
lines changed

2 files changed

+129
-7
lines changed

src/__snapshots__/return-dispatch.spec.ts.snap

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,19 @@ exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when u
7575
"Attempting to identify run ID from steps...",
7676
]
7777
`;
78+
79+
exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable to find over time 1`] = `"Attempting to identify run ID from steps..."`;
80+
81+
exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable to find over time 2`] = `"Exhausted searching IDs in known runs, attempt 1..."`;
82+
83+
exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable to find over time 3`] = `"Attempting to identify Run ID for workflow.yml (123)"`;
84+
85+
exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable to find over time 4`] = `"Attempting to get step names for Run IDs: [0]"`;
86+
87+
exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable to find over time 5`] = `"Exhausted searching IDs in known runs, attempt 2..."`;
88+
89+
exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable to find over time 6`] = `"Attempting to get step names for Run IDs: [0]"`;
90+
91+
exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable to find over time 7`] = `"Exhausted searching IDs in known runs, attempt 3..."`;
92+
93+
exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable to find over time 8`] = `"Attempting to get step names for Run IDs: [0]"`;

src/return-dispatch.spec.ts

Lines changed: 113 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ import * as utils from "./utils.ts";
2929

3030
vi.mock("@actions/core");
3131
vi.mock("./api.ts");
32+
// vi.mock(import('./utils.ts'), async (importOriginal) => {
33+
// const mod = await importOriginal() // type is inferred
34+
// return {
35+
// ...mod,
36+
// // replace some exports
37+
// total: vi.fn(mod.sleep),
38+
// }
39+
// })
3240

3341
describe("return-dispatch", () => {
3442
const {
@@ -694,40 +702,48 @@ describe("return-dispatch", () => {
694702
// First attempt
695703
expect(apiRetryOrTimeoutMock).toHaveBeenCalledOnce();
696704
await vi.advanceTimersByTimeAsync(1); // deplete queue
705+
697706
assertOnlyCalled(coreDebugLogMock, coreInfoLogMock);
698707

699708
expect(coreInfoLogMock).toHaveBeenCalledTimes(2);
700709
expect(coreInfoLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
701710
expect(coreInfoLogMock.mock.calls[1]?.[0]).toMatchSnapshot();
702711

703-
expect(coreDebugLogMock).toHaveBeenCalledTimes(1);
712+
expect(coreDebugLogMock).toHaveBeenCalledOnce();
704713
expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
705-
resetLogMocks();
706714

707-
// Sleep should be called
708715
expect(utilSleepMock).toHaveBeenCalledOnce();
709716
expect(utilSleepMock).toHaveBeenCalledWith(5000);
717+
718+
resetLogMocks();
710719
await vi.advanceTimersByTimeAsync(5000);
711720

712721
// Second attempt
713722
expect(apiRetryOrTimeoutMock).toHaveBeenCalledTimes(2);
714-
await vi.advanceTimersByTimeAsync(1); // deplete queue
723+
715724
assertOnlyCalled(coreInfoLogMock);
716725

717726
expect(coreInfoLogMock).toHaveBeenCalledOnce();
718727
expect(coreInfoLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
728+
729+
expect(utilSleepMock).toHaveBeenCalledTimes(2);
730+
expect(utilSleepMock).toHaveBeenCalledWith(5000);
731+
719732
resetLogMocks();
720733
await vi.advanceTimersByTimeAsync(5000);
721734

722735
// Third attempt
723736
expect(apiRetryOrTimeoutMock).toHaveBeenCalledTimes(3);
724-
await vi.advanceTimersByTimeAsync(1); // deplete queue
737+
expect(apiFetchWorkflowRunJobStepsMock).toHaveBeenCalledOnce();
738+
expect(apiFetchWorkflowRunUrlMock).toHaveBeenCalledOnce();
739+
725740
assertOnlyCalled(coreDebugLogMock);
726741

727742
expect(coreDebugLogMock).toHaveBeenCalledOnce();
728743
expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
744+
745+
expect(utilSleepMock).toHaveBeenCalledTimes(2);
729746
resetLogMocks();
730-
await vi.advanceTimersByTimeAsync(5000);
731747

732748
// Result
733749
const run = await getRunIdAndUrlPromise;
@@ -736,6 +752,10 @@ describe("return-dispatch", () => {
736752
}
737753
expect(run.value.id).toStrictEqual(runId);
738754
expect(run.value.url).toStrictEqual(runUrl);
755+
expect(apiRetryOrTimeoutMock).toHaveBeenCalledTimes(3);
756+
expect(apiFetchWorkflowRunJobStepsMock).toHaveBeenCalledOnce();
757+
expect(apiFetchWorkflowRunIdsMock).not.toHaveBeenCalled();
758+
expect(apiFetchWorkflowRunUrlMock).toHaveBeenCalledOnce();
739759
assertNoneCalled();
740760
});
741761

@@ -763,6 +783,7 @@ describe("return-dispatch", () => {
763783
expect(apiRetryOrTimeoutMock).toHaveBeenCalledOnce();
764784
expect(apiFetchWorkflowRunJobStepsMock).not.toHaveBeenCalled();
765785
expect(apiFetchWorkflowRunIdsMock).not.toHaveBeenCalled();
786+
expect(apiFetchWorkflowRunUrlMock).not.toHaveBeenCalled();
766787
expect(utilSleepMock).not.toHaveBeenCalled();
767788

768789
// Logging
@@ -777,7 +798,92 @@ describe("return-dispatch", () => {
777798
expect(coreInfoLogMock.mock.calls[0]).toMatchSnapshot();
778799
});
779800

780-
it("should timeout when unable to find over time");
801+
it("should timeout when unable to find over time", async () => {
802+
const runId = 0;
803+
const runUrl = "test-url";
804+
apiRetryOrTimeoutMock.mockResolvedValue({
805+
success: true,
806+
value: [runId],
807+
});
808+
apiFetchWorkflowRunJobStepsMock.mockResolvedValue([]);
809+
apiFetchWorkflowRunUrlMock.mockResolvedValue(runUrl);
810+
vi.spyOn(
811+
constants,
812+
"WORKFLOW_JOB_STEPS_RETRY_MS",
813+
"get",
814+
).mockReturnValue(5000);
815+
816+
const getRunIdAndUrlPromise = getRunIdAndUrl({
817+
...defaultOpts,
818+
workflowTimeoutMs: 10 * 1000,
819+
});
820+
821+
// First attempt
822+
expect(apiRetryOrTimeoutMock).toHaveBeenCalledOnce();
823+
await vi.advanceTimersByTimeAsync(1); // deplete queue
824+
expect(apiFetchWorkflowRunJobStepsMock).toHaveBeenCalledOnce();
825+
assertOnlyCalled(coreDebugLogMock, coreInfoLogMock);
826+
827+
expect(coreInfoLogMock).toHaveBeenCalledTimes(2);
828+
expect(coreInfoLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
829+
expect(coreInfoLogMock.mock.calls[1]?.[0]).toMatchSnapshot();
830+
831+
expect(coreDebugLogMock).toHaveBeenCalledTimes(2);
832+
expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
833+
expect(coreDebugLogMock.mock.calls[1]?.[0]).toMatchSnapshot();
834+
835+
expect(utilSleepMock).toHaveBeenCalledOnce();
836+
expect(utilSleepMock).toHaveBeenCalledWith(5000);
837+
838+
resetLogMocks();
839+
await vi.advanceTimersByTimeAsync(5000);
840+
841+
// Second attempt
842+
expect(apiRetryOrTimeoutMock).toHaveBeenCalledTimes(2);
843+
expect(apiFetchWorkflowRunJobStepsMock).toHaveBeenCalledTimes(2);
844+
assertOnlyCalled(coreDebugLogMock, coreInfoLogMock);
845+
846+
expect(coreInfoLogMock).toHaveBeenCalledOnce();
847+
expect(coreInfoLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
848+
849+
expect(coreDebugLogMock).toHaveBeenCalledOnce();
850+
expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
851+
852+
expect(utilSleepMock).toHaveBeenCalledTimes(2);
853+
expect(utilSleepMock).toHaveBeenCalledWith(5000);
854+
855+
resetLogMocks();
856+
await vi.advanceTimersByTimeAsync(5000);
857+
858+
// Timeout attempt
859+
expect(apiRetryOrTimeoutMock).toHaveBeenCalledTimes(3);
860+
expect(apiFetchWorkflowRunJobStepsMock).toHaveBeenCalledTimes(3);
861+
assertOnlyCalled(coreDebugLogMock, coreInfoLogMock);
862+
863+
expect(coreInfoLogMock).toHaveBeenCalledOnce();
864+
expect(coreInfoLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
865+
866+
expect(coreDebugLogMock).toHaveBeenCalledOnce();
867+
expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
868+
869+
expect(utilSleepMock).toHaveBeenCalledTimes(3);
870+
expect(utilSleepMock).toHaveBeenCalledWith(5000);
871+
872+
resetLogMocks();
873+
await vi.advanceTimersByTimeAsync(5000);
874+
875+
// Result
876+
const run = await getRunIdAndUrlPromise;
877+
if (run.success) {
878+
expect.fail("expected call to fail");
879+
}
880+
expect(run.reason).toStrictEqual("timeout");
881+
expect(apiRetryOrTimeoutMock).toHaveBeenCalledTimes(3);
882+
expect(apiFetchWorkflowRunJobStepsMock).toHaveBeenCalledTimes(3);
883+
expect(apiFetchWorkflowRunIdsMock).not.toHaveBeenCalled();
884+
expect(apiFetchWorkflowRunUrlMock).not.toHaveBeenCalled();
885+
assertNoneCalled();
886+
});
781887
});
782888
});
783889
});

0 commit comments

Comments
 (0)