import { hash, compare } from "./password";

describe("password", () => {
  describe("hash()", () => {
    it("returns a valid bcrypt hash", async () => {
      const result = await hash("testpassword");
      expect(result).toMatch(/^\$2b\$/);
    });

    it("produces different hashes for the same password (salt)", async () => {
      const hash1 = await hash("samepassword");
      const hash2 = await hash("samepassword");
      expect(hash1).not.toBe(hash2);
    });
  });

  describe("compare()", () => {
    it("returns true for the correct password", async () => {
      const hashed = await hash("correct");
      const result = await compare("correct", hashed);
      expect(result).toBe(true);
    });

    it("returns false for a wrong password", async () => {
      const hashed = await hash("correct");
      const result = await compare("wrong", hashed);
      expect(result).toBe(false);
    });

    it("handles special characters and unicode", async () => {
      const password = "p@$$w0rd!#%& äöü 🔑";
      const hashed = await hash(password);
      expect(await compare(password, hashed)).toBe(true);
      expect(await compare("other", hashed)).toBe(false);
    });
  });
});
