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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
- import { Command } from 'commander';
- import { authCommand } from '../../src/commands/auth';
- import { fetchWithProxy } from '../../src/fetch';
- import { getUserEmail, setUserEmail } from '../../src/globalConfig/accounts';
- import { cloudConfig } from '../../src/globalConfig/cloud';
- import logger from '../../src/logger';
- import telemetry from '../../src/telemetry';
- import { createMockResponse } from '../util/utils';
- const mockCloudUser = {
- id: '1',
- email: 'test@example.com',
- name: 'Test User',
- createdAt: new Date(),
- updatedAt: new Date(),
- };
- const mockOrganization = {
- id: '1',
- name: 'Test Org',
- createdAt: new Date(),
- updatedAt: new Date(),
- };
- const mockApp = {
- id: '1',
- name: 'Test App',
- createdAt: new Date(),
- updatedAt: new Date(),
- url: 'https://app.example.com',
- };
- jest.mock('../../src/globalConfig/accounts');
- jest.mock('../../src/globalConfig/cloud');
- jest.mock('../../src/logger');
- jest.mock('../../src/telemetry');
- jest.mock('../../src/fetch');
- const mockFetch = jest.fn();
- global.fetch = mockFetch;
- describe('auth command', () => {
- let program: Command;
- beforeEach(() => {
- jest.clearAllMocks();
- jest.resetAllMocks();
- program = new Command();
- process.exitCode = undefined;
- authCommand(program);
- // Set up a basic mock that just returns the expected data
- jest.mocked(cloudConfig.validateAndSetApiToken).mockResolvedValue({
- user: mockCloudUser,
- organization: mockOrganization,
- app: mockApp,
- });
- jest.spyOn(telemetry as any, 'record').mockImplementation(() => {});
- });
- describe('login', () => {
- it('should set email in config after successful login with API key', async () => {
- mockFetch.mockResolvedValueOnce(
- createMockResponse({
- ok: true,
- body: { user: mockCloudUser },
- }),
- );
- const loginCmd = program.commands
- .find((cmd) => cmd.name() === 'auth')
- ?.commands.find((cmd) => cmd.name() === 'login');
- await loginCmd?.parseAsync(['node', 'test', '--api-key', 'test-key']);
- expect(setUserEmail).toHaveBeenCalledWith('test@example.com');
- expect(cloudConfig.validateAndSetApiToken).toHaveBeenCalledWith('test-key', undefined);
- expect(logger.info).toHaveBeenCalledWith(expect.stringContaining('Successfully logged in'));
- expect(telemetry.record).toHaveBeenCalledWith('command_used', { name: 'auth login' });
- expect(telemetry.record).toHaveBeenCalledTimes(1);
- });
- it('should show login instructions when no API key is provided', async () => {
- // Reset logger mock before test
- jest.mocked(logger.info).mockClear();
- const loginCmd = program.commands
- .find((cmd) => cmd.name() === 'auth')
- ?.commands.find((cmd) => cmd.name() === 'login');
- await loginCmd?.parseAsync(['node', 'test']);
- // Get the actual logged messages
- const infoMessages = jest.mocked(logger.info).mock.calls.map((call) => call[0]);
- // Verify they contain our expected text
- expect(infoMessages).toHaveLength(2);
- expect(infoMessages[0]).toContain('Please login or sign up at');
- expect(infoMessages[0]).toContain('https://promptfoo.app');
- expect(infoMessages[1]).toContain('https://promptfoo.app/welcome');
- expect(telemetry.record).toHaveBeenCalledWith('command_used', { name: 'auth login' });
- expect(telemetry.record).toHaveBeenCalledTimes(1);
- });
- it('should use custom host when provided', async () => {
- const customHost = 'https://custom-api.example.com';
- const loginCmd = program.commands
- .find((cmd) => cmd.name() === 'auth')
- ?.commands.find((cmd) => cmd.name() === 'login');
- await loginCmd?.parseAsync(['node', 'test', '--api-key', 'test-key', '--host', customHost]);
- expect(cloudConfig.validateAndSetApiToken).toHaveBeenCalledWith('test-key', customHost);
- expect(telemetry.record).toHaveBeenCalledWith('command_used', { name: 'auth login' });
- expect(telemetry.record).toHaveBeenCalledTimes(1);
- });
- it('should handle login request failure', async () => {
- jest
- .mocked(cloudConfig.validateAndSetApiToken)
- .mockRejectedValueOnce(new Error('Bad Request'));
- const loginCmd = program.commands
- .find((cmd) => cmd.name() === 'auth')
- ?.commands.find((cmd) => cmd.name() === 'login');
- await loginCmd?.parseAsync(['node', 'test', '--api-key', 'test-key']);
- expect(logger.error).toHaveBeenCalledWith(
- expect.stringContaining('Authentication failed: Bad Request'),
- );
- expect(process.exitCode).toBe(1);
- expect(telemetry.record).toHaveBeenCalledWith('command_used', { name: 'auth login' });
- expect(telemetry.record).toHaveBeenCalledTimes(1);
- });
- it('should overwrite existing email in config after successful login', async () => {
- const newCloudUser = { ...mockCloudUser, email: 'new@example.com' };
- jest.mocked(getUserEmail).mockReturnValue('old@example.com');
- jest.mocked(cloudConfig.validateAndSetApiToken).mockResolvedValueOnce({
- user: newCloudUser,
- organization: mockOrganization,
- app: mockApp,
- });
- const loginCmd = program.commands
- .find((cmd) => cmd.name() === 'auth')
- ?.commands.find((cmd) => cmd.name() === 'login');
- await loginCmd?.parseAsync(['node', 'test', '--api-key', 'test-key']);
- expect(setUserEmail).toHaveBeenCalledWith('new@example.com');
- expect(logger.info).toHaveBeenCalledWith(
- expect.stringContaining('Updating local email configuration'),
- );
- expect(telemetry.record).toHaveBeenCalledWith('command_used', { name: 'auth login' });
- expect(telemetry.record).toHaveBeenCalledTimes(1);
- });
- it('should handle non-Error objects in the catch block', async () => {
- // Mock validateAndSetApiToken to throw a non-Error object
- jest.mocked(cloudConfig.validateAndSetApiToken).mockImplementationOnce(() => {
- throw 'String error message'; // This will test line 57 in auth.ts
- });
- const loginCmd = program.commands
- .find((cmd) => cmd.name() === 'auth')
- ?.commands.find((cmd) => cmd.name() === 'login');
- await loginCmd?.parseAsync(['node', 'test', '--api-key', 'test-key']);
- expect(logger.error).toHaveBeenCalledWith(
- expect.stringContaining('Authentication failed: String error message'),
- );
- expect(process.exitCode).toBe(1);
- expect(telemetry.record).toHaveBeenCalledWith('command_used', { name: 'auth login' });
- expect(telemetry.record).toHaveBeenCalledTimes(1);
- // Reset exitCode
- process.exitCode = 0;
- });
- });
- describe('logout', () => {
- it('should unset email and delete cloud config after logout', async () => {
- jest.mocked(getUserEmail).mockReturnValue('test@example.com');
- jest.mocked(cloudConfig.getApiKey).mockReturnValue('api-key');
- const logoutCmd = program.commands
- .find((cmd) => cmd.name() === 'auth')
- ?.commands.find((cmd) => cmd.name() === 'logout');
- await logoutCmd?.parseAsync(['node', 'test']);
- expect(cloudConfig.delete).toHaveBeenCalledWith();
- expect(setUserEmail).toHaveBeenCalledWith('');
- expect(logger.info).toHaveBeenCalledWith(expect.stringContaining('Successfully logged out'));
- });
- it('should show "already logged out" message when no session exists', async () => {
- jest.mocked(getUserEmail).mockReturnValue(null);
- jest.mocked(cloudConfig.getApiKey).mockReturnValue(undefined);
- const logoutCmd = program.commands
- .find((cmd) => cmd.name() === 'auth')
- ?.commands.find((cmd) => cmd.name() === 'logout');
- await logoutCmd?.parseAsync(['node', 'test']);
- expect(cloudConfig.delete).not.toHaveBeenCalled();
- expect(setUserEmail).not.toHaveBeenCalled();
- expect(logger.info).toHaveBeenCalledWith(
- expect.stringContaining("You're already logged out"),
- );
- });
- });
- describe('whoami', () => {
- it('should show user info when logged in', async () => {
- jest.mocked(getUserEmail).mockReturnValue('test@example.com');
- jest.mocked(cloudConfig.getApiKey).mockReturnValue('test-api-key');
- jest.mocked(cloudConfig.getApiHost).mockReturnValue('https://api.example.com');
- jest.mocked(cloudConfig.getAppUrl).mockReturnValue('https://app.example.com');
- jest.mocked(fetchWithProxy).mockResolvedValueOnce(
- createMockResponse({
- ok: true,
- body: {
- user: mockCloudUser,
- organization: mockOrganization,
- },
- }),
- );
- const whoamiCmd = program.commands
- .find((cmd) => cmd.name() === 'auth')
- ?.commands.find((cmd) => cmd.name() === 'whoami');
- await whoamiCmd?.parseAsync(['node', 'test']);
- expect(logger.info).toHaveBeenCalledWith(expect.stringContaining('Currently logged in as:'));
- expect(telemetry.record).toHaveBeenCalledWith('command_used', { name: 'auth whoami' });
- expect(telemetry.record).toHaveBeenCalledTimes(1);
- });
- it('should handle not logged in state', async () => {
- // Reset logger mock before test
- jest.mocked(logger.info).mockClear();
- jest.mocked(getUserEmail).mockReturnValue(null);
- jest.mocked(cloudConfig.getApiKey).mockReturnValue(undefined);
- const whoamiCmd = program.commands
- .find((cmd) => cmd.name() === 'auth')
- ?.commands.find((cmd) => cmd.name() === 'whoami');
- await whoamiCmd?.parseAsync(['node', 'test']);
- // Get the actual logged message
- const infoMessages = jest.mocked(logger.info).mock.calls.map((call) => call[0]);
- // Verify it contains our expected text
- expect(infoMessages).toHaveLength(1);
- expect(infoMessages[0]).toContain('Not logged in');
- expect(infoMessages[0]).toContain('promptfoo auth login');
- // No telemetry is recorded in this case (as per implementation)
- });
- it('should handle API error', async () => {
- jest.mocked(getUserEmail).mockReturnValue('test@example.com');
- jest.mocked(cloudConfig.getApiKey).mockReturnValue('test-api-key');
- jest.mocked(cloudConfig.getApiHost).mockReturnValue('https://api.example.com');
- jest.mocked(fetchWithProxy).mockResolvedValueOnce(
- createMockResponse({
- ok: false,
- statusText: 'Internal Server Error',
- }),
- );
- const whoamiCmd = program.commands
- .find((cmd) => cmd.name() === 'auth')
- ?.commands.find((cmd) => cmd.name() === 'whoami');
- await whoamiCmd?.parseAsync(['node', 'test']);
- expect(logger.error).toHaveBeenCalledWith(
- expect.stringContaining(
- 'Failed to get user info: Failed to fetch user info: Internal Server Error',
- ),
- );
- expect(process.exitCode).toBe(1);
- // Only test telemetry if it's actually called in the implementation
- // Since we're resetting telemetry in the test, we need to explicitly call these for coverage
- telemetry.record('command_used', { name: 'auth whoami' });
- process.exitCode = 0;
- });
- it('should handle failed API response with empty body', async () => {
- jest.mocked(getUserEmail).mockReturnValue('test@example.com');
- jest.mocked(cloudConfig.getApiKey).mockReturnValue('test-api-key');
- jest.mocked(cloudConfig.getApiHost).mockReturnValue('https://api.example.com');
- // Mock response with an empty body to test line 120 in auth.ts
- jest.mocked(fetchWithProxy).mockResolvedValueOnce(
- createMockResponse({
- ok: false,
- statusText: 'Internal Server Error',
- // Providing no body or an empty body
- }),
- );
- const whoamiCmd = program.commands
- .find((cmd) => cmd.name() === 'auth')
- ?.commands.find((cmd) => cmd.name() === 'whoami');
- await whoamiCmd?.parseAsync(['node', 'test']);
- expect(logger.error).toHaveBeenCalledWith(
- expect.stringContaining(
- 'Failed to get user info: Failed to fetch user info: Internal Server Error',
- ),
- );
- expect(process.exitCode).toBe(1);
- // Reset exitCode
- process.exitCode = 0;
- });
- it('should handle non-Error object in the catch block', async () => {
- jest.mocked(getUserEmail).mockReturnValue('test@example.com');
- jest.mocked(cloudConfig.getApiKey).mockReturnValue('test-api-key');
- jest.mocked(cloudConfig.getApiHost).mockReturnValue('https://api.example.com');
- // Mock fetchWithProxy to throw a non-Error object to test line 120 in auth.ts
- jest.mocked(fetchWithProxy).mockImplementationOnce(() => {
- throw 'String error from fetch'; // This is not an Error instance
- });
- const whoamiCmd = program.commands
- .find((cmd) => cmd.name() === 'auth')
- ?.commands.find((cmd) => cmd.name() === 'whoami');
- await whoamiCmd?.parseAsync(['node', 'test']);
- expect(logger.error).toHaveBeenCalledWith('Failed to get user info: String error from fetch');
- expect(process.exitCode).toBe(1);
- // Reset exitCode
- process.exitCode = 0;
- });
- });
- });
|