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

9.9 KB
Raw Blame
/**
 * @vitest-environment jsdom
 */
/* eslint-disable jest-dom/prefer-in-document -- @testing-library/jest-dom is not a dependency of packages/web */
/* eslint-disable testing-library/no-container, testing-library/no-node-access -- <video> has no implicit ARIA role */
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { render, screen, fireEvent, within } from '@testing-library/react';
import * as React from 'react';
import { beforeEach, describe, expect, it, vi } from 'vitest';

let teamProjectsLimit: number | null = 0;
let aiProvidersEnabled = false;
let edition = 'cloud';
const createProject = vi.fn();

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

vi.mock('lucide-react', async (importOriginal) => ({
  ...(await importOriginal<Record<string, unknown>>()),
  Gem: (props: Record<string, unknown>) =>
    React.createElement('span', { ...props, 'data-testid': 'gem' }),
  Plus: (props: Record<string, unknown>) =>
    React.createElement('span', { ...props, 'data-testid': 'plus' }),
}));

vi.mock('@/hooks/platform-hooks', () => ({
  platformHooks: {
    useCurrentPlatform: () => ({
      platform: {
        plan: {
          billedTeamProjectsLimit: teamProjectsLimit,
          globalConnectionsEnabled: false,
          aiProvidersEnabled,
        },
      },
    }),
  },
}));
vi.mock('@/hooks/user-hooks', () => ({
  userHooks: {
    getCurrentUserPlatformRole: () => 'ADMIN',
    useCurrentUser: () => ({ data: undefined }),
  },
}));
vi.mock('@/hooks/authorization-hooks', () => ({
  useIsPlatformAdmin: () => true,
}));
vi.mock('@/hooks/flags-hooks', () => ({
  flagsHooks: {
    useFlag: () => ({ data: edition }),
    useFlags: () => ({ data: {} }),
  },
}));
vi.mock('@/features/billing/hooks/billing-dialog-hooks', () => ({
  billingDialogHooks: {
    useManagePlanDialogStore: () => ({ openDialog: vi.fn() }),
  },
}));
vi.mock('@/features/billing', async (importOriginal) => ({
  ...(await importOriginal<Record<string, unknown>>()),
  billingDialogHooks: {
    useManagePlanDialogStore: () => ({ openDialog: vi.fn() }),
  },
}));
vi.mock('@/features/connections', () => ({
  globalConnectionsQueries: {
    useGlobalConnections: () => ({ data: { data: [] }, isLoading: false }),
  },
}));
vi.mock('@/features/projects', () => ({
  projectCollectionUtils: {
    invalidate: vi.fn(),
    useCreateProject: () => ({ mutate: createProject, isPending: false }),
  },
}));
vi.mock('@/components/custom/global-connection-utils', () => ({
  DefaultTag: () => null,
}));
vi.mock('@/components/custom/multi-select-piece-property', () => ({
  MultiSelectPieceProperty: () => null,
}));
vi.mock('@/components/ui/sonner', () => ({ internalErrorToast: vi.fn() }));
vi.mock('@/components/custom/animated-icon-button', () => ({
  AnimatedIconButton: ({
    children,
    icon: _icon,
    iconSize: _iconSize,
    ...rest
  }: React.PropsWithChildren<Record<string, unknown>>) => (
    <button {...rest} data-testid="new-project-trigger">
      {children}
    </button>
  ),
}));
vi.mock('@/components/icons/plus', () => ({ PlusIcon: () => null }));
vi.mock('@/components/ui/sidebar-shadcn', () => ({
  SidebarMenuButton: ({
    children,
    ...rest
  }: React.PropsWithChildren<Record<string, unknown>>) => (
    <button {...rest}>{children}</button>
  ),
}));

import { FeatureSample } from '@/app/components/feature-sample';
import { FeatureTeaser } from '@/app/components/feature-teaser';
import { PlanFeatureSample } from '@/app/routes/platform/plan-feature-sample';
import { CreateProjectButton } from '@/features/projects/components/create-project-button';

const LIMIT_REACHED_COPY = "You've reached your team project limit";
const SHOWCASE_VIDEO =
  'https://cdn.activepieces.com/videos/showcase/api-keys.mp4';

const renderWithQueryClient = (ui: React.ReactElement) =>
  render(
    <QueryClientProvider
      client={
        new QueryClient({ defaultOptions: { queries: { retry: false } } })
      }
    >
      {ui}
    </QueryClientProvider>,
  );

const usedTeamProjects = (count: number) =>
  Array.from({ length: count }, () => ({ type: 'TEAM' })) as never;

beforeEach(() => {
  teamProjectsLimit = 0;
  aiProvidersEnabled = false;
  edition = 'cloud';
  createProject.mockClear();
});

