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.3 KB
Raw Blame
import { render, screen } from '@testing-library/react';
import type { ReactNode } from 'react';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { WorkspaceAgentModelPolicy } from './WorkspaceAgentModelPolicy';

const testState = vi.hoisted(() => ({
  agent: {
    agentMap: {
      'agent-1': {
        agencyConfig: {
          modelSelectionPolicy: 'member' as const,
        },
        model: 'gpt-4',
        visibility: 'public' as 'private' | 'public',
        workspaceId: 'workspace-1',
      },
    } as Record<string, object>,
    updateAgentConfigById: vi.fn(),
  },
}));

vi.mock('@/features/ModelSelect', () => ({
  default: () => <div data-testid="model-select" />,
}));

vi.mock('./WorkspaceAgentPolicyCard', () => ({
  WorkspaceAgentPolicyCard: ({ children, title }: { children: ReactNode; title: string }) => (
    <div>
      <span>{title}</span>
      {children}
    </div>
  ),
}));

vi.mock('@/store/agent', () => ({
  useAgentStore: (selector: (state: typeof testState.agent) => unknown) =>
    selector(testState.agent),
}));

describe('WorkspaceAgentModelPolicy', () => {
  beforeEach(() => {
    testState.agent.agentMap['agent-1'] = {
      agencyConfig: { modelSelectionPolicy: 'member' },
      model: 'gpt-4',
      visibility: 'public',
      workspaceId: 'workspace-1',
    };
  });

  it('renders the policy card for a loaded workspace agent', () => {
    render(<WorkspaceAgentModelPolicy agentId="agent-1" />);

    expect(screen.getByText('settingAgent.modelPolicy.title')).toBeTruthy();
    expect(screen.getByTestId('model-select')).toBeTruthy();
  });

  it('renders the model picker without a member-switch control — that moved to the Permission page', () => {
    testState.agent.agentMap['agent-1'] = {
      agencyConfig: { modelSelectionPolicy: 'fixed' },
      model: 'gpt-4',
      visibility: 'public',
      workspaceId: 'workspace-1',
    };

    render(<WorkspaceAgentModelPolicy agentId="agent-1" />);

    expect(screen.getByTestId('model-select')).toBeTruthy();
    expect(screen.queryByTestId('policy-menu')).toBeNull();
  });

  it('renders nothing instead of crashing while the agent config is not loaded yet', () => {
    const { container } = render(<WorkspaceAgentModelPolicy agentId="missing-agent" />);

    expect(container.firstChild).toBeNull();
  });
});