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

show.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
253
254
255
256
257
258
259
260
261
262
263
  1. import { Command } from 'commander';
  2. import { handleDataset, handleEval, handlePrompt, showCommand } from '../../src/commands/show';
  3. import logger from '../../src/logger';
  4. import Eval from '../../src/models/eval';
  5. import { getDatasetFromHash, getEvalFromId, getPromptFromHash } from '../../src/util/database';
  6. import type { Prompt } from '../../src/types/prompts';
  7. jest.mock('../../src/logger');
  8. jest.mock('../../src/models/eval');
  9. jest.mock('../../src/util/database');
  10. describe('show command', () => {
  11. let program: Command;
  12. beforeEach(() => {
  13. program = new Command();
  14. jest.resetAllMocks();
  15. process.exitCode = undefined;
  16. });
  17. describe('show [id]', () => {
  18. it('should show latest eval if no id provided', async () => {
  19. const mockLatestEval = {
  20. id: 'latest123',
  21. createdAt: new Date(),
  22. config: {},
  23. results: [],
  24. prompts: [],
  25. vars: [],
  26. testResults: [],
  27. metrics: {},
  28. getTable: jest.fn().mockResolvedValue({
  29. head: { prompts: [], vars: [] },
  30. body: [],
  31. }),
  32. };
  33. jest.mocked(Eval.latest).mockResolvedValue(mockLatestEval as any);
  34. jest.mocked(Eval.findById).mockResolvedValue(mockLatestEval as any);
  35. await showCommand(program);
  36. await program.parseAsync(['node', 'test', 'show']);
  37. expect(Eval.latest).toHaveBeenCalledWith();
  38. });
  39. it('should show error if no eval found and no id provided', async () => {
  40. jest.mocked(Eval.latest).mockResolvedValue(undefined);
  41. await showCommand(program);
  42. await program.parseAsync(['node', 'test', 'show']);
  43. expect(logger.error).toHaveBeenCalledWith('No eval found');
  44. expect(process.exitCode).toBe(1);
  45. });
  46. it('should show eval if id matches eval', async () => {
  47. const evalId = 'eval123';
  48. const mockEval = {
  49. id: evalId,
  50. date: new Date(),
  51. config: {},
  52. results: [],
  53. getTable: jest.fn().mockResolvedValue({
  54. head: { prompts: [], vars: [] },
  55. body: [],
  56. }),
  57. };
  58. jest.mocked(getEvalFromId).mockResolvedValue(mockEval as any);
  59. jest.mocked(Eval.findById).mockResolvedValue(mockEval as any);
  60. await showCommand(program);
  61. await program.parseAsync(['node', 'test', 'show', evalId]);
  62. expect(getEvalFromId).toHaveBeenCalledWith(evalId);
  63. });
  64. it('should show prompt if id matches prompt', async () => {
  65. const promptId = 'prompt123';
  66. const mockPrompt: Prompt = {
  67. raw: 'test',
  68. label: 'Test Prompt',
  69. };
  70. jest.mocked(getPromptFromHash).mockResolvedValue({
  71. id: promptId,
  72. prompt: mockPrompt,
  73. evals: [],
  74. } as any);
  75. await showCommand(program);
  76. await program.parseAsync(['node', 'test', 'show', promptId]);
  77. expect(getPromptFromHash).toHaveBeenCalledWith(promptId);
  78. });
  79. it('should show dataset if id matches dataset', async () => {
  80. const datasetId = 'dataset123';
  81. jest.mocked(getDatasetFromHash).mockResolvedValue({
  82. id: datasetId,
  83. prompts: [],
  84. recentEvalDate: new Date(),
  85. recentEvalId: 'eval123',
  86. count: 0,
  87. testCases: [],
  88. } as any);
  89. await showCommand(program);
  90. await program.parseAsync(['node', 'test', 'show', datasetId]);
  91. expect(getDatasetFromHash).toHaveBeenCalledWith(datasetId);
  92. });
  93. it('should show error if no resource found with id', async () => {
  94. const invalidId = 'invalid123';
  95. jest.mocked(getEvalFromId).mockResolvedValue(undefined);
  96. jest.mocked(getPromptFromHash).mockResolvedValue(undefined);
  97. jest.mocked(getDatasetFromHash).mockResolvedValue(undefined);
  98. await showCommand(program);
  99. await program.parseAsync(['node', 'test', 'show', invalidId]);
  100. expect(logger.error).toHaveBeenCalledWith(`No resource found with ID ${invalidId}`);
  101. });
  102. });
  103. describe('show eval [id]', () => {
  104. it('should show latest eval if no id provided', async () => {
  105. const mockLatestEval = {
  106. id: 'latest123',
  107. createdAt: new Date(),
  108. config: {},
  109. results: [],
  110. prompts: [],
  111. vars: [],
  112. testResults: [],
  113. metrics: {},
  114. getTable: jest.fn().mockResolvedValue({
  115. head: { prompts: [], vars: [] },
  116. body: [],
  117. }),
  118. };
  119. jest.mocked(Eval.latest).mockResolvedValue(mockLatestEval as any);
  120. jest.mocked(Eval.findById).mockResolvedValue(mockLatestEval as any);
  121. await showCommand(program);
  122. await program.parseAsync(['node', 'test', 'show', 'eval']);
  123. expect(Eval.latest).toHaveBeenCalledWith();
  124. });
  125. it('should show error if no eval found and no id provided', async () => {
  126. jest.mocked(Eval.latest).mockResolvedValue(undefined);
  127. await showCommand(program);
  128. await program.parseAsync(['node', 'test', 'show', 'eval']);
  129. expect(logger.error).toHaveBeenCalledWith('No eval found');
  130. expect(process.exitCode).toBe(1);
  131. });
  132. it('should show eval details for provided id', async () => {
  133. const evalId = 'eval123';
  134. jest.mocked(Eval.findById).mockResolvedValue({
  135. id: evalId,
  136. createdAt: new Date(),
  137. config: {},
  138. results: [],
  139. prompts: [],
  140. vars: [],
  141. testResults: [],
  142. metrics: {},
  143. getTable: jest.fn().mockResolvedValue({
  144. head: { prompts: [], vars: [] },
  145. body: [],
  146. }),
  147. } as any);
  148. await showCommand(program);
  149. await program.parseAsync(['node', 'test', 'show', 'eval', evalId]);
  150. expect(Eval.findById).toHaveBeenCalledWith(evalId);
  151. });
  152. });
  153. describe('show prompt', () => {
  154. it('should show prompt details', async () => {
  155. const promptId = 'prompt123';
  156. const mockPrompt: Prompt = {
  157. raw: 'test',
  158. label: 'Test Prompt',
  159. };
  160. jest.mocked(getPromptFromHash).mockResolvedValue({
  161. id: promptId,
  162. prompt: mockPrompt,
  163. evals: [],
  164. } as any);
  165. await showCommand(program);
  166. await program.parseAsync(['node', 'test', 'show', 'prompt', promptId]);
  167. expect(getPromptFromHash).toHaveBeenCalledWith(promptId);
  168. });
  169. });
  170. describe('show dataset', () => {
  171. it('should show dataset details', async () => {
  172. const datasetId = 'dataset123';
  173. jest.mocked(getDatasetFromHash).mockResolvedValue({
  174. id: datasetId,
  175. prompts: [],
  176. recentEvalDate: new Date(),
  177. recentEvalId: 'eval123',
  178. count: 0,
  179. testCases: [],
  180. } as any);
  181. await showCommand(program);
  182. await program.parseAsync(['node', 'test', 'show', 'dataset', datasetId]);
  183. expect(getDatasetFromHash).toHaveBeenCalledWith(datasetId);
  184. });
  185. });
  186. });
  187. describe('handlers', () => {
  188. beforeEach(() => {
  189. jest.resetAllMocks();
  190. process.exitCode = undefined;
  191. });
  192. describe('handlePrompt', () => {
  193. it('should handle prompt not found', async () => {
  194. const promptId = 'nonexistent';
  195. jest.mocked(getPromptFromHash).mockResolvedValue(undefined);
  196. await handlePrompt(promptId);
  197. expect(logger.error).toHaveBeenCalledWith(`Prompt with ID ${promptId} not found.`);
  198. });
  199. });
  200. describe('handleEval', () => {
  201. it('should handle eval not found', async () => {
  202. const evalId = 'nonexistent';
  203. jest.mocked(Eval.findById).mockResolvedValue(undefined);
  204. await handleEval(evalId);
  205. expect(logger.error).toHaveBeenCalledWith(`No evaluation found with ID ${evalId}`);
  206. });
  207. });
  208. describe('handleDataset', () => {
  209. it('should handle dataset not found', async () => {
  210. const datasetId = 'nonexistent';
  211. jest.mocked(getDatasetFromHash).mockResolvedValue(undefined);
  212. await handleDataset(datasetId);
  213. expect(logger.error).toHaveBeenCalledWith(`Dataset with ID ${datasetId} not found.`);
  214. });
  215. });
  216. });
Tip!

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

Comments

Loading...