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.

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

import { ConnectorToolModel } from '@/database/models/connectorTool';
import { deviceGateway } from '@/server/services/deviceGateway';
import { after } from '@/server/utils/scheduleAfterResponse';

import {
  buildLastSyncedAtMap,
  CONNECTOR_TOOLS_REFRESH_TTL_MS,
  connectorSyncContextForRow,
  scheduleStaleConnectorToolsRefresh,
} from './refresh';
import { syncConnectorToolsById } from './sync';

// Capture deferred work instead of running it, so each test controls exactly
// when (and whether) the background refresh executes.
const deferred: Array<() => Promise<unknown>> = [];
vi.mock('@/server/utils/scheduleAfterResponse', () => ({
  after: vi.fn((work: () => Promise<unknown>) => {
    deferred.push(work);
  }),
}));
vi.mock('./sync', () => ({ syncConnectorToolsById: vi.fn().mockResolvedValue({ toolCount: 3 }) }));
vi.mock('@/server/services/deviceGateway', () => ({ deviceGateway: { isConfigured: false } }));

const ctx = {} as any;
const NOW = 1_000_000_000_000;

// Unique connector id per test keeps the module-level in-flight guard isolated
// across tests (a fresh id is never "in flight" from a previous test).
let seq = 0;
const nextId = () => `conn-${seq++}`;

const httpConnector = (id: string) => ({
  id,
  mcpConnectionType: 'http',
  mcpServerUrl: 'https://mcp.example.com',
});

/** Run and await all currently-captured deferred works. */
const flushDeferred = async () => {
  const works = deferred.splice(0);
  for (const work of works) await work();
};

beforeEach(() => {
  vi.clearAllMocks();
  deferred.length = 0;
  vi.mocked(syncConnectorToolsById).mockResolvedValue({ toolCount: 3 });
  (deviceGateway as any).isConfigured = false;
});

describe('buildLastSyncedAtMap', () => {
  it('keeps the max updatedAt per connector', () => {
    const map = buildLastSyncedAtMap([
      { updatedAt: new Date(100), userConnectorId: 'a' },
      { updatedAt: new Date(300), userConnectorId: 'a' },
      { updatedAt: new Date(200), userConnectorId: 'b' },
    ]);
    expect(map.get('a')).toBe(300);
    expect(map.get('b')).toBe(200);
  });

  it('treats a missing/null updatedAt as 0', () => {
    const map = buildLastSyncedAtMap([{ updatedAt: null, userConnectorId: 'a' }]);
    expect(map.get('a')).toBe(0);
  });

  it('returns an empty map for no tools', () => {
    expect(buildLastSyncedAtMap([]).size).toBe(0);
  });
});

describe('connectorSyncContextForRow', () => {
  const db = {} as any;
  const runContext = (forRow: (row: unknown) => unknown) =>
    ({ connectorModel: { forRow }, connectorToolModel: { scope: 'run' } }) as any;

  it("keeps the run's own context for a row in the run's scope", () => {
    const ctx = runContext(function (this: unknown) {
      return ctx.connectorModel;
    });
    const row = { userId: 'user-1', workspaceId: 'ws-1' };

    expect(connectorSyncContextForRow(ctx, row, db)).toBe(ctx);
  });

  /**
   * @example
   * expect(connectorSyncContextForRow(workspaceRun, ownersPersonalRow, db).connectorModel).toBe(personalModel);
   */
  it("writes the owner's personal row through the personal scope", () => {
    // ROOT CAUSE:
    //
    // A private agent's workspace run resolves its owner's personal connector, but its stale-tool
    // refresh used the run's workspace-scoped models, which cannot find or write that row, so the
    // tool list stayed at its install-time state.
    const personalModel = { scope: 'personal' };
    const ctx = runContext(() => personalModel);
    const row = { userId: 'user-1', workspaceId: null };

    const result = connectorSyncContextForRow(ctx, row, db);

    expect(result?.connectorModel).toBe(personalModel);
    expect(result?.connectorToolModel).toBeInstanceOf(ConnectorToolModel);
    expect(result?.connectorToolModel).not.toBe(ctx.connectorToolModel);
  });

  it('has no context for a personal row without a database to reach it', () => {
    const ctx = runContext(() => ({ scope: 'personal' }));

    expect(
      connectorSyncContextForRow(ctx, { userId: 'user-1', workspaceId: null }, undefined),
    ).toBe(undefined);
  });
});

