-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathtest_case_loader.ts
More file actions
86 lines (74 loc) · 2.63 KB
/
test_case_loader.ts
File metadata and controls
86 lines (74 loc) · 2.63 KB
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
/**
* Shared test case loading and filtering utilities
*/
import { readFileSync } from 'node:fs';
import { dirname as pathDirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import type { BaseTestCase, TestData } from './types.js';
/**
* Load test cases from a JSON file
* Supports both relative and absolute paths
*
* @param filePath - Path to test cases JSON file (relative to caller or absolute)
* @returns Test data with version and test cases
*/
export function loadTestCases(filePath: string): TestData {
const filename = fileURLToPath(import.meta.url);
const dirname = pathDirname(filename);
// Support both relative (from evals/) and absolute paths
let testCasesPath: string;
if (filePath.startsWith('/')) {
testCasesPath = filePath;
} else {
// Relative to evals/ directory (two levels up from shared/)
testCasesPath = join(dirname, '..', filePath);
}
const fileContent = readFileSync(testCasesPath, 'utf-8');
return JSON.parse(fileContent) as TestData;
}
/**
* Filter test cases by category
* Supports wildcard patterns (e.g., "search-actors*" matches "search-actors-1", "search-actors-2", etc.)
*
* @param testCases - Array of test cases to filter
* @param category - Category pattern (supports * wildcard)
* @returns Filtered test cases
*/
export function filterByCategory<T extends BaseTestCase>(testCases: T[], category: string): T[] {
// Convert wildcard pattern to regex
const pattern = category.replace(/\*/g, '.*');
const regex = new RegExp(`^${pattern}$`);
return testCases.filter((testCase) => regex.test(testCase.category));
}
/**
* Filter test cases by ID using regex pattern
*
* @param testCases - Array of test cases to filter
* @param idPattern - Regex pattern to match against test case IDs
* @returns Filtered test cases
*/
export function filterById<T extends BaseTestCase>(testCases: T[], idPattern: string): T[] {
const regex = new RegExp(idPattern);
return testCases.filter((testCase) => regex.test(testCase.id));
}
/**
* Filter test cases by ID or category
* Generic filter function for workflow evaluations
*
* @param testCases - Array of test cases to filter
* @param options - Filter options (id and/or category)
* @returns Filtered test cases
*/
export function filterTestCases<T extends BaseTestCase>(
testCases: T[],
options: { id?: string; category?: string },
): T[] {
let filtered = testCases;
if (options.id) {
filtered = filterById(filtered, options.id);
}
if (options.category) {
filtered = filterByCategory(filtered, options.category);
}
return filtered;
}