forked from Lilly-Protocol/lily-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv-schema.test.ts
More file actions
98 lines (77 loc) · 2.89 KB
/
Copy pathenv-schema.test.ts
File metadata and controls
98 lines (77 loc) · 2.89 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
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
describe("Env schema parser (issue #137)", () => {
const originalEnv = process.env;
beforeEach(() => {
vi.resetModules();
process.env = { ...originalEnv };
});
afterEach(() => {
process.env = originalEnv;
vi.restoreAllMocks();
});
it("should apply defaults for PORT, APP_NAME, and API_PREFIX", async () => {
delete process.env.PORT;
delete process.env.APP_NAME;
delete process.env.API_PREFIX;
process.env.NODE_ENV = "test";
const { env } = await import("../src/config/env");
expect(env.PORT).toBe(4000);
expect(env.APP_NAME).toBe("Lily Backend");
expect(env.API_PREFIX).toBe("/api/v1");
});
it("should coerce PORT string to number", async () => {
process.env.PORT = "8080";
process.env.NODE_ENV = "test";
const { env } = await import("../src/config/env");
expect(env.PORT).toBe(8080);
expect(typeof env.PORT).toBe("number");
});
it("should reject invalid NODE_ENV values", async () => {
process.env.NODE_ENV = "staging";
await expect(import("../src/config/env")).rejects.toThrow(
"Invalid environment configuration",
);
});
it("should transform numeric hop count string to a number", async () => {
process.env.TRUST_PROXY = "1";
process.env.NODE_ENV = "test";
const { env } = await import("../src/config/env");
expect(env.TRUST_PROXY).toBe(1);
expect(typeof env.TRUST_PROXY).toBe("number");
});
it("should transform TRUST_PROXY string 'false' to boolean false", async () => {
process.env.TRUST_PROXY = "false";
process.env.NODE_ENV = "test";
const { env } = await import("../src/config/env");
expect(env.TRUST_PROXY).toBe(false);
expect(typeof env.TRUST_PROXY).toBe("boolean");
});
it("should reject unsafe TRUST_PROXY string 'true'", async () => {
process.env.TRUST_PROXY = "true";
process.env.NODE_ENV = "test";
await expect(import("../src/config/env")).rejects.toThrow(
"Invalid environment configuration",
);
});
it("should default TRUST_PROXY to false", async () => {
delete process.env.TRUST_PROXY;
process.env.NODE_ENV = "test";
const { env } = await import("../src/config/env");
expect(env.TRUST_PROXY).toBe(false);
});
it("should parse rate limit values as positive integers", async () => {
process.env.RATE_LIMIT_WINDOW_MS = "60000";
process.env.RATE_LIMIT_MAX_REQUESTS = "50";
process.env.NODE_ENV = "test";
const { env } = await import("../src/config/env");
expect(env.RATE_LIMIT_WINDOW_MS).toBe(60000);
expect(env.RATE_LIMIT_MAX_REQUESTS).toBe(50);
});
it("should reject non-positive rate limit values", async () => {
process.env.RATE_LIMIT_MAX_REQUESTS = "0";
process.env.NODE_ENV = "test";
await expect(import("../src/config/env")).rejects.toThrow(
"Invalid environment configuration",
);
});
});