import { describe, expect, it, vi } from "vitest"; import { loadSession, signIn, WRONG_CREDENTIALS } from "@/lib/auth-client"; const json = (body: unknown, status = 200) => new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } }); describe("signIn", () => { it("wrong password gives the one message", async () => { const f = vi.fn(async () => json({ error: "Email or password is incorrect." }, 401)); expect(await signIn("a@b.co", "bad", f as unknown as typeof fetch)).toEqual({ ok: false, message: WRONG_CREDENTIALS }); }); it("success returns the person and posts JSON to /api/session", async () => { const f = vi.fn(async () => json({ name: "Ann", email: "a@b.co", pictureUrl: null })); const r = await signIn("a@b.co", "pw", f as unknown as typeof fetch); expect(r).toEqual({ ok: true, person: { name: "Ann", email: "a@b.co", pictureUrl: null } }); expect(f.mock.calls[0]).toMatchObject(["/api/session", { method: "POST" }]); }); it("network failure is not the wrong-password message", async () => { const f = vi.fn(async () => { throw new Error("down"); }); const r = await signIn("a@b.co", "pw", f as unknown as typeof fetch); expect(r.ok).toBe(false); if (!r.ok) expect(r.message).not.toBe(WRONG_CREDENTIALS); }); }); describe("loadSession", () => { it("401 means signed out", async () => { const f = vi.fn(async () => json({ error: "sign_in" }, 401)); expect(await loadSession(f as unknown as typeof fetch)).toBeNull(); }); it("200 returns the person", async () => { const f = vi.fn(async () => json({ name: "Ann", email: "a@b.co", pictureUrl: "u" })); expect((await loadSession(f as unknown as typeof fetch))?.email).toBe("a@b.co"); }); });