Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

3.9 KB
Raw Blame
import { afterEach, describe, expect, it, vi } from "vitest";
import { screen } from "@testing-library/react";
import { renderWithProviders as render } from "@/../tests/test-utils";
import ChatLayout from "./layout";

const { mockUseAuthorized, mockUseUISettings, mockReplace, mockUiHref, state } = vi.hoisted(() => {
  const state = {
    enableChatUI: false,
    isUISettingsLoading: false,
    isIdentityLoading: false,
    isAuthorized: true,
  };
  return {
    state,
    mockReplace: vi.fn(),
    mockUiHref: vi.fn((segment: string) => `/mocked-ui/${segment}`),
    mockUseAuthorized: vi.fn(() => ({
      accessToken: "token-123",
      userRole: "Internal User",
      userId: "user-1",
      userEmail: "[email protected]",
      premiumUser: false,
      isLoading: state.isIdentityLoading,
      isAuthorized: state.isAuthorized,
    })),
    mockUseUISettings: vi.fn(() => ({
      data: { values: { enable_chat_ui: state.enableChatUI } },
      isLoading: state.isUISettingsLoading,
    })),
  };
});

vi.mock("next/navigation", () => ({
  useRouter: () => ({ replace: mockReplace }),
}));
vi.mock("@/app/(dashboard)/hooks/useAuthorized", () => ({ default: mockUseAuthorized }));
vi.mock("@/app/(dashboard)/hooks/uiSettings/useUISettings", () => ({ useUISettings: mockUseUISettings }));
vi.mock("@/utils/uiHref", () => ({ uiHref: mockUiHref }));
vi.mock("@/components/navbar", () => ({ default: () => <div data-testid="navbar" /> }));
vi.mock("@/contexts/ThemeContext", () => ({
  ThemeProvider: ({ children }: { children: React.ReactNode }) => <>{children}</>,
}));
vi.mock("@/contexts/ChatShellContext", () => ({
  ChatShellProvider: ({ children }: { children: React.ReactNode }) => <>{children}</>,
}));
vi.mock("@/components/chat/ChatShell", () => ({
  default: ({ children }: { children: React.ReactNode }) => <div data-testid="chat-shell">{children}</div>,
}));

describe("ChatLayout", () => {
  afterEach(() => {
    state.enableChatUI = false;
    state.isUISettingsLoading = false;
    state.isIdentityLoading = false;
    state.isAuthorized = true;
    mockReplace.mockClear();
    mockUiHref.mockClear();
  });

  it("renders the chat shell when enable_chat_ui is on", () => {
    state.enableChatUI = true;
    render(
      <ChatLayout>
        <div data-testid="page-content" />
      </ChatLayout>,
    );
    expect(screen.getByTestId("chat-shell")).toBeInTheDocument();
    expect(screen.getByTestId("page-content")).toBeInTheDocument();
    expect(mockReplace).not.toHaveBeenCalled();
  });

  it("redirects to the dashboard when enable_chat_ui is off", () => {
    state.enableChatUI = false;
    render(
      <ChatLayout>
        <div data-testid="page-content" />
      </ChatLayout>,
    );
    expect(screen.queryByTestId("chat-shell")).not.toBeInTheDocument();
    expect(mockReplace).toHaveBeenCalledWith("/mocked-ui/");
  });

  it("renders nothing while UI settings are still loading", () => {
    state.isUISettingsLoading = true;
    render(
      <ChatLayout>
        <div data-testid="page-content" />
      </ChatLayout>,
    );
    expect(screen.queryByTestId("chat-shell")).not.toBeInTheDocument();
    expect(mockReplace).not.toHaveBeenCalled();
  });

  it("renders nothing until the signed-in user's identity has loaded", () => {
    state.enableChatUI = true;
    state.isIdentityLoading = true;
    render(
      <ChatLayout>
        <div data-testid="page-content" />
      </ChatLayout>,
    );
    expect(screen.queryByTestId("chat-shell")).not.toBeInTheDocument();
    expect(screen.queryByTestId("page-content")).not.toBeInTheDocument();
  });

  it("renders nothing when the signed-in user's identity could not be confirmed", () => {
    state.enableChatUI = true;
    state.isAuthorized = false;
    render(
      <ChatLayout>
        <div data-testid="page-content" />
      </ChatLayout>,
    );
    expect(screen.queryByTestId("chat-shell")).not.toBeInTheDocument();
    expect(screen.queryByTestId("page-content")).not.toBeInTheDocument();
  });
});