Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

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

import { ComposioService } from './index';

const mocks = vi.hoisted(() => ({
  connectorPickLegacyMirrors: vi.fn(),
  connectorQuery: vi.fn(),
  connectorQueryByIdentifiers: vi.fn(),
  connectorToolQueryAll: vi.fn(),
  isClientAvailable: vi.fn(),
  isComposioNotFound: vi.fn(),
  markComposioUnavailable: vi.fn(),
  pluginQuery: vi.fn(),
  toolsExecute: vi.fn(),
}));

vi.mock('@/database/models/connector', () => ({
  ConnectorModel: vi.fn().mockImplementation(function () {
    return {
      // Runtime resolution goes through the agent-aware resolvers; map them to the
      // same fixtures (the priority/dedup logic itself is covered by the model's
      // own connectorAgentScope tests).
      resolveAll: mocks.connectorQuery,
      resolveByIdentifiers: mocks.connectorQueryByIdentifiers,
      // Health writes follow the resolved row's own scope; the scoped model is
      // an implementation detail here, the call itself is what the test asserts.
      forRow: () => ({ markComposioConnectionUnavailable: mocks.markComposioUnavailable }),
      pickLegacyComposioMirrors: mocks.connectorPickLegacyMirrors,
      markComposioConnectionUnavailable: mocks.markComposioUnavailable,
    };
  }),
}));

vi.mock('@/database/models/connectorTool', () => ({
  ConnectorToolModel: vi.fn().mockImplementation(function () {
    return {
      queryAllByConnectorIds: mocks.connectorToolQueryAll,
    };
  }),
}));

vi.mock('@/database/models/plugin', () => ({
  PluginModel: vi.fn().mockImplementation(function () {
    return { queryComposioMirrors: mocks.pluginQuery };
  }),
}));

vi.mock('@/libs/composio', () => ({
  getComposioClient: () => ({ tools: { execute: mocks.toolsExecute } }),
  isComposioClientAvailable: mocks.isClientAvailable,
  isComposioConnectedAccountNotFoundError: mocks.isComposioNotFound,
}));

const service = () => new ComposioService({ db: {} as any, userId: 'user-1' });

/** Legacy plugin-projection rows; the query filters by identifier like the SQL does. */
const mockPlugins = (rows: Record<string, any>[]) =>
  mocks.pluginQuery.mockImplementation(async (identifiers?: string[]) =>
    rows.filter((row) => !identifiers || identifiers.includes(row.identifier)),
  );

const pluginRow = (
  identifier: string,
  composio: Record<string, unknown> = {},
  overrides: Record<string, any> = {},
) => ({
  customParams: {
    composio: { connectedAccountId: `ca-${identifier}`, status: 'ACTIVE', ...composio },
  },
  identifier,
  manifest: { api: [{ name: `${identifier.toUpperCase()}_TOOL` }], meta: {} },
  userId: 'owner-plugin',
  ...overrides,
});

const activeConnectorRow = (overrides: Record<string, any> = {}) => ({
  id: 'conn-gmail',
  identifier: 'gmail',
  isEnabled: true,
  metadata: {
    avatar: '🔌',
    composio: { appSlug: 'GMAIL', connectedAccountId: 'ca-connector', status: 'ACTIVE' },
  },
  name: 'Gmail',
  ...overrides,
});

beforeEach(() => {
  vi.clearAllMocks();
  mocks.isClientAvailable.mockReturnValue(true);
  mocks.isComposioNotFound.mockImplementation(function (error: unknown) {
    return (
      typeof error === 'object' &&
      error !== null &&
      'code' in error &&
      error.code === 'CONNECTED_ACCOUNT_NOT_FOUND'
    );
  });
  mocks.markComposioUnavailable.mockResolvedValue(false);
  mocks.connectorPickLegacyMirrors.mockImplementation(async (mirrors: unknown[]) => mirrors);
  mocks.connectorQuery.mockResolvedValue([]);
  mocks.connectorQueryByIdentifiers.mockResolvedValue([]);
  mocks.connectorToolQueryAll.mockResolvedValue([]);
  mockPlugins([]);
});

