n8n/packages/@n8n/instance-ai/evaluations/utils/load-eval-cases.ts
José Braulio González Valido 632ef22c0c
Some checks are pending
Build: Benchmark Image / build (push) Waiting to run
CI: Master (Build, Test, Lint) / Build for Github Cache (push) Waiting to run
CI: Master (Build, Test, Lint) / Unit tests (22.22.3) (push) Waiting to run
CI: Master (Build, Test, Lint) / Unit tests (24.18.0) (push) Waiting to run
CI: Master (Build, Test, Lint) / Lint (push) Waiting to run
CI: Master (Build, Test, Lint) / Performance (push) Waiting to run
CI: Master (Build, Test, Lint) / Notify Slack on failure (push) Blocked by required conditions
Util: Sync API Docs / sync-public-api (push) Waiting to run
feat(ai-builder): Carry an eval case's conversation seed in the case body (no-changelog) (#35074)
2026-07-28 16:26:08 +00:00

52 lines
1.5 KiB
TypeScript

import { readFileSync } from 'fs';
import { basename } from 'path';
import { getJsonFiles } from './get-json-files';
import { EvalTestCaseSchema } from '../harness/schema';
import type { WorkflowTestCase } from '../types';
export interface WorkflowTestCaseWithFile {
testCase: WorkflowTestCase;
/** Filename without extension, e.g. "contact-form-automation" */
fileSlug: string;
}
function parseTestCaseFile(filePath: string): WorkflowTestCase {
const content = readFileSync(filePath, 'utf-8');
let raw: unknown;
try {
raw = JSON.parse(content);
} catch (error) {
throw new Error(
`Failed to parse test case ${filePath}: ${error instanceof Error ? error.message : String(error)}`,
);
}
const parsed = EvalTestCaseSchema.safeParse(raw);
if (!parsed.success) {
const issues = parsed.error.issues
.map((i) => ` - ${i.path.join('.') || '(root)'}: ${i.message}`)
.join('\n');
throw new Error(`Invalid test case ${filePath}:\n${issues}`);
}
return parsed.data;
}
/** Load test cases with their file slugs (for LangSmith dataset sync derived IDs). */
export function loadEvalCasesFromDir(
dataDir: string,
filter?: string,
exclude?: string,
transform?: (testCase: WorkflowTestCase) => WorkflowTestCase,
): WorkflowTestCaseWithFile[] {
return getJsonFiles(dataDir, filter, exclude).map((f) => {
const testCase = parseTestCaseFile(f);
return {
testCase: transform ? transform(testCase) : testCase,
fileSlug: basename(f, '.json'),
};
});
}