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.

2.2 KB
Raw Blame
import type { SendMessageParams } from '@lobechat/types';
import { describe, expect, it, vi } from 'vitest';

import { sendAppMessage } from './sendAppMessage';

describe('sendAppMessage', () => {
  it('preserves the draft when a send finishes without accepting the message', async () => {
    // Gateway task creation can fail internally without rejecting the outer send.
    const send = vi.fn().mockResolvedValue(undefined);

    await expect(sendAppMessage(send, 'Follow up', 'weather')).resolves.toBe(false);
    expect(send).toHaveBeenCalledWith(expect.objectContaining({ preserveComposer: true }));
  });

  it('lets the caller cancel the send until it is accepted', async () => {
    const send = vi.fn().mockResolvedValue(undefined);
    const view = new AbortController();

    await sendAppMessage(send, 'Follow up', 'weather', view.signal);

    expect(send).toHaveBeenCalledWith(expect.objectContaining({ signal: view.signal }));
  });

  it('waits for acceptance without waiting for the agent run to finish', async () => {
    let params!: SendMessageParams;
    const send = vi.fn((value: SendMessageParams) => {
      params = value;
      return new Promise<void>(() => {});
    });
    const settled = vi.fn();
    const result = sendAppMessage(send, 'Follow up', 'weather').then(settled);
    await Promise.resolve();
    expect(settled).not.toHaveBeenCalled();

    expect(params.message).toBe('Follow up');
    expect(params.metadata?.mcpAppSource).toEqual({ identifier: 'weather' });
    expect(params.onMessageAccepted).toBeTypeOf('function');
    params.onMessageAccepted?.();
    await result;
    expect(settled).toHaveBeenCalledWith(true);
  });

  it('reports a rejection before acceptance', async () => {
    const send = vi.fn().mockRejectedValue(new Error('Could not create task'));

    await expect(sendAppMessage(send, 'Follow up', 'weather')).rejects.toThrow(
      'Could not create task',
    );
  });

  it('keeps an accepted message successful if the subsequent run fails', async () => {
    const send = vi.fn(async (params: SendMessageParams) => {
      params.onMessageAccepted?.();
      throw new Error('Agent run failed after storing the message');
    });

    await expect(sendAppMessage(send, 'Follow up', 'weather')).resolves.toBe(true);
  });
});