describe('ComposioService.getComposioManifests', () => {
  it('builds manifests from connector rows + connector tools (new path)', async () => {
    mocks.connectorQuery.mockResolvedValue([activeConnectorRow()]);
    mocks.connectorToolQueryAll.mockResolvedValue([
      {
        description: 'Send an email',
        inputSchema: { properties: { to: {} }, type: 'object' },
        toolName: 'GMAIL_SEND_EMAIL',
        userConnectorId: 'conn-gmail',
      },
    ]);

    const manifests = await service().getComposioManifests();

    expect(manifests).toHaveLength(1);
    expect(manifests[0].identifier).toBe('gmail');
    expect(manifests[0].api).toHaveLength(1);
    expect(manifests[0].api[0].name).toBe('GMAIL_SEND_EMAIL');
    expect(manifests[0].api[0].parameters).toEqual({ properties: { to: {} }, type: 'object' });
    // Plugin table must NOT be consulted once the connector covers the identifier.
    expect(mocks.pluginQuery).toHaveBeenCalledTimes(1); // called, but yields nothing new
  });

  it('reads only the requested identifiers', async () => {
    mocks.connectorQueryByIdentifiers.mockResolvedValue([activeConnectorRow()]);
    mocks.connectorToolQueryAll.mockResolvedValue([
      { description: 'Send', toolName: 'GMAIL_SEND_EMAIL', userConnectorId: 'conn-gmail' },
    ]);
    mockPlugins([pluginRow('slack')]);

    const manifests = await service().getComposioManifests('agent-1', {
      identifiers: ['gmail', 'not-composio'],
    });

    expect(mocks.connectorQuery).not.toHaveBeenCalled();
    expect(mocks.connectorQueryByIdentifiers).toHaveBeenCalledWith(['gmail'], 'agent-1');
    expect(manifests.map((m) => m.identifier)).toEqual(['gmail']);
  });

  it('reads nothing when no requested identifier is a Composio app', async () => {
    const manifests = await service().getComposioManifests('agent-1', {
      identifiers: ['deepwiki'],
    });

    expect(manifests).toEqual([]);
    expect(mocks.connectorQuery).not.toHaveBeenCalled();
    expect(mocks.connectorQueryByIdentifiers).not.toHaveBeenCalled();
    expect(mocks.pluginQuery).not.toHaveBeenCalled();
  });

  it('falls back to plugin table for identifiers without a connector projection', async () => {
    mocks.connectorQuery.mockResolvedValue([]);
    mockPlugins([
      pluginRow(
        'slack',
        {},
        {
          manifest: {
            api: [{ description: 'post', name: 'SLACK_POST', parameters: { type: 'object' } }],
            meta: { avatar: '☁️', title: 'Slack' },
          },
        },
      ),
    ]);

    const manifests = await service().getComposioManifests();

    expect(manifests).toHaveLength(1);
    expect(manifests[0].identifier).toBe('slack');
    expect(manifests[0].api[0].name).toBe('SLACK_POST');
  });

  it('does not fall back to the plugin projection when a connector row exists but the run cannot see it', async () => {
    // A shared Composio connector switched to private: runtime resolution leaves
    // it out, and the mirrored plugin row must not serve it instead.
    mocks.connectorQuery.mockResolvedValue([]);
    mocks.connectorPickLegacyMirrors.mockResolvedValue([]);
    mockPlugins([pluginRow('gmail', { linkedByUserId: 'creator' })]);

    const manifests = await service().getComposioManifests();

    expect(manifests).toEqual([]);
    expect(mocks.connectorPickLegacyMirrors).toHaveBeenCalledWith([
      expect.objectContaining({
        connectedAccountId: 'ca-gmail',
        identifier: 'gmail',
        ownerUserId: 'creator',
      }),
    ]);
  });

  it('advertises a legacy connection next to a projection row a connector claims', async () => {
    // A teammate's private Composio connection leaves its own projection row
    // for the same app. Only that row is the connector's mirror; the
    // workspace's legacy connection stays available to everyone else.
    const claimed = pluginRow(
      'gmail',
      { connectedAccountId: 'ca-teammate-private' },
      { manifest: { api: [{ name: 'GMAIL_PRIVATE' }], meta: {} }, userId: 'teammate' },
    );
    const legacy = pluginRow('gmail', { connectedAccountId: 'ca-legacy' });
    mockPlugins([claimed, legacy]);
    mocks.connectorPickLegacyMirrors.mockImplementation(async (mirrors: any[]) =>
      mirrors.filter((mirror) => mirror.plugin !== claimed),
    );

    const manifests = await service().getComposioManifests();

    expect(manifests).toHaveLength(1);
    expect(manifests[0].api[0].name).toBe('GMAIL_TOOL');
  });

  it('unions both sources and dedupes by identifier (connector wins)', async () => {
    mocks.connectorQuery.mockResolvedValue([activeConnectorRow()]);
    mocks.connectorToolQueryAll.mockResolvedValue([
      { toolName: 'GMAIL_SEND_EMAIL', userConnectorId: 'conn-gmail' },
    ]);
    mockPlugins([
      // Same identifier as the connector → must be dropped (connector wins).
      pluginRow('gmail', {}, { manifest: { api: [{ name: 'GMAIL_OLD' }], meta: {} } }),
      // Distinct identifier → included via fallback.
      pluginRow('slack', {}, { manifest: { api: [{ name: 'SLACK_POST' }], meta: {} } }),
    ]);

    const manifests = await service().getComposioManifests();

    expect(manifests.map((m) => m.identifier).sort()).toEqual(['gmail', 'slack']);
    const gmail = manifests.find((m) => m.identifier === 'gmail')!;
    // connector version (GMAIL_SEND_EMAIL), not the plugin's GMAIL_OLD
    expect(gmail.api[0].name).toBe('GMAIL_SEND_EMAIL');
  });

  it('ignores connector rows that are not ACTIVE or are disabled', async () => {
    mocks.connectorQuery.mockResolvedValue([
      activeConnectorRow({ metadata: { composio: { status: 'PENDING' } } }),
      activeConnectorRow({ id: 'c2', identifier: 'jira', isEnabled: false }),
    ]);

    const manifests = await service().getComposioManifests();

    expect(manifests).toHaveLength(0);
  });

  /** @example Legacy GitHub Composio rows are not exposed after Market becomes canonical. */
  it('ignores connector rows removed from the Composio catalog', async () => {
    mocks.connectorQuery.mockResolvedValue([
      activeConnectorRow({ id: 'conn-github', identifier: 'github', name: 'GitHub' }),
    ]);

    await expect(service().getComposioManifests()).resolves.toEqual([]);
    expect(mocks.connectorToolQueryAll).not.toHaveBeenCalled();
  });

  it('returns empty when there are no composio connections in either source', async () => {
    const manifests = await service().getComposioManifests();
    expect(manifests).toEqual([]);
  });
});

