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 9.8 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
  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('Failed to create shareable URL');
  65. expect(logger.info).not.toHaveBeenCalled();
  66. });
  67. });
  68. describe('shareCommand', () => {
  69. let program: Command;
  70. beforeEach(() => {
  71. jest.clearAllMocks();
  72. program = new Command();
  73. shareCommand(program);
  74. });
  75. it('should register share command with correct options', () => {
  76. const cmd = program.commands.find((c) => c.name() === 'share');
  77. expect(cmd).toBeDefined();
  78. expect(cmd?.description()).toContain('Create a shareable URL');
  79. const options = cmd?.options;
  80. expect(options?.find((o) => o.long === '--show-auth')).toBeDefined();
  81. expect(options?.find((o) => o.long === '--env-path')).toBeDefined();
  82. expect(options?.find((o) => o.long === '--yes')).toBeDefined();
  83. });
  84. it('should handle specific evalId not found', async () => {
  85. jest.spyOn(Eval, 'findById').mockImplementation().mockResolvedValue(undefined);
  86. const shareCmd = program.commands.find((c) => c.name() === 'share');
  87. await shareCmd?.parseAsync(['node', 'test', 'non-existent-id']);
  88. expect(Eval.findById).toHaveBeenCalledWith('non-existent-id');
  89. expect(logger.error).toHaveBeenCalledWith(
  90. expect.stringContaining('Could not find eval with ID'),
  91. );
  92. expect(process.exitCode).toBe(1);
  93. });
  94. it('should handle no evals available', async () => {
  95. jest.spyOn(Eval, 'latest').mockImplementation().mockResolvedValue(undefined);
  96. const shareCmd = program.commands.find((c) => c.name() === 'share');
  97. await shareCmd?.parseAsync(['node', 'test']);
  98. expect(Eval.latest).toHaveBeenCalledWith();
  99. expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('Could not load results'));
  100. expect(process.exitCode).toBe(1);
  101. });
  102. it('should handle eval with empty prompts', async () => {
  103. const mockEval = { prompts: [] } as unknown as Eval;
  104. jest.spyOn(Eval, 'latest').mockImplementation().mockResolvedValue(mockEval);
  105. const shareCmd = program.commands.find((c) => c.name() === 'share');
  106. await shareCmd?.parseAsync(['node', 'test']);
  107. expect(Eval.latest).toHaveBeenCalledWith();
  108. expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('cannot be shared'));
  109. expect(process.exitCode).toBe(1);
  110. });
  111. it('should accept -y flag for backwards compatibility', async () => {
  112. const mockEval = { prompts: ['test'] } as unknown as Eval;
  113. jest.spyOn(Eval, 'latest').mockImplementation().mockResolvedValue(mockEval);
  114. jest.mocked(isSharingEnabled).mockReturnValue(true);
  115. jest.mocked(createShareableUrl).mockResolvedValue('https://example.com/share');
  116. const shareCmd = program.commands.find((c) => c.name() === 'share');
  117. await shareCmd?.parseAsync(['node', 'test', '-y']);
  118. expect(Eval.latest).toHaveBeenCalledWith();
  119. expect(createShareableUrl).toHaveBeenCalledWith(mockEval, false);
  120. expect(logger.info).toHaveBeenCalledWith(expect.stringContaining('View results:'));
  121. });
  122. it('should use promptfoo.app by default if no environment variables are set', () => {
  123. jest.mocked(envars.getEnvString).mockImplementation(() => '');
  124. const baseUrl =
  125. envars.getEnvString('PROMPTFOO_SHARING_APP_BASE_URL') ||
  126. envars.getEnvString('PROMPTFOO_REMOTE_APP_BASE_URL');
  127. const hostname = baseUrl ? new URL(baseUrl).hostname : 'promptfoo.app';
  128. expect(hostname).toBe('promptfoo.app');
  129. });
  130. it('should use PROMPTFOO_SHARING_APP_BASE_URL for hostname when set', () => {
  131. jest.mocked(envars.getEnvString).mockImplementation((key) => {
  132. if (key === 'PROMPTFOO_SHARING_APP_BASE_URL') {
  133. return 'https://custom-domain.com';
  134. }
  135. return '';
  136. });
  137. const baseUrl =
  138. envars.getEnvString('PROMPTFOO_SHARING_APP_BASE_URL') ||
  139. envars.getEnvString('PROMPTFOO_REMOTE_APP_BASE_URL');
  140. const hostname = baseUrl ? new URL(baseUrl).hostname : 'app.promptfoo.dev';
  141. expect(hostname).toBe('custom-domain.com');
  142. });
  143. it('should use PROMPTFOO_REMOTE_APP_BASE_URL for hostname when PROMPTFOO_SHARING_APP_BASE_URL is not set', () => {
  144. jest.mocked(envars.getEnvString).mockImplementation((key) => {
  145. if (key === 'PROMPTFOO_REMOTE_APP_BASE_URL') {
  146. return 'https://self-hosted-domain.com';
  147. }
  148. return '';
  149. });
  150. const baseUrl =
  151. envars.getEnvString('PROMPTFOO_SHARING_APP_BASE_URL') ||
  152. envars.getEnvString('PROMPTFOO_REMOTE_APP_BASE_URL');
  153. const hostname = baseUrl ? new URL(baseUrl).hostname : 'app.promptfoo.dev';
  154. expect(hostname).toBe('self-hosted-domain.com');
  155. });
  156. it('should prioritize PROMPTFOO_SHARING_APP_BASE_URL over PROMPTFOO_REMOTE_APP_BASE_URL', () => {
  157. jest.mocked(envars.getEnvString).mockImplementation((key) => {
  158. if (key === 'PROMPTFOO_SHARING_APP_BASE_URL') {
  159. return 'https://sharing-domain.com';
  160. }
  161. if (key === 'PROMPTFOO_REMOTE_APP_BASE_URL') {
  162. return 'https://remote-domain.com';
  163. }
  164. return '';
  165. });
  166. const baseUrl =
  167. envars.getEnvString('PROMPTFOO_SHARING_APP_BASE_URL') ||
  168. envars.getEnvString('PROMPTFOO_REMOTE_APP_BASE_URL');
  169. const hostname = baseUrl ? new URL(baseUrl).hostname : 'app.promptfoo.dev';
  170. expect(hostname).toBe('sharing-domain.com');
  171. });
  172. it('should use sharing config from promptfooconfig.yaml', async () => {
  173. const mockEval = {
  174. id: 'test-eval-id',
  175. prompts: ['test prompt'],
  176. config: {},
  177. save: jest.fn().mockResolvedValue(undefined),
  178. } as unknown as Eval;
  179. jest.spyOn(Eval, 'latest').mockResolvedValue(mockEval);
  180. const mockSharing = {
  181. apiBaseUrl: 'https://custom-api.example.com',
  182. appBaseUrl: 'https://custom-app.example.com',
  183. };
  184. jest.mocked(loadDefaultConfig).mockResolvedValue({
  185. defaultConfig: {
  186. sharing: mockSharing,
  187. },
  188. defaultConfigPath: 'promptfooconfig.yaml',
  189. });
  190. jest.mocked(isSharingEnabled).mockImplementation((evalObj) => {
  191. return !!evalObj.config.sharing;
  192. });
  193. jest
  194. .mocked(createShareableUrl)
  195. .mockResolvedValue('https://custom-app.example.com/eval/test-eval-id');
  196. const shareCmd = program.commands.find((c) => c.name() === 'share');
  197. await shareCmd?.parseAsync(['node', 'test']);
  198. expect(loadDefaultConfig).toHaveBeenCalledTimes(1);
  199. expect(mockEval.config.sharing).toEqual(mockSharing);
  200. expect(isSharingEnabled).toHaveBeenCalledWith(mockEval);
  201. expect(createShareableUrl).toHaveBeenCalledWith(mockEval, false);
  202. });
  203. it('should show cloud instructions and return null when sharing is not enabled', async () => {
  204. jest.mocked(isSharingEnabled).mockReturnValue(false);
  205. const shareCmd = program.commands.find((c) => c.name() === 'share');
  206. await shareCmd?.parseAsync(['node', 'test']);
  207. expect(logger.info).toHaveBeenCalledWith(
  208. expect.stringContaining('You need to have a cloud account'),
  209. );
  210. expect(process.exitCode).toBe(1);
  211. });
  212. });
  213. });
Tip!

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

Comments

Loading...