-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathaction.spec.ts
More file actions
150 lines (123 loc) · 5.04 KB
/
action.spec.ts
File metadata and controls
150 lines (123 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { randomUUID } from "node:crypto";
import * as core from "@actions/core";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { type ActionConfig, getConfig } from "./action.ts";
vi.mock("node:crypto", () => ({
randomUUID: vi.fn(),
}));
vi.mock("@actions/core");
describe("Action", () => {
const workflowInputs = {
cake: "delicious",
};
describe("getConfig", () => {
// Represent the process.env inputs.
let mockEnvConfig: any;
beforeEach(() => {
mockEnvConfig = {
token: "secret",
ref: "feature_branch",
repo: "repository",
owner: "owner",
workflow: "workflow_name",
workflow_inputs: JSON.stringify(workflowInputs),
workflow_timeout_seconds: "60",
workflow_job_steps_retry_seconds: "3",
distinct_id: "distinct_id",
};
vi.spyOn(core, "getInput").mockImplementation((input: string): string => {
/* eslint-disable @typescript-eslint/no-unsafe-return */
switch (input) {
case "token":
return mockEnvConfig.token;
case "ref":
return mockEnvConfig.ref;
case "repo":
return mockEnvConfig.repo;
case "owner":
return mockEnvConfig.owner;
case "workflow":
return mockEnvConfig.workflow;
case "workflow_inputs":
return mockEnvConfig.workflow_inputs;
case "workflow_timeout_seconds":
return mockEnvConfig.workflow_timeout_seconds;
case "workflow_job_steps_retry_seconds":
return mockEnvConfig.workflow_job_steps_retry_seconds;
case "distinct_id":
return mockEnvConfig.distinct_id;
default:
throw new Error("invalid input requested");
}
/* eslint-enable @typescript-eslint/no-unsafe-return */
});
});
afterEach(() => {
vi.restoreAllMocks();
});
it("should return a valid config", () => {
const config: ActionConfig = getConfig();
// Assert that the numbers / types have been properly loaded.
expect(config.token).toStrictEqual("secret");
expect(config.ref).toStrictEqual("feature_branch");
expect(config.repo).toStrictEqual("repository");
expect(config.owner).toStrictEqual("owner");
expect(config.workflow).toStrictEqual("workflow_name");
expect(config.workflowInputs).toStrictEqual(workflowInputs);
expect(config.workflowTimeoutSeconds).toStrictEqual(60);
expect(config.workflowJobStepsRetrySeconds).toStrictEqual(3);
expect(config.distinctId).toStrictEqual("distinct_id");
});
it("should have a number for a workflow when given a workflow ID", () => {
mockEnvConfig.workflow = "123456";
const config: ActionConfig = getConfig();
expect(config.workflow).toStrictEqual(123456);
});
it("should provide a default workflow timeout if none is supplied", () => {
mockEnvConfig.workflow_timeout_seconds = "";
const config: ActionConfig = getConfig();
expect(config.workflowTimeoutSeconds).toStrictEqual(300);
});
it("should provide a default workflow job step retry if none is supplied", () => {
mockEnvConfig.workflow_job_steps_retry_seconds = "";
const config: ActionConfig = getConfig();
expect(config.workflowJobStepsRetrySeconds).toStrictEqual(5);
});
it("should handle no inputs being provided", () => {
mockEnvConfig.workflow_inputs = "";
const config: ActionConfig = getConfig();
expect(config.workflowInputs).toBeUndefined();
});
it("should throw if invalid workflow inputs JSON is provided", () => {
mockEnvConfig.workflow_inputs = "{";
expect(() => getConfig()).toThrow(Error);
});
it("should handle workflow inputs JSON containing strings numbers or booleans", () => {
mockEnvConfig.workflow_inputs =
'{"cake":"delicious","pie":9001,"parfait":false}';
expect(() => getConfig()).not.toThrow(Error);
});
it("should throw if a workflow inputs JSON doesn't contain strings numbers or booleans", () => {
const debugMock = vi
.spyOn(core, "debug")
.mockImplementation(() => undefined);
const callAndAssert = (input: string, errorMsg: string) => {
debugMock.mockClear();
mockEnvConfig.workflow_inputs = input;
expect(() => getConfig()).toThrow(errorMsg);
expect(debugMock).toHaveBeenCalledOnce();
};
callAndAssert('{"pie":{"powerLevel":9001}}', '"pie" value is object');
callAndAssert('{"vegetable":null}', '"vegetable" value is null');
callAndAssert('{"fruit":[]}', '"fruit" value is Array');
});
it("should handle no distinct_id being provided", () => {
const v4Mock = vi.mocked(randomUUID);
v4Mock.mockImplementationOnce(() => "test-mocked-uuid-is-used");
mockEnvConfig.distinct_id = "";
const config: ActionConfig = getConfig();
expect(config.distinctId).toStrictEqual("test-mocked-uuid-is-used");
expect(v4Mock).toHaveBeenCalledOnce();
});
});
});