describe('scheduleStaleConnectorToolsRefresh β€” eligibility', () => {
  it('schedules a refresh for an HTTP connector whose tools are stale', async () => {
    const id = nextId();
    const lastSynced = NOW - CONNECTOR_TOOLS_REFRESH_TTL_MS - 1;
    scheduleStaleConnectorToolsRefresh([httpConnector(id)], new Map([[id, lastSynced]]), ctx, NOW);

    expect(after).toHaveBeenCalledTimes(1);
    await flushDeferred();
    expect(syncConnectorToolsById).toHaveBeenCalledWith(id, ctx);
  });

  it('schedules a refresh for a connector that has never synced (no map entry)', async () => {
    const id = nextId();
    scheduleStaleConnectorToolsRefresh([httpConnector(id)], new Map(), ctx, NOW);

    await flushDeferred();
    expect(syncConnectorToolsById).toHaveBeenCalledWith(id, ctx);
  });

  it('skips a connector synced within the TTL', async () => {
    const id = nextId();
    scheduleStaleConnectorToolsRefresh([httpConnector(id)], new Map([[id, NOW - 1000]]), ctx, NOW);

    expect(after).not.toHaveBeenCalled();
    await flushDeferred();
    expect(syncConnectorToolsById).not.toHaveBeenCalled();
  });

  it('skips stdio connectors (must run on the user machine, not the server)', async () => {
    const id = nextId();
    scheduleStaleConnectorToolsRefresh(
      [{ id, mcpConnectionType: 'stdio', mcpServerUrl: null }],
      new Map(),
      ctx,
      NOW,
    );
    await flushDeferred();
    expect(syncConnectorToolsById).not.toHaveBeenCalled();
  });

  it('skips connectors without an HTTP endpoint', async () => {
    const id = nextId();
    scheduleStaleConnectorToolsRefresh(
      [{ id, mcpConnectionType: 'http', mcpServerUrl: null }],
      new Map(),
      ctx,
      NOW,
    );
    await flushDeferred();
    expect(syncConnectorToolsById).not.toHaveBeenCalled();
  });

  it('skips local/private-network endpoints on a cloud deployment (device gateway configured)', async () => {
    (deviceGateway as any).isConfigured = true;
    const id = nextId();
    scheduleStaleConnectorToolsRefresh(
      [{ id, mcpConnectionType: 'http', mcpServerUrl: 'http://192.168.1.10:8080/mcp' }],
      new Map(),
      ctx,
      NOW,
    );
    await flushDeferred();
    expect(syncConnectorToolsById).not.toHaveBeenCalled();
  });

  it('still refreshes local endpoints when self-hosted (no device gateway)', async () => {
    // A self-hosted server may share a LAN with the endpoint β€” the cloud-only
    // skip must not fire there.
    const id = nextId();
    scheduleStaleConnectorToolsRefresh(
      [{ id, mcpConnectionType: 'http', mcpServerUrl: 'http://192.168.1.10:8080/mcp' }],
      new Map(),
      ctx,
      NOW,
    );
    await flushDeferred();
    expect(syncConnectorToolsById).toHaveBeenCalledWith(id, ctx);
  });

  it('throttles a second call for the same connector at the same instant', async () => {
    const id = nextId();
    scheduleStaleConnectorToolsRefresh([httpConnector(id)], new Map(), ctx, NOW);
    // Second call at the same `now` β†’ within TTL of the recorded attempt β†’ skipped.
    scheduleStaleConnectorToolsRefresh([httpConnector(id)], new Map(), ctx, NOW);

    expect(after).toHaveBeenCalledTimes(1);
    await flushDeferred();
    expect(syncConnectorToolsById).toHaveBeenCalledTimes(1);
  });

  it('keeps throttling a connector whose upstream tool list is empty', async () => {
    // Empty upstream list β†’ no tool row records the sync β†’ DB marker stays 0.
    // The in-memory attempt marker must still honor the TTL so it does not
    // re-fire on every run.
    const id = nextId();
    scheduleStaleConnectorToolsRefresh([httpConnector(id)], new Map(), ctx, NOW);
    await flushDeferred();
    // Later, but still within the TTL, with an empty DB marker again β†’ skipped.
    scheduleStaleConnectorToolsRefresh([httpConnector(id)], new Map(), ctx, NOW + 1000);
    await flushDeferred();
    expect(syncConnectorToolsById).toHaveBeenCalledTimes(1);
  });
});

