-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathenv.test.ts
More file actions
82 lines (72 loc) · 2.45 KB
/
Copy pathenv.test.ts
File metadata and controls
82 lines (72 loc) · 2.45 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
import { describe, expect, it } from "vitest";
import {
DEFAULT_INTELLIGENCE_API_URL,
DEFAULT_INTELLIGENCE_CHANNEL_NAME,
DEFAULT_INTELLIGENCE_GATEWAY_WS_URL,
parsePort,
readEnvironment,
} from "./env.js";
const requiredEnvironment = {
AGENT_URL: "http://localhost:8123/",
INTELLIGENCE_API_KEY: "cpk_test",
};
describe("readEnvironment", () => {
it("requires AGENT_URL", () => {
expect(() =>
readEnvironment({ INTELLIGENCE_API_KEY: "cpk_test" }),
).toThrow("Missing required env var: AGENT_URL");
});
it("requires INTELLIGENCE_API_KEY", () => {
expect(() =>
readEnvironment({ AGENT_URL: "http://localhost:8123/" }),
).toThrow("Missing required env var: INTELLIGENCE_API_KEY");
});
it("uses the Intelligence, channel-name, and port defaults", () => {
expect(readEnvironment(requiredEnvironment)).toMatchObject({
agentUrl: "http://localhost:8123/",
intelligenceApiKey: "cpk_test",
intelligenceApiUrl: DEFAULT_INTELLIGENCE_API_URL,
intelligenceGatewayWsUrl: DEFAULT_INTELLIGENCE_GATEWAY_WS_URL,
channelName: DEFAULT_INTELLIGENCE_CHANNEL_NAME,
port: 3000,
});
});
it("honors Intelligence URL and channel-name overrides", () => {
expect(
readEnvironment({
...requiredEnvironment,
INTELLIGENCE_API_URL: "https://intelligence.example.test",
INTELLIGENCE_GATEWAY_WS_URL: "wss://realtime.example.test",
INTELLIGENCE_CHANNEL_NAME: "custom-channel",
}),
).toMatchObject({
intelligenceApiUrl: "https://intelligence.example.test",
intelligenceGatewayWsUrl: "wss://realtime.example.test",
channelName: "custom-channel",
});
});
it("does not expose platform credentials owned by Intelligence", () => {
const environment = readEnvironment({
...requiredEnvironment,
SLACK_BOT_TOKEN: "xoxb-unused",
TEAMS_CLIENT_ID: "teams-unused",
});
expect(environment).not.toHaveProperty("slackBotToken");
expect(environment).not.toHaveProperty("teamsClientId");
expect(environment).not.toHaveProperty("teamsPort");
});
});
describe("parsePort", () => {
it("defaults to 3000", () => {
expect(parsePort(undefined)).toBe(3000);
});
it("accepts a valid integer port", () => {
expect(parsePort("4242")).toBe(4242);
});
it.each(["", "0", "65536", "12.5", "abc"])(
"rejects invalid PORT %j",
(raw) => {
expect(() => parsePort(raw)).toThrow(`Invalid PORT: "${raw}"`);
},
);
});