Skip to content

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

5.3 KB
Raw Blame
import { renderHook, waitFor } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";

/**
 * The Team purchase mints its Stripe session as the buyer's SaaS account, so what the hook sends
 * is the contract worth pinning: the sized capacity, and no email (the edge function reads that
 * from the JWT). Both are asserted through an injected session creator, which is also what keeps
 * the test off a Supabase client.
 */
const getLicenseInfo = vi.fn();
const getInstallationId = vi.fn();

vi.mock("@editor/services/licenseService", () => ({
  default: {
    getLicenseInfo: () => getLicenseInfo(),
    getInstallationId: () => getInstallationId(),
  },
}));
vi.mock("@editor/utils/licenseCheckoutUtils", () => ({
  resyncExistingLicense: vi.fn(),
  pollTeamCheckout: vi.fn(),
}));
vi.mock("@editor/utils/protocolDetection", () => ({
  getCheckoutMode: () => "embedded",
}));
vi.mock("@editor/services/serverPlanCheckout", () => ({
  createServerPlanCheckoutSession: vi.fn(),
}));

import { useCheckoutSession } from "@editor/components/shared/stripeCheckout/hooks/useCheckoutSession";
import { pollTeamCheckout } from "@editor/utils/licenseCheckoutUtils";
import type { CheckoutState } from "@editor/components/shared/stripeCheckout/types/checkout";

const plan = {
  id: "selfhosted:team:yearly",
  name: "Team",
  price: 0,
  currency: "usd",
  period: "yearly",
  features: [],
  highlights: [],
  lookupKey: "selfhosted:team:yearly",
  requiresSeats: false,
};

function mount(
  createSession: ReturnType<typeof vi.fn>,
  state: CheckoutState = { currentStage: "payment" },
  selectedPlan = plan,
  installationId: string | null = "install-abc",
) {
  const setState = vi.fn();
  const hook = renderHook(() =>
    useCheckoutSession(
      selectedPlan,
      state,
      setState,
      installationId,
      vi.fn(),
      null,
      vi.fn(),
      vi.fn(),
      7,
      3,
      vi.fn(),
      undefined,
      undefined,
      undefined,
      createSession as never,
    ),
  );
  return { hook, setState };
}

beforeEach(() => {
  getLicenseInfo.mockReset().mockResolvedValue({ licenseType: "NORMAL" });
  getInstallationId.mockReset();
});

describe("useCheckoutSession", () => {
  it("confirms Team entitlement instead of polling for a licence key", async () => {
    vi.mocked(pollTeamCheckout).mockResolvedValue({ success: true });
    const { hook } = mount(vi.fn(), {
      currentStage: "payment",
      sessionId: "cs_team",
    });
    await hook.result.current.handlePaymentComplete();
    expect(pollTeamCheckout).toHaveBeenCalledWith(
      "cs_team",
      3,
      expect.any(Object),
    );
  });
  it("sends the sized capacity and never an email", async () => {
    const createSession = vi.fn().mockResolvedValue({
      clientSecret: "cs_1",
      url: null,
      sessionId: "sess_1",
    });
    const { hook } = mount(createSession, {
      currentStage: "payment",
      email: "[email protected]",
    });

    await hook.result.current.createCheckoutSession();

    await waitFor(() => expect(createSession).toHaveBeenCalledTimes(1));
    const request = createSession.mock.calls[0][0];
    expect(request).not.toHaveProperty("email");
    expect(request).toMatchObject({
      lookupKey: "selfhosted:team:yearly",
      serverQuantity: 3,
      seatCount: 7,
      installationId: "install-abc",
      uiMode: "embedded",
    });
    expect(request.successUrl).toContain("payment_status=success");
  });

  it("carries a premium licence key through as upgrade metadata", async () => {
    getLicenseInfo.mockResolvedValue({
      licenseType: "SERVER",
      licenseKey: "key-1",
    });
    const createSession = vi
      .fn()
      .mockResolvedValue({ clientSecret: "cs", url: null, sessionId: "s" });
    const { hook } = mount(createSession, undefined, {
      ...plan,
      requiresSeats: true,
    });

    await hook.result.current.createCheckoutSession();

    expect(createSession.mock.calls[0][0].currentLicenseKey).toBe("key-1");
  });

  it("surfaces a transport failure as the error stage", async () => {
    const createSession = vi.fn().mockRejectedValue(new Error("Unauthorized"));
    const onError = vi.fn();
    const setState = vi.fn();
    const hook = renderHook(() =>
      useCheckoutSession(
        plan,
        { currentStage: "payment" },
        setState,
        "install-abc",
        vi.fn(),
        null,
        vi.fn(),
        vi.fn(),
        1,
        1,
        vi.fn(),
        undefined,
        onError,
        undefined,
        createSession as never,
      ),
    );

    await hook.result.current.createCheckoutSession();

    expect(onError).toHaveBeenCalledWith("Unauthorized");
    expect(setState).toHaveBeenCalledWith(
      expect.objectContaining({ currentStage: "error", error: "Unauthorized" }),
    );
  });
});

it("creates Team checkout without accessing installation admin endpoints", async () => {
  const createSession = vi
    .fn()
    .mockResolvedValue({ clientSecret: "cs", sessionId: "s" });
  const { hook } = mount(createSession, undefined, plan, null);
  await hook.result.current.createCheckoutSession();
  expect(createSession).toHaveBeenCalledWith(
    expect.objectContaining({
      serverQuantity: 3,
      installationId: undefined,
      currentLicenseKey: undefined,
    }),
  );
  expect(getInstallationId).not.toHaveBeenCalled();
  expect(getLicenseInfo).not.toHaveBeenCalled();
});