import { describe, expect, test } from "vitest";
import {
  buildSignupWithoutVerificationSuccessPath,
  buildVerificationLinks,
  buildVerificationRequestedLoginHref,
  buildVerificationRequestedPath,
} from "./verification-links";

const WEBAPP_URL = "http://localhost:3000";

describe("verification link helpers", () => {
  test("builds a verification requested path with token only", () => {
    expect(buildVerificationRequestedPath({ token: "abc123" })).toBe(
      "/auth/verification-requested?token=abc123"
    );
  });

  test("builds a verification requested path that preserves callback URL", () => {
    expect(
      buildVerificationRequestedPath({
        token: "abc123",
        callbackUrl: "http://localhost:3000/invite?token=invite-token",
      })
    ).toBe(
      "/auth/verification-requested?token=abc123&callbackUrl=http%3A%2F%2Flocalhost%3A3000%2Finvite%3Ftoken%3Dinvite-token"
    );
  });

  test("builds a verification requested path that preserves SSO recovery purpose", () => {
    expect(
      buildVerificationRequestedPath({
        token: "abc123",
        callbackUrl: "http://localhost:3000/invite?token=invite-token",
        purpose: "sso_recovery",
      })
    ).toBe(
      "/auth/verification-requested?token=abc123&callbackUrl=http%3A%2F%2Flocalhost%3A3000%2Finvite%3Ftoken%3Dinvite-token&purpose=sso_recovery"
    );
  });

  // ENG-2099: nothing about what happened to one address may ride along in this path. The
  // verification-requested page decides its "we couldn't send it" copy from IS_SMTP_CONFIGURED, which is
  // the same for every visitor — a per-sign-up flag here would have made the URL an account-existence
  // signal, since a send is only ever attempted for an address that was actually created.
  test("carries nothing beyond the token, callback URL, and purpose", () => {
    const path = buildVerificationRequestedPath({
      token: "abc123",
      callbackUrl: "http://localhost:3000/invite?token=invite-token",
      purpose: "sso_recovery",
    });

    expect([...new URL(path, WEBAPP_URL).searchParams.keys()].toSorted()).toEqual([
      "callbackUrl",
      "purpose",
      "token",
    ]);
  });

  // ENG-2091: this is the EMAIL_VERIFICATION_DISABLED=1 landing page — the self-hosted default. It has
  // to carry the invite callback for the same reason the verification path does, or the log-in button on
  // it drops an invited visitor at the app root with the invite unreachable.
  test("builds a no-verification success path that preserves the callback URL", () => {
    expect(
      buildSignupWithoutVerificationSuccessPath({
        token: "abc123",
        callbackUrl: "http://localhost:3000/invite?token=invite-token",
      })
    ).toBe(
      "/auth/signup-without-verification-success?token=abc123&callbackUrl=http%3A%2F%2Flocalhost%3A3000%2Finvite%3Ftoken%3Dinvite-token"
    );
  });

  test("omits the callback URL from the no-verification success path when there is none", () => {
    expect(buildSignupWithoutVerificationSuccessPath({ token: "abc123" })).toBe(
      "/auth/signup-without-verification-success?token=abc123"
    );
  });

  test("builds absolute verification links that preserve a valid callback URL", () => {
    expect(
      buildVerificationLinks({
        token: "abc123",
        webAppUrl: WEBAPP_URL,
        callbackUrl: "http://localhost:3000/environments/test?foo=bar",
      })
    ).toEqual({
      verificationRequestLink:
        "http://localhost:3000/auth/verification-requested?token=abc123&callbackUrl=http%3A%2F%2Flocalhost%3A3000%2Fenvironments%2Ftest%3Ffoo%3Dbar",
      verifyLink:
        "http://localhost:3000/api/auth/sso-recovery/sign-in?token=abc123&callbackUrl=http%3A%2F%2Flocalhost%3A3000%2Fenvironments%2Ftest%3Ffoo%3Dbar",
    });
  });

  test("drops invalid callback URLs from absolute verification links", () => {
    expect(
      buildVerificationLinks({
        token: "abc123",
        webAppUrl: WEBAPP_URL,
        callbackUrl: "https://evil.example/phish",
      })
    ).toEqual({
      verificationRequestLink: "http://localhost:3000/auth/verification-requested?token=abc123",
      verifyLink: "http://localhost:3000/api/auth/sso-recovery/sign-in?token=abc123",
    });
  });

  test("routes the SSO recovery verify link to the Better Auth sign-in endpoint", () => {
    expect(
      buildVerificationLinks({
        token: "abc123",
        webAppUrl: WEBAPP_URL,
        callbackUrl: "http://localhost:3000/environments/test?foo=bar",
        purpose: "sso_recovery",
        verificationRequestToken: "email-token",
      })
    ).toEqual({
      verificationRequestLink:
        "http://localhost:3000/auth/verification-requested?token=email-token&callbackUrl=http%3A%2F%2Flocalhost%3A3000%2Fenvironments%2Ftest%3Ffoo%3Dbar&purpose=sso_recovery",
      // The verify link always resolves at Better Auth's SSO-recovery endpoint now (email verification
      // is Better Auth-native — the legacy /auth/verify page is gone).
      verifyLink:
        "http://localhost:3000/api/auth/sso-recovery/sign-in?token=abc123&callbackUrl=http%3A%2F%2Flocalhost%3A3000%2Fenvironments%2Ftest%3Ffoo%3Dbar",
    });
  });
});

describe("buildVerificationRequestedLoginHref", () => {
  test("carries an ordinary callback, so an invited visitor lands back on the invite", () => {
    expect(buildVerificationRequestedLoginHref(`${WEBAPP_URL}/invite?token=abc`)).toBe(
      `/auth/login?callbackUrl=${encodeURIComponent(`${WEBAPP_URL}/invite?token=abc`)}`
    );
  });

  test("plain log-in when there is no callback", () => {
    expect(buildVerificationRequestedLoginHref(null)).toBe("/auth/login");
  });

  /**
   * ENG-7310. Only the browser that opened the emailed link can finish SSO recovery, so a log-in from
   * this page that followed the completion URL would land on an error.
   */
  test.each([
    `${WEBAPP_URL}/api/auth/sso/recovery/complete?state=abc`,
    `${WEBAPP_URL}/api//auth/sso/recovery///complete?state=abc`,
  ])("drops an SSO recovery completion callback: %s", (callbackUrl) => {
    expect(buildVerificationRequestedLoginHref(callbackUrl)).toBe("/auth/login");
  });
});
