/**
 * Unit tests for use-telegram-auth: isLocalhost.
 * Ported from webapp/js/auth.test.js. getInitData is handled by SDK in the hook.
 */

import { describe, it, expect, afterEach } from "vitest";
import { isLocalhost } from "./use-telegram-auth";

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

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

  it("returns true for localhost", () => {
    Object.defineProperty(window, "location", {
      value: { ...origLocation, hostname: "localhost" },
      writable: true,
    });
    expect(isLocalhost()).toBe(true);
  });

  it("returns true for 127.0.0.1", () => {
    Object.defineProperty(window, "location", {
      value: { ...origLocation, hostname: "127.0.0.1" },
      writable: true,
    });
    expect(isLocalhost()).toBe(true);
  });

  it("returns true for empty hostname", () => {
    Object.defineProperty(window, "location", {
      value: { ...origLocation, hostname: "" },
      writable: true,
    });
    expect(isLocalhost()).toBe(true);
  });

  it("returns false for other hostnames", () => {
    Object.defineProperty(window, "location", {
      value: { ...origLocation, hostname: "example.com" },
      writable: true,
    });
    expect(isLocalhost()).toBe(false);
  });
});
