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.

13.4 KB
Raw Blame
/**
 * @vitest-environment happy-dom
 */
import { fireEvent, render, screen, waitFor, within } from '@testing-library/react';
import { MemoryRouter } from 'react-router';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { schema as discordSchema } from '@/server/services/bot/platforms/discord/schema';
import type { SerializedPlatformDefinition } from '@/server/services/bot/platforms/types';

import agentEn from '../../../../packages/locales/src/default/agent';
import PlatformDetail, { type CurrentConfig } from './index';

const mocks = vi.hoisted(() => ({
  connectBot: vi.fn(),
  heterogeneous: false,
  updateBotProvider: vi.fn(),
}));

vi.mock('react-i18next', () => ({
  Trans: ({ i18nKey }: { i18nKey: string }) => <span>{i18nKey}</span>,
  useTranslation: () => ({
    t: (key: string, options?: Record<string, unknown>) =>
      ((agentEn as Record<string, string>)[key] ?? key).replace(
        '{{max}}',
        String(options?.max ?? ''),
      ),
  }),
}));
vi.mock('@/business/client/hooks/useActiveWorkspaceId', () => ({
  useActiveWorkspaceId: () => undefined,
}));
vi.mock('@/features/Workspace/useWorkspaceAwareNavigate', () => ({
  useWorkspaceAwareNavigate: () => vi.fn(),
}));
vi.mock('@/hooks/usePermission', () => ({ usePermission: () => ({ allowed: true }) }));
vi.mock('@/hooks/useAppOrigin', () => ({ useAppOrigin: () => 'https://example.test' }));
vi.mock('@/libs/swr', () => ({
  useClientDataSWR: () => ({ data: undefined, mutate: vi.fn() }),
}));
vi.mock('@/services/agentBotProvider', () => ({
  agentBotProviderService: { getRuntimeStatus: vi.fn(), wechatGetQrCode: vi.fn() },
}));
vi.mock('@/store/agent', () => ({
  useAgentStore: (selector: (state: Record<string, unknown>) => unknown) =>
    selector({
      connectBot: mocks.connectBot,
      createBotProvider: vi.fn(),
      deleteBotProvider: vi.fn(),
      testConnection: vi.fn(),
      updateBotProvider: mocks.updateBotProvider,
    }),
}));
vi.mock('@/store/agent/selectors', () => ({
  agentByIdSelectors: { isAgentHeterogeneousById: () => () => mocks.heterogeneous },
  agentSelectors: { getAgentMetaById: () => () => ({ title: 'Nasa' }) },
}));
vi.mock('@/store/serverConfig', () => ({
  useServerConfigStore: (selector: (s: { featureFlags: Record<string, unknown> }) => unknown) =>
    selector({ featureFlags: {} }),
}));
vi.mock('@/components/InfoTooltip', () => ({ default: () => null }));
vi.mock('@/features/AgentChannel/const', () => ({ getPlatformIcon: () => null }));
// A narrow window keeps the chat preview in a drawer, out of the way.
vi.mock('./useNarrowLayout', () => ({ useNarrowLayout: () => true }));

const platformDef = {
  documentation: {},
  id: 'discord',
  name: 'Discord',
  schema: discordSchema,
} as unknown as SerializedPlatformDefinition;

const config: CurrentConfig = {
  applicationId: '100000000000000001',
  credentials: { botToken: '••••••••', publicKey: '••••••••' },
  enabled: true,
  id: 'provider-1',
  platform: 'discord',
  // A custom pace, so the numbers are on the page to edit.
  settings: { botWakeDisabled: false, botWakePace: 'custom', showUsageStats: false },
};

const en = agentEn as Record<string, string>;
const openTab = (name: string) =>
  fireEvent.click(screen.getByRole('tab', { name: new RegExp(name) }));

