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.

6.5 KB
Raw Blame
// @vitest-environment node
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { subAgentCallback } from '../subAgentCallback';

const mockCompleteSubAgentBridge = vi.fn();
const mockGetOperationMetadata = vi.fn();
const mockAiAgentService = vi.fn();
const mockOperationRows = vi.hoisted(() => ({ rows: [] as any[] }));
const mockServerDB = vi.hoisted(() => ({
  select: () => ({
    from: () => ({ where: () => ({ limit: async () => mockOperationRows.rows }) }),
  }),
}));

vi.mock('@/server/services/aiAgent', () => ({
  AiAgentService: vi.fn().mockImplementation(function (...args: any[]) {
    mockAiAgentService(...args);
    return { completeSubAgentBridge: mockCompleteSubAgentBridge };
  }),
}));

vi.mock('@/server/modules/AgentRuntime', () => ({
  AgentRuntimeCoordinator: vi.fn().mockImplementation(function () {
    return {
      getOperationMetadata: mockGetOperationMetadata,
    };
  }),
}));

vi.mock('@/database/core/db-adaptor', () => ({
  getServerDB: vi.fn().mockResolvedValue(mockServerDB),
}));

function buildContext(opts: { body?: unknown; jsonThrows?: boolean }) {
  const captures: Array<{ body: any; status: number }> = [];
  const ctx = {
    json: (b: any, status = 200) => {
      captures.push({ body: b, status });
      return Response.json(b, { status });
    },
    req: {
      json: opts.jsonThrows
        ? async () => {
            throw new Error('bad json');
          }
        : async () => opts.body,
    },
  } as any;
  return { ctx, getCaptures: () => captures };
}

const validBody = {
  operationId: 'op-child-1',
  parentOperationId: 'op-parent-1',
  reason: 'done',
  threadId: 'thread-1',
  toolMessageId: 'msg-tool-1',
};

describe('subAgentCallback handler', () => {
  beforeEach(() => {
    mockCompleteSubAgentBridge.mockReset();
    mockGetOperationMetadata.mockReset();
    mockAiAgentService.mockReset();
    mockGetOperationMetadata.mockResolvedValue({ userId: 'user-1', workspaceId: 'ws-1' });
    mockOperationRows.rows = [];
  });

  afterEach(() => {
    vi.clearAllMocks();
  });

  it('returns 400 when JSON parsing throws', async () => {
    const { ctx } = buildContext({ jsonThrows: true });
    const res = await subAgentCallback(ctx);
    expect(res.status).toBe(400);
    expect(mockCompleteSubAgentBridge).not.toHaveBeenCalled();
  });

  it.each([
    ['operationId', { ...validBody, operationId: undefined }],
    ['parentOperationId', { ...validBody, parentOperationId: undefined }],
    ['toolMessageId', { ...validBody, toolMessageId: undefined }],
  ])('returns 400 when required field %s is missing', async (_field, body) => {
    const { ctx, getCaptures } = buildContext({ body });
    const res = await subAgentCallback(ctx);
    expect(res.status).toBe(400);
    expect(getCaptures()[0].body.error).toMatch(/Missing required fields/);
    expect(mockCompleteSubAgentBridge).not.toHaveBeenCalled();
  });

  it('falls back to the durable operation row when the child metadata is gone', async () => {
    // Regression: runStep's orphan recovery resumes the parent
    // precisely because the child's coordinator metadata expired, so a
    // metadata-only lookup 401'd every redelivery and stranded the parent.
    mockGetOperationMetadata.mockResolvedValue(null);
    mockOperationRows.rows = [{ userId: 'user-row', workspaceId: 'ws-row' }];
    mockCompleteSubAgentBridge.mockResolvedValue(true);
    const { ctx } = buildContext({ body: { ...validBody, streamOwnerUserId: 'visitor-1' } });

    const res = await subAgentCallback(ctx);

    expect(res.status).toBe(200);
    expect(mockAiAgentService).toHaveBeenCalledWith(mockServerDB, 'user-row', {
      includeShareVisitor: true,
      workspaceId: 'ws-row',
    });
    expect(mockCompleteSubAgentBridge).toHaveBeenCalledTimes(1);
  });

  it('returns 401 when the child operation has no userId', async () => {
    mockGetOperationMetadata.mockResolvedValue(undefined);
    const { ctx } = buildContext({ body: validBody });

    const res = await subAgentCallback(ctx);

    expect(res.status).toBe(401);
    expect(mockCompleteSubAgentBridge).not.toHaveBeenCalled();
  });

  it('runs the bridge and returns 200 on happy path', async () => {
    mockCompleteSubAgentBridge.mockResolvedValue(true);
    const { ctx, getCaptures } = buildContext({ body: validBody });

    const res = await subAgentCallback(ctx);

    expect(res.status).toBe(200);
    expect(getCaptures()[0].body).toEqual({
      operationId: 'op-child-1',
      parentOperationId: 'op-parent-1',
      resumed: true,
      success: true,
    });
    expect(mockCompleteSubAgentBridge).toHaveBeenCalledWith({
      operationId: 'op-child-1',
      parentOperationId: 'op-parent-1',
      reason: 'done',
      threadId: 'thread-1',
      toolMessageId: 'msg-tool-1',
    });
    // Workspace-scoped like the /run step worker — a personal-scoped runtime
    // would miss workspace rows in the backfill / barrier queries.
    expect(mockAiAgentService).toHaveBeenCalledWith(
      expect.anything(),
      'user-1',
      expect.objectContaining({ includeShareVisitor: false, workspaceId: 'ws-1' }),
    );
  });

  it('forwards the abandon reason so the parent sees why the sub-agent failed', async () => {
    mockCompleteSubAgentBridge.mockResolvedValue(true);
    const { ctx } = buildContext({
      body: {
        ...validBody,
        errorMessage: 'Operation abandoned: inactivity_watchdog',
        reason: 'error',
      },
    });

    await subAgentCallback(ctx);

    expect(mockCompleteSubAgentBridge).toHaveBeenCalledWith(
      expect.objectContaining({
        errorMessage: 'Operation abandoned: inactivity_watchdog',
        reason: 'error',
      }),
    );
  });

  it('defaults reason to done and threadId to empty string when absent', async () => {
    mockCompleteSubAgentBridge.mockResolvedValue(false);
    const { ctx } = buildContext({
      body: { ...validBody, reason: undefined, threadId: undefined },
    });

    const res = await subAgentCallback(ctx);

    expect(res.status).toBe(200);
    expect(mockCompleteSubAgentBridge).toHaveBeenCalledWith(
      expect.objectContaining({ reason: 'done', threadId: '' }),
    );
  });

  it('returns 500 with the error message when the bridge throws (QStash redelivers)', async () => {
    mockCompleteSubAgentBridge.mockRejectedValue(new Error('redis down'));
    const { ctx, getCaptures } = buildContext({ body: validBody });

    const res = await subAgentCallback(ctx);

    expect(res.status).toBe(500);
    expect(getCaptures()[0].body).toEqual({ error: 'redis down' });
  });
});