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.6 KB
Raw Blame
/**
 * @vitest-environment happy-dom
 */
import type { UIChatMessage } from '@lobechat/types';
import { render, screen } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';

import { AssistantActionsBar } from '../../Assistant/Actions';
import { UserActionsBar } from '../../User/Actions';
import { GroupActionsBar } from './index';

const storeMock = vi.hoisted(() => ({ isGenerating: false }));

// Stub the action bar to expose the resolved `bar` / `menu` slots verbatim.
vi.mock('../../components/MessageActionBar', () => ({
  MessageActionBar: ({
    bar,
    leading,
    menu,
  }: {
    bar?: string[];
    leading?: React.ReactNode;
    menu?: string[];
  }) => (
    <div
      data-bar={(bar ?? []).join(',')}
      data-has-leading={!!leading}
      data-menu={(menu ?? []).join(',')}
      data-testid="action-bar"
    >
      {leading}
    </div>
  ),
}));

vi.mock('../../../components/Reaction', () => ({
  ReactionPicker: () => <span data-testid="reaction-picker" />,
}));

// isAssistantGroupItemGenerating(id) is called through useConversationStore; the
// mocked hook feeds the selector our controllable flag.
vi.mock('../../../store', () => ({
  messageStateSelectors: {
    isAssistantGroupItemGenerating: () => (isGenerating: boolean) => isGenerating,
  },
  useConversationStore: (selector: (v: boolean) => unknown) => selector(storeMock.isGenerating),
}));

const data = { id: 'group-1', role: 'assistantGroup', tools: [] } as unknown as UIChatMessage;

const renderBar = (props: { contentId?: string }) =>
  render(<GroupActionsBar data={data} id="group-1" {...props} />);

describe('message action defaults', () => {
  it('does not expose the retired text-to-speech action', () => {
    render(
      <>
        <AssistantActionsBar
          data={{ content: 'Assistant reply', role: 'assistant' } as UIChatMessage}
          id="assistant-1"
        />
        <UserActionsBar
          data={{ content: 'User prompt', role: 'user' } as UIChatMessage}
          id="user-1"
        />
      </>,
    );

    for (const bar of screen.getAllByTestId('action-bar')) {
      expect(bar.getAttribute('data-menu')?.split(',')).not.toContain('tts');
    }
  });
});

describe('GroupActionsBar β€” hetero (assistantGroup) forward/select gating', () => {
  it('still generating with no text block β†’ only delete', () => {
    storeMock.isGenerating = true;
    renderBar({ contentId: undefined });

    const bar = screen.getByTestId('action-bar');
    expect(bar).toHaveAttribute('data-bar', 'del');
    expect(bar).toHaveAttribute('data-menu', '');
  });

  it('finished but last block is a tool call β†’ exposes share, select, and delete', () => {
    storeMock.isGenerating = false;
    renderBar({ contentId: undefined });

    const bar = screen.getByTestId('action-bar');
    const menu = bar.getAttribute('data-menu') ?? '';
    expect(menu.split(',')).toContain('select');
    expect(menu.split(',')).toContain('share');
    expect(menu.split(',')).toContain('del');
    expect(bar).toHaveAttribute('data-bar', 'delAndRegenerate');
  });

  it('finished with a trailing text block β†’ full menu', () => {
    storeMock.isGenerating = false;
    renderBar({ contentId: 'block-text' });

    const bar = screen.getByTestId('action-bar');
    const menu = bar.getAttribute('data-menu') ?? '';
    expect(menu.split(',')).toContain('select');
    expect(menu.split(',')).toContain('share');
    expect(menu.split(',')).toContain('edit');
    expect(bar).toHaveAttribute('data-has-leading', 'true');
    expect(screen.getByTestId('reaction-picker')).toBeInTheDocument();
  });
});