Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

22.6 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { coerceWorkflowProposal } from '../../lib/workflows/workflowProposal';
import {
  buildWorkflow,
  discoverWorkflows,
  dismissSuggestion,
  flowsBuildCancel,
  type FlowSuggestion,
  getApprovalManifest,
  getFlowRun,
  listAllFlowRuns,
  listFlowRuns,
  listFlows,
  listSuggestions,
  markSuggestionBuilt,
  resumeFlow,
  runFlow,
  runFlowDetached,
  setFlowEnabled,
} from './flowsApi';

const mockCallCoreRpc = vi.fn();
vi.mock('../coreRpcClient', () => ({ callCoreRpc: (...a: unknown[]) => mockCallCoreRpc(...a) }));
vi.mock('../../lib/workflows/workflowProposal', async importOriginal => {
  const actual = await importOriginal<typeof import('../../lib/workflows/workflowProposal')>();
  return { ...actual, coerceWorkflowProposal: vi.fn(actual.coerceWorkflowProposal) };
});

/** Every `flows_*` handler wraps its payload via `Outcome::single_log`. */
function cliEnvelope<T>(
  result: T,
  logs: string[] = ['did something']
): { result: T; logs: string[] } {
  return { result, logs };
}

describe('flowsApi', () => {
  beforeEach(() => {
    mockCallCoreRpc.mockReset();
    vi.mocked(coerceWorkflowProposal).mockClear();
  });

  describe('resumeFlow', () => {
    it('calls openhuman.flows_resume with id, thread_id, approvals', async () => {
      mockCallCoreRpc.mockResolvedValue(
        cliEnvelope({ output: { nodes: {} }, pending_approvals: [], thread_id: 't1' })
      );

      const result = await resumeFlow('flow-1', 't1', ['node-a']);

      expect(mockCallCoreRpc).toHaveBeenCalledWith({
        method: 'openhuman.flows_resume',
        params: { id: 'flow-1', thread_id: 't1', approvals: ['node-a'] },
        // flows_resume can run ~600s server-side, so the client budget is raised.
        timeoutMs: 610_000,
      });
      expect(result).toEqual({ output: { nodes: {} }, pending_approvals: [], thread_id: 't1' });
    });

    it('unwraps the { result, logs } envelope', async () => {
      const payload = { output: null, pending_approvals: ['node-b'], thread_id: 't2' };
      mockCallCoreRpc.mockResolvedValue(cliEnvelope(payload));

      const result = await resumeFlow('flow-1', 't2', ['node-b']);

      expect(result).toEqual(payload);
    });

    it('passes through a bare (unwrapped) payload unchanged', async () => {
      const payload = { output: null, pending_approvals: [], thread_id: 't3' };
      mockCallCoreRpc.mockResolvedValue(payload);

      const result = await resumeFlow('flow-1', 't3', []);

      expect(result).toEqual(payload);
    });

    it('propagates rejection from callCoreRpc', async () => {
      mockCallCoreRpc.mockRejectedValue(new Error('no pending approval matches'));

      await expect(resumeFlow('flow-1', 't1', ['wrong-node'])).rejects.toThrow(
        'no pending approval matches'
      );
    });
  });

  describe('flowsBuildCancel', () => {
    it('calls openhuman.flows_build_cancel with thread_id + null request_id and returns cancelled', async () => {
      mockCallCoreRpc.mockResolvedValue(cliEnvelope({ cancelled: true }));

      const cancelled = await flowsBuildCancel('t1');

      expect(mockCallCoreRpc).toHaveBeenCalledWith({
        method: 'openhuman.flows_build_cancel',
        params: { thread_id: 't1', request_id: null },
      });
      expect(cancelled).toBe(true);
    });

    it('scopes the cancel with request_id when given', async () => {
      mockCallCoreRpc.mockResolvedValue(cliEnvelope({ cancelled: false }));

      const cancelled = await flowsBuildCancel('t1', 'req-9');

      expect(mockCallCoreRpc).toHaveBeenCalledWith({
        method: 'openhuman.flows_build_cancel',
        params: { thread_id: 't1', request_id: 'req-9' },
      });
      // `false` is not an error — it just means nothing was in flight to cancel.
      expect(cancelled).toBe(false);
    });

    it('defaults to false when the payload omits `cancelled`', async () => {
      mockCallCoreRpc.mockResolvedValue(cliEnvelope({}));
      await expect(flowsBuildCancel('t1')).resolves.toBe(false);
    });

    it('propagates rejection from callCoreRpc', async () => {
      mockCallCoreRpc.mockRejectedValue(new Error('rpc down'));
      await expect(flowsBuildCancel('t1')).rejects.toThrow('rpc down');
    });
  });

  describe('listFlowRuns', () => {
    it('calls openhuman.flows_list_runs with id', async () => {
      mockCallCoreRpc.mockResolvedValue(cliEnvelope([]));

      await listFlowRuns('flow-1');

      expect(mockCallCoreRpc).toHaveBeenCalledWith({
        method: 'openhuman.flows_list_runs',
        params: { id: 'flow-1' },
      });
    });

    it('passes limit when provided', async () => {
      mockCallCoreRpc.mockResolvedValue(cliEnvelope([]));

      await listFlowRuns('flow-1', 5);

      expect(mockCallCoreRpc).toHaveBeenCalledWith({
        method: 'openhuman.flows_list_runs',
        params: { id: 'flow-1', limit: 5 },
      });
    });

    it('unwraps the { result, logs } envelope into the run array', async () => {
      const runs = [
        {
          id: 't1',
          flow_id: 'flow-1',
          thread_id: 't1',
          status: 'completed' as const,
          started_at: '2026-01-01T00:00:00Z',
          finished_at: '2026-01-01T00:01:00Z',
          steps: [],
          pending_approvals: [],
          error: null,
        },
      ];
      mockCallCoreRpc.mockResolvedValue(cliEnvelope(runs));

      const result = await listFlowRuns('flow-1');

      expect(result).toEqual(runs);
    });

    it('propagates rejection from callCoreRpc', async () => {
      mockCallCoreRpc.mockRejectedValue(new Error('boom'));

      await expect(listFlowRuns('flow-1')).rejects.toThrow('boom');
    });
  });

  describe('listAllFlowRuns', () => {
    it('calls openhuman.flows_list_all_runs with no params by default', async () => {
      mockCallCoreRpc.mockResolvedValue(cliEnvelope([]));

      await listAllFlowRuns();

      expect(mockCallCoreRpc).toHaveBeenCalledWith({
        method: 'openhuman.flows_list_all_runs',
        params: {},
      });
    });

    it('passes limit when provided and unwraps the envelope', async () => {
      const runs = [
        {
          id: 't1',
          flow_id: 'flow-1',
          thread_id: 't1',
          status: 'failed' as const,
          started_at: '2026-01-01T00:00:00Z',
          finished_at: null,
          steps: [],
          pending_approvals: [],
          error: 'boom',
        },
      ];
      mockCallCoreRpc.mockResolvedValue(cliEnvelope(runs));

      const result = await listAllFlowRuns(50);

      expect(mockCallCoreRpc).toHaveBeenCalledWith({
        method: 'openhuman.flows_list_all_runs',
        params: { limit: 50 },
      });
      expect(result).toEqual(runs);
    });

    it('propagates rejection from callCoreRpc', async () => {
      mockCallCoreRpc.mockRejectedValue(new Error('rpc down'));

      await expect(listAllFlowRuns()).rejects.toThrow('rpc down');
    });
  });

  describe('getFlowRun', () => {
    it('calls openhuman.flows_get_run with run_id', async () => {
      const run = {
        id: 't1',
        flow_id: 'flow-1',
        thread_id: 't1',
        status: 'pending_approval' as const,
        started_at: '2026-01-01T00:00:00Z',
        finished_at: null,
        steps: [{ node_id: 'n1', output: { ok: true } }],
        pending_approvals: ['n2'],
        error: null,
      };
      mockCallCoreRpc.mockResolvedValue(cliEnvelope(run));

      const result = await getFlowRun('t1');

      expect(mockCallCoreRpc).toHaveBeenCalledWith({
        method: 'openhuman.flows_get_run',
        params: { run_id: 't1' },
      });
      expect(result).toEqual(run);
    });

    it('propagates rejection from callCoreRpc', async () => {
      mockCallCoreRpc.mockRejectedValue(new Error('flow run not found'));

      await expect(getFlowRun('missing')).rejects.toThrow('flow run not found');
    });
  });

  describe('listFlows', () => {
    const flow = {
      id: 'flow-1',
      name: 'Demo flow',
      enabled: true,
      graph: { nodes: [], edges: [] },
      created_at: '2026-01-01T00:00:00Z',
      updated_at: '2026-01-01T00:00:00Z',
      last_run_at: null,
      last_status: null,
      require_approval: false,
    };

    it('calls openhuman.flows_list with no params', async () => {
      mockCallCoreRpc.mockResolvedValue(cliEnvelope([flow]));

      await listFlows();

      expect(mockCallCoreRpc).toHaveBeenCalledWith({ method: 'openhuman.flows_list', params: {} });
    });

    it('unwraps the { result, logs } envelope into the flow array', async () => {
      mockCallCoreRpc.mockResolvedValue(cliEnvelope([flow]));

      const result = await listFlows();

      expect(result).toEqual([flow]);
    });

    it('propagates rejection from callCoreRpc', async () => {
      mockCallCoreRpc.mockRejectedValue(new Error('boom'));

      await expect(listFlows()).rejects.toThrow('boom');
    });
  });

  describe('setFlowEnabled', () => {
    it('calls openhuman.flows_set_enabled with id and enabled', async () => {
      const flow = {
        id: 'flow-1',
        name: 'Demo flow',
        enabled: false,
        graph: {},
        created_at: '2026-01-01T00:00:00Z',
        updated_at: '2026-01-01T00:00:00Z',
        last_run_at: null,
        last_status: null,
        require_approval: false,
      };
      mockCallCoreRpc.mockResolvedValue(cliEnvelope(flow));

      const result = await setFlowEnabled('flow-1', false);

      expect(mockCallCoreRpc).toHaveBeenCalledWith({
        method: 'openhuman.flows_set_enabled',
        params: { id: 'flow-1', enabled: false },
      });
      expect(result).toEqual(flow);
    });

    it('propagates rejection from callCoreRpc', async () => {
      mockCallCoreRpc.mockRejectedValue(new Error('flow not found'));

      await expect(setFlowEnabled('missing', true)).rejects.toThrow('flow not found');
    });
  });

  describe('runFlow', () => {
    it('calls openhuman.flows_run with id, input, and the extended timeout', async () => {
      mockCallCoreRpc.mockResolvedValue(
        cliEnvelope({ output: { nodes: {} }, pending_approvals: [], thread_id: 't1' })
      );

      const result = await runFlow('flow-1');

      expect(mockCallCoreRpc).toHaveBeenCalledWith({
        method: 'openhuman.flows_run',
        params: { id: 'flow-1', input: null, inputs: null },
        timeoutMs: 610_000,
      });
      expect(result).toEqual({ output: { nodes: {} }, pending_approvals: [], thread_id: 't1' });
    });

    it('passes a supplied input payload through', async () => {
      mockCallCoreRpc.mockResolvedValue(
        cliEnvelope({ output: null, pending_approvals: [], thread_id: 't2' })
      );

      await runFlow('flow-1', { trigger: 'manual' });

      expect(mockCallCoreRpc).toHaveBeenCalledWith({
        method: 'openhuman.flows_run',
        params: { id: 'flow-1', input: { trigger: 'manual' }, inputs: null },
        timeoutMs: 610_000,
      });
    });

    it('passes declared workflow inputs alongside the trigger payload', async () => {
      mockCallCoreRpc.mockResolvedValue(
        cliEnvelope({ output: null, pending_approvals: [], thread_id: 't3' })
      );

      await runFlow('flow-1', {}, { repo: 'acme/api', depth: 3 });

      expect(mockCallCoreRpc).toHaveBeenCalledWith({
        method: 'openhuman.flows_run',
        params: { id: 'flow-1', input: {}, inputs: { repo: 'acme/api', depth: 3 } },
        timeoutMs: 610_000,
      });
    });

    it('unwraps the { result, logs } envelope', async () => {
      const payload = { output: null, pending_approvals: ['node-a'], thread_id: 't3' };
      mockCallCoreRpc.mockResolvedValue(cliEnvelope(payload));

      const result = await runFlow('flow-1');

      expect(result).toEqual(payload);
    });

    it('propagates rejection from callCoreRpc', async () => {
      mockCallCoreRpc.mockRejectedValue(new Error('flow disabled'));

      await expect(runFlow('flow-1')).rejects.toThrow('flow disabled');
    });
  });

  // F-M1/F-M2: `flows_run_detached` registers the run and returns immediately
  // — it must NOT share `runFlow`'s extended `FLOW_RESUME_TIMEOUT_MS` budget,
  // since (unlike `runFlow`) it never waits for the engine.
  describe('runFlowDetached', () => {
    it('calls openhuman.flows_run_detached with id/input and the DEFAULT timeout (no timeoutMs override)', async () => {
      mockCallCoreRpc.mockResolvedValue(
        cliEnvelope({
          run_id: 'flow:flow-1:t1',
          flow_id: 'flow-1',
          status: 'running',
          detached: true,
        })
      );

      const result = await runFlowDetached('flow-1');

      expect(mockCallCoreRpc).toHaveBeenCalledWith({
        method: 'openhuman.flows_run_detached',
        params: { id: 'flow-1', input: null, inputs: null },
      });
      // No `timeoutMs` key at all — asserted structurally above via
      // `toHaveBeenCalledWith` (an object with an extra `timeoutMs` key would
      // NOT match), rather than a brittle `not.toHaveProperty` on the mock
      // call args.
      expect(result).toEqual({
        run_id: 'flow:flow-1:t1',
        flow_id: 'flow-1',
        status: 'running',
        detached: true,
      });
    });

    it('passes a supplied input payload through', async () => {
      mockCallCoreRpc.mockResolvedValue(
        cliEnvelope({
          run_id: 'flow:flow-1:t2',
          flow_id: 'flow-1',
          status: 'running',
          detached: true,
        })
      );

      await runFlowDetached('flow-1', { trigger: 'manual' });

      expect(mockCallCoreRpc).toHaveBeenCalledWith({
        method: 'openhuman.flows_run_detached',
        params: { id: 'flow-1', input: { trigger: 'manual' }, inputs: null },
      });
    });

    it('passes declared workflow inputs — the only way a parameterized flow runs from the UI', async () => {
      mockCallCoreRpc.mockResolvedValue(
        cliEnvelope({
          run_id: 'flow:flow-1:t4',
          flow_id: 'flow-1',
          status: 'running',
          detached: true,
        })
      );

      await runFlowDetached('flow-1', {}, { repo: 'acme/api' });

      expect(mockCallCoreRpc).toHaveBeenCalledWith({
        method: 'openhuman.flows_run_detached',
        params: { id: 'flow-1', input: {}, inputs: { repo: 'acme/api' } },
      });
    });

    it('unwraps the { result, logs } envelope', async () => {
      const payload = {
        run_id: 'flow:flow-1:t3',
        flow_id: 'flow-1',
        status: 'running',
        detached: true,
      };
      mockCallCoreRpc.mockResolvedValue(cliEnvelope(payload));

      const result = await runFlowDetached('flow-1');

      expect(result).toEqual(payload);
    });

    it('propagates rejection from callCoreRpc', async () => {
      mockCallCoreRpc.mockRejectedValue(new Error('flow disabled'));

      await expect(runFlowDetached('flow-1')).rejects.toThrow('flow disabled');
    });
  });

  describe('Flow Scout suggestions', () => {
    const suggestion: FlowSuggestion = {
      id: 'sug_1',
      title: 'Auto-file receipts',
      one_liner: 'Add each Gmail receipt to your sheet.',
      rationale: 'You forward receipts weekly.',
      trigger_hint: 'app_event',
      steps_outline: ['Watch Gmail', 'Append row'],
      suggested_connections: ['composio:gmail:c1'],
      suggested_slugs: ['GMAIL_NEW_GMAIL_MESSAGE'],
      build_prompt: 'Build a workflow that…',
      confidence: 0.8,
      status: 'new',
      created_at: '2026-07-05T00:00:00Z',
      source_run_id: null,
    };

    it('discoverWorkflows calls flows_discover with the extended timeout', async () => {
      mockCallCoreRpc.mockResolvedValue(cliEnvelope([suggestion]));

      const result = await discoverWorkflows();

      expect(mockCallCoreRpc).toHaveBeenCalledWith({
        method: 'openhuman.flows_discover',
        params: {},
        timeoutMs: 610_000,
      });
      expect(result).toEqual([suggestion]);
    });

    it('discoverWorkflows passes thread_id when a chat thread is given', async () => {
      mockCallCoreRpc.mockResolvedValue(cliEnvelope([suggestion]));

      await discoverWorkflows('scout-thread-1');

      expect(mockCallCoreRpc).toHaveBeenCalledWith({
        method: 'openhuman.flows_discover',
        params: { thread_id: 'scout-thread-1' },
        timeoutMs: 610_000,
      });
    });

    it('listSuggestions omits status when not provided', async () => {
      mockCallCoreRpc.mockResolvedValue(cliEnvelope([]));

      await listSuggestions();

      expect(mockCallCoreRpc).toHaveBeenCalledWith({
        method: 'openhuman.flows_list_suggestions',
        params: {},
      });
    });

    it('listSuggestions passes the status filter', async () => {
      mockCallCoreRpc.mockResolvedValue(cliEnvelope([suggestion]));

      const result = await listSuggestions('new');

      expect(mockCallCoreRpc).toHaveBeenCalledWith({
        method: 'openhuman.flows_list_suggestions',
        params: { status: 'new' },
      });
      expect(result).toEqual([suggestion]);
    });

    it('dismissSuggestion returns the dismissed flag', async () => {
      mockCallCoreRpc.mockResolvedValue(cliEnvelope({ id: 'sug_1', dismissed: true }));

      const result = await dismissSuggestion('sug_1');

      expect(mockCallCoreRpc).toHaveBeenCalledWith({
        method: 'openhuman.flows_dismiss_suggestion',
        params: { id: 'sug_1' },
      });
      expect(result).toBe(true);
    });

    it('markSuggestionBuilt returns the built flag', async () => {
      mockCallCoreRpc.mockResolvedValue(cliEnvelope({ id: 'sug_1', built: true }));

      const result = await markSuggestionBuilt('sug_1');

      expect(mockCallCoreRpc).toHaveBeenCalledWith({
        method: 'openhuman.flows_mark_suggestion_built',
        params: { id: 'sug_1' },
      });
      expect(result).toBe(true);
    });

    it('propagates rejection from callCoreRpc', async () => {
      mockCallCoreRpc.mockRejectedValue(new Error('boom'));

      await expect(discoverWorkflows()).rejects.toThrow('boom');
    });
  });

  describe('buildWorkflow', () => {
    const proposalPayload = {
      type: 'workflow_proposal',
      name: 'Digest',
      graph: { schema_version: 1, name: 'g', nodes: [], edges: [] },
      require_approval: true,
      summary: { trigger: 'manual', steps: [] },
    };

    it('calls flows_build with the structured request and no thread_id when omitted', async () => {
      mockCallCoreRpc.mockResolvedValue(
        cliEnvelope({ proposal: proposalPayload, assistant_text: 'here you go', error: null })
      );

      const result = await buildWorkflow({ mode: 'create', instruction: 'email me a digest' });

      expect(mockCallCoreRpc).toHaveBeenCalledWith({
        method: 'openhuman.flows_build',
        params: {
          mode: 'create',
          instruction: 'email me a digest',
          graph: null,
          flow_id: null,
          run_id: null,
          error: null,
          failing_node_ids: [],
        },
        timeoutMs: 610_000,
      });
      expect(result.assistantText).toBe('here you go');
      expect(result.proposal?.name).toBe('Digest');
      expect(result.error).toBeNull();
    });

    it('threads the chat thread_id into flows_build params when provided', async () => {
      mockCallCoreRpc.mockResolvedValue(
        cliEnvelope({ proposal: null, assistant_text: '', error: null })
      );

      await buildWorkflow({ mode: 'revise', instruction: 'add a Slack step' }, 'builder-thread-9');

      expect(mockCallCoreRpc).toHaveBeenCalledWith({
        method: 'openhuman.flows_build',
        params: {
          mode: 'revise',
          instruction: 'add a Slack step',
          graph: null,
          flow_id: null,
          run_id: null,
          error: null,
          failing_node_ids: [],
          thread_id: 'builder-thread-9',
        },
        timeoutMs: 610_000,
      });
    });

    it.each([
      ['a malformed payload', { type: 'not_a_workflow_proposal' }, null],
      [
        'missing require_approval',
        { type: 'workflow_proposal', name: 'Default approval', graph: {} },
        {
          name: 'Default approval',
          graph: {},
          requireApproval: true,
          summary: { trigger: '', steps: [] },
        },
      ],
      [
        'invalid summary steps',
        {
          type: 'workflow_proposal',
          name: 'Mixed steps',
          graph: {},
          summary: {
            trigger: 42,
            steps: [null, 'invalid', { kind: 7, name: false, config_hint: [] }],
          },
        },
        {
          name: 'Mixed steps',
          graph: {},
          requireApproval: true,
          summary: { trigger: '', steps: [{ kind: 'unknown', name: '', config_hint: undefined }] },
        },
      ],
      [
        'explicit false approval',
        { type: 'workflow_proposal', name: 'No approval', graph: {}, require_approval: false },
        {
          name: 'No approval',
          graph: {},
          requireApproval: false,
          summary: { trigger: '', steps: [] },
        },
      ],
    ])('coerces %s through the canonical proposal mapper', async (_label, raw, expected) => {
      mockCallCoreRpc.mockResolvedValue(
        cliEnvelope({ proposal: raw, assistant_text: '', error: null })
      );

      const result = await buildWorkflow({ mode: 'create', instruction: 'build it' });

      expect(coerceWorkflowProposal).toHaveBeenCalledWith(raw);
      expect(result.proposal).toEqual(expected);
    });
  });
});

