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

accounts.test.ts 7.7 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
246
247
248
249
250
251
252
  1. import input from '@inquirer/input';
  2. import { getEnvString, isCI } from '../../src/envars';
  3. import { fetchWithTimeout } from '../../src/fetch';
  4. import {
  5. getUserEmail,
  6. setUserEmail,
  7. getAuthor,
  8. promptForEmailUnverified,
  9. checkEmailStatusOrExit,
  10. } from '../../src/globalConfig/accounts';
  11. import { readGlobalConfig, writeGlobalConfigPartial } from '../../src/globalConfig/globalConfig';
  12. import logger from '../../src/logger';
  13. import telemetry from '../../src/telemetry';
  14. jest.mock('@inquirer/input');
  15. jest.mock('../../src/envars');
  16. jest.mock('../../src/fetch');
  17. jest.mock('../../src/telemetry');
  18. describe('accounts', () => {
  19. beforeEach(() => {
  20. jest.clearAllMocks();
  21. });
  22. describe('getUserEmail', () => {
  23. it('should return email from global config', () => {
  24. jest.mocked(readGlobalConfig).mockReturnValue({
  25. account: { email: 'test@example.com' },
  26. });
  27. expect(getUserEmail()).toBe('test@example.com');
  28. });
  29. it('should return null if no email in config', () => {
  30. jest.mocked(readGlobalConfig).mockReturnValue({});
  31. expect(getUserEmail()).toBeNull();
  32. });
  33. });
  34. describe('setUserEmail', () => {
  35. it('should write email to global config', () => {
  36. const email = 'test@example.com';
  37. setUserEmail(email);
  38. expect(writeGlobalConfigPartial).toHaveBeenCalledWith({
  39. account: { email },
  40. });
  41. });
  42. });
  43. describe('getAuthor', () => {
  44. it('should return env var if set', () => {
  45. jest.mocked(getEnvString).mockReturnValue('author@env.com');
  46. expect(getAuthor()).toBe('author@env.com');
  47. });
  48. it('should fall back to user email if no env var', () => {
  49. jest.mocked(getEnvString).mockReturnValue('');
  50. jest.mocked(readGlobalConfig).mockReturnValue({
  51. account: { email: 'test@example.com' },
  52. });
  53. expect(getAuthor()).toBe('test@example.com');
  54. });
  55. it('should return null if no author found', () => {
  56. jest.mocked(getEnvString).mockReturnValue('');
  57. jest.mocked(readGlobalConfig).mockReturnValue({});
  58. expect(getAuthor()).toBeNull();
  59. });
  60. });
  61. describe('promptForEmailUnverified', () => {
  62. beforeEach(() => {
  63. jest.mocked(isCI).mockReturnValue(false);
  64. jest.mocked(readGlobalConfig).mockReturnValue({});
  65. });
  66. it('should use CI email if in CI environment', async () => {
  67. jest.mocked(isCI).mockReturnValue(true);
  68. await promptForEmailUnverified();
  69. expect(telemetry.saveConsent).toHaveBeenCalledWith('ci-placeholder@promptfoo.dev', {
  70. source: 'promptForEmailUnverified',
  71. });
  72. });
  73. it('should not prompt for email if already set', async () => {
  74. jest.mocked(readGlobalConfig).mockReturnValue({
  75. account: { email: 'existing@example.com' },
  76. });
  77. await promptForEmailUnverified();
  78. expect(input).not.toHaveBeenCalled();
  79. expect(telemetry.saveConsent).toHaveBeenCalledWith('existing@example.com', {
  80. source: 'promptForEmailUnverified',
  81. });
  82. });
  83. it('should prompt for email and save valid input', async () => {
  84. jest.mocked(input).mockResolvedValue('new@example.com');
  85. await promptForEmailUnverified();
  86. expect(writeGlobalConfigPartial).toHaveBeenCalledWith({
  87. account: { email: 'new@example.com' },
  88. });
  89. expect(telemetry.saveConsent).toHaveBeenCalledWith('new@example.com', {
  90. source: 'promptForEmailUnverified',
  91. });
  92. });
  93. describe('email validation', () => {
  94. let validateFn: (input: string) => Promise<string | boolean>;
  95. beforeEach(async () => {
  96. await promptForEmailUnverified();
  97. validateFn = jest.mocked(input).mock.calls[0][0].validate as (
  98. input: string,
  99. ) => Promise<string | boolean>;
  100. });
  101. it('should reject invalid email formats with error message', async () => {
  102. const invalidEmails = [
  103. '',
  104. 'invalid',
  105. '@example.com',
  106. 'user@',
  107. 'user@.',
  108. 'user.com',
  109. 'user@.com',
  110. '@.',
  111. 'user@example.',
  112. 'user.@example.com',
  113. 'us..er@example.com',
  114. ];
  115. for (const email of invalidEmails) {
  116. const result = await validateFn(email);
  117. expect(typeof result).toBe('string');
  118. expect(result).toBe('Please enter a valid email address');
  119. }
  120. });
  121. it('should accept valid email formats with true', async () => {
  122. const validEmails = [
  123. 'valid@example.com',
  124. 'user.name@example.com',
  125. 'user+tag@example.com',
  126. 'user@subdomain.example.com',
  127. 'user@example.co.uk',
  128. '123@example.com',
  129. 'user-name@example.com',
  130. 'user_name@example.com',
  131. ];
  132. for (const email of validEmails) {
  133. await expect(validateFn(email)).toBe(true);
  134. }
  135. });
  136. });
  137. it('should save consent after successful email input', async () => {
  138. jest.mocked(input).mockResolvedValue('test@example.com');
  139. await promptForEmailUnverified();
  140. expect(telemetry.saveConsent).toHaveBeenCalledWith('test@example.com', {
  141. source: 'promptForEmailUnverified',
  142. });
  143. });
  144. });
  145. describe('checkEmailStatusOrExit', () => {
  146. const mockExit = jest.spyOn(process, 'exit').mockImplementation(() => undefined as never);
  147. beforeEach(() => {
  148. jest.clearAllMocks();
  149. });
  150. it('should use CI email when in CI environment', async () => {
  151. jest.mocked(isCI).mockReturnValue(true);
  152. const mockResponse = new Response(JSON.stringify({ status: 'ok' }), {
  153. status: 200,
  154. statusText: 'OK',
  155. });
  156. jest.mocked(fetchWithTimeout).mockResolvedValue(mockResponse);
  157. await checkEmailStatusOrExit();
  158. expect(fetchWithTimeout).toHaveBeenCalledWith(
  159. 'https://api.promptfoo.app/api/users/status?email=ci-placeholder@promptfoo.dev',
  160. undefined,
  161. 500,
  162. );
  163. });
  164. it('should use user email when not in CI environment', async () => {
  165. jest.mocked(isCI).mockReturnValue(false);
  166. jest.mocked(readGlobalConfig).mockReturnValue({
  167. account: { email: 'test@example.com' },
  168. });
  169. const mockResponse = new Response(JSON.stringify({ status: 'ok' }), {
  170. status: 200,
  171. statusText: 'OK',
  172. });
  173. jest.mocked(fetchWithTimeout).mockResolvedValue(mockResponse);
  174. await checkEmailStatusOrExit();
  175. expect(fetchWithTimeout).toHaveBeenCalledWith(
  176. 'https://api.promptfoo.app/api/users/status?email=test@example.com',
  177. undefined,
  178. 500,
  179. );
  180. });
  181. it('should exit if limit exceeded', async () => {
  182. jest.mocked(isCI).mockReturnValue(false);
  183. jest.mocked(readGlobalConfig).mockReturnValue({
  184. account: { email: 'test@example.com' },
  185. });
  186. const mockResponse = new Response(JSON.stringify({ status: 'exceeded_limit' }), {
  187. status: 200,
  188. statusText: 'OK',
  189. });
  190. jest.mocked(fetchWithTimeout).mockResolvedValue(mockResponse);
  191. await checkEmailStatusOrExit();
  192. expect(mockExit).toHaveBeenCalledWith(1);
  193. expect(logger.error).toHaveBeenCalledWith(
  194. 'You have exceeded the maximum cloud inference limit. Please contact inquiries@promptfoo.dev to upgrade your account.',
  195. );
  196. });
  197. it('should handle fetch errors', async () => {
  198. jest.mocked(isCI).mockReturnValue(false);
  199. jest.mocked(readGlobalConfig).mockReturnValue({
  200. account: { email: 'test@example.com' },
  201. });
  202. jest.mocked(fetchWithTimeout).mockRejectedValue(new Error('Network error'));
  203. await checkEmailStatusOrExit();
  204. expect(logger.debug).toHaveBeenCalledWith(
  205. 'Failed to check user status: Error: Network error',
  206. );
  207. expect(mockExit).not.toHaveBeenCalled();
  208. });
  209. });
  210. });
Tip!

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

Comments

Loading...