import { describe, it, expect } from "vitest"; import { emailSchema, passwordSchema, loginSchema, registerSchema, EMAIL_REGEX, MIN_PASSWORD_LENGTH, } from "../src/domain/account/validation/AccountValidation"; import { AccountEntity } from "../src/domain/account/entity/AccountEntity"; import { InvalidEmailFormatError, WeakPasswordError, } from "../src/domain/account/errors/AccountErrors"; describe("AccountValidation", () => { describe("Email validation consistency", () => { const validEmails = [ "test@example.com", "user.name@domain.co.uk", "firstname+lastname@example.org", ]; const invalidEmails = [ "invalid-email", "@example.com", "test@", "test", "test@domain", "", ]; it("should validate same emails in Zod and Entity", () => { validEmails.forEach((email) => { expect(emailSchema.safeParse(email).success).toBe(true); expect(() => AccountEntity.create(email, "password123")).not.toThrow( InvalidEmailFormatError, ); }); invalidEmails.forEach((email) => { expect(emailSchema.safeParse(email).success).toBe(false); expect(() => AccountEntity.create(email, "password123")).toThrow( InvalidEmailFormatError, ); }); }); it("should use same regex pattern", () => { validEmails.forEach((email) => { expect(EMAIL_REGEX.test(email)).toBe(true); }); invalidEmails.forEach((email) => { expect(EMAIL_REGEX.test(email)).toBe(false); }); }); }); 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 = { email: "test@example.com", password: "password123" }; const invalidLogin = { email: "invalid", password: "123" }; expect(loginSchema.safeParse(validLogin).success).toBe(true); expect(loginSchema.safeParse(invalidLogin).success).toBe(false); }); it("should validate register schema correctly", () => { const validRegister = { email: "test@example.com", password: "password123", }; const invalidRegister = { email: "invalid", 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 email error message", () => { const result = emailSchema.safeParse("invalid-email"); expect(result.success).toBe(false); if (!result.success) { expect(result.error.issues[0]?.message).toBe("Format d'email invalide"); } }); }); });