import { renderToStaticMarkup } from "react-dom/server";
import { describe, expect, it, vi } from "vitest";

const mocks = vi.hoisted(() => ({
  screenProps: vi.fn(),
}));

vi.mock("../subscription-screen", () => ({
  SubscriptionScreen: (props: Record<string, unknown>) => {
    mocks.screenProps(props);
    return <main>Global subscription</main>;
  },
}));

import SubscriptionPage from "../page";

describe("SubscriptionPage", () => {
  it("renders outside CharacterProvider and treats character as return context", async () => {
    const page = await SubscriptionPage({
      searchParams: Promise.resolve({
        character: "maya",
        returnTo: "profile",
        type: "topup",
      }),
    });

    expect(renderToStaticMarkup(page)).toContain("Global subscription");
    expect(mocks.screenProps).toHaveBeenCalledWith(
      expect.objectContaining({
        returnTo: "profile",
        sourceCharacterSlug: "maya",
        subscriptionType: "topup",
      }),
    );
  });

  it("keeps the recipient empty when source navigation has no character", async () => {
    const page = await SubscriptionPage({
      searchParams: Promise.resolve({}),
    });

    renderToStaticMarkup(page);
    expect(mocks.screenProps).toHaveBeenLastCalledWith(
      expect.objectContaining({ sourceCharacterSlug: null }),
    );
  });
});
