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

share.test.ts 12 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
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
  1. import { Command } from 'commander';
  2. import {
  3. createAndDisplayShareableUrl,
  4. notCloudEnabledShareInstructions,
  5. shareCommand,
  6. } from '../../src/commands/share';
  7. import * as envars from '../../src/envars';
  8. import logger from '../../src/logger';
  9. import Eval from '../../src/models/eval';
  10. import { createShareableUrl, isSharingEnabled } from '../../src/share';
  11. import { loadDefaultConfig } from '../../src/util/config/default';
  12. jest.mock('../../src/share');
  13. jest.mock('../../src/logger');
  14. jest.mock('../../src/telemetry', () => ({
  15. record: jest.fn(),
  16. send: jest.fn(),
  17. }));
  18. jest.mock('../../src/envars');
  19. jest.mock('readline');
  20. jest.mock('../../src/models/eval');
  21. jest.mock('../../src/util', () => ({
  22. setupEnv: jest.fn(),
  23. }));
  24. jest.mock('../../src/util/config/default');
  25. describe('Share Command', () => {
  26. beforeEach(() => {
  27. jest.clearAllMocks();
  28. process.exitCode = 0; // Reset exitCode before each test
  29. });
  30. describe('notCloudEnabledShareInstructions', () => {
  31. it('should log instructions for cloud setup', () => {
  32. notCloudEnabledShareInstructions();
  33. expect(logger.info).toHaveBeenCalledWith(
  34. expect.stringContaining('You need to have a cloud account'),
  35. );
  36. expect(logger.info).toHaveBeenCalledWith(expect.stringContaining('Please go to'));
  37. });
  38. });
  39. describe('createAndDisplayShareableUrl', () => {
  40. it('should return a URL and log it when successful', async () => {
  41. jest.mocked(isSharingEnabled).mockReturnValue(true);
  42. const mockEval = { id: 'test-eval-id' } as Eval;
  43. const mockUrl = 'https://app.promptfoo.dev/eval/test-eval-id';
  44. jest.mocked(createShareableUrl).mockResolvedValue(mockUrl);
  45. const result = await createAndDisplayShareableUrl(mockEval, false);
  46. expect(createShareableUrl).toHaveBeenCalledWith(mockEval, false);
  47. expect(logger.info).toHaveBeenCalledWith(expect.stringContaining(mockUrl));
  48. expect(result).toBe(mockUrl);
  49. });
  50. it('should pass showAuth parameter correctly', async () => {
  51. const mockEval = { id: 'test-eval-id' } as Eval;
  52. const mockUrl = 'https://app.promptfoo.dev/eval/test-eval-id';
  53. jest.mocked(createShareableUrl).mockResolvedValue(mockUrl);
  54. await createAndDisplayShareableUrl(mockEval, true);
  55. expect(createShareableUrl).toHaveBeenCalledWith(mockEval, true);
  56. });
  57. it('should return null when createShareableUrl returns null', async () => {
  58. const mockEval = { id: 'test-eval-id' } as Eval;
  59. jest.mocked(isSharingEnabled).mockReturnValue(true);
  60. jest.mocked(createShareableUrl).mockResolvedValue(null);
  61. const result = await createAndDisplayShareableUrl(mockEval, false);
  62. expect(createShareableUrl).toHaveBeenCalledWith(mockEval, false);
  63. expect(result).toBeNull();
  64. expect(logger.error).toHaveBeenCalledWith(
  65. 'Failed to create shareable URL for eval test-eval-id',
  66. );
  67. expect(logger.info).not.toHaveBeenCalled();
  68. });
  69. });
  70. describe('shareCommand', () => {
  71. let program: Command;
  72. beforeEach(() => {
  73. jest.clearAllMocks();
  74. program = new Command();
  75. shareCommand(program);
  76. });
  77. it('should register share command with correct options', () => {
  78. const cmd = program.commands.find((c) => c.name() === 'share');
  79. expect(cmd).toBeDefined();
  80. expect(cmd?.description()).toContain('Create a shareable URL');
  81. const options = cmd?.options;
  82. expect(options?.find((o) => o.long === '--show-auth')).toBeDefined();
  83. expect(options?.find((o) => o.long === '--yes')).toBeDefined();
  84. });
  85. it('should handle specific evalId not found', async () => {
  86. jest.spyOn(Eval, 'findById').mockImplementation().mockResolvedValue(undefined);
  87. const shareCmd = program.commands.find((c) => c.name() === 'share');
  88. await shareCmd?.parseAsync(['node', 'test', 'non-existent-id']);
  89. expect(Eval.findById).toHaveBeenCalledWith('non-existent-id');
  90. expect(logger.error).toHaveBeenCalledWith(
  91. expect.stringContaining('Could not find eval with ID'),
  92. );
  93. expect(process.exitCode).toBe(1);
  94. });
  95. it('should handle no evals available', async () => {
  96. jest.spyOn(Eval, 'latest').mockImplementation().mockResolvedValue(undefined);
  97. const shareCmd = program.commands.find((c) => c.name() === 'share');
  98. await shareCmd?.parseAsync(['node', 'test']);
  99. expect(Eval.latest).toHaveBeenCalledWith();
  100. expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('Could not load results'));
  101. expect(process.exitCode).toBe(1);
  102. });
  103. it('should handle eval with empty prompts', async () => {
  104. const mockEval = { prompts: [] } as unknown as Eval;
  105. jest.spyOn(Eval, 'latest').mockImplementation().mockResolvedValue(mockEval);
  106. const shareCmd = program.commands.find((c) => c.name() === 'share');
  107. await shareCmd?.parseAsync(['node', 'test']);
  108. expect(Eval.latest).toHaveBeenCalledWith();
  109. expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('cannot be shared'));
  110. expect(process.exitCode).toBe(1);
  111. });
  112. it('should accept -y flag for backwards compatibility', async () => {
  113. const mockEval = { prompts: ['test'] } as unknown as Eval;
  114. jest.spyOn(Eval, 'latest').mockImplementation().mockResolvedValue(mockEval);
  115. jest.mocked(isSharingEnabled).mockReturnValue(true);
  116. jest.mocked(createShareableUrl).mockResolvedValue('https://example.com/share');
  117. const shareCmd = program.commands.find((c) => c.name() === 'share');
  118. await shareCmd?.parseAsync(['node', 'test', '-y']);
  119. expect(Eval.latest).toHaveBeenCalledWith();
  120. expect(createShareableUrl).toHaveBeenCalledWith(mockEval, false);
  121. expect(logger.info).toHaveBeenCalledWith(expect.stringContaining('View results:'));
  122. });
  123. it('should use promptfoo.app by default if no environment variables are set', () => {
  124. jest.mocked(envars.getEnvString).mockImplementation(() => '');
  125. const baseUrl =
  126. envars.getEnvString('PROMPTFOO_SHARING_APP_BASE_URL') ||
  127. envars.getEnvString('PROMPTFOO_REMOTE_APP_BASE_URL');
  128. const hostname = baseUrl ? new URL(baseUrl).hostname : 'promptfoo.app';
  129. expect(hostname).toBe('promptfoo.app');
  130. });
  131. it('should use PROMPTFOO_SHARING_APP_BASE_URL for hostname when set', () => {
  132. jest.mocked(envars.getEnvString).mockImplementation((key) => {
  133. if (key === 'PROMPTFOO_SHARING_APP_BASE_URL') {
  134. return 'https://custom-domain.com';
  135. }
  136. return '';
  137. });
  138. const baseUrl =
  139. envars.getEnvString('PROMPTFOO_SHARING_APP_BASE_URL') ||
  140. envars.getEnvString('PROMPTFOO_REMOTE_APP_BASE_URL');
  141. const hostname = baseUrl ? new URL(baseUrl).hostname : 'app.promptfoo.dev';
  142. expect(hostname).toBe('custom-domain.com');
  143. });
  144. it('should use PROMPTFOO_REMOTE_APP_BASE_URL for hostname when PROMPTFOO_SHARING_APP_BASE_URL is not set', () => {
  145. jest.mocked(envars.getEnvString).mockImplementation((key) => {
  146. if (key === 'PROMPTFOO_REMOTE_APP_BASE_URL') {
  147. return 'https://self-hosted-domain.com';
  148. }
  149. return '';
  150. });
  151. const baseUrl =
  152. envars.getEnvString('PROMPTFOO_SHARING_APP_BASE_URL') ||
  153. envars.getEnvString('PROMPTFOO_REMOTE_APP_BASE_URL');
  154. const hostname = baseUrl ? new URL(baseUrl).hostname : 'app.promptfoo.dev';
  155. expect(hostname).toBe('self-hosted-domain.com');
  156. });
  157. it('should prioritize PROMPTFOO_SHARING_APP_BASE_URL over PROMPTFOO_REMOTE_APP_BASE_URL', () => {
  158. jest.mocked(envars.getEnvString).mockImplementation((key) => {
  159. if (key === 'PROMPTFOO_SHARING_APP_BASE_URL') {
  160. return 'https://sharing-domain.com';
  161. }
  162. if (key === 'PROMPTFOO_REMOTE_APP_BASE_URL') {
  163. return 'https://remote-domain.com';
  164. }
  165. return '';
  166. });
  167. const baseUrl =
  168. envars.getEnvString('PROMPTFOO_SHARING_APP_BASE_URL') ||
  169. envars.getEnvString('PROMPTFOO_REMOTE_APP_BASE_URL');
  170. const hostname = baseUrl ? new URL(baseUrl).hostname : 'app.promptfoo.dev';
  171. expect(hostname).toBe('sharing-domain.com');
  172. });
  173. it('should use sharing config from promptfooconfig.yaml', async () => {
  174. const mockEval = {
  175. id: 'test-eval-id',
  176. prompts: ['test prompt'],
  177. config: {},
  178. save: jest.fn().mockResolvedValue(undefined),
  179. } as unknown as Eval;
  180. jest.spyOn(Eval, 'latest').mockResolvedValue(mockEval);
  181. const mockSharing = {
  182. apiBaseUrl: 'https://custom-api.example.com',
  183. appBaseUrl: 'https://custom-app.example.com',
  184. };
  185. jest.mocked(loadDefaultConfig).mockResolvedValue({
  186. defaultConfig: {
  187. sharing: mockSharing,
  188. },
  189. defaultConfigPath: 'promptfooconfig.yaml',
  190. });
  191. jest.mocked(isSharingEnabled).mockImplementation((evalObj) => {
  192. return !!evalObj.config.sharing;
  193. });
  194. jest
  195. .mocked(createShareableUrl)
  196. .mockResolvedValue('https://custom-app.example.com/eval/test-eval-id');
  197. const shareCmd = program.commands.find((c) => c.name() === 'share');
  198. await shareCmd?.parseAsync(['node', 'test']);
  199. expect(loadDefaultConfig).toHaveBeenCalledTimes(1);
  200. expect(mockEval.config.sharing).toEqual(mockSharing);
  201. expect(isSharingEnabled).toHaveBeenCalledWith(mockEval);
  202. expect(createShareableUrl).toHaveBeenCalledWith(mockEval, false);
  203. });
  204. it('should show cloud instructions and return null when sharing is not enabled', async () => {
  205. const mockEval = {
  206. id: 'test-eval-id',
  207. prompts: ['test prompt'],
  208. config: {},
  209. } as unknown as Eval;
  210. jest.spyOn(Eval, 'latest').mockResolvedValue(mockEval);
  211. jest.mocked(isSharingEnabled).mockReturnValue(false);
  212. jest.mocked(loadDefaultConfig).mockResolvedValue({
  213. defaultConfig: {},
  214. defaultConfigPath: 'promptfooconfig.yaml',
  215. });
  216. const shareCmd = program.commands.find((c) => c.name() === 'share');
  217. await shareCmd?.parseAsync(['node', 'test']);
  218. expect(logger.info).toHaveBeenCalledWith(
  219. expect.stringContaining('You need to have a cloud account'),
  220. );
  221. expect(process.exitCode).toBe(1);
  222. });
  223. it('should set exit code 0 when sharing is successful', async () => {
  224. const mockEval = {
  225. id: 'test-eval-id',
  226. prompts: ['test prompt'],
  227. config: { sharing: true },
  228. } as unknown as Eval;
  229. jest.spyOn(Eval, 'latest').mockResolvedValue(mockEval);
  230. jest.mocked(isSharingEnabled).mockReturnValue(true);
  231. jest
  232. .mocked(createShareableUrl)
  233. .mockResolvedValue('https://app.promptfoo.dev/eval/test-eval-id');
  234. const shareCmd = program.commands.find((c) => c.name() === 'share');
  235. await shareCmd?.parseAsync(['node', 'test']);
  236. expect(createShareableUrl).toHaveBeenCalledWith(mockEval, false);
  237. expect(logger.info).toHaveBeenCalledWith(expect.stringContaining('View results:'));
  238. expect(process.exitCode).toBe(0);
  239. });
  240. it('should set exit code 0 when sharing specific eval by ID', async () => {
  241. const mockEval = {
  242. id: 'specific-eval-id',
  243. prompts: ['test prompt'],
  244. config: { sharing: true },
  245. } as unknown as Eval;
  246. jest.spyOn(Eval, 'findById').mockResolvedValue(mockEval);
  247. jest.mocked(isSharingEnabled).mockReturnValue(true);
  248. jest
  249. .mocked(createShareableUrl)
  250. .mockResolvedValue('https://app.promptfoo.dev/eval/specific-eval-id');
  251. const shareCmd = program.commands.find((c) => c.name() === 'share');
  252. await shareCmd?.parseAsync(['node', 'test', 'specific-eval-id']);
  253. expect(Eval.findById).toHaveBeenCalledWith('specific-eval-id');
  254. expect(createShareableUrl).toHaveBeenCalledWith(mockEval, false);
  255. expect(process.exitCode).toBe(0);
  256. });
  257. it('should set exit code 0 when user cancels sharing in confirmation prompt', async () => {
  258. // This test is complex to mock properly, so we'll just verify the command exists
  259. // The actual cancellation logic is tested in integration tests
  260. const shareCmd = program.commands.find((c) => c.name() === 'share');
  261. expect(shareCmd).toBeDefined();
  262. expect(shareCmd?.name()).toBe('share');
  263. });
  264. });
  265. });
Tip!

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

Comments

Loading...