forked from Lilly-Protocol/lily-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.test.ts
More file actions
78 lines (64 loc) · 2.33 KB
/
Copy pathenv.test.ts
File metadata and controls
78 lines (64 loc) · 2.33 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
import { afterEach, describe, expect, it, vi } from "vitest";
const resetEnv = () => {
vi.resetModules();
vi.unstubAllEnvs();
};
describe("env schema", () => {
afterEach(() => {
resetEnv();
});
it("applies defaults for PORT, APP_NAME, and API_PREFIX when unset", async () => {
vi.stubEnv("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("coerces PORT string to number within valid range", async () => {
vi.stubEnv("NODE_ENV", "test");
vi.stubEnv("PORT", "8080");
const { env } = await import("../src/config/env");
expect(env.PORT).toBe(8080);
expect(typeof env.PORT).toBe("number");
});
it("rejects invalid NODE_ENV values", async () => {
vi.stubEnv("NODE_ENV", "staging");
await expect(() => import("../src/config/env")).rejects.toThrow(
/Invalid environment configuration/,
);
});
it("rejects invalid LOG_LEVEL values", async () => {
vi.stubEnv("NODE_ENV", "test");
vi.stubEnv("LOG_LEVEL", "verbose");
await expect(() => import("../src/config/env")).rejects.toThrow(
/Invalid environment configuration/,
);
});
it("transforms TRUST_PROXY numeric hop count string to a number", async () => {
vi.stubEnv("NODE_ENV", "test");
vi.stubEnv("TRUST_PROXY", "1");
const { env } = await import("../src/config/env");
expect(env.TRUST_PROXY).toBe(1);
expect(typeof env.TRUST_PROXY).toBe("number");
});
it("transforms TRUST_PROXY string 'false' to boolean false", async () => {
vi.stubEnv("NODE_ENV", "test");
vi.stubEnv("TRUST_PROXY", "false");
const { env } = await import("../src/config/env");
expect(env.TRUST_PROXY).toBe(false);
});
it("rejects unsafe TRUST_PROXY value 'true'", async () => {
vi.stubEnv("NODE_ENV", "test");
vi.stubEnv("TRUST_PROXY", "true");
await expect(() => import("../src/config/env")).rejects.toThrow(
/Invalid environment configuration/,
);
});
it("validates RATE_LIMIT_MAX_REQUESTS as positive integer", async () => {
vi.stubEnv("NODE_ENV", "test");
vi.stubEnv("RATE_LIMIT_MAX_REQUESTS", "0");
await expect(() => import("../src/config/env")).rejects.toThrow(
/Invalid environment configuration/,
);
});
});