forked from promptfoo/promptfoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.test.ts
247 lines (216 loc) · 6.42 KB
/
table.test.ts
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
import chalk from 'chalk';
import Table from 'cli-table3';
import { TERMINAL_MAX_WIDTH } from '../src/constants';
import { generateTable, wrapTable } from '../src/table';
import { ResultFailureReason, type EvaluateTable } from '../src/types';
jest.mock('cli-table3');
describe('table', () => {
beforeEach(() => {
jest.clearAllMocks();
// @ts-ignore
jest.mocked(Table).mockImplementation(() => ({
push: jest.fn(),
toString: jest.fn().mockReturnValue('mocked table string'),
options: {},
width: 0,
length: 0,
pop: jest.fn(),
}));
});
describe('generateTable', () => {
const mockEvaluateTable: EvaluateTable = {
head: {
vars: ['var1', 'var2'],
prompts: [
{
provider: 'test-provider',
label: 'test-label',
raw: 'test prompt',
id: 'test-id',
display: 'test display',
config: {},
// @ts-ignore
metrics: {},
},
],
},
body: [
{
vars: ['value1', 'value2'],
outputs: [
{
pass: true,
score: 1,
text: 'passing test',
failureReason: ResultFailureReason.NONE,
cost: 0,
id: 'test',
latencyMs: 0,
namedScores: {},
tokenUsage: {},
metadata: {},
prompt: 'test prompt',
testCase: {},
},
],
test: {},
testIdx: 0,
},
{
vars: ['value3', 'value4'],
outputs: [
{
pass: false,
score: 0,
text: 'failing test',
failureReason: ResultFailureReason.ASSERT,
cost: 0,
id: 'test',
latencyMs: 0,
namedScores: {},
tokenUsage: {},
metadata: {},
prompt: 'test prompt',
testCase: {},
},
],
test: {},
testIdx: 1,
},
],
};
it('should generate table with correct headers', () => {
generateTable(mockEvaluateTable);
expect(Table).toHaveBeenCalledWith({
head: ['var1', 'var2', '[test-provider] test-label'],
colWidths: [40, 40, 40],
wordWrap: true,
wrapOnWordBoundary: true,
style: { head: ['blue', 'bold'] },
});
});
it('should handle passing and failing rows correctly', () => {
const table = new Table({});
jest.mocked(Table).mockReturnValue(table);
generateTable(mockEvaluateTable);
expect(table.push).toHaveBeenCalledWith([
'value1',
'value2',
chalk.green('[PASS] ') + 'passing test',
]);
expect(table.push).toHaveBeenCalledWith([
'value3',
'value4',
chalk.red('[FAIL] ') + chalk.red.bold('failing test'),
]);
});
it('should respect maxRows parameter', () => {
const table = new Table({});
jest.mocked(Table).mockReturnValue(table);
generateTable(mockEvaluateTable, 250, 1);
expect(table.push).toHaveBeenCalledTimes(1);
});
it('should respect tableCellMaxLength parameter', () => {
const longText = 'a'.repeat(300);
const shortMaxLength = 10;
const testTable: EvaluateTable = {
head: {
vars: [longText],
prompts: [],
},
body: [
{
vars: [longText],
outputs: [
{
pass: true,
score: 1,
text: 'test',
failureReason: ResultFailureReason.NONE,
cost: 0,
id: 'test',
latencyMs: 0,
namedScores: {},
tokenUsage: {},
metadata: {},
prompt: 'test prompt',
testCase: {},
},
],
test: {},
testIdx: 0,
},
],
};
generateTable(testTable, shortMaxLength);
expect(Table).toHaveBeenCalledWith(
expect.objectContaining({
head: [expect.stringMatching(/^a{7}\.{3}$/)],
}),
);
});
});
describe('wrapTable', () => {
it('should return message when no rows provided', () => {
const result = wrapTable([]);
expect(result).toBe('No data to display');
});
it('should create table with correct headers and data', () => {
const rows = [
{ col1: 'value1', col2: 'value2' },
{ col1: 'value3', col2: 'value4' },
];
const table = new Table({});
jest.mocked(Table).mockReturnValue(table);
wrapTable(rows);
expect(Table).toHaveBeenCalledWith({
head: ['col1', 'col2'],
colWidths: expect.any(Array),
wordWrap: true,
wrapOnWordBoundary: true,
});
expect(table.push).toHaveBeenCalledWith(['value1', 'value2']);
expect(table.push).toHaveBeenCalledWith(['value3', 'value4']);
});
it('should create table with custom column widths', () => {
const rows = [
{ col1: 'value1', col2: 'value2', col3: 'value3' },
{ col1: 'value4', col2: 'value5', col3: 'value6' },
];
const columnWidths = {
col1: 10,
col2: 20,
col3: 15,
};
const table = new Table({});
jest.mocked(Table).mockReturnValue(table);
wrapTable(rows, columnWidths);
expect(Table).toHaveBeenCalledWith({
head: ['col1', 'col2', 'col3'],
colWidths: [10, 20, 15],
wordWrap: true,
wrapOnWordBoundary: true,
});
expect(table.push).toHaveBeenCalledWith(['value1', 'value2', 'value3']);
expect(table.push).toHaveBeenCalledWith(['value4', 'value5', 'value6']);
});
it('should use default width for columns not specified in columnWidths', () => {
const rows = [{ col1: 'value1', col2: 'value2', col3: 'value3' }];
const columnWidths = {
col1: 10,
// col2 not specified
col3: 15,
};
const table = new Table({});
jest.mocked(Table).mockReturnValue(table);
wrapTable(rows, columnWidths);
const defaultWidth = Math.floor(TERMINAL_MAX_WIDTH / 3); // 3 columns
expect(Table).toHaveBeenCalledWith({
head: ['col1', 'col2', 'col3'],
colWidths: [10, defaultWidth, 15],
wordWrap: true,
wrapOnWordBoundary: true,
});
});
});
});