import { describe, it, expect, beforeEach, afterEach } from "bun:test"; import AccountRepository from "../../src/domain/account/repository/AccoutRepository"; import { AccountEntity } from "../../src/domain/account/entity/AccountEntity"; import { initTestDatabase } from "../utils"; import type { DatabaseInterface } from "../../src/database/DatabaseInterface"; describe("AccountRepository", () => { let accountRepository: AccountRepository; let database: DatabaseInterface; beforeEach(async () => { database = await initTestDatabase(); accountRepository = new AccountRepository(database); }); afterEach(async () => { await database?.close(); }); describe("findByUsername", () => { it("should return account when found", async () => { const username = "testaccount"; const createAccount = await AccountEntity.create(username, "password123"); await accountRepository.insert(createAccount); const result = await accountRepository.findByUsername(username); expect(result).toBeInstanceOf(AccountEntity); expect(result?.username).toBe(username); }); it("should return null when account not found", async () => { const username = "nonexistentaccount"; const result = await accountRepository.findByUsername(username); expect(result).toBeNull(); }); }); describe("findById", () => { it("should return account when found", async () => { const createAccount = await AccountEntity.create( "test@example.com", "password123", ); const newId = await accountRepository.insert(createAccount); const result = await accountRepository.findById(newId); expect(result).toBeInstanceOf(AccountEntity); expect(result?.id).toBe(newId); }); it("should return null when account not found", async () => { const id = 999; const result = await accountRepository.findById(id); expect(result).toBeNull(); }); }); describe("insert", () => { it("should insert account successfully", async () => { const account = await AccountEntity.create( "test@example.com", "password123", ); const result = await accountRepository.insert(account); expect(result).toBeGreaterThan(0); }); }); });