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

import {
  buildHostedChannelSelectionUrl,
  buildHostedPairingUrl,
  hasHostedPairingRequest,
  isHostedStaticApp,
  readHostedPairingRequest,
} from "./hostedPairing";

describe("hostedPairing", () => {
  afterEach(() => {
    vi.unstubAllEnvs();
  });

  it("reads hosted pairing host and query token parameters", () => {
    const url = new URL(
      "https://app.trifecta.belweave.ai/pair?host=100.64.1.2:3773&token=ABCD1234",
    );

    expect(readHostedPairingRequest(url)).toEqual({
      host: "100.64.1.2:3773",
      token: "ABCD1234",
      label: "",
    });
    expect(hasHostedPairingRequest(url)).toBe(true);
  });

  it("prefers hash tokens so generated hosted links do not put credentials in search params", () => {
    vi.stubEnv("VITE_HOSTED_APP_URL", "https://preview.trifecta.belweave.ai");

    const url = new URL(
      buildHostedPairingUrl({
        host: "https://backend.example.com:3773",
        token: "pairing-token",
        label: "Workstation",
      }),
    );

    expect(url.origin).toBe("https://preview.trifecta.belweave.ai");
    expect(url.pathname).toBe("/pair");
    expect(url.searchParams.get("host")).toBe("https://backend.example.com:3773");
    expect(url.searchParams.get("label")).toBe("Workstation");
    expect(url.searchParams.has("token")).toBe(false);
    expect(url.hash).toBe("#token=pairing-token");
  });

  it("builds hosted channel selection URLs through the configured router origin", () => {
    vi.stubEnv("VITE_HOSTED_APP_URL", "https://app.trifecta.belweave.ai");

    const url = new URL(
      buildHostedChannelSelectionUrl({
        channel: "nightly",
      }),
    );

    expect(url.origin).toBe("https://app.trifecta.belweave.ai");
    expect(url.pathname).toBe("/__belweave/channel");
    expect(url.searchParams.get("channel")).toBe("nightly");
    expect(url.searchParams.has("next")).toBe(false);
  });

  it("ignores incomplete hosted pairing requests", () => {
    expect(
      hasHostedPairingRequest(
        new URL("https://app.trifecta.belweave.ai/pair?host=backend.example.com"),
      ),
    ).toBe(false);
    expect(
      hasHostedPairingRequest(new URL("https://app.trifecta.belweave.ai/pair?token=ABCD1234")),
    ).toBe(false);
  });

  it("detects the hosted static app only when no backend URL is configured", () => {
    vi.stubEnv("VITE_HOSTED_APP_URL", "https://preview.trifecta.belweave.ai");
    vi.stubEnv("VITE_HTTP_URL", "");
    vi.stubEnv("VITE_WS_URL", "");

    expect(isHostedStaticApp(new URL("https://preview.trifecta.belweave.ai/"))).toBe(true);
    expect(isHostedStaticApp(new URL("https://preview.trifecta.belweave.ai/pair"))).toBe(true);
    expect(isHostedStaticApp(new URL("https://backend.example.com/"))).toBe(false);

    vi.stubEnv("VITE_HTTP_URL", "https://backend.example.com");
    expect(isHostedStaticApp(new URL("https://preview.trifecta.belweave.ai/"))).toBe(false);
  });

  it("detects hosted channel aliases as static apps", () => {
    vi.stubEnv("VITE_HOSTED_APP_URL", "https://app.trifecta.belweave.ai");
    vi.stubEnv("VITE_HOSTED_APP_CHANNEL", "nightly");
    vi.stubEnv("VITE_HTTP_URL", "");
    vi.stubEnv("VITE_WS_URL", "");

    expect(isHostedStaticApp(new URL("https://nightly.app.trifecta.belweave.ai/"))).toBe(true);

    vi.stubEnv("VITE_HTTP_URL", "https://backend.example.com");
    expect(isHostedStaticApp(new URL("https://nightly.app.trifecta.belweave.ai/"))).toBe(false);
  });

  it("detects any TLD for canonical trifecta domains (e.g. .com and .ai)", () => {
    vi.stubEnv("VITE_HOSTED_APP_URL", "");
    vi.stubEnv("VITE_HTTP_URL", "");
    vi.stubEnv("VITE_WS_URL", "");

    expect(isHostedStaticApp(new URL("https://app.trifecta.belweave.ai/"))).toBe(true);
    expect(isHostedStaticApp(new URL("https://app.trifecta.belweave.com/"))).toBe(true);
    expect(isHostedStaticApp(new URL("https://latest.app.trifecta.belweave.com/"))).toBe(true);
    expect(isHostedStaticApp(new URL("https://nightly.app.trifecta.belweave.ai/"))).toBe(true);
    expect(isHostedStaticApp(new URL("https://backend.example.com/"))).toBe(false);
  });

  it("treats browser-only HTTPS custom domains as hosted static apps", () => {
    vi.stubEnv("VITE_HOSTED_APP_URL", "");
    vi.stubEnv("VITE_HTTP_URL", "");
    vi.stubEnv("VITE_WS_URL", "");
    vi.stubGlobal("window", {
      location: new URL("https://agentmeld.com/"),
      desktopBridge: undefined,
    });

    expect(isHostedStaticApp(new URL("https://agentmeld.com/"))).toBe(true);
    vi.stubGlobal("window", {
      location: new URL("https://www.agentmeld.com/"),
      desktopBridge: undefined,
    });
    expect(isHostedStaticApp(new URL("https://www.agentmeld.com/"))).toBe(true);
    vi.stubGlobal("window", {
      location: new URL("https://trifecta-web-app.vercel.app/"),
      desktopBridge: undefined,
    });
    expect(isHostedStaticApp(new URL("https://trifecta-web-app.vercel.app/"))).toBe(true);
  });

  it("does not treat loopback or desktop-managed origins as hosted static apps", () => {
    vi.stubEnv("VITE_HOSTED_APP_URL", "");
    vi.stubEnv("VITE_HTTP_URL", "");
    vi.stubEnv("VITE_WS_URL", "");
    vi.stubGlobal("window", {
      location: new URL("https://backend.example.com/"),
      desktopBridge: {
        getLocalEnvironmentBootstrap: () => null,
      },
    });

    expect(isHostedStaticApp(new URL("https://backend.example.com/"))).toBe(false);

    vi.stubGlobal("window", {
      location: new URL("http://localhost:5735/"),
      desktopBridge: undefined,
    });
    expect(isHostedStaticApp(new URL("http://localhost:5735/"))).toBe(false);
  });
});