describe('getApprovalManifest', () => {
  beforeEach(() => mockCallCoreRpc.mockReset());

  const manifest = {
    entries: [
      { kind: 'approvable', node_id: 'n1', tool_name: 'flows_http_request', label: 'Call API' },
    ],
    missing: ['flows_http_request'],
    already_trusted: ['GMAIL_SEND_EMAIL'],
    gate_installed: true,
  };

  it('targets a saved flow by id', async () => {
    mockCallCoreRpc.mockResolvedValue(cliEnvelope(manifest));

    const result = await getApprovalManifest({ id: 'flow-1' });

    expect(mockCallCoreRpc).toHaveBeenCalledWith({
      method: 'openhuman.flows_approval_manifest',
      params: { id: 'flow-1' },
    });
    expect(result).toEqual(manifest);
  });

  it('targets a candidate graph when no id exists yet', async () => {
    mockCallCoreRpc.mockResolvedValue(cliEnvelope(manifest));

    await getApprovalManifest({ graph: { nodes: [], edges: [] } });

    expect(mockCallCoreRpc).toHaveBeenCalledWith({
      method: 'openhuman.flows_approval_manifest',
      params: { graph: { nodes: [], edges: [] } },
    });
  });

  it('defaults every field when the envelope payload is sparse', async () => {
    mockCallCoreRpc.mockResolvedValue(cliEnvelope({}));

    const result = await getApprovalManifest({ id: 'flow-1' });

    expect(result).toEqual({ entries: [], missing: [], already_trusted: [], gate_installed: true });
  });

  it('propagates rejection from callCoreRpc', async () => {
    mockCallCoreRpc.mockRejectedValueOnce(new Error('manifest down'));

    await expect(getApprovalManifest({ id: 'flow-1' })).rejects.toThrow('manifest down');
  });
});