import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { renderToStaticMarkup } from "react-dom/server"; const mocks = vi.hoisted(() => ({ session: vi.fn(), userRows: vi.fn(), select: vi.fn() })); vi.mock("next/navigation", () => ({ redirect: (path: string) => { throw new Error(`redirect:${path}`); } })); vi.mock("@/lib/auth/server", () => ({ getCustomerSession: mocks.session })); vi.mock("@/db", () => ({ getDb: () => ({ select: mocks.select }) })); vi.mock("@/components/public/site-header", () => ({ SiteHeader: () => null })); vi.mock("./account-form", () => ({ AccountForm: () =>
account details
})); vi.mock("./profile-form", () => ({ ProfileForm: ({ name, nextPath, setup }: { name: string; nextPath: string; setup: boolean; }) =>
{name}
})); const { AccountPage } = await import("./account-page"); beforeEach(() => { vi.clearAllMocks(); vi.stubEnv("NODE_ENV", "development"); mocks.session.mockResolvedValue({ user: { id: "user-1", email: "reader@example.test", emailVerified: false, role: "user" }, session: { id: "session-1" } }); mocks.select.mockReturnValue({ from: () => ({ where: () => ({ limit: mocks.userRows }) }) }); mocks.userRows.mockResolvedValue([{ name: "Registered Name", email: "reader@example.test", image: null }]); }); afterEach(() => { vi.unstubAllEnvs(); }); describe("registration profile step", () => { it("resumes setup for an unverified session using current profile data and the requested destination", async () => { const html = renderToStaticMarkup(await AccountPage({ mode: "register", setup: true, next: "/commission?from=signup" })); expect(html).toContain("Registered Name"); expect(html).toContain('data-setup="true"'); expect(html).toContain('data-next="/commission?from=signup"'); expect(mocks.select).toHaveBeenCalledOnce(); }); it("returns signed-out visitors to account registration", async () => { mocks.session.mockResolvedValue(null); const html = renderToStaticMarkup(await AccountPage({ mode: "register", setup: true, next: "/commission" })); expect(html).toContain("account details"); expect(mocks.select).not.toHaveBeenCalled(); }); it("sanitizes external and unauthorized admin destinations before completing setup", async () => { for (const next of ["https://elsewhere.test", "/admin/guides"]) { const html = renderToStaticMarkup(await AccountPage({ mode: "register", setup: true, next })); expect(html).toContain('data-next="/"'); } }); it("preserves ordinary signed-in login and registration redirects", async () => { for (const mode of ["login", "register"] as const) { await expect(AccountPage({ mode, next: "/commission" })).rejects.toThrow("redirect:/commission"); } expect(mocks.select).not.toHaveBeenCalled(); }); it("does not expose profile setup through the login page", async () => { await expect(AccountPage({ mode: "login", setup: true, next: "/" })).rejects.toThrow("redirect:/"); expect(mocks.select).not.toHaveBeenCalled(); }); });