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
|
- import { getDb } from '../../src/database';
- import { getUserEmail } from '../../src/globalConfig/accounts';
- import { runDbMigrations } from '../../src/migrate';
- import Eval, { getEvalSummaries } from '../../src/models/eval';
- import type { Prompt } from '../../src/types';
- import EvalFactory from '../factories/evalFactory';
- jest.mock('../../src/globalConfig/accounts', () => ({
- ...jest.requireActual('../../src/globalConfig/accounts'),
- getUserEmail: jest.fn(),
- }));
- describe('evaluator', () => {
- beforeAll(async () => {
- await runDbMigrations();
- });
- beforeEach(async () => {
- // Clear all tables before each test
- const db = getDb();
- // Delete related tables first
- await db.run('DELETE FROM eval_results');
- await db.run('DELETE FROM evals_to_datasets');
- await db.run('DELETE FROM evals_to_prompts');
- await db.run('DELETE FROM evals_to_tags');
- // Then delete from main table
- await db.run('DELETE FROM evals');
- });
- describe('summaryResults', () => {
- it('should return all evaluations', async () => {
- const eval1 = await EvalFactory.create();
- const eval2 = await EvalFactory.create();
- const eval3 = await EvalFactory.createOldResult();
- const evaluations = await getEvalSummaries();
- expect(evaluations).toHaveLength(3);
- expect(evaluations).toContainEqual(
- expect.objectContaining({
- evalId: eval1.id,
- createdAt: eval1.createdAt,
- description: null,
- numTests: 2,
- isRedteam: 0,
- passRate: 50,
- label: eval1.id,
- }),
- );
- expect(evaluations).toContainEqual(
- expect.objectContaining({
- evalId: eval2.id,
- createdAt: eval2.createdAt,
- description: null,
- numTests: 2,
- isRedteam: 0,
- passRate: 50,
- label: eval2.id,
- }),
- );
- expect(evaluations).toContainEqual(expect.objectContaining({ evalId: eval3.id }));
- });
- it('should return evaluations in descending order by createdAt', async () => {
- const eval1 = await EvalFactory.create();
- const eval2 = await EvalFactory.create();
- const eval3 = await EvalFactory.create();
- const evaluations = await getEvalSummaries();
- expect(evaluations).toHaveLength(3);
- expect(evaluations[0].evalId).toBe(eval3.id);
- expect(evaluations[1].evalId).toBe(eval2.id);
- expect(evaluations[2].evalId).toBe(eval1.id);
- });
- });
- describe('delete', () => {
- it('should delete an evaluation', async () => {
- const eval1 = await EvalFactory.create();
- const eval_ = await Eval.findById(eval1.id);
- expect(eval_).toBeDefined();
- await eval1.delete();
- const eval_2 = await Eval.findById(eval1.id);
- expect(eval_2).toBeUndefined();
- });
- });
- describe('create', () => {
- it('should use provided author when available', async () => {
- const providedAuthor = 'provided@example.com';
- const config = { description: 'Test eval' };
- const renderedPrompts: Prompt[] = [
- { raw: 'Test prompt', display: 'Test prompt', label: 'Test label' } as Prompt,
- ];
- const evaluation = await Eval.create(config, renderedPrompts, { author: providedAuthor });
- expect(evaluation.author).toBe(providedAuthor);
- const persistedEval = await Eval.findById(evaluation.id);
- expect(persistedEval?.author).toBe(providedAuthor);
- });
- it('should use default author from getUserEmail when not provided', async () => {
- const mockEmail = 'default@example.com';
- jest.mocked(getUserEmail).mockReturnValue(mockEmail);
- const config = { description: 'Test eval' };
- const renderedPrompts: Prompt[] = [
- { raw: 'Test prompt', display: 'Test prompt', label: 'Test label' } as Prompt,
- ];
- const evaluation = await Eval.create(config, renderedPrompts);
- const persistedEval = await Eval.findById(evaluation.id);
- expect(persistedEval?.author).toBe(mockEmail);
- });
- });
- describe('getStats', () => {
- it('should accumulate assertion token usage correctly', () => {
- const eval1 = new Eval({});
- eval1.prompts = [
- {
- raw: 'test',
- metrics: {
- tokenUsage: {
- prompt: 10,
- completion: 20,
- cached: 5,
- total: 35,
- numRequests: 1,
- assertions: {
- total: 100,
- prompt: 40,
- completion: 50,
- cached: 10,
- },
- },
- },
- } as any,
- {
- raw: 'test2',
- metrics: {
- tokenUsage: {
- prompt: 15,
- completion: 25,
- cached: 10,
- total: 50,
- numRequests: 1,
- assertions: {
- total: 200,
- prompt: 80,
- completion: 100,
- cached: 20,
- },
- },
- },
- } as any,
- ];
- const stats = eval1.getStats();
- expect(stats.tokenUsage.assertions).toEqual({
- total: 300,
- prompt: 120,
- completion: 150,
- cached: 30,
- });
- });
- it('should handle missing assertion token usage', () => {
- const eval1 = new Eval({});
- eval1.prompts = [
- {
- raw: 'test',
- metrics: {
- tokenUsage: {
- prompt: 10,
- completion: 20,
- cached: 5,
- total: 35,
- numRequests: 1,
- },
- },
- } as any,
- ];
- const stats = eval1.getStats();
- expect(stats.tokenUsage.assertions).toEqual({
- total: 0,
- prompt: 0,
- completion: 0,
- cached: 0,
- });
- });
- it('should handle mix of prompts with and without assertion usage', () => {
- const eval1 = new Eval({});
- eval1.prompts = [
- {
- raw: 'test1',
- metrics: {
- tokenUsage: {
- prompt: 10,
- completion: 20,
- cached: 5,
- total: 35,
- numRequests: 1,
- assertions: {
- total: 100,
- prompt: 40,
- completion: 50,
- cached: 10,
- },
- },
- },
- } as any,
- {
- raw: 'test2',
- metrics: {
- tokenUsage: {
- prompt: 15,
- completion: 25,
- cached: 10,
- total: 50,
- numRequests: 1,
- },
- },
- } as any,
- ];
- const stats = eval1.getStats();
- expect(stats.tokenUsage.assertions).toEqual({
- total: 100,
- prompt: 40,
- completion: 50,
- cached: 10,
- });
- });
- });
- });
|