Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

5.1 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from "vitest";
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import { MemoryRouter } from "react-router-dom";
import { MantineProvider } from "@mantine/core";
import type { ReactNode } from "react";
import { PortalProviders } from "@editor/portal/PortalProviders";
import { PlatformProviders } from "@editor/components/PlatformProviders";
import { ServerLicenseSection } from "@editor/portal/components/billing/ServerLicenseSection";

const { fetchAppConfig, getLicenseInfo } = vi.hoisted(() => ({
  fetchAppConfig: vi.fn(),
  getLicenseInfo: vi.fn(),
}));

vi.mock("@editor/auth", () => ({
  useAuth: () => ({ user: { id: "owner" }, isAdmin: true, loading: false }),
}));
vi.mock("@editor/auth/UseSession", async (importOriginal) => ({
  ...(await importOriginal<typeof import("@editor/auth/UseSession")>()),
  AuthProvider: ({ children }: { children: ReactNode }) => children,
}));
vi.mock("@editor/api/config", () => ({
  DEFAULT_APP_CONFIG: { enableLogin: true },
  fetchAppConfig,
}));
vi.mock("@editor/services/licenseService", () => ({
  default: { getLicenseInfo },
}));
vi.mock("@editor/testing/serverExperienceSimulations", () => ({
  getSimulatedLicenseInfo: () => null,
}));
vi.mock("@editor/portal/contexts/TierContext", () => ({
  TierProvider: ({ children }: { children: ReactNode }) => children,
}));
vi.mock("@editor/portal/contexts/LinkContext", () => ({
  LinkProvider: ({ children }: { children: ReactNode }) => children,
}));
vi.mock("@editor/portal/contexts/UIContext", () => ({
  UIProvider: ({ children }: { children: ReactNode }) => children,
  useUI: () => ({ linkModalOpen: false }),
}));
vi.mock("@editor/portal/contexts/AccountLinkContext", () => ({
  AccountLinkProvider: ({ children }: { children: ReactNode }) => children,
}));
vi.mock("@editor/contexts/CheckoutContext", () => ({
  CheckoutProvider: ({ children }: { children: ReactNode }) => children,
}));
vi.mock("@editor/portal/components/account-link/LinkAccountModal", () => ({
  LinkAccountModalHost: () => null,
}));
vi.mock("@editor/portal/components/account-link/ConnectCallbackHost", () => ({
  ConnectCallbackHost: () => null,
}));
vi.mock("@editor/portal/hooks/useFreeTierExhaustedPrompt", () => ({
  useFreeTierExhaustedPrompt: () => {},
}));
vi.mock("@editor/portal/components/PortalChrome", () => ({
  PortalChrome: () => <ServerLicenseSection onSaved={() => {}} />,
}));

function renderPortal() {
  return render(
    <MemoryRouter initialEntries={["/processor/usage"]}>
      <MantineProvider>
        <PlatformProviders>
          <PortalProviders />
        </PlatformProviders>
      </MantineProvider>
    </MemoryRouter>,
  );
}

describe("Processor license loading", () => {
  beforeEach(() => {
    fetchAppConfig
      .mockReset()
      .mockResolvedValue({ isAdmin: true, enableLogin: true });
    getLicenseInfo
      .mockReset()
      .mockResolvedValue({ licenseKey: null, licenseType: "NORMAL" });
  });

  it("loads local config before fetching the license on a direct portal visit", async () => {
    let resolveConfig!: (config: {
      isAdmin: boolean;
      enableLogin: boolean;
    }) => void;
    fetchAppConfig.mockReturnValue(
      new Promise((resolve) => {
        resolveConfig = resolve;
      }),
    );
    renderPortal();
    await waitFor(() => expect(fetchAppConfig).toHaveBeenCalledTimes(1));
    expect(getLicenseInfo).not.toHaveBeenCalled();
    expect(
      screen.queryByRole("button", { name: "Add" }),
    ).not.toBeInTheDocument();

    resolveConfig({ isAdmin: true, enableLogin: true });
    expect(await screen.findByText("No license installed")).toBeInTheDocument();
    expect(getLicenseInfo).toHaveBeenCalledTimes(1);
    fireEvent.click(screen.getByRole("button", { name: "Add" }));
    expect(
      screen.getByLabelText("License Key", {
        selector: 'input[type="password"]',
      }),
    ).not.toBeDisabled();
  });

  it("settles a failed license request and retries without leaving a spinner", async () => {
    getLicenseInfo.mockRejectedValueOnce(new Error("License request failed"));
    renderPortal();
    expect(
      await screen.findByText("License request failed"),
    ).toBeInTheDocument();
    fireEvent.click(screen.getByRole("button", { name: "Try again" }));
    expect(await screen.findByText("No license installed")).toBeInTheDocument();
    expect(getLicenseInfo).toHaveBeenCalledTimes(2);
  });

  it("reports a failed config read instead of claiming no license is installed", async () => {
    fetchAppConfig.mockRejectedValueOnce(new Error("Config request failed"));
    renderPortal();
    expect(
      await screen.findByText("Config request failed"),
    ).toBeInTheDocument();
    expect(screen.queryByText("No license installed")).not.toBeInTheDocument();
    expect(
      screen.queryByRole("button", { name: "Add" }),
    ).not.toBeInTheDocument();
    expect(getLicenseInfo).not.toHaveBeenCalled();
    fireEvent.click(screen.getByRole("button", { name: "Try again" }));
    expect(await screen.findByText("No license installed")).toBeInTheDocument();
    expect(getLicenseInfo).toHaveBeenCalledOnce();
  });
});