-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathenv.spec.ts
More file actions
285 lines (251 loc) · 9.96 KB
/
env.spec.ts
File metadata and controls
285 lines (251 loc) · 9.96 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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import { mkdtempSync, rmdirSync } from 'node:fs';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
// Mock debug utilities
vi.mock('../utils/debug.js', () => ({
logWarn: vi.fn(),
}));
// Mock clack prompts
vi.mock('../utils/clack.js', () => ({
default: {
log: {
success: vi.fn(),
error: vi.fn(),
info: vi.fn(),
step: vi.fn(),
warn: vi.fn(),
},
text: vi.fn(),
select: vi.fn(),
password: vi.fn(),
isCancel: vi.fn(() => false),
},
}));
let testDir: string;
vi.mock('node:os', async (importOriginal) => {
const original = await importOriginal<typeof import('node:os')>();
return {
...original,
default: {
...original,
homedir: () => testDir,
},
homedir: () => testDir,
};
});
const { getConfig, setInsecureConfigStorage, clearConfig } = await import('../lib/config-store.js');
const { runEnvAdd, runEnvRemove, runEnvSwitch, runEnvList } = await import('./env.js');
const { setOutputMode } = await import('../utils/output.js');
const clack = (await import('../utils/clack.js')).default;
// Spy on process.exit
vi.spyOn(process, 'exit').mockImplementation((() => {
throw new Error('process.exit called');
}) as any);
describe('env commands', () => {
beforeEach(() => {
testDir = mkdtempSync(join(tmpdir(), 'env-cmd-test-'));
setInsecureConfigStorage(true);
vi.clearAllMocks();
});
afterEach(() => {
clearConfig();
try {
rmdirSync(join(testDir, '.workos'), { recursive: true });
} catch {}
try {
rmdirSync(testDir);
} catch {}
});
describe('runEnvAdd (non-interactive)', () => {
it('adds an environment with provided args', async () => {
await runEnvAdd({ name: 'prod', apiKey: 'sk_live_abc123' });
const config = getConfig();
expect(config?.environments.prod).toBeDefined();
expect(config?.environments.prod.apiKey).toBe('sk_live_abc123');
expect(config?.environments.prod.type).toBe('production');
});
it('detects sandbox type from sk_test_ prefix', async () => {
await runEnvAdd({ name: 'sandbox', apiKey: 'sk_test_abc123' });
const config = getConfig();
expect(config?.environments.sandbox.type).toBe('sandbox');
});
it('stores endpoint when provided', async () => {
await runEnvAdd({ name: 'local', apiKey: 'sk_test_abc', endpoint: 'http://localhost:8001' });
const config = getConfig();
expect(config?.environments.local.endpoint).toBe('http://localhost:8001');
});
it('auto-sets active environment on first add', async () => {
await runEnvAdd({ name: 'prod', apiKey: 'sk_live_abc' });
const config = getConfig();
expect(config?.activeEnvironment).toBe('prod');
});
it('does not change active environment on subsequent adds', async () => {
await runEnvAdd({ name: 'prod', apiKey: 'sk_live_abc' });
await runEnvAdd({ name: 'sandbox', apiKey: 'sk_test_abc' });
const config = getConfig();
expect(config?.activeEnvironment).toBe('prod');
});
it('rejects invalid environment name', async () => {
await expect(runEnvAdd({ name: 'INVALID NAME', apiKey: 'sk_test' })).rejects.toThrow('process.exit');
});
});
describe('runEnvRemove', () => {
it('removes an existing environment', async () => {
await runEnvAdd({ name: 'prod', apiKey: 'sk_live_abc' });
await runEnvRemove('prod');
const config = getConfig();
expect(config?.environments.prod).toBeUndefined();
});
it('switches active env when removing the active one', async () => {
await runEnvAdd({ name: 'prod', apiKey: 'sk_live_abc' });
await runEnvAdd({ name: 'sandbox', apiKey: 'sk_test_abc' });
// prod is active (first added)
await runEnvRemove('prod');
const config = getConfig();
expect(config?.activeEnvironment).toBe('sandbox');
});
it('errors for non-existent environment', async () => {
await runEnvAdd({ name: 'prod', apiKey: 'sk_live_abc' });
await expect(runEnvRemove('missing')).rejects.toThrow('process.exit');
});
it('errors when no environments configured', async () => {
await expect(runEnvRemove('anything')).rejects.toThrow('process.exit');
});
});
describe('runEnvSwitch', () => {
it('switches to a named environment', async () => {
await runEnvAdd({ name: 'prod', apiKey: 'sk_live_abc' });
await runEnvAdd({ name: 'sandbox', apiKey: 'sk_test_abc' });
await runEnvSwitch('sandbox');
const config = getConfig();
expect(config?.activeEnvironment).toBe('sandbox');
});
it('errors for non-existent environment', async () => {
await runEnvAdd({ name: 'prod', apiKey: 'sk_live_abc' });
await expect(runEnvSwitch('missing')).rejects.toThrow('process.exit');
});
it('errors when no environments configured', async () => {
await expect(runEnvSwitch('anything')).rejects.toThrow('process.exit');
});
it('warns when WORKOS_API_KEY env var is set', async () => {
const original = process.env.WORKOS_API_KEY;
process.env.WORKOS_API_KEY = 'sk_test_override';
const stderrOutput: string[] = [];
vi.spyOn(console, 'error').mockImplementation((...args: unknown[]) => {
stderrOutput.push(args.map(String).join(' '));
});
try {
await runEnvAdd({ name: 'prod', apiKey: 'sk_live_abc' });
await runEnvAdd({ name: 'sandbox', apiKey: 'sk_test_abc' });
await runEnvSwitch('sandbox');
expect(stderrOutput.some((s) => s.includes('WORKOS_API_KEY'))).toBe(true);
} finally {
if (original === undefined) delete process.env.WORKOS_API_KEY;
else process.env.WORKOS_API_KEY = original;
}
});
it('does not warn when WORKOS_API_KEY env var is not set', async () => {
const original = process.env.WORKOS_API_KEY;
delete process.env.WORKOS_API_KEY;
const stderrOutput: string[] = [];
vi.spyOn(console, 'error').mockImplementation((...args: unknown[]) => {
stderrOutput.push(args.map(String).join(' '));
});
try {
await runEnvAdd({ name: 'prod', apiKey: 'sk_live_abc' });
await runEnvAdd({ name: 'sandbox', apiKey: 'sk_test_abc' });
await runEnvSwitch('sandbox');
expect(stderrOutput).toHaveLength(0);
} finally {
if (original === undefined) delete process.env.WORKOS_API_KEY;
else process.env.WORKOS_API_KEY = original;
}
});
});
describe('runEnvList', () => {
it('shows info message when no environments', async () => {
await runEnvList();
expect(clack.log.info).toHaveBeenCalledWith(expect.stringContaining('No environments configured'));
});
it('does not throw when environments exist', async () => {
await runEnvAdd({ name: 'prod', apiKey: 'sk_live_abc' });
await expect(runEnvList()).resolves.not.toThrow();
});
});
describe('JSON output mode', () => {
let consoleOutput: string[];
beforeEach(() => {
setOutputMode('json');
consoleOutput = [];
vi.spyOn(console, 'log').mockImplementation((...args: unknown[]) => {
consoleOutput.push(args.map(String).join(' '));
});
});
afterEach(() => {
setOutputMode('human');
});
it('runEnvAdd outputs JSON success', async () => {
await runEnvAdd({ name: 'prod', apiKey: 'sk_live_abc' });
const output = JSON.parse(consoleOutput[0]);
expect(output.status).toBe('ok');
expect(output.message).toBe('Environment added');
expect(output.data.name).toBe('prod');
expect(output.data.type).toBe('production');
expect(output.data.active).toBe(true);
});
it('runEnvRemove outputs JSON success', async () => {
await runEnvAdd({ name: 'prod', apiKey: 'sk_live_abc' });
consoleOutput = [];
await runEnvRemove('prod');
const output = JSON.parse(consoleOutput[0]);
expect(output.status).toBe('ok');
expect(output.message).toBe('Environment removed');
expect(output.data.name).toBe('prod');
});
it('runEnvSwitch outputs JSON success', async () => {
await runEnvAdd({ name: 'prod', apiKey: 'sk_live_abc' });
await runEnvAdd({ name: 'sandbox', apiKey: 'sk_test_abc' });
consoleOutput = [];
await runEnvSwitch('sandbox');
const output = JSON.parse(consoleOutput[0]);
expect(output.status).toBe('ok');
expect(output.message).toBe('Switched environment');
expect(output.data.name).toBe('sandbox');
});
it('runEnvSwitch includes warnings in JSON when WORKOS_API_KEY is set', async () => {
const original = process.env.WORKOS_API_KEY;
process.env.WORKOS_API_KEY = 'sk_test_override';
try {
await runEnvAdd({ name: 'prod', apiKey: 'sk_live_abc' });
await runEnvAdd({ name: 'sandbox', apiKey: 'sk_test_abc' });
consoleOutput = [];
await runEnvSwitch('sandbox');
const output = JSON.parse(consoleOutput[0]);
expect(output.status).toBe('ok');
expect(output.warnings).toHaveLength(1);
expect(output.warnings[0].code).toBe('env_var_override');
} finally {
if (original === undefined) delete process.env.WORKOS_API_KEY;
else process.env.WORKOS_API_KEY = original;
}
});
it('runEnvList outputs JSON with data array', async () => {
await runEnvAdd({ name: 'prod', apiKey: 'sk_live_abc' });
await runEnvAdd({ name: 'sandbox', apiKey: 'sk_test_abc' });
consoleOutput = [];
await runEnvList();
const output = JSON.parse(consoleOutput[0]);
expect(output.data).toHaveLength(2);
expect(output.data[0].name).toBe('prod');
expect(output.data[0].active).toBe(true);
expect(output.data[1].name).toBe('sandbox');
expect(output.data[1].active).toBe(false);
});
it('runEnvList outputs empty data array when no environments', async () => {
await runEnvList();
const output = JSON.parse(consoleOutput[0]);
expect(output.data).toEqual([]);
});
});
});