n8n/packages/@n8n/instance-ai/evaluations/data/workflows/index.ts
José Braulio González Valido 30d9a168bc
feat(ai-builder): Add --prebuilt-workflows flag for eval CLI (no-changelog) (#29830)
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-07 11:47:29 +00:00

65 lines
1.8 KiB
TypeScript

import { readFileSync, readdirSync } from 'fs';
import { basename, join } from 'path';
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');
try {
return JSON.parse(content) as WorkflowTestCase;
} catch (error) {
throw new Error(
`Failed to parse test case ${filePath}: ${error instanceof Error ? error.message : String(error)}`,
);
}
}
/** Split a comma-separated CLI value into a normalized list of substring tokens. */
function parseSubstringList(value: string | undefined): string[] {
if (!value) return [];
return value
.split(',')
.map((s) => s.trim().toLowerCase())
.filter((s) => s.length > 0);
}
function getJsonFiles(filter?: string, exclude?: string): string[] {
const dir = __dirname;
let files = readdirSync(dir).filter((f) => f.endsWith('.json'));
const includeTokens = parseSubstringList(filter);
if (includeTokens.length > 0) {
files = files.filter((f) => {
const lower = f.toLowerCase();
return includeTokens.some((t) => lower.includes(t));
});
}
const excludeTokens = parseSubstringList(exclude);
if (excludeTokens.length > 0) {
files = files.filter((f) => {
const lower = f.toLowerCase();
return !excludeTokens.some((t) => lower.includes(t));
});
}
return files.map((f) => join(dir, f));
}
/** Load test cases with their file slugs (for LangSmith dataset sync derived IDs). */
export function loadWorkflowTestCasesWithFiles(
filter?: string,
exclude?: string,
): WorkflowTestCaseWithFile[] {
return getJsonFiles(filter, exclude).map((f) => ({
testCase: parseTestCaseFile(f),
fileSlug: basename(f, '.json'),
}));
}