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.

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

import { AcceptanceService } from '../acceptanceService';

const getAgentAvatarsByIds = vi.fn();
const findTopicById = vi.fn();

vi.mock('@/database/models/agent', () => ({
  AgentModel: vi.fn().mockImplementation(function () {
    return { getAgentAvatarsByIds };
  }),
}));

vi.mock('@/database/models/topic', () => ({
  TopicModel: vi.fn().mockImplementation(function () {
    return { findById: findTopicById };
  }),
}));

const agentRow = (id: string) => ({ avatar: '🦊', backgroundColor: null, id, title: 'Fox' });
const runWithOrigin = (origin: Record<string, string>) => ({ metadata: { origin } }) as never;

/**
 * Rounds published from a dispatched run (task / goal / device) carry only the
 * topic — the connector strips the ambient agent id. The topic row still names
 * its agent, so the origin must resolve to it instead of hiding the
 * conversation panel and the agent chip.
 */
describe('AcceptanceService.resolveOrigin', () => {
  const service = new AcceptanceService({} as never, 'user', 'ws');

  beforeEach(() => {
    getAgentAvatarsByIds.mockReset();
    findTopicById.mockReset();
  });

  it("falls back to the topic's agent when the round recorded none", async () => {
    findTopicById.mockResolvedValue({ agentId: 'agt_topic', id: 'tpc_1', title: 'Harbor' });
    getAgentAvatarsByIds.mockResolvedValue([agentRow('agt_topic')]);

    const origin = await service.resolveOrigin([runWithOrigin({ topicId: 'tpc_1' })]);

    expect(getAgentAvatarsByIds).toHaveBeenCalledWith(['agt_topic']);
    expect(origin).toEqual({
      agent: agentRow('agt_topic'),
      topic: { id: 'tpc_1', title: 'Harbor' },
    });
  });

  it('keeps the recorded agent over the topic owner', async () => {
    findTopicById.mockResolvedValue({ agentId: 'agt_topic', id: 'tpc_1', title: 'Harbor' });
    getAgentAvatarsByIds.mockResolvedValue([agentRow('agt_recorded')]);

    const origin = await service.resolveOrigin([
      runWithOrigin({ agentId: 'agt_recorded', topicId: 'tpc_1' }),
    ]);

    expect(getAgentAvatarsByIds).toHaveBeenCalledWith(['agt_recorded']);
    expect(origin?.agent?.id).toBe('agt_recorded');
  });

  it('looks up a recorded agent without waiting on the topic read', async () => {
    let resolveTopic!: (row: unknown) => void;
    findTopicById.mockReturnValue(new Promise((resolve) => (resolveTopic = resolve)));
    getAgentAvatarsByIds.mockResolvedValue([agentRow('agt_recorded')]);

    const pending = service.resolveOrigin([
      runWithOrigin({ agentId: 'agt_recorded', topicId: 'tpc_1' }),
    ]);
    await Promise.resolve();

    expect(getAgentAvatarsByIds).toHaveBeenCalledWith(['agt_recorded']);
    resolveTopic({ agentId: 'agt_topic', id: 'tpc_1', title: 'Harbor' });
    expect((await pending)?.agent?.id).toBe('agt_recorded');
  });

  it('leaves the agent empty when neither the round nor the topic names one', async () => {
    findTopicById.mockResolvedValue({ agentId: null, id: 'tpc_1', title: 'Harbor' });

    const origin = await service.resolveOrigin([runWithOrigin({ topicId: 'tpc_1' })]);

    expect(getAgentAvatarsByIds).not.toHaveBeenCalled();
    expect(origin).toEqual({ agent: null, topic: { id: 'tpc_1', title: 'Harbor' } });
  });
});

describe('AcceptanceService.findRepairOrigin', () => {
  it.each([
    {
      expected: { agentId: 'agt_2', topicId: 'tpc_2' },
      runs: [{ origin: { topicId: 'tpc_1' } }, { origin: { agentId: 'agt_2', topicId: 'tpc_2' } }],
    },
    {
      expected: { agentId: undefined, topicId: 'tpc_1' },
      runs: [{ origin: { topicId: 'tpc_1' } }],
    },
    { expected: null, runs: [{ origin: { agentId: 'agt_1' } }] },
    { expected: null, runs: [] },
  ])('resolves the latest recorded origin to $expected', async ({ expected, runs }) => {
    const service = new AcceptanceService({} as never, 'user', 'ws');
    vi.spyOn((service as any).runModel, 'listByAcceptance').mockResolvedValue(
      runs.map(({ origin }) => ({ metadata: { origin } })),
    );

    await expect(service.findRepairOrigin('acc-1')).resolves.toEqual(expected);
  });
});