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

import {
  buildGlobalPageUrl,
  resolveGlobalRouteContext,
  resolveGlobalRouteContextValue,
} from "../global-route-context";
import { ROUTES } from "../routes";

describe("global route context", () => {
  it("preserves a known character chat as the return target", () => {
    const context = resolveGlobalRouteContext("/characters/maya/chat");

    expect(context).toMatchObject({
      characterSlug: "maya",
      chatUrl: "/characters/maya/chat",
      splashUrl: "/characters/maya/splash",
    });
    expect(context.profileUrl).toBe(
      "/profile?returnTo=%2Fcharacters%2Fmaya%2Fchat",
    );
    expect(context.feedbackUrl).toBe(
      "/feedback?returnTo=%2Fcharacters%2Fmaya%2Fchat",
    );
    expect(context.coinsRulesUrl).toBe(
      "/coins-rules?returnTo=%2Fcharacters%2Fmaya%2Fchat",
    );
  });

  it("uses only the first repeated return target", () => {
    expect(
      resolveGlobalRouteContextValue([
        "/characters/nayeli/chat",
        "/characters/maya/chat",
      ]),
    ).toBe("/characters/nayeli/chat");
  });

  it.each([
    undefined,
    null,
    "",
    "https://example.com/characters/maya/chat",
    "//example.com/characters/maya/chat",
    "/characters/unknown/chat",
    "/characters/maya/private-zone",
    "/characters/maya/chat?image=1",
  ])("falls back to Elio for an invalid return target", (value) => {
    expect(resolveGlobalRouteContextValue(value)).toBe(
      "/characters/elio/chat",
    );
  });

  it("sanitizes return targets when building a global page url", () => {
    expect(buildGlobalPageUrl(ROUTES.profile, "https://example.com")).toBe(
      "/profile?returnTo=%2Fcharacters%2Felio%2Fchat",
    );
  });
});