describe('scheduleStaleConnectorToolsRefresh β€” remote result handling', () => {
  it('swallows a remote sync failure (never rejects, never surfaces)', async () => {
    const id = nextId();
    vi.mocked(syncConnectorToolsById).mockRejectedValueOnce(new Error('remote MCP down'));
    scheduleStaleConnectorToolsRefresh([httpConnector(id)], new Map(), ctx, NOW);
    // The deferred work must resolve, not reject, even though the sync failed.
    await expect(flushDeferred()).resolves.toBeUndefined();
  });

  it('backs off a failed connector for a TTL, then retries', async () => {
    const id = nextId();
    vi.mocked(syncConnectorToolsById).mockRejectedValueOnce(new Error('remote MCP down'));
    scheduleStaleConnectorToolsRefresh([httpConnector(id)], new Map(), ctx, NOW);
    await flushDeferred();

    // Within the TTL after a failure β†’ still backed off, not retried.
    scheduleStaleConnectorToolsRefresh([httpConnector(id)], new Map(), ctx, NOW + 1000);
    await flushDeferred();
    expect(syncConnectorToolsById).toHaveBeenCalledTimes(1);

    // After the TTL β†’ retried, and this time it succeeds.
    vi.mocked(syncConnectorToolsById).mockResolvedValueOnce({ toolCount: 5 });
    scheduleStaleConnectorToolsRefresh(
      [httpConnector(id)],
      new Map(),
      ctx,
      NOW + CONNECTOR_TOOLS_REFRESH_TTL_MS + 1,
    );
    await flushDeferred();
    expect(syncConnectorToolsById).toHaveBeenCalledTimes(2);
  });
});

describe('scheduleStaleConnectorToolsRefresh β€” never throws into the caller', () => {
  it('does not throw when the sync throws synchronously', async () => {
    const id = nextId();
    vi.mocked(syncConnectorToolsById).mockImplementationOnce(() => {
      throw new Error('sync exploded synchronously');
    });
    scheduleStaleConnectorToolsRefresh([httpConnector(id)], new Map(), ctx, NOW);
    await expect(flushDeferred()).resolves.toBeUndefined();
  });

  it('does not throw when after() itself throws, and frees the guard for retry', async () => {
    const id = nextId();
    vi.mocked(after).mockImplementationOnce(() => {
      throw new Error('scheduling failed');
    });
    expect(() =>
      scheduleStaleConnectorToolsRefresh([httpConnector(id)], new Map(), ctx, NOW),
    ).not.toThrow();
    expect(syncConnectorToolsById).not.toHaveBeenCalled();

    // Guard was released despite the scheduling failure β†’ next call schedules.
    scheduleStaleConnectorToolsRefresh([httpConnector(id)], new Map(), ctx, NOW);
    await flushDeferred();
    expect(syncConnectorToolsById).toHaveBeenCalledTimes(1);
  });

  it('processes remaining connectors even if one entry is malformed', async () => {
    const goodId = nextId();
    // A null entry would throw on property access β€” the per-item guard must
    // catch it and still schedule the healthy connector.
    scheduleStaleConnectorToolsRefresh([null as any, httpConnector(goodId)], new Map(), ctx, NOW);
    await flushDeferred();
    expect(syncConnectorToolsById).toHaveBeenCalledWith(goodId, ctx);
  });
});