Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

config.test.ts 9.3 KB

You have to be logged in to leave a comment. Sign In
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
  1. import confirm from '@inquirer/confirm';
  2. import { Command } from 'commander';
  3. import { configCommand } from '../../src/commands/config';
  4. import { getUserEmail, setUserEmail } from '../../src/globalConfig/accounts';
  5. import { cloudConfig } from '../../src/globalConfig/cloud';
  6. import logger from '../../src/logger';
  7. import telemetry from '../../src/telemetry';
  8. jest.mock('../../src/globalConfig/accounts');
  9. jest.mock('../../src/globalConfig/cloud');
  10. jest.mock('../../src/telemetry', () => ({
  11. record: jest.fn(),
  12. send: jest.fn().mockResolvedValue(undefined),
  13. }));
  14. jest.mock('@inquirer/confirm');
  15. describe('config command', () => {
  16. let program: Command;
  17. beforeEach(() => {
  18. jest.clearAllMocks();
  19. program = new Command();
  20. configCommand(program);
  21. });
  22. describe('set email', () => {
  23. it('should not allow setting email when user is logged in', async () => {
  24. // Mock logged in state
  25. jest.mocked(cloudConfig.getApiKey).mockReturnValue('test-api-key');
  26. // Execute set email command
  27. const setEmailCmd = program.commands
  28. .find((cmd) => cmd.name() === 'config')
  29. ?.commands.find((cmd) => cmd.name() === 'set')
  30. ?.commands.find((cmd) => cmd.name() === 'email');
  31. await setEmailCmd?.parseAsync(['node', 'test', 'new@example.com']);
  32. // Verify email was not set and error was shown
  33. expect(setUserEmail).not.toHaveBeenCalled();
  34. expect(logger.error).toHaveBeenCalledWith(
  35. expect.stringContaining(
  36. "Cannot update email while logged in. Email is managed through 'promptfoo auth login'",
  37. ),
  38. );
  39. expect(process.exitCode).toBe(1);
  40. });
  41. it('should allow setting email when user is not logged in', async () => {
  42. // Mock logged out state
  43. jest.mocked(cloudConfig.getApiKey).mockReturnValue(undefined);
  44. // Execute set email command
  45. const setEmailCmd = program.commands
  46. .find((cmd) => cmd.name() === 'config')
  47. ?.commands.find((cmd) => cmd.name() === 'set')
  48. ?.commands.find((cmd) => cmd.name() === 'email');
  49. await setEmailCmd?.parseAsync(['node', 'test', 'test@example.com']);
  50. // Verify email was set
  51. expect(setUserEmail).toHaveBeenCalledWith('test@example.com');
  52. expect(logger.info).toHaveBeenCalledWith(
  53. expect.stringContaining('Email set to test@example.com'),
  54. );
  55. expect(telemetry.record).toHaveBeenCalledWith('command_used', {
  56. name: 'config set',
  57. configKey: 'email',
  58. });
  59. });
  60. it('should validate email format even when not logged in', async () => {
  61. // Mock logged out state
  62. jest.mocked(cloudConfig.getApiKey).mockReturnValue(undefined);
  63. // Execute set email command with invalid email
  64. const setEmailCmd = program.commands
  65. .find((cmd) => cmd.name() === 'config')
  66. ?.commands.find((cmd) => cmd.name() === 'set')
  67. ?.commands.find((cmd) => cmd.name() === 'email');
  68. await setEmailCmd?.parseAsync(['node', 'test', 'invalid-email']);
  69. // Verify email was not set and error was shown
  70. expect(setUserEmail).not.toHaveBeenCalled();
  71. expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('Invalid email address'));
  72. expect(process.exitCode).toBe(1);
  73. expect(telemetry.record).not.toHaveBeenCalled();
  74. expect(telemetry.record).not.toHaveBeenCalled();
  75. });
  76. });
  77. describe('unset email', () => {
  78. it('should not allow unsetting email when user is logged in', async () => {
  79. // Mock logged in state
  80. jest.mocked(cloudConfig.getApiKey).mockReturnValue('test-api-key');
  81. jest.mocked(getUserEmail).mockReturnValue('test@example.com');
  82. // Execute unset email command
  83. const unsetEmailCmd = program.commands
  84. .find((cmd) => cmd.name() === 'config')
  85. ?.commands.find((cmd) => cmd.name() === 'unset')
  86. ?.commands.find((cmd) => cmd.name() === 'email');
  87. await unsetEmailCmd?.parseAsync(['node', 'test']);
  88. // Verify email was not unset and error was shown
  89. expect(setUserEmail).not.toHaveBeenCalled();
  90. expect(logger.error).toHaveBeenCalledWith(
  91. expect.stringContaining(
  92. "Cannot update email while logged in. Email is managed through 'promptfoo auth login'",
  93. ),
  94. );
  95. expect(process.exitCode).toBe(1);
  96. });
  97. it('should allow unsetting email when user is not logged in with force flag', async () => {
  98. // Mock logged out state
  99. jest.mocked(cloudConfig.getApiKey).mockReturnValue(undefined);
  100. jest.mocked(getUserEmail).mockReturnValue('test@example.com');
  101. // Execute unset email command with force flag
  102. const unsetEmailCmd = program.commands
  103. .find((cmd) => cmd.name() === 'config')
  104. ?.commands.find((cmd) => cmd.name() === 'unset')
  105. ?.commands.find((cmd) => cmd.name() === 'email');
  106. await unsetEmailCmd?.parseAsync(['node', 'test', '--force']);
  107. // Verify email was unset
  108. expect(setUserEmail).toHaveBeenCalledWith('');
  109. expect(logger.info).toHaveBeenCalledWith('Email has been unset.');
  110. expect(telemetry.record).toHaveBeenCalledWith('command_used', {
  111. name: 'config unset',
  112. configKey: 'email',
  113. });
  114. });
  115. it('should handle user confirmation for unsetting email', async () => {
  116. // Mock logged out state and user confirmation
  117. jest.mocked(cloudConfig.getApiKey).mockReturnValue(undefined);
  118. jest.mocked(getUserEmail).mockReturnValue('test@example.com');
  119. jest.mocked(confirm).mockResolvedValueOnce(true);
  120. // Execute unset email command without force flag
  121. const unsetEmailCmd = program.commands
  122. .find((cmd) => cmd.name() === 'config')
  123. ?.commands.find((cmd) => cmd.name() === 'unset')
  124. ?.commands.find((cmd) => cmd.name() === 'email');
  125. await unsetEmailCmd?.parseAsync(['node', 'test']);
  126. // Verify email was unset after confirmation
  127. expect(confirm).toHaveBeenCalledWith({
  128. message: 'Are you sure you want to unset the email "test@example.com"?',
  129. default: false,
  130. });
  131. expect(setUserEmail).toHaveBeenCalledWith('');
  132. expect(logger.info).toHaveBeenCalledWith('Email has been unset.');
  133. });
  134. it('should handle user cancellation for unsetting email', async () => {
  135. // Mock logged out state and user cancellation
  136. jest.mocked(cloudConfig.getApiKey).mockReturnValue(undefined);
  137. jest.mocked(getUserEmail).mockReturnValue('test@example.com');
  138. jest.mocked(confirm).mockResolvedValueOnce(false);
  139. // Execute unset email command without force flag
  140. const unsetEmailCmd = program.commands
  141. .find((cmd) => cmd.name() === 'config')
  142. ?.commands.find((cmd) => cmd.name() === 'unset')
  143. ?.commands.find((cmd) => cmd.name() === 'email');
  144. await unsetEmailCmd?.parseAsync(['node', 'test']);
  145. // Verify operation was cancelled
  146. expect(setUserEmail).not.toHaveBeenCalled();
  147. expect(logger.info).toHaveBeenCalledWith('Operation cancelled.');
  148. expect(telemetry.record).not.toHaveBeenCalled();
  149. expect(telemetry.record).not.toHaveBeenCalled();
  150. });
  151. it('should handle case when no email is set', async () => {
  152. // Mock logged out state and no existing email
  153. jest.mocked(cloudConfig.getApiKey).mockReturnValue(undefined);
  154. jest.mocked(getUserEmail).mockReturnValue(null);
  155. // Execute unset email command
  156. const unsetEmailCmd = program.commands
  157. .find((cmd) => cmd.name() === 'config')
  158. ?.commands.find((cmd) => cmd.name() === 'unset')
  159. ?.commands.find((cmd) => cmd.name() === 'email');
  160. await unsetEmailCmd?.parseAsync(['node', 'test']);
  161. // Verify appropriate message was shown
  162. expect(logger.info).toHaveBeenCalledWith('No email is currently set.');
  163. expect(setUserEmail).not.toHaveBeenCalled();
  164. expect(telemetry.record).not.toHaveBeenCalled();
  165. expect(telemetry.record).not.toHaveBeenCalled();
  166. });
  167. });
  168. describe('get email', () => {
  169. it('should show email when it exists', async () => {
  170. // Mock existing email
  171. jest.mocked(getUserEmail).mockReturnValue('test@example.com');
  172. // Execute get email command
  173. const getEmailCmd = program.commands
  174. .find((cmd) => cmd.name() === 'config')
  175. ?.commands.find((cmd) => cmd.name() === 'get')
  176. ?.commands.find((cmd) => cmd.name() === 'email');
  177. await getEmailCmd?.parseAsync(['node', 'test']);
  178. // Verify email was shown and telemetry was recorded
  179. expect(logger.info).toHaveBeenCalledWith('test@example.com');
  180. expect(telemetry.record).toHaveBeenCalledWith('command_used', {
  181. name: 'config get',
  182. configKey: 'email',
  183. });
  184. });
  185. it('should show message when no email is set', async () => {
  186. // Mock no existing email
  187. jest.mocked(getUserEmail).mockReturnValue(null);
  188. // Execute get email command
  189. const getEmailCmd = program.commands
  190. .find((cmd) => cmd.name() === 'config')
  191. ?.commands.find((cmd) => cmd.name() === 'get')
  192. ?.commands.find((cmd) => cmd.name() === 'email');
  193. await getEmailCmd?.parseAsync(['node', 'test']);
  194. // Verify message was shown and telemetry was recorded
  195. expect(logger.info).toHaveBeenCalledWith('No email set.');
  196. expect(telemetry.record).toHaveBeenCalledWith('command_used', {
  197. name: 'config get',
  198. configKey: 'email',
  199. });
  200. });
  201. });
  202. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...