Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

3.1 KB
Raw Blame
/**
 * @vitest-environment jsdom
 */
import { render, screen } from '@testing-library/react';
import * as React from 'react';
import { MemoryRouter } from 'react-router-dom';
import { describe, expect, it, vi } from 'vitest';

import { PlatformNav } from '@/app/components/sidebar/platform';

const plan = vi.hoisted(() => ({ aiProvidersEnabled: false }));
const flags = vi.hoisted(() => ({ edition: 'cloud' }));

vi.mock('i18next', () => ({ t: (key: string) => key }));

vi.mock('@/hooks/platform-hooks', () => ({
  platformHooks: {
    useCurrentPlatform: () => ({ platform: { plan } }),
  },
}));

vi.mock('@/hooks/flags-hooks', () => ({
  flagsHooks: {
    useFlag: () => ({ data: flags.edition }),
  },
}));

vi.mock('@/hooks/authorization-hooks', () => ({
  useAuthorization: () => ({ checkAccess: () => true }),
}));

vi.mock('@/lib/route-utils', () => ({
  determineDefaultRoute: () => '/',
}));

vi.mock('@/components/ui/scroll-area', () => ({
  ScrollArea: ({ children }: React.PropsWithChildren) => <>{children}</>,
}));

vi.mock('@/components/ui/sidebar-shadcn', () => {
  const Passthrough = ({ children }: React.PropsWithChildren) => (
    <>{children}</>
  );
  return {
    Sidebar: Passthrough,
    SidebarContent: Passthrough,
    SidebarFooter: Passthrough,
    SidebarMenu: Passthrough,
    SidebarHeader: Passthrough,
    SidebarGroup: Passthrough,
    SidebarGroupContent: Passthrough,
    SidebarGroupLabel: Passthrough,
  };
});

vi.mock('@/app/components/sidebar/sidebar-user', () => ({
  SidebarUser: () => null,
}));

vi.mock('@/app/components/sidebar/ap-sidebar-item', () => ({
  ApSidebarItem: ({
    label,
    subItems,
  }: {
    label: string;
    subItems?: { label: string; locked?: boolean }[];
  }) => (
    <ul aria-label={label}>
      {(subItems ?? []).map((subItem) => (
        <li
          key={subItem.label}
          aria-label={
            subItem.locked ? `${subItem.label} locked` : subItem.label
          }
        />
      ))}
    </ul>
  ),
}));

function renderSidebar() {
  render(
    <MemoryRouter>
      <PlatformNav />
    </MemoryRouter>,
  );
}

describe('platform sidebar AI Center', () => {
  it('leaves Providers unlocked when aiProvidersEnabled is on', () => {
    plan.aiProvidersEnabled = true;

    renderSidebar();

    expect(screen.queryAllByRole('listitem', { name: 'Providers' })).toEqual([
      expect.any(HTMLLIElement),
    ]);
    expect(
      screen.queryAllByRole('listitem', { name: 'Providers locked' }),
    ).toEqual([]);
  });

  it('shows Tiers between Providers and Capabilities on cloud', () => {
    plan.aiProvidersEnabled = true;
    flags.edition = 'cloud';

    renderSidebar();

    const aiCenter = screen.getByRole('list', { name: 'AI Center' });
    expect(
      Array.from(aiCenter.children).map((item) =>
        item.getAttribute('aria-label'),
      ),
    ).toEqual(['Providers', 'Tiers', 'Capabilities']);
  });

  it('hides Tiers on self-hosted enterprise', () => {
    plan.aiProvidersEnabled = true;
    flags.edition = 'ee';

    renderSidebar();

    expect(screen.queryAllByRole('listitem', { name: 'Tiers' })).toEqual([]);
  });
});