This repository has been archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathparams.test.ts
229 lines (219 loc) · 8.65 KB
/
params.test.ts
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import { Params } from "./params";
import { silence } from "./logging";
const fs = require('fs');
jest.mock('fs');
beforeAll(() => silence(true));
afterAll(() => silence(false));
describe("constructor", () => {
it("sets all params to default", () => {
const params = new Params();
expect(params.port).toBe(8000);
expect(params.executablePath).toBe("Puppeteer-bundled");
expect(params.maxPages).toBe(10);
expect(params.maxHits).toBe(300);
expect(params.maxAgeUnused).toBe(60);
expect(params.maxBrowsers).toBe(10);
expect(params.maxCacheSize).toBe(10);
expect(params.maxQueueSize).toBe(100);
expect(params.log).toBe("normal");
expect(params.timeout).toBe(7000);
});
});
describe("parse", () => {
it("creates a params with all to default", () => {
const env: NodeJS.ProcessEnv = {};
const params = new Params();
params.parse(env);
expect(params.port).toBe(8000);
expect(params.executablePath).toBe("Puppeteer-bundled");
expect(params.maxPages).toBe(10);
expect(params.maxHits).toBe(300);
expect(params.maxAgeUnused).toBe(60);
expect(params.maxBrowsers).toBe(10);
expect(params.maxCacheSize).toBe(10);
expect(params.maxQueueSize).toBe(100);
expect(params.log).toBe("normal");
expect(params.timeout).toBe(7000);
});
it("creates a params with all from environment", () => {
jest.resetAllMocks();
fs.existsSync.mockReturnValue(true);
const env: NodeJS.ProcessEnv = {
PORT: "8888",
CHROME_BIN: "/path",
MAX_PAGES: "100",
MAX_HITS: "3000",
MAX_AGE_UNUSED: "600",
MAX_BROWSERS: "100",
MAX_CACHE_SIZE: "100",
MAX_QUEUE_SIZE: "1000",
LOG: "json",
TIMEOUT: "8000",
};
const params = new Params();
params.parse(env);
expect(params.port).toBe(8888);
expect(params.executablePath).toBe("/path");
expect(params.maxPages).toBe(100);
expect(params.maxHits).toBe(3000);
expect(params.maxAgeUnused).toBe(600);
expect(params.maxBrowsers).toBe(100);
expect(params.maxCacheSize).toBe(100);
expect(params.maxQueueSize).toBe(1000);
expect(params.log).toBe("json");
expect(params.timeout).toBe(8000);
expect(fs.existsSync).toHaveBeenCalled();
});
it("raises an error", () => {
const env: NodeJS.ProcessEnv = {
PORT: "troll",
CHROME_BIN: "/",
MAX_PAGES: "100",
MAX_HITS: "3000",
MAX_AGE_UNUSED: "600",
MAX_BROWSERS: "100",
MAX_CACHE_SIZE: "100",
MAX_QUEUE_SIZE: "1000",
LOG: "json",
TIMEOUT: "8000",
};
const params = new Params();
const f = () => params.parse(env);
expect(f).toThrowError("Environment variable PORT 'troll' is not a number");
});
});
describe("getPort", () => {
it("returns default 0", () => {
const port = Params.getPort(undefined, 0);
expect(port).toBe(0);
});
it("throws an error when it's not a number", () => {
const f = () => Params.getPort("troll", 0)
expect(f).toThrowError("Environment variable PORT 'troll' is not a number");
});
it("throws an error when it's not an integer", () => {
const f = () => Params.getPort("1.2", 0)
expect(f).toThrowError("Environment variable PORT 1.2 is not an integer");
});
it("throws an error when it's not a positive integer", () => {
const f = () => Params.getPort("-5", 0)
expect(f).toThrowError("Environment variable PORT -5 must be positive");
});
it("returns reserved port when running as root", () => {
const port = Params.getPort("500", 0);
expect(port).toBe(500);
});
it("returns reserved port when running on Windows", () => {
const port = Params.getPort("500", -1);
expect(port).toBe(500);
});
it("throws an error when it's a reserved port and not running as root or on Windows", () => {
const f = () => Params.getPort("500", 1)
expect(f).toThrowError("Environment variable PORT 500 cannot be in the reserved system ports range (1 to 1023) when running without root");
});
it("throws an error when it's above 65535", () => {
const f = () => Params.getPort("65536", 0)
expect(f).toThrowError("Environment variable PORT 65536 cannot be higher than 65535");
});
it("returns port", () => {
const port = Params.getPort("8888", 0);
expect(port).toBe(8888);
});
});
describe("getExecutablePath", () => {
it("returns default ''", () => {
const path = Params.getExecutablePath(undefined);
expect(path).toBe("");
});
it("throws an error when the file does not exist", () => {
jest.resetAllMocks();
fs.existsSync.mockReturnValue(false);
const f = () => Params.getExecutablePath("/path")
expect(f).toThrowError("/path does not exist");
expect(fs.existsSync).toHaveBeenCalled();
});
it("returns the executable path", () => {
jest.resetAllMocks();
fs.existsSync.mockReturnValue(true);
const path = Params.getExecutablePath("/path")
expect(path).toBe("/path");
expect(fs.existsSync).toHaveBeenCalled();
});
});
describe("getMax", () => {
it("returns default 0", () => {
const n = Params.getMax(undefined, "X");
expect(n).toBe(0);
});
it("throws an error when it's not a number", () => {
const f = () => Params.getMax("troll", "X");
expect(f).toThrowError("Environment variable X 'troll' is not a number");
});
it("throws an error when it's not an integer", () => {
const f = () => Params.getMax("1.2", "X");
expect(f).toThrowError("Environment variable X 1.2 is not an integer");
});
it("returns Infinity on -1", () => {
const n = Params.getMax("-1", "X");
expect(n).toBe(Infinity);
});
it("throws an error when it's zero", () => {
const f = () => Params.getMax("0", "X");
expect(f).toThrowError("Environment variable X must be a positive integer or -1 (infinite)");
});
it("throws an error when it's negative and not -1", () => {
const f = () => Params.getMax("-2", "X");
expect(f).toThrowError("Environment variable X must be a positive integer or -1 (infinite)");
});
it("returns the number", () => {
const n = Params.getMax("15", "X");
expect(n).toBe(15);
});
});
describe("getLog", () => {
it("returns default", () => {
const log = Params.getLog(undefined);
expect(log).toBe("");
});
it("throws an error when it's not valid", () => {
const f = () => Params.getLog("troll")
expect(f).toThrowError("Environment variable LOG 'troll' is unrecognized");
});
it("returns the log", () => {
const log = Params.getLog("json");
expect(log).toBe("json");
});
});
describe("getTimeout", () => {
it("returns default 0", () => {
const port = Params.getTimeout(undefined);
expect(port).toBe(0);
});
it("returns Infinity for -1", () => {
const port = Params.getTimeout("-1");
expect(port).toBe(Infinity);
});
it("returns timeout", () => {
const port = Params.getTimeout("8888");
expect(port).toBe(8888);
});
it("throws an error when it's not a number", () => {
const f = () => Params.getTimeout("troll")
expect(f).toThrowError("Environment variable TIMEOUT 'troll' is not a number");
});
it("throws an error when it's not an integer", () => {
const f = () => Params.getTimeout("1.2")
expect(f).toThrowError("Environment variable TIMEOUT 1.2 is not an integer");
});
it("throws an error when it's not a positive integer and not -1", () => {
const f = () => Params.getTimeout("-5")
expect(f).toThrowError("Environment variable TIMEOUT -5 must be positive");
});
});
describe("toString", () => {
it("returns stringified params", () => {
const p = new Params();
const s = p.toString();
expect(s).toBe("{\"port\":8000,\"executablePath\":\"Puppeteer-bundled\",\"maxPages\":10,\"maxHits\":300,\"maxAgeUnused\":60,\"maxBrowsers\":10,\"maxCacheSize\":10,\"maxQueueSize\":100,\"log\":\"normal\",\"timeout\":7000}");
});
});