import { beforeEach, describe, expect, it, vi } from "vitest";

import { ROUTES } from "@/router/routes";

import { consumePendingChatImageReturn } from "../chat_image_return_session";
import {
  consumeSubscriptionExitUrl,
  consumeSubscriptionExplicitExitUrl,
  getSubscriptionFallbackExitUrl,
} from "../subscription_exit";

vi.mock("../chat_image_return_session", () => ({
  consumePendingChatImageReturn: vi.fn(),
}));

const consumePendingChatImageReturnMock = vi.mocked(
  consumePendingChatImageReturn,
);

describe("subscription exit helpers", () => {
  beforeEach(() => {
    consumePendingChatImageReturnMock.mockReset();
  });

  it("uses explicit chat image return urls first", () => {
    consumePendingChatImageReturnMock.mockReturnValue({
      reason: "image_paywall",
      messageId: "msg_1",
      returnUrl: "/chat/image/msg_1",
      createdAt: 1,
    });

    expect(consumeSubscriptionExplicitExitUrl()).toBe("/chat/image/msg_1");
  });

  it("falls back to chat when requested", () => {
    consumePendingChatImageReturnMock.mockReturnValue(null);

    expect(consumeSubscriptionExitUrl("chat")).toBe(ROUTES.chat);
  });

  it("falls back to sidebar by default", () => {
    consumePendingChatImageReturnMock.mockReturnValue(null);

    expect(getSubscriptionFallbackExitUrl(null)).toBe(ROUTES.sidebar);
    expect(consumeSubscriptionExitUrl(null)).toBe(ROUTES.sidebar);
  });
});
