/**
 * Unit tests for launch-params: getStartParamFromUrl (deep link start_param).
 */

import { describe, it, expect, afterEach } from "vitest";
import { getStartParamFromUrl } from "./launch-params";

describe("getStartParamFromUrl", () => {
  const origLocation = window.location;

  afterEach(() => {
    Object.defineProperty(window, "location", {
      value: origLocation,
      writable: true,
    });
  });

  it("returns start_param from search", () => {
    Object.defineProperty(window, "location", {
      value: { ...origLocation, search: "?tgWebAppStartParam=duty", hash: "" },
      writable: true,
    });
    expect(getStartParamFromUrl()).toBe("duty");
  });

  it("returns start_param from hash when not in search", () => {
    Object.defineProperty(window, "location", {
      value: { ...origLocation, search: "", hash: "#tgWebAppStartParam=duty" },
      writable: true,
    });
    expect(getStartParamFromUrl()).toBe("duty");
  });

  it("prefers search over hash", () => {
    Object.defineProperty(window, "location", {
      value: {
        ...origLocation,
        search: "?tgWebAppStartParam=calendar",
        hash: "#tgWebAppStartParam=duty",
      },
      writable: true,
    });
    expect(getStartParamFromUrl()).toBe("calendar");
  });

  it("returns undefined when param is absent", () => {
    Object.defineProperty(window, "location", {
      value: { ...origLocation, search: "", hash: "" },
      writable: true,
    });
    expect(getStartParamFromUrl()).toBeUndefined();
  });
});
