n8n/packages/@n8n/instance-ai/evaluations/binaryChecks/checks/switch-fallback-output-enabled.ts
José Braulio González Valido 700b32237f
feat(ai-builder): Surface WHAT-dimension binary checks per built workflow (no-changelog) (#30932)
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-26 12:18:52 +01:00

49 lines
1.5 KiB
TypeScript

import { validateWorkflow } from '@n8n/workflow-sdk';
import type { WorkflowJSON } from '@n8n/workflow-sdk';
import type { WorkflowResponse } from '../../clients/n8n-client';
import type { BinaryCheck } from '../types';
const SWITCH_NODE_TYPE = 'n8n-nodes-base.switch';
function toWorkflowJson(workflow: WorkflowResponse): WorkflowJSON {
return {
name: workflow.name,
nodes: (workflow.nodes ?? []).map((n, i) => ({
id: String(i),
name: n.name,
type: n.type,
typeVersion: n.typeVersion ?? 1,
parameters: n.parameters ?? {},
position: [0, 0],
...(n.onError ? { onError: n.onError } : {}),
})),
connections: workflow.connections,
} as unknown as WorkflowJSON;
}
export const switchFallbackOutputEnabled: BinaryCheck = {
name: 'switch_fallback_output_enabled',
description: 'Switch fallback branches are only wired when the extra fallback output exists',
kind: 'deterministic',
dimension: 'connection_topology',
run(workflow: WorkflowResponse) {
const switchNodes = (workflow.nodes ?? []).filter((n) => n.type === SWITCH_NODE_TYPE);
if (switchNodes.length === 0) return { pass: true, applicable: false };
const result = validateWorkflow(toWorkflowJson(workflow), {
allowNoTrigger: true,
allowDisconnectedNodes: true,
validateSchema: false,
});
const warnings = result.warnings.filter((w) => w.code === 'SWITCH_FALLBACK_OUTPUT_DISABLED');
if (warnings.length === 0) return { pass: true };
return {
pass: false,
comment: warnings.map((w) => w.message).join('; '),
};
},
};