import bcrypt from "bcrypt";
import { HashPassword } from "./hash-password";

describe("HashPassword", () => {
  test("debe crear una instancia de HashPassword desde un texto plano válido", () => {
    const result = HashPassword.createFromPlainText("securepassword");
    expect(result.isSuccess).toBe(true);
    expect(result.data).toBeInstanceOf(HashPassword);
  });

  test("debe fallar al crear una instancia con una contraseña demasiado corta", () => {
    const result = HashPassword.createFromPlainText("123");
    expect(result.isFailure).toBe(true);
    expect(result.error.message).toBe("Password must be at least 6 characters long");
  });

  test("debe crear una instancia de HashPassword desde un hash válido", () => {
    const hashedPassword = bcrypt.hashSync("securepassword", 10);
    const result = HashPassword.createFromHash(hashedPassword);
    expect(result.isSuccess).toBe(true);
    expect(result.data).toBeInstanceOf(HashPassword);
  });

  test("debe verificar correctamente una contraseña válida", async () => {
    const password = "securepassword";
    const result = HashPassword.createFromPlainText(password);
    expect(result.isSuccess).toBe(true);
    const hashPasswordInstance = result.data;

    const isValid = await hashPasswordInstance.verifyPassword(password);
    expect(isValid).toBe(true);
  });

  test("debe fallar la verificación con una contraseña incorrecta", async () => {
    const password = "securepassword";
    const wrongPassword = "wrongpassword";
    const result = HashPassword.createFromPlainText(password);
    expect(result.isSuccess).toBe(true);
    const hashPasswordInstance = result.data;

    const isValid = await hashPasswordInstance.verifyPassword(wrongPassword);
    expect(isValid).toBe(false);
  });
});
