import { describe, it, expect } from "vitest"; import { signInController } from "@/interface-adapters/controllers/sign-in.controller"; import { MockUsersRepository } from "@/infrastructure/repositories/users.repository.mock"; import { MockAuthenticationService } from "@/infrastructure/services/authentication.service.mock"; import { signInUseCase } from "@/application/use-cases/sign-in.use-case"; import { InputParseError } from "@/entities/errors/common"; import { userFactory } from "@/__factories__/user.factory"; describe("signInController", () => { it("returns a cookie on valid credentials", async () => { const users = new MockUsersRepository([]); const auth = new MockAuthenticationService(users); const seedUser = userFactory.build({ username: "alice", passwordHash: "hashed_testpassword", }); await users.createUser(seedUser); const useCase = signInUseCase(users, auth); const controller = signInController(useCase); const cookie = await controller({ username: "alice", password: "testpassword" }); expect(cookie.name).toBe("session"); }); it("throws InputParseError on missing username", async () => { const users = new MockUsersRepository([]); const auth = new MockAuthenticationService(users); const useCase = signInUseCase(users, auth); const controller = signInController(useCase); await expect( controller({ password: "anything" }), ).rejects.toBeInstanceOf(InputParseError); }); it("throws InputParseError on too-short password", async () => { const users = new MockUsersRepository([]); const auth = new MockAuthenticationService(users); const useCase = signInUseCase(users, auth); const controller = signInController(useCase); await expect( controller({ username: "alice", password: "abc" }), ).rejects.toBeInstanceOf(InputParseError); }); });