describe('create project gating', () => {
  it('marks the button with a gem when the plan includes no team projects', () => {
    teamProjectsLimit = 0;
    renderWithQueryClient(
      <CreateProjectButton variant="full" projects={usedTeamProjects(0)} />,
    );
    expect(screen.getByTestId('gem')).toBeDefined();
  });

  it('marks the button with a gem when the team project allowance is used up', () => {
    teamProjectsLimit = 3;
    renderWithQueryClient(
      <CreateProjectButton variant="full" projects={usedTeamProjects(3)} />,
    );
    expect(screen.getByTestId('gem')).toBeDefined();
  });

  it('keeps the plus on the sidebar rail and names the lock in its label', () => {
    teamProjectsLimit = 3;
    renderWithQueryClient(
      <CreateProjectButton variant="icon" projects={usedTeamProjects(3)} />,
    );
    const button = screen.getByRole('button', {
      name: 'Create Project, Requires a plan upgrade',
    });
    expect(within(button).getByTestId('plus')).toBeDefined();
    expect(screen.queryByTestId('gem')).toBeNull();
  });

  it('refuses on the click instead of after the form is filled in', () => {
    teamProjectsLimit = 3;
    renderWithQueryClient(
      <CreateProjectButton variant="full" projects={usedTeamProjects(3)} />,
    );
    fireEvent.click(screen.getByRole('button', { name: /new project/i }));
    expect(screen.getByText(LIMIT_REACHED_COPY)).toBeDefined();
    expect(screen.queryAllByRole('textbox')).toHaveLength(0);
    expect(createProject).not.toHaveBeenCalled();
  });

  it('offers contact sales instead of a dead upgrade button on community', () => {
    edition = 'ce';
    teamProjectsLimit = 1;
    renderWithQueryClient(
      <CreateProjectButton variant="full" projects={usedTeamProjects(1)} />,
    );
    fireEvent.click(screen.getByRole('button', { name: /new project/i }));
    expect(screen.getByRole('button', { name: /contact sales/i })).toBeDefined();
    expect(screen.queryByRole('button', { name: /explore plans/i })).toBeNull();
  });

  it('keeps explore plans where the plan dialog is actually mounted', () => {
    edition = 'cloud';
    teamProjectsLimit = 1;
    renderWithQueryClient(
      <CreateProjectButton variant="full" projects={usedTeamProjects(1)} />,
    );
    fireEvent.click(screen.getByRole('button', { name: /new project/i }));
    expect(screen.getByRole('button', { name: /explore plans/i })).toBeDefined();
    expect(screen.queryByRole('button', { name: /contact sales/i })).toBeNull();
  });

  it('still opens the form when the plan has room', () => {
    teamProjectsLimit = 3;
    renderWithQueryClient(
      <CreateProjectButton variant="full" projects={usedTeamProjects(1)} />,
    );
    fireEvent.click(screen.getByRole('button', { name: /new project/i }));
    expect(screen.queryByText(LIMIT_REACHED_COPY)).toBeNull();
    expect(screen.queryAllByRole('textbox').length).toBeGreaterThan(0);
  });
});

describe('feature teaser', () => {
  it('plays the showcase video on community, same as every other edition', () => {
    edition = 'ce';
    const { container } = render(
      <FeatureTeaser
        featureKey="API"
        title="Enable API Keys"
        description=""
        videoUrl={SHOWCASE_VIDEO}
      />,
    );
    expect(container.querySelector('video')?.getAttribute('src')).toBe(
      SHOWCASE_VIDEO,
    );
  });

  it('offers contact sales on community, as the locked page did before', () => {
    edition = 'ce';
    render(
      <FeatureTeaser featureKey="API" title="Enable API Keys" description="" />,
    );
    expect(screen.getByRole('button', { name: /contact sales/i })).toBeDefined();
  });

  it('drops contact sales only where the caller opts out', () => {
    edition = 'ce';
    render(
      <FeatureTeaser
        featureKey="BILLING"
        title="Enable Billing"
        description=""
        showContactSales={false}
      />,
    );
    expect(screen.queryByRole('button', { name: /contact sales/i })).toBeNull();
  });

  it('plays the showcase video on cloud', () => {
    edition = 'cloud';
    const { container } = render(
      <FeatureTeaser
        featureKey="API"
        title="Enable API Keys"
        description=""
        videoUrl={SHOWCASE_VIDEO}
      />,
    );
    expect(container.querySelector('video')?.getAttribute('src')).toBe(
      SHOWCASE_VIDEO,
    );
  });
});

describe('feature sample', () => {
  it('offers contact sales on community', () => {
    edition = 'ce';
    render(
      <FeatureSample locked title="Unlock Audit Logs" featureKey="AUDIT_LOGS">
        <div />
      </FeatureSample>,
    );
    expect(screen.getByRole('button', { name: /contact sales/i })).toBeDefined();
  });

  it('shows no sales form for a surface with no feature key', () => {
    edition = 'ce';
    render(
      <FeatureSample locked title="Unlock Audit Logs">
        <div />
      </FeatureSample>,
    );
    expect(screen.queryByRole('button', { name: /contact sales/i })).toBeNull();
  });
});

describe('AI providers plan lock', () => {
  it('locks the providers page when the plan lacks aiProvidersEnabled', () => {
    render(
      <PlanFeatureSample feature="aiProviders">
        <h1>AI Center providers</h1>
      </PlanFeatureSample>,
    );
    expect(
      screen.getByRole('heading', { name: 'Unlock AI Center' }),
    ).toBeDefined();
    expect(
      screen.queryByRole('heading', { name: 'AI Center providers' }),
    ).toBeNull();
  });

  it('renders the providers page untouched when aiProvidersEnabled is on', () => {
    aiProvidersEnabled = true;
    render(
      <PlanFeatureSample feature="aiProviders">
        <h1>AI Center providers</h1>
      </PlanFeatureSample>,
    );
    expect(
      screen.getByRole('heading', { name: 'AI Center providers' }),
    ).toBeDefined();
    expect(
      screen.queryByRole('heading', { name: 'Unlock AI Center' }),
    ).toBeNull();
  });
});