Skip to content

Commit c5e18d0

Browse files
committed
prog
1 parent 9fbf558 commit c5e18d0

File tree

3 files changed

+290
-2
lines changed

3 files changed

+290
-2
lines changed

src/main.spec.ts

Lines changed: 286 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,286 @@
1+
import * as core from "@actions/core";
2+
import {
3+
afterAll,
4+
afterEach,
5+
beforeEach,
6+
describe,
7+
expect,
8+
it,
9+
vi,
10+
type MockInstance,
11+
} from "vitest";
12+
13+
import * as action from "./action.ts";
14+
import * as api from "./api.ts";
15+
import * as awaitRemoteRun from "./await-remote-run.ts";
16+
import { main } from "./main.ts";
17+
import { mockLoggingFunctions } from "./test-utils/logging.mock.ts";
18+
import type { Result } from "./types.ts";
19+
// import * as utils from "./utils.ts";
20+
21+
vi.mock("@actions/core");
22+
vi.mock("./action.ts");
23+
vi.mock("./api.ts");
24+
vi.mock("./await-remote-run.ts");
25+
// vi.mock("./utils.ts");
26+
27+
describe("main", () => {
28+
const {
29+
coreDebugLogMock,
30+
coreErrorLogMock,
31+
coreInfoLogMock,
32+
assertOnlyCalled,
33+
} = mockLoggingFunctions();
34+
const testCfg: action.ActionConfig = {
35+
token: "secret",
36+
repo: "repository",
37+
owner: "owner",
38+
runId: 123456,
39+
runTimeoutSeconds: 300,
40+
pollIntervalMs: 2500,
41+
} satisfies Partial<action.ActionConfig> as action.ActionConfig;
42+
43+
// Core
44+
let coreSetFailedMock: MockInstance<typeof core.setFailed>;
45+
46+
// Action
47+
let actionGetConfigMock: MockInstance<typeof action.getConfig>;
48+
49+
// API
50+
let apiFetchWorkflowRunActiveJobUrlRetry: MockInstance<
51+
typeof api.fetchWorkflowRunActiveJobUrlRetry
52+
>;
53+
let apiInitMock: MockInstance<typeof api.init>;
54+
55+
// Utils
56+
// let utilsSleepMock: MockInstance<typeof utils.sleep>;
57+
58+
// Await Remote Run
59+
let awaitRemoteRunGetWorkflowRunStatusResult: MockInstance<
60+
typeof awaitRemoteRun.getWorkflowRunStatusResult
61+
>;
62+
let awaitRemoteRunGetWorkflowRunConclusionResult: MockInstance<
63+
typeof awaitRemoteRun.getWorkflowRunConclusionResult
64+
>;
65+
let awaitRemoteRunHandleActionFail: MockInstance<
66+
typeof awaitRemoteRun.handleActionFail
67+
>;
68+
let awaitRemoteRunGetWorkflowRunResult: MockInstance<
69+
typeof awaitRemoteRun.getWorkflowRunResult
70+
>;
71+
72+
afterAll(() => {
73+
vi.restoreAllMocks();
74+
});
75+
76+
beforeEach(() => {
77+
vi.useFakeTimers();
78+
79+
coreSetFailedMock = vi.spyOn(core, "setFailed");
80+
81+
actionGetConfigMock = vi
82+
.spyOn(action, "getConfig")
83+
.mockReturnValue(testCfg);
84+
85+
apiFetchWorkflowRunActiveJobUrlRetry = vi.spyOn(
86+
api,
87+
"fetchWorkflowRunActiveJobUrlRetry",
88+
);
89+
apiInitMock = vi.spyOn(api, "init");
90+
91+
// utilsGetBranchNameMock = vi.spyOn(utils, "getBranchName");
92+
// utilsLogInfoForBranchNameResult = vi.spyOn(
93+
// utils,
94+
// "logInfoForBranchNameResult",
95+
// );
96+
// utilsCreateDistinctIdRegexMock = vi.spyOn(utils, "createDistinctIdRegex");
97+
98+
awaitRemoteRunGetWorkflowRunStatusResult = vi.spyOn(
99+
awaitRemoteRun,
100+
"getWorkflowRunStatusResult",
101+
);
102+
awaitRemoteRunGetWorkflowRunConclusionResult = vi.spyOn(
103+
awaitRemoteRun,
104+
"getWorkflowRunConclusionResult",
105+
);
106+
awaitRemoteRunHandleActionFail = vi.spyOn(
107+
awaitRemoteRun,
108+
"handleActionFail",
109+
);
110+
awaitRemoteRunGetWorkflowRunResult = vi.spyOn(
111+
awaitRemoteRun,
112+
"getWorkflowRunResult",
113+
);
114+
});
115+
116+
afterEach(() => {
117+
vi.useRealTimers();
118+
vi.resetAllMocks();
119+
});
120+
121+
it("should successfully complete", async () => {
122+
// const distinctIdRegex = new RegExp(testCfg.distinctId);
123+
// const returnDispatchSuccessResult = {
124+
// success: true,
125+
// value: {
126+
// id: 0,
127+
// url: "test-url",
128+
// },
129+
// } as const;
130+
131+
// utilsCreateDistinctIdRegexMock.mockReturnValue(distinctIdRegex);
132+
// returnDispatchGetWorkflowIdMock.mockResolvedValue(0);
133+
// returnDispatchGetRunIdAndUrlMock.mockResolvedValue(
134+
// returnDispatchSuccessResult,
135+
// );
136+
const apiFetchWorkflowRunActiveJobUrlRetryResult: Result<string> = {
137+
success: true,
138+
value: "test-url",
139+
};
140+
apiFetchWorkflowRunActiveJobUrlRetry.mockResolvedValue(
141+
apiFetchWorkflowRunActiveJobUrlRetryResult,
142+
);
143+
144+
await main();
145+
146+
// Behaviour
147+
// Setup
148+
expect(actionGetConfigMock).toHaveBeenCalledOnce();
149+
expect(apiInitMock).toHaveBeenCalledOnce();
150+
expect(apiInitMock).toHaveBeenCalledWith(testCfg);
151+
152+
// Active Job URL
153+
expect(apiFetchWorkflowRunActiveJobUrlRetry).toHaveBeenCalledOnce();
154+
155+
// Workflow ID
156+
// expect(returnDispatchGetWorkflowIdMock).toHaveBeenCalledOnce();
157+
// expect(returnDispatchGetWorkflowIdMock).toHaveBeenCalledWith(
158+
// testCfg.workflow,
159+
// );
160+
161+
// Dispatch
162+
// expect(apiDispatchWorkflowMock).toHaveBeenCalledOnce();
163+
// expect(apiDispatchWorkflowMock).toHaveBeenCalledWith(testCfg.distinctId);
164+
165+
// Branch name
166+
// expect(utilsGetBranchNameMock).toHaveBeenCalledOnce();
167+
// expect(utilsGetBranchNameMock).toHaveBeenCalledWith(testCfg.ref);
168+
// expect(utilsLogInfoForBranchNameResult).toHaveBeenCalledOnce();
169+
// expect(utilsLogInfoForBranchNameResult).toHaveBeenCalledWith(
170+
// testBranch,
171+
// testCfg.ref,
172+
// );
173+
// expect(utilsCreateDistinctIdRegexMock).toHaveBeenCalledOnce();
174+
// expect(utilsCreateDistinctIdRegexMock).toHaveBeenCalledWith(
175+
// testCfg.distinctId,
176+
// );
177+
178+
// Get run ID
179+
// expect(returnDispatchGetRunIdAndUrlMock).toHaveBeenCalledOnce();
180+
// expect(returnDispatchGetRunIdAndUrlMock).toHaveBeenCalledWith({
181+
// startTime: Date.now(),
182+
// branch: testBranch,
183+
// distinctIdRegex: distinctIdRegex,
184+
// workflowId: 0,
185+
// workflowTimeoutMs: testCfg.workflowTimeoutSeconds * 1000,
186+
// workflowJobStepsRetryMs: testCfg.workflowJobStepsRetrySeconds * 1000,
187+
// });
188+
189+
// Result
190+
expect(coreSetFailedMock).not.toHaveBeenCalled();
191+
// expect(returnDispatchHandleFailMock).not.toHaveBeenCalled();
192+
// expect(returnDispatchHandleSuccessMock).toHaveBeenCalledOnce();
193+
// expect(returnDispatchHandleSuccessMock).toHaveBeenCalledWith(
194+
// returnDispatchSuccessResult.value.id,
195+
// returnDispatchSuccessResult.value.url,
196+
// );
197+
198+
// Logging
199+
assertOnlyCalled(coreInfoLogMock, coreDebugLogMock);
200+
expect(coreInfoLogMock).toHaveBeenCalledTimes(2);
201+
expect(coreInfoLogMock.mock.calls[0]?.[0]).toMatchInlineSnapshot(
202+
`"Attempt to extract branch name from ref..."`,
203+
);
204+
expect(coreInfoLogMock.mock.calls[1]?.[0]).toMatchInlineSnapshot(
205+
`"Attempting to identify run ID from steps..."`,
206+
);
207+
expect(coreDebugLogMock).toHaveBeenCalledTimes(2);
208+
expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchInlineSnapshot(
209+
`"Attempting to identify run ID for test-workflow (0)"`,
210+
);
211+
expect(coreDebugLogMock.mock.calls[1]?.[0]).toMatchInlineSnapshot(
212+
`"Completed (0ms)"`,
213+
);
214+
});
215+
216+
// it("should fail for an unhandled error", async () => {
217+
// const testError = new Error("test error");
218+
// actionGetConfigMock.mockImplementation(() => {
219+
// throw testError;
220+
// });
221+
222+
// await main();
223+
224+
// // Behaviour
225+
// expect(actionGetConfigMock).toHaveBeenCalledOnce();
226+
227+
// expect(apiInitMock).not.toHaveBeenCalled();
228+
// expect(returnDispatchGetWorkflowIdMock).not.toHaveBeenCalled();
229+
// expect(apiDispatchWorkflowMock).not.toHaveBeenCalled();
230+
// expect(utilsGetBranchNameMock).not.toHaveBeenCalled();
231+
// expect(utilsLogInfoForBranchNameResult).not.toHaveBeenCalled();
232+
// expect(returnDispatchGetRunIdAndUrlMock).not.toHaveBeenCalled();
233+
// expect(returnDispatchHandleFailMock).not.toHaveBeenCalled();
234+
// expect(returnDispatchHandleSuccessMock).not.toHaveBeenCalled();
235+
236+
// expect(coreSetFailedMock).toHaveBeenCalledOnce();
237+
// expect(coreSetFailedMock.mock.calls[0]?.[0]).toMatchInlineSnapshot(
238+
// `"Failed: An unhandled error has occurred: test error"`,
239+
// );
240+
241+
// // Logging
242+
// assertOnlyCalled(coreDebugLogMock, coreErrorLogMock);
243+
// expect(coreErrorLogMock).toHaveBeenCalledOnce();
244+
// expect(coreErrorLogMock.mock.calls[0]?.[0]).toMatchInlineSnapshot(
245+
// `"Failed: An unhandled error has occurred: test error"`,
246+
// );
247+
// expect(coreDebugLogMock).toHaveBeenCalledOnce();
248+
// expect(coreDebugLogMock.mock.calls[0]?.[0]).toStrictEqual(testError.stack);
249+
// });
250+
251+
// it("should fail for an unhandled unknown", async () => {
252+
// const testError = "some other error";
253+
// actionGetConfigMock.mockImplementation(() => {
254+
// // eslint-disable-next-line @typescript-eslint/only-throw-error
255+
// throw testError;
256+
// });
257+
258+
// await main();
259+
260+
// // Behaviour
261+
// expect(actionGetConfigMock).toHaveBeenCalledOnce();
262+
263+
// expect(apiInitMock).not.toHaveBeenCalled();
264+
// expect(returnDispatchGetWorkflowIdMock).not.toHaveBeenCalled();
265+
// expect(apiDispatchWorkflowMock).not.toHaveBeenCalled();
266+
// expect(utilsGetBranchNameMock).not.toHaveBeenCalled();
267+
// expect(utilsLogInfoForBranchNameResult).not.toHaveBeenCalled();
268+
// expect(returnDispatchGetRunIdAndUrlMock).not.toHaveBeenCalled();
269+
// expect(returnDispatchHandleFailMock).not.toHaveBeenCalled();
270+
// expect(returnDispatchHandleSuccessMock).not.toHaveBeenCalled();
271+
272+
// expect(coreSetFailedMock).toHaveBeenCalledOnce();
273+
// expect(coreSetFailedMock.mock.calls[0]?.[0]).toMatchInlineSnapshot(
274+
// `"Failed: An unknown error has occurred: some other error"`,
275+
// );
276+
277+
// // Logging
278+
// assertOnlyCalled(coreDebugLogMock, coreErrorLogMock);
279+
// expect(coreErrorLogMock).toHaveBeenCalledOnce();
280+
// expect(coreErrorLogMock.mock.calls[0]?.[0]).toMatchInlineSnapshot(
281+
// `"Failed: An unknown error has occurred: some other error"`,
282+
// );
283+
// expect(coreDebugLogMock).toHaveBeenCalledOnce();
284+
// expect(coreDebugLogMock.mock.calls[0]?.[0]).toStrictEqual(testError);
285+
// });
286+
});

