import { describe, expect, it } from "vitest"; import { appRouter } from "./routers"; import { COOKIE_NAME } from "../shared/const"; import type { TrpcContext } from "./_core/context"; import type { User } from "../drizzle/schema"; type CookieCall = { name: string; options: Record; }; function createAuthContext(): { ctx: TrpcContext; clearedCookies: CookieCall[] } { const clearedCookies: CookieCall[] = []; const user: User = { id: 1, login: "admin", email: "admin@itinova.fr", passwordHash: "$2a$10$hashedpassword", firstName: "Admin", lastName: "Itinova", role: "admin", isActive: true, createdAt: new Date(), updatedAt: new Date(), lastSignedIn: null, }; const ctx: TrpcContext = { user, req: { protocol: "https", headers: {}, } as TrpcContext["req"], res: { clearCookie: (name: string, options: Record) => { clearedCookies.push({ name, options }); }, } as TrpcContext["res"], }; return { ctx, clearedCookies }; } describe("auth.logout", () => { it("clears the session cookie and reports success", async () => { const { ctx, clearedCookies } = createAuthContext(); const caller = appRouter.createCaller(ctx); const result = await caller.auth.logout(); expect(result).toEqual({ success: true }); expect(clearedCookies).toHaveLength(1); expect(clearedCookies[0]?.name).toBe(COOKIE_NAME); expect(clearedCookies[0]?.options).toMatchObject({ maxAge: -1, secure: true, sameSite: "none", httpOnly: true, path: "/", }); }); });