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.

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

import { messageService } from '@/services/message';
import * as agentStore from '@/store/agent';
import * as serverConfigStore from '@/store/serverConfig';
import { useUserStore } from '@/store/user';

import {
  readConversationMessageListPage,
  readConversationMessages,
  readEarlierConversationMessages,
} from './conversationMessageRead';

const mockGatewayMode = ({
  gatewayMode,
  protocolV2 = true,
}: {
  gatewayMode: boolean;
  protocolV2?: boolean;
}) => {
  vi.spyOn(serverConfigStore, 'getServerConfigStoreState').mockReturnValue({
    serverConfig: {
      ...(protocolV2 && { agentGatewayProtocol: 2 }),
      ...(gatewayMode && { agentGatewayUrl: 'https://gateway.test', enableGatewayMode: true }),
    },
  } as unknown as ReturnType<typeof serverConfigStore.getServerConfigStoreState>);
  vi.spyOn(agentStore, 'getAgentStoreState').mockReturnValue({
    activeAgentId: 'agent-1',
    agentMap: { 'agent-1': { chatConfig: {} } },
  } as unknown as ReturnType<typeof agentStore.getAgentStoreState>);
  useUserStore.setState({ settings: {} } as never);
};

describe('readConversationMessages', () => {
  it('asks for projected tool payloads when the run executes on the server', async () => {
    mockGatewayMode({ gatewayMode: true });
    const getMessages = vi.spyOn(messageService, 'getMessages').mockResolvedValue([]);

    await readConversationMessages({ agentId: 'agent-1', topicId: 'topic-1' });

    expect(getMessages).toHaveBeenCalledWith({
      agentId: 'agent-1',
      projectToolPayloads: true,
      topicId: 'topic-1',
    });
  });

  it('keeps whole payloads outside the protocol-v2 rollout', async () => {
    mockGatewayMode({ gatewayMode: true, protocolV2: false });
    const getMessages = vi.spyOn(messageService, 'getMessages').mockResolvedValue([]);

    await readConversationMessages({ agentId: 'agent-1', topicId: 'topic-1' });

    expect(getMessages).toHaveBeenCalledWith(
      expect.objectContaining({ projectToolPayloads: false }),
    );
  });

  it('keeps whole payloads when the run can execute in the browser', async () => {
    // The browser assembles its LLM context from this very list, so a projected
    // tool result would silently disappear from the model's view.
    mockGatewayMode({ gatewayMode: false });
    const getMessages = vi.spyOn(messageService, 'getMessages').mockResolvedValue([]);

    await readConversationMessages({ agentId: 'agent-1', topicId: 'topic-1' });

    expect(getMessages).toHaveBeenCalledWith({
      agentId: 'agent-1',
      projectToolPayloads: false,
      topicId: 'topic-1',
    });
  });
});

describe('readConversationMessageListPage', () => {
  it('applies the same projection gate to the paged list read', async () => {
    mockGatewayMode({ gatewayMode: true });
    const getPage = vi.spyOn(messageService, 'getMessageListPage').mockResolvedValue([]);

    await readConversationMessageListPage({ agentId: 'agent-1', topicId: 'topic-1' });

    expect(getPage).toHaveBeenCalledWith({
      agentId: 'agent-1',
      projectToolPayloads: true,
      topicId: 'topic-1',
    });
  });
});

describe('readEarlierConversationMessages', () => {
  it('pages older history with the same projection gate as the head page', async () => {
    mockGatewayMode({ gatewayMode: true });
    const getEarlierMessages = vi
      .spyOn(messageService, 'getEarlierMessages')
      .mockResolvedValue({ messages: [], olderCursor: null });
    const cursor = { createdAt: '2026-10-27T00:00:00.000Z', id: 'msg_1' };

    await readEarlierConversationMessages({ agentId: 'agent-1', topicId: 'topic-1' }, cursor);

    expect(getEarlierMessages).toHaveBeenCalledWith(
      { agentId: 'agent-1', projectToolPayloads: true, topicId: 'topic-1' },
      cursor,
    );
  });
});