import { describe, it, expect, beforeAll, afterAll, beforeEach } from "vitest"; import type { FastifyInstance } from "fastify"; import { createTestApp, cleanDb, requestOtpCode, signupUser } from "./helpers.js"; let app: FastifyInstance; beforeAll(async () => { app = await createTestApp(); }); afterAll(async () => { await app.close(); }); beforeEach(async () => { await cleanDb(); }); describe("POST /signup", () => { it("creates user, account, and returns tokens", async () => { const res = await signupUser(app, "new@example.com", "My Org"); const body = res.json(); expect(res.statusCode).toBe(201); expect(body.accessToken).toBeDefined(); expect(body.refreshToken).toBeDefined(); expect(body.user.email).toBe("new@example.com"); expect(body.accounts).toHaveLength(1); expect(body.accounts[0].name).toBe("My Org"); expect(body.accounts[0].role).toBe("owner"); }); it("returns 409 if user already exists", async () => { await signupUser(app, "existing@example.com", "Org 1"); const code = await requestOtpCode(app, "existing@example.com"); const res = await app.inject({ method: "POST", url: "/signup", payload: { email: "existing@example.com", code, accountName: "Org 2" }, }); expect(res.statusCode).toBe(409); }); it("returns 400 with invalid OTP code", async () => { await requestOtpCode(app, "test@example.com"); const res = await app.inject({ method: "POST", url: "/signup", payload: { email: "test@example.com", code: "000000", accountName: "Org" }, }); expect(res.statusCode).toBe(400); }); it("returns 400 if fields are missing", async () => { const res = await app.inject({ method: "POST", url: "/signup", payload: { email: "test@example.com" }, }); expect(res.statusCode).toBe(400); }); });