Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

5.0 KB
Raw Blame
// @vitest-environment jsdom
import {
  AiRouterAction,
  AiRouterMatchMode,
  BranchExecutionType,
  CodeAction,
  EmptyTrigger,
  FlowAction,
  FlowActionType,
  FlowTriggerType,
  FlowVersion,
  FlowVersionState,
} from '@activepieces/shared';
import { describe, expect, it, vi } from 'vitest';

import { flowCanvasUtils } from '@/app/builder/flow-canvas/utils/flow-canvas-utils';
import { ApEdgeType, ApNodeType } from '@/app/builder/flow-canvas/utils/types';

vi.mock('@/features/flow-runs', () => ({
  flowRunUtils: {
    extractStepOutput: () => undefined,
    findLastStepWithStatus: () => null,
  },
}));

const createCodeAction = (
  name: string,
  nextAction?: CodeAction,
): CodeAction => ({
  name,
  valid: true,
  displayName: name,
  lastUpdatedDate: '2026-01-01T00:00:00.000Z',
  type: FlowActionType.CODE,
  settings: {
    sourceCode: {
      code: 'export const code = async () => true;',
      packageJson: '{}',
    },
    input: {},
    errorHandlingOptions: {},
  },
  nextAction,
});

const createAiRouter = (): AiRouterAction => ({
  name: 'step_1',
  valid: true,
  displayName: 'AI Router',
  lastUpdatedDate: '2026-01-01T00:00:00.000Z',
  type: FlowActionType.AI_ROUTER,
  settings: {
    text: '{{trigger.body.message}}',
    question: 'Which team?',
    matchMode: AiRouterMatchMode.BEST_MATCH,
    branches: [
      {
        branchType: BranchExecutionType.CONDITION,
        branchName: 'Billing',
        description: 'payments',
      },
      {
        branchType: BranchExecutionType.CONDITION,
        branchName: 'Technical',
        description: 'bugs',
      },
      {
        branchType: BranchExecutionType.FALLBACK,
        branchName: 'Otherwise',
        description: 'else',
      },
    ],
  },
  children: [null, null, null],
});

const createFlowVersion = (firstAction?: FlowAction): FlowVersion => {
  const trigger: EmptyTrigger = {
    name: 'trigger',
    valid: false,
    displayName: 'Select Trigger',
    type: FlowTriggerType.EMPTY,
    settings: {},
    lastUpdatedDate: '2026-01-01T00:00:00.000Z',
    nextAction: firstAction,
  };
  return {
    id: 'version-id',
    created: '2026-01-01T00:00:00.000Z',
    updated: '2026-01-01T00:00:00.000Z',
    flowId: 'flow-id',
    displayName: 'Test flow',
    trigger,
    updatedBy: null,
    valid: false,
    schemaVersion: null,
    agentIds: [],
    state: FlowVersionState.DRAFT,
    connectionIds: [],
    backupFiles: null,
    notes: [],
  };
};

const getStepNode = (
  graph: ReturnType<typeof flowCanvasUtils.createFlowGraph>,
  name: string,
) => {
  const node = graph.nodes.find(
    (n) => n.id === name && n.type === ApNodeType.STEP,
  );
  expect(node).toBeDefined();
  return node!;
};

describe('flowCanvasUtils.createFlowGraph', () => {
  it('keeps the existing vertical layout untouched', () => {
    const graph = flowCanvasUtils.createFlowGraph({
      version: createFlowVersion(createCodeAction('step_1')),
      notes: [],
      orientation: 'vertical',
    });
    expect(getStepNode(graph, 'trigger').position).toEqual({ x: 0, y: 0 });
    expect(getStepNode(graph, 'step_1').position).toEqual({ x: 0, y: 120 });
  });

  it('lays steps out left to right in horizontal orientation', () => {
    const graph = flowCanvasUtils.createFlowGraph({
      version: createFlowVersion(
        createCodeAction('step_1', createCodeAction('step_2')),
      ),
      notes: [],
      orientation: 'horizontal',
    });
    expect(getStepNode(graph, 'trigger').position).toEqual({ x: 0, y: 0 });
    expect(getStepNode(graph, 'step_1').position).toEqual({ x: 160, y: 0 });
    expect(getStepNode(graph, 'step_2').position).toEqual({ x: 320, y: 0 });
  });

  it('connects each step edge to its subgraph end node', () => {
    const graph = flowCanvasUtils.createFlowGraph({
      version: createFlowVersion(createCodeAction('step_1')),
      notes: [],
      orientation: 'vertical',
    });
    const triggerEdge = graph.edges.find((edge) => edge.source === 'trigger');
    expect(triggerEdge?.type).toEqual(ApEdgeType.STRAIGHT_LINE);
    expect(triggerEdge?.target).toEqual('trigger-subgraph-end');
    const lastEdge = graph.edges.find((edge) => edge.source === 'step_1');
    expect(lastEdge?.target).toEqual('step_1-subgraph-end');
  });
});

describe('flowCanvasUtils.createFlowGraph with an AI router', () => {
  it('draws one labelled branch per route', () => {
    const graph = flowCanvasUtils.createFlowGraph({
      version: createFlowVersion(createAiRouter()),
      notes: [],
      orientation: 'vertical',
    });

    const branchLabels = graph.edges
      .filter((edge) => edge.type === ApEdgeType.ROUTER_START_EDGE)
      .map((edge) => edge.data?.label);

    expect(branchLabels).toEqual(['Billing', 'Technical', 'Otherwise']);
  });

  it('gives every empty route its own add button', () => {
    const graph = flowCanvasUtils.createFlowGraph({
      version: createFlowVersion(createAiRouter()),
      notes: [],
      orientation: 'vertical',
    });

    const addButtons = graph.nodes.filter(
      (node) => node.type === ApNodeType.BIG_ADD_BUTTON,
    );

    expect(addButtons).toHaveLength(3);
  });
});