import { describe, it, expect } from "bun:test"; import { InvalidUsernameFormatError, WeakPasswordError, } from "../../src/domain/account/errors/AccountErrors"; import { MAX_USERNAME_LENGTH, MIN_PASSWORD_LENGTH, } from "../../src/domain/account/validation/AccountValidation"; import { AccountEntity } from "../../src/domain/account/entity/AccountEntity"; describe("AccountEntity", () => { describe("create", () => { it("should create an account with valid username and password", async () => { const email = "testaccount"; const password = "a".repeat(MIN_PASSWORD_LENGTH); const account = await AccountEntity.create(email, password); expect(account.username).toBe(email); expect(account.roleId).toBe(1); expect(account).toBeDefined(); }); it("should throw InvalidUsernameFormatError for invalid username", () => { const invalidUsername = "a".repeat(MAX_USERNAME_LENGTH + 1); const password = "a".repeat(MIN_PASSWORD_LENGTH); expect(AccountEntity.create(invalidUsername, password)).rejects.toThrow( InvalidUsernameFormatError, ); }); it("should throw WeakPasswordError for short password", () => { const username = "testaccount"; const shortPassword = "a".repeat(MIN_PASSWORD_LENGTH - 1); expect(AccountEntity.create(username, shortPassword)).rejects.toThrow( WeakPasswordError, ); }); }); describe("verifyPassword", () => { it("should return true for correct password", async () => { const createAccount = await AccountEntity.create( "test@example.com", "password123", ); const account = new AccountEntity( 1, createAccount.username, createAccount.hashedPassword, createAccount.roleId, new Date(), new Date(), ); expect(await account.verifyPassword("password123")).toBe(true); }); it("should return false for incorrect password", async () => { const createAccount = await AccountEntity.create( "test@example.com", "password123", ); const account = new AccountEntity( 1, createAccount.username, createAccount.hashedPassword, createAccount.roleId, new Date(), new Date(), ); expect(await account.verifyPassword("wrongpassword")).toBe(false); }); }); });