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
|
- import { handleTraceSpanCount } from '../../src/assertions/traceSpanCount';
- import type { ApiProvider, AssertionParams, AtomicTestCase } from '../../src/types';
- import type { TraceData } from '../../src/types/tracing';
- const mockProvider: ApiProvider = {
- id: () => 'mock',
- callApi: async () => ({ output: 'mock' }),
- };
- const mockTraceData: TraceData = {
- traceId: 'test-trace-id',
- spans: [
- {
- spanId: 'span-1',
- name: 'llm.completion',
- startTime: 1000,
- endTime: 1500,
- },
- {
- spanId: 'span-2',
- name: 'llm.chat',
- startTime: 1600,
- endTime: 2000,
- },
- {
- spanId: 'span-3',
- name: 'database.query',
- startTime: 2100,
- endTime: 2200,
- },
- {
- spanId: 'span-4',
- name: 'retrieval.search',
- startTime: 2300,
- endTime: 2400,
- },
- {
- spanId: 'span-5',
- name: 'api.external_call',
- startTime: 2500,
- endTime: 2600,
- },
- ],
- };
- const defaultParams = {
- baseType: 'trace-span-count' as const,
- context: {
- vars: {},
- test: {} as AtomicTestCase,
- prompt: 'test prompt',
- logProbs: undefined,
- provider: mockProvider,
- providerResponse: { output: 'test output' },
- },
- output: 'test output',
- outputString: 'test output',
- providerResponse: { output: 'test output' },
- test: {} as AtomicTestCase,
- inverse: false,
- };
- describe('handleTraceSpanCount', () => {
- it('should pass when span count is within max limit', () => {
- const params: AssertionParams = {
- ...defaultParams,
- assertion: {
- type: 'trace-span-count',
- value: { pattern: '*llm*', max: 3 },
- },
- renderedValue: { pattern: '*llm*', max: 3 },
- context: {
- ...defaultParams.context,
- trace: mockTraceData,
- },
- };
- const result = handleTraceSpanCount(params);
- expect(result).toEqual({
- pass: true,
- score: 1,
- reason:
- 'Found 2 spans matching pattern "*llm*" (expected at most 3). Matched spans: llm.completion, llm.chat',
- assertion: params.assertion,
- });
- });
- it('should fail when span count exceeds max limit', () => {
- const params: AssertionParams = {
- ...defaultParams,
- assertion: {
- type: 'trace-span-count',
- value: { pattern: '*llm*', max: 1 },
- },
- renderedValue: { pattern: '*llm*', max: 1 },
- context: {
- ...defaultParams.context,
- trace: mockTraceData,
- },
- };
- const result = handleTraceSpanCount(params);
- expect(result).toEqual({
- pass: false,
- score: 0,
- reason:
- 'Found 2 spans matching pattern "*llm*", expected at most 1. Matched spans: llm.completion, llm.chat',
- assertion: params.assertion,
- });
- });
- it('should pass when span count meets min requirement', () => {
- const params: AssertionParams = {
- ...defaultParams,
- assertion: {
- type: 'trace-span-count',
- value: { pattern: '*retrieval*', min: 1 },
- },
- renderedValue: { pattern: '*retrieval*', min: 1 },
- context: {
- ...defaultParams.context,
- trace: mockTraceData,
- },
- };
- const result = handleTraceSpanCount(params);
- expect(result).toEqual({
- pass: true,
- score: 1,
- reason:
- 'Found 1 spans matching pattern "*retrieval*" (expected at least 1). Matched spans: retrieval.search',
- assertion: params.assertion,
- });
- });
- it('should fail when span count is below min requirement', () => {
- const params: AssertionParams = {
- ...defaultParams,
- assertion: {
- type: 'trace-span-count',
- value: { pattern: '*retrieval*', min: 2 },
- },
- renderedValue: { pattern: '*retrieval*', min: 2 },
- context: {
- ...defaultParams.context,
- trace: mockTraceData,
- },
- };
- const result = handleTraceSpanCount(params);
- expect(result).toEqual({
- pass: false,
- score: 0,
- reason:
- 'Found 1 spans matching pattern "*retrieval*", expected at least 2. Matched spans: retrieval.search',
- assertion: params.assertion,
- });
- });
- it('should pass when span count is within min and max range', () => {
- const params: AssertionParams = {
- ...defaultParams,
- assertion: {
- type: 'trace-span-count',
- value: { pattern: '*', min: 3, max: 10 },
- },
- renderedValue: { pattern: '*', min: 3, max: 10 },
- context: {
- ...defaultParams.context,
- trace: mockTraceData,
- },
- };
- const result = handleTraceSpanCount(params);
- expect(result).toEqual({
- pass: true,
- score: 1,
- reason:
- 'Found 5 spans matching pattern "*" (expected 3-10). Matched spans: llm.completion, llm.chat, database.query, retrieval.search, api.external_call',
- assertion: params.assertion,
- });
- });
- it('should handle patterns with ? wildcard', () => {
- const params: AssertionParams = {
- ...defaultParams,
- assertion: {
- type: 'trace-span-count',
- value: { pattern: 'llm.c?at', max: 1 },
- },
- renderedValue: { pattern: 'llm.c?at', max: 1 },
- context: {
- ...defaultParams.context,
- trace: mockTraceData,
- },
- };
- const result = handleTraceSpanCount(params);
- expect(result).toEqual({
- pass: true,
- score: 1,
- reason:
- 'Found 1 spans matching pattern "llm.c?at" (expected at most 1). Matched spans: llm.chat',
- assertion: params.assertion,
- });
- });
- it('should handle case-insensitive matching', () => {
- const params: AssertionParams = {
- ...defaultParams,
- assertion: {
- type: 'trace-span-count',
- value: { pattern: '*LLM*', max: 5 },
- },
- renderedValue: { pattern: '*LLM*', max: 5 },
- context: {
- ...defaultParams.context,
- trace: mockTraceData,
- },
- };
- const result = handleTraceSpanCount(params);
- expect(result).toEqual({
- pass: true,
- score: 1,
- reason:
- 'Found 2 spans matching pattern "*LLM*" (expected at most 5). Matched spans: llm.completion, llm.chat',
- assertion: params.assertion,
- });
- });
- it('should fail when no trace data is available', () => {
- const params: AssertionParams = {
- ...defaultParams,
- assertion: {
- type: 'trace-span-count',
- value: { pattern: '*', min: 1 },
- },
- renderedValue: { pattern: '*', min: 1 },
- };
- expect(() => handleTraceSpanCount(params)).toThrow(
- 'No trace data available for trace-span-count assertion',
- );
- });
- it('should handle empty trace spans', () => {
- const params: AssertionParams = {
- ...defaultParams,
- assertion: {
- type: 'trace-span-count',
- value: { pattern: '*', max: 0 },
- },
- renderedValue: { pattern: '*', max: 0 },
- context: {
- ...defaultParams.context,
- trace: { traceId: 'empty-trace', spans: [] },
- },
- };
- const result = handleTraceSpanCount(params);
- expect(result).toEqual({
- pass: true,
- score: 1,
- reason: 'Found 0 spans matching pattern "*" (expected at most 0)',
- assertion: params.assertion,
- });
- });
- it('should throw error for invalid value format', () => {
- const params: AssertionParams = {
- ...defaultParams,
- assertion: { type: 'trace-span-count', value: 'invalid' },
- renderedValue: 'invalid',
- context: {
- ...defaultParams.context,
- trace: mockTraceData,
- },
- };
- expect(() => handleTraceSpanCount(params)).toThrow(
- 'trace-span-count assertion must have a value object with pattern property',
- );
- });
- it('should throw error for missing pattern', () => {
- const params: AssertionParams = {
- ...defaultParams,
- assertion: { type: 'trace-span-count', value: { max: 5 } },
- renderedValue: { max: 5 },
- context: {
- ...defaultParams.context,
- trace: mockTraceData,
- },
- };
- expect(() => handleTraceSpanCount(params)).toThrow(
- 'trace-span-count assertion must have a value object with pattern property',
- );
- });
- });
|