import { describe, it, expect } from "vitest";
import { hashPassword, verifyPassword } from "./hash";

describe("Password Hashing Functions", () => {
	describe("hashPassword", () => {
		it("should generate different hashes for the same password", async () => {
			const password = "mySecurePassword123";
			const hash1 = await hashPassword(password);
			const hash2 = await hashPassword(password);

			expect(hash1).not.toBe(hash2);
			expect(typeof hash1).toBe("string");
			expect(hash1).toMatch(/^\$argon2id\$/);
		});
	});

	describe("verifyPassword", () => {
		it("should verify correct password successfully", async () => {
			const password = "correctPassword123";
			const hash = await hashPassword(password);

			const isValid = await verifyPassword({ hash, password });
			expect(isValid).toBe(true);
		});

		it("should reject incorrect password", async () => {
			const password = "correctPassword123";
			const wrongPassword = "wrongPassword123";
			const hash = await hashPassword(password);

			const isValid = await verifyPassword({
				hash,
				password: wrongPassword,
			});
			expect(isValid).toBe(false);
		});

		it("should handle empty password", async () => {
			const password = "";
			const hash = await hashPassword(password);

			const isValid = await verifyPassword({ hash, password });
			expect(isValid).toBe(true);
		});

		it("should return false for invalid hash format", async () => {
			const isValid = await verifyPassword({
				hash: "invalid-hash-format",
				password: "password123",
			});
			expect(isValid).toBe(false);
		});
	});
});
