import { afterEach, describe, expect, it, vi } from "vitest";
import { cn, generateSecurePassword } from "./utils";

afterEach(() => {
  vi.restoreAllMocks();
  vi.unstubAllGlobals();
});

describe("cn utility", () => {
  it("merges class names correctly", () => {
    expect(cn("a", "b")).toBe("a b");
    expect(cn("a", { b: true, c: false })).toBe("a b");
  });

  it("handles tailwind class conflicts", () => {
    expect(cn("px-2 py-2", "px-4")).toBe("py-2 px-4");
  });
});

describe("generateSecurePassword", () => {
  it("uses crypto random values when available", () => {
    vi.stubGlobal("crypto", {
      getRandomValues: vi.fn((values: Uint32Array) => {
        values.set([0, 1, 2, 3]);
        return values;
      }),
    });

    expect(generateSecurePassword(4)).toBe("abcd");
  });

  it("falls back to Math.random when crypto is unavailable", () => {
    vi.stubGlobal("crypto", undefined);
    vi.spyOn(Math, "random").mockReturnValue(0);

    expect(generateSecurePassword(3)).toBe("aaa");
  });
});