src/main.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ import { getWorkflowRunResult, handleActionFail } from "./await-remote-run.ts";
66
import * as constants from "./constants.ts";
77
import { WorkflowRunConclusion } from "./types.ts";
88

9-
async function main(): Promise<void> {
9+
export async function main(): Promise<void> {
1010
try {
1111
const startTime = Date.now();
1212

1313
const config = getConfig();
1414
api.init(config);
1515

16+
// Attempt to get the active job URL
1617
const activeJobUrlResult = await api.fetchWorkflowRunActiveJobUrlRetry(
1718
config.runId,
1819
constants.WORKFLOW_RUN_ACTIVE_JOB_TIMEOUT_MS,
@@ -29,6 +30,7 @@ async function main(): Promise<void> {
2930
` URL: ${activeJobUrlResult.value}`,
3031
);
3132

33+
// Await the result
3234
const runResult = await getWorkflowRunResult({
3335
startTime,
3436
pollIntervalMs: config.pollIntervalMs,

src/test-utils/logging.mock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function mockLoggingFunctions(): MockedLoggingFunctions {
2323
const coreInfoLogMock: MockInstance<typeof core.info> = vi
2424
.spyOn(core, "info")
2525
.mockImplementation(() => undefined);
26-
const coreWarningLogMock: MockInstance<typeof core.error> = vi.spyOn(
26+
const coreWarningLogMock: MockInstance<typeof core.warning> = vi.spyOn(
2727
core,
2828
"warning",
2929
);

0 commit comments

Comments
 (0)