import { describe, it, expect } from "bun:test"; import { passwordSchema, loginSchema, registerSchema, MIN_PASSWORD_LENGTH, } from "../../src/domain/account/validation/AccountValidation"; import { WeakPasswordError } from "../../src/domain/account/errors/AccountErrors"; import { AccountEntity } from "../../src/domain/account/entity/AccountEntity"; describe("AccountValidation", () => { describe("Password validation consistency", () => { const validPasswords = ["password123", "abcdefgh", "12345678", "P@ssw0rd!"]; const invalidPasswords = ["1234567", "short", "", "abc"]; it("should validate same passwords in Zod and Entity", () => { validPasswords.forEach((password) => { expect(passwordSchema.safeParse(password).success).toBe(true); expect(() => AccountEntity.create("test@example.com", password), ).not.toThrow(WeakPasswordError); }); invalidPasswords.forEach((password) => { expect(passwordSchema.safeParse(password).success).toBe(false); expect(() => AccountEntity.create("test@example.com", password), ).toThrow(WeakPasswordError); }); }); it("should use same minimum length", () => { validPasswords.forEach((password) => { expect(password.length >= MIN_PASSWORD_LENGTH).toBe(true); }); invalidPasswords.forEach((password) => { expect(password.length >= MIN_PASSWORD_LENGTH).toBe(false); }); }); }); describe("Complete schemas", () => { it("should validate login schema correctly", () => { const validLogin = { username: "testaccount", password: "password123", }; const invalidLogin = { username: "ii", password: "123" }; expect(loginSchema.safeParse(validLogin).success).toBe(true); expect(loginSchema.safeParse(invalidLogin).success).toBe(false); }); it("should validate register schema correctly", () => { const validRegister = { username: "testaccount", password: "password123", }; const invalidRegister = { username: "ii", password: "123" }; expect(registerSchema.safeParse(validRegister).success).toBe(true); expect(registerSchema.safeParse(invalidRegister).success).toBe(false); }); }); describe("Error messages consistency", () => { it("should return consistent password error message", () => { const result = passwordSchema.safeParse("123"); expect(result.success).toBe(false); if (!result.success) { expect(result.error.issues[0]?.message).toContain( `${MIN_PASSWORD_LENGTH} caractères`, ); } }); it("should return consistent username error message", () => { const result = registerSchema.safeParse({ username: "ii", password: "validPassword123", }); expect(result.success).toBe(false); if (!result.success) { expect(result.error.issues[0]?.message).toContain( "doit contenir au moins", ); } }); }); });