describe('ComposioService.executeComposioTool', () => {
  const params = { args: { to: '[email protected]' }, identifier: 'gmail', toolSlug: 'GMAIL_SEND_EMAIL' };

  /** @example Legacy GitHub Composio tools cannot execute after Market becomes canonical. */
  it('rejects apps removed from the Composio catalog', async () => {
    const result = await service().executeComposioTool({
      args: {},
      identifier: 'github',
      toolSlug: 'GITHUB_GET_REPOSITORY',
    });

    expect(result).toMatchObject({ error: { code: 'COMPOSIO_APP_UNSUPPORTED' }, success: false });
    expect(mocks.connectorQueryByIdentifiers).not.toHaveBeenCalled();
    expect(mocks.toolsExecute).not.toHaveBeenCalled();
  });

  it('returns COMPOSIO_NOT_CONFIGURED when the client is unavailable', async () => {
    mocks.isClientAvailable.mockReturnValue(false);

    const result = await service().executeComposioTool(params);

    expect(result.success).toBe(false);
    expect(result.error?.code).toBe('COMPOSIO_NOT_CONFIGURED');
    expect(mocks.toolsExecute).not.toHaveBeenCalled();
  });

  it('resolves connectedAccountId from connector metadata (new path)', async () => {
    mocks.connectorQueryByIdentifiers.mockResolvedValue([activeConnectorRow()]);
    mocks.toolsExecute.mockResolvedValue({ data: 'sent' });

    const result = await service().executeComposioTool(params);

    expect(result.success).toBe(true);
    expect(result.content).toBe('sent');
    expect(mocks.toolsExecute).toHaveBeenCalledWith(
      'GMAIL_SEND_EMAIL',
      expect.objectContaining({ connectedAccountId: 'ca-connector', userId: 'user-1' }),
    );
    // connector hit → plugin fallback not consulted
    expect(mocks.pluginQuery).not.toHaveBeenCalled();
  });

  /**
   * @example
   * expect(connector.status).toBe('error');
   */
  it('delegates a connected-account 404 to ConnectorModel after tool execution fails', async () => {
    const notFound = Object.assign(new Error('connected account not found'), {
      code: 'CONNECTED_ACCOUNT_NOT_FOUND',
    });
    mocks.connectorQueryByIdentifiers.mockResolvedValue([activeConnectorRow()]);
    mocks.toolsExecute.mockRejectedValue(notFound);
    mocks.markComposioUnavailable.mockResolvedValue(true);

    const result = await service().executeComposioTool(params);

    expect(result.success).toBe(false);
    expect(mocks.markComposioUnavailable).toHaveBeenCalledWith('conn-gmail', 'ca-connector');
  });

  /**
   * @example
   * expect(connector.status).toBe('connected');
   */
  it('does not disable a connector when a tool reports a remote resource 404', async () => {
    // ROOT CAUSE:
    //
    // A generic HTTP 404 can mean the requested email, repository, or other provider resource is
    // missing. Treating every 404 as a missing Composio connected account disabled healthy connectors.
    // We now require Composio's explicit connected-account-not-found classification at this boundary.
    mocks.connectorQueryByIdentifiers.mockResolvedValue([activeConnectorRow()]);
    mocks.toolsExecute.mockRejectedValue(
      Object.assign(new Error('repository not found'), { status: 404 }),
    );

    const result = await service().executeComposioTool(params);

    expect(result.success).toBe(false);
    expect(mocks.markComposioUnavailable).not.toHaveBeenCalled();
  });

  it('executes under the account OWNER entity, not the caller (workspace-shared connector)', async () => {
    // Regression: a workspace member runs a shared agent whose Composio connector
    // was linked by another user (openomy). buildWorkspaceWhere resolves that
    // owner's row; the connected account is bound to the owner's Composio entity,
    // so execute MUST pass the owner's userId — passing the caller's ('user-1')
    // failed Composio's account/entity validation ("Error executing the tool").
    mocks.connectorQueryByIdentifiers.mockResolvedValue([
      activeConnectorRow({ userId: 'owner-openomy' }),
    ]);
    mocks.toolsExecute.mockResolvedValue({ data: 'sent' });

    const result = await service().executeComposioTool(params);

    expect(result.success).toBe(true);
    expect(mocks.toolsExecute).toHaveBeenCalledWith(
      'GMAIL_SEND_EMAIL',
      expect.objectContaining({ connectedAccountId: 'ca-connector', userId: 'owner-openomy' }),
    );
  });

  it('prefers metadata.composio.linkedByUserId over the row creator (owner reconnected a member row)', async () => {
    // A workspace owner reconnected Gmail on a member-created connector row:
    // upsertComposioConnector refreshes metadata (incl. linkedByUserId=owner) but
    // keeps userId=member (the row creator, for manage-rights). The account is now
    // linked under the owner, so execute must run under linkedByUserId, NOT userId.
    mocks.connectorQueryByIdentifiers.mockResolvedValue([
      activeConnectorRow({
        metadata: {
          composio: {
            connectedAccountId: 'ca-connector',
            linkedByUserId: 'owner-reconnector',
            status: 'ACTIVE',
          },
        },
        userId: 'member-creator',
      }),
    ]);
    mocks.toolsExecute.mockResolvedValue({ data: 'sent' });

    const result = await service().executeComposioTool(params);

    expect(result.success).toBe(true);
    expect(mocks.toolsExecute).toHaveBeenCalledWith(
      'GMAIL_SEND_EMAIL',
      expect.objectContaining({ connectedAccountId: 'ca-connector', userId: 'owner-reconnector' }),
    );
  });

  it('falls back to plugin customParams when the connector has no account', async () => {
    mocks.connectorQueryByIdentifiers.mockResolvedValue([]);
    mockPlugins([pluginRow('gmail', { connectedAccountId: 'ca-plugin' })]);
    mocks.toolsExecute.mockResolvedValue({ data: 'sent' });

    const result = await service().executeComposioTool(params);

    expect(result.success).toBe(true);
    expect(mocks.toolsExecute).toHaveBeenCalledWith(
      'GMAIL_SEND_EMAIL',
      expect.objectContaining({ connectedAccountId: 'ca-plugin', userId: 'owner-plugin' }),
    );
  });

  it('runs the legacy connection, not a projection row a connector claims', async () => {
    mocks.connectorQueryByIdentifiers.mockResolvedValue([]);
    const claimed = pluginRow(
      'gmail',
      { connectedAccountId: 'ca-teammate-private', linkedByUserId: 'teammate' },
      { userId: 'teammate' },
    );
    mockPlugins([
      claimed,
      pluginRow('gmail', { connectedAccountId: 'ca-pending', status: 'PENDING' }),
      pluginRow('gmail', { connectedAccountId: 'ca-legacy', linkedByUserId: 'linker' }),
    ]);
    mocks.connectorPickLegacyMirrors.mockImplementation(async (mirrors: any[]) =>
      mirrors.filter((mirror) => mirror.plugin !== claimed),
    );
    mocks.toolsExecute.mockResolvedValue({ data: 'sent' });

    const result = await service().executeComposioTool(params);

    expect(result.success).toBe(true);
    // An active legacy connection wins over a pending one.
    expect(mocks.toolsExecute).toHaveBeenCalledWith(
      'GMAIL_SEND_EMAIL',
      expect.objectContaining({ connectedAccountId: 'ca-legacy', userId: 'linker' }),
    );
  });

  it('returns COMPOSIO_CONFIG_NOT_FOUND when neither source has an account', async () => {
    mocks.connectorQueryByIdentifiers.mockResolvedValue([]);

    const result = await service().executeComposioTool(params);

    expect(result.success).toBe(false);
    expect(result.error?.code).toBe('COMPOSIO_CONFIG_NOT_FOUND');
    expect(mocks.toolsExecute).not.toHaveBeenCalled();
  });
});