describe('PlatformDetail saving', () => {
  beforeEach(() => {
    mocks.heterogeneous = false;
    vi.clearAllMocks();
    mocks.updateBotProvider.mockResolvedValue(undefined);
    mocks.connectBot.mockResolvedValue({ status: 'connected' });
  });

  it('saves a reply setting by itself a moment after it changes, and writes nothing else', async () => {
    render(<PlatformDetail agentId="agt" currentConfig={config} platformDef={platformDef} />, {
      wrapper: MemoryRouter,
    });

    openTab(en['channel.tab.reply']);
    fireEvent.click(screen.getByRole('switch', { name: new RegExp(en['channel.showUsageStats']) }));

    await waitFor(() => expect(mocks.updateBotProvider).toHaveBeenCalledTimes(1), {
      timeout: 3000,
    });
    const [id, , patch] = mocks.updateBotProvider.mock.calls[0];
    expect(id).toBe('provider-1');
    expect(patch.settings.showUsageStats).toBe(true);
    expect(patch).not.toHaveProperty('credentials');
    expect(patch).not.toHaveProperty('applicationId');
  });

  it('keeps a failed reply-setting autosave on screen after the switch rolls back', async () => {
    mocks.updateBotProvider.mockRejectedValue(new Error('network down'));
    render(<PlatformDetail agentId="agt" currentConfig={config} platformDef={platformDef} />, {
      wrapper: MemoryRouter,
    });

    openTab(en['channel.tab.reply']);
    fireEvent.click(screen.getByRole('switch', { name: new RegExp(en['channel.showUsageStats']) }));

    await waitFor(() => expect(mocks.updateBotProvider).toHaveBeenCalledTimes(1), {
      timeout: 3000,
    });
    // The post-save recompute is not a user edit, so it must not clear the failure.
    expect(await screen.findByText(en['channel.updateFailedRetry'])).toBeInTheDocument();
    await new Promise((resolve) => setTimeout(resolve, 50));
    expect(screen.getByText(en['channel.updateFailedRetry'])).toBeInTheDocument();
  });

  it('saves the wake switch by itself as soon as it is turned off, and hides the options', async () => {
    render(<PlatformDetail agentId="agt" currentConfig={config} platformDef={platformDef} />, {
      wrapper: MemoryRouter,
    });

    openTab(en['channel.tab.wake']);
    expect(screen.getByText(en['channel.wake.section.pace'])).toBeVisible();
    fireEvent.click(screen.getByRole('switch', { name: new RegExp(en['channel.botWakeEnabled']) }));

    await waitFor(() => expect(mocks.updateBotProvider).toHaveBeenCalledTimes(1), {
      timeout: 3000,
    });
    const [, , patch] = mocks.updateBotProvider.mock.calls[0];
    expect(patch.settings.botWakeDisabled).toBe(true);
    // The rest of the wake options are written as stored.
    expect(patch.settings.botWakePace).toBe('custom');
    expect(screen.queryByText(en['channel.wake.section.pace'])).toBeNull();
  });

  it.each([
    ['with no switch stored', { showUsageStats: false }],
    ['with the retired botWakeEnabled: false', { botWakeEnabled: false, showUsageStats: false }],
  ])(
    'shows a bot saved %s with the wake switch on, and keeps it on when saving something else',
    async (_, settings) => {
      render(
        <PlatformDetail
          agentId="agt"
          currentConfig={{ ...config, settings }}
          platformDef={platformDef}
        />,
        { wrapper: MemoryRouter },
      );

      openTab(en['channel.tab.wake']);
      expect(
        screen.getByRole('switch', { name: new RegExp(en['channel.botWakeEnabled']) }),
      ).toHaveAttribute('aria-checked', 'true');
      expect(screen.getByText(en['channel.wake.section.pace'])).toBeVisible();

      openTab(en['channel.tab.reply']);
      fireEvent.click(
        screen.getByRole('switch', { name: new RegExp(en['channel.showUsageStats']) }),
      );

      await waitFor(() => expect(mocks.updateBotProvider).toHaveBeenCalledTimes(1), {
        timeout: 3000,
      });
      expect(mocks.updateBotProvider.mock.calls[0][2].settings.botWakeDisabled).not.toBe(true);
    },
  );

  it('writes an operator’s mode override back untouched when it saves something else', async () => {
    const override = { classifier: { mode: 'off' }, mode: 'shadow' };
    render(
      <PlatformDetail
        agentId="agt"
        platformDef={platformDef}
        currentConfig={{
          ...config,
          settings: { ...config.settings, botWake: override, botWakeDisabled: false },
        }}
      />,
      { wrapper: MemoryRouter },
    );

    openTab(en['channel.tab.reply']);
    fireEvent.click(screen.getByRole('switch', { name: new RegExp(en['channel.showUsageStats']) }));

    await waitFor(() => expect(mocks.updateBotProvider).toHaveBeenCalledTimes(1), {
      timeout: 3000,
    });
    expect(mocks.updateBotProvider.mock.calls[0][2].settings.botWake).toEqual(override);
  });

  it('shows a Save in each wake block, and on the replies only for how the bot talks', () => {
    render(<PlatformDetail agentId="agt" currentConfig={config} platformDef={platformDef} />, {
      wrapper: MemoryRouter,
    });

    openTab(en['channel.tab.wake']);
    expect(screen.getAllByRole('button', { name: en['channel.module.save'] })).toHaveLength(4);

    openTab(en['channel.tab.reply']);
    const style = screen
      .getByText(en['channel.reply.section.style'])
      .closest('div[class*="header"]')!.parentElement!;
    expect(screen.getAllByRole('button', { name: en['channel.module.save'] })).toEqual([
      within(style).getByRole('button', { name: en['channel.module.save'] }),
    ]);
  });

  it('keeps the reply style off the advanced tab and saves it only with its own Save', async () => {
    render(<PlatformDetail agentId="agt" currentConfig={config} platformDef={platformDef} />, {
      wrapper: MemoryRouter,
    });

    openTab(en['channel.tab.advanced']);
    expect(screen.queryByText(en['channel.replyStyle'])).not.toBeVisible();

    openTab(en['channel.tab.reply']);
    const area = screen.getByPlaceholderText(en['channel.replyStylePlaceholder']);
    expect(area).toBeVisible();
    fireEvent.change(area, { target: { value: 'Short and casual.' } });
    fireEvent.blur(area);

    await waitFor(() => expect(screen.getByText(en['channel.module.unsaved'])).toBeVisible());
    // Typed text is not an instant reply setting: nothing is written until Save.
    await new Promise((resolve) => setTimeout(resolve, 600));
    expect(mocks.updateBotProvider).not.toHaveBeenCalled();

    const style = screen
      .getByText(en['channel.reply.section.style'])
      .closest('div[class*="header"]')!.parentElement!;
    fireEvent.click(within(style).getByRole('button', { name: en['channel.module.save'] }));

    await waitFor(() => expect(mocks.updateBotProvider).toHaveBeenCalledTimes(1));
    expect(mocks.updateBotProvider.mock.calls[0][2].settings.replyStyle).toBe('Short and casual.');
  });

  it('keeps Remove and Test connection on the Connect tab, off the settings tabs', () => {
    render(<PlatformDetail agentId="agt" currentConfig={config} platformDef={platformDef} />, {
      wrapper: MemoryRouter,
    });
    const remove = () =>
      screen.queryByRole('button', { name: new RegExp(en['channel.removeChannel']) });
    const test = () =>
      screen.queryByRole('button', { name: new RegExp(en['channel.testConnection']) });

    // A saved bot opens on a settings tab.
    expect(remove()).toBeNull();
    expect(test()).toBeNull();

    openTab(en['channel.tab.connect']);
    expect(remove()).toBeVisible();
    expect(test()).toBeVisible();
    expect(screen.queryByRole('button', { name: en['channel.save'] })).toBeNull();
    expect(screen.queryByRole('button', { name: en['channel.saveAndConnect'] })).toBeNull();
  });

  it('saves only the block that was changed, and keeps another block’s unsaved edit', async () => {
    render(<PlatformDetail agentId="agt" currentConfig={config} platformDef={platformDef} />, {
      wrapper: MemoryRouter,
    });
    openTab(en['channel.tab.wake']);

    // Edit the silent-wake limit (Pace) and the duty sensitivity (Duties).
    const limit = screen.getByRole('textbox', {
      name: new RegExp(en['channel.botWakeMaxSilentWakes']),
    });
    fireEvent.change(limit, { target: { value: '5' } });
    fireEvent.blur(limit);
    const sensitivity = screen.getByRole('textbox', {
      name: new RegExp(en['channel.botWakeDutiesThreshold']),
    });
    fireEvent.change(sensitivity, { target: { value: '0.9' } });
    fireEvent.blur(sensitivity);

    await waitFor(() => expect(screen.getAllByText(en['channel.module.unsaved'])).toHaveLength(2));

    // Save Pace only.
    const pace = screen
      .getByText(en['channel.wake.section.pace'])
      .closest('div[class*="header"]')!.parentElement!;
    fireEvent.click(within(pace).getByRole('button', { name: en['channel.module.save'] }));

    await waitFor(() => expect(mocks.updateBotProvider).toHaveBeenCalledTimes(1));
    const [, , patch] = mocks.updateBotProvider.mock.calls[0];
    expect(patch.settings.botWakeMaxSilentWakes).toBe(5);
    // The sensitivity is edited but not saved: the write carries the stored value.
    expect(patch.settings.botWakeDutiesThreshold).toBe(0.65);
    expect(patch).not.toHaveProperty('credentials');
    expect(mocks.connectBot).not.toHaveBeenCalled();

    // Duties still has its unsaved edit.
    await waitFor(() => expect(screen.getAllByText(en['channel.module.unsaved'])).toHaveLength(1));
    expect(sensitivity).toHaveValue('0.9');
  });
});

describe('PlatformDetail status reactions', () => {
  beforeEach(() => {
    mocks.heterogeneous = false;
    vi.clearAllMocks();
  });

  it('starts a new bot with status reactions off', () => {
    render(<PlatformDetail agentId="agt" platformDef={platformDef} />, { wrapper: MemoryRouter });

    openTab(en['channel.tab.reply']);
    expect(screen.getByText(en['channel.reactionModeNoneHint'])).toBeInTheDocument();
    expect(screen.queryByText(en['channel.reactionModeMinimalHint'])).toBeNull();
  });

  it('shows a bot saved before the field existed with reactions on, as the runtime reads it', () => {
    render(<PlatformDetail agentId="agt" currentConfig={config} platformDef={platformDef} />, {
      wrapper: MemoryRouter,
    });

    openTab(en['channel.tab.reply']);
    expect(screen.getByText(en['channel.reactionModeMinimalHint'])).toBeInTheDocument();
    expect(screen.queryByText(en['channel.reactionModeNoneHint'])).toBeNull();
  });
});