// @vitest-environment node import { beforeEach, describe, expect, it, vi } from "vitest"; vi.mock("../db.server", () => ({ db: { registration: { create: vi.fn(), update: vi.fn(), findFirst: vi.fn(), findUnique: vi.fn(), }, }, })); import { db } from "../db.server"; import { createRegistration, getRegistrationByEmail, getRegistrationByToken, updateRegistration } from "./registration.db.server"; const registrationMock = vi.mocked(db.registration, true); describe("registration db helpers", () => { beforeEach(() => { vi.clearAllMocks(); }); it("looks up pending registrations by email and token", async () => { registrationMock.findFirst.mockResolvedValueOnce({ id: "registration_1" } as Awaited>); registrationMock.findUnique.mockResolvedValueOnce({ id: "registration_2" } as Awaited>); await expect(getRegistrationByEmail("ada@example.test")).resolves.toMatchObject({ id: "registration_1" }); await expect(getRegistrationByToken("token_1")).resolves.toMatchObject({ id: "registration_2" }); expect(registrationMock.findFirst).toHaveBeenCalledWith({ where: { email: "ada@example.test" } }); expect(registrationMock.findUnique).toHaveBeenCalledWith({ where: { token: "token_1" } }); }); it("persists lead conversion context on new registrations", async () => { registrationMock.create.mockResolvedValueOnce({ id: "registration_1" } as Awaited>); await createRegistration({ email: "ada@example.test", firstName: "Ada", lastName: "Lovelace", token: "registration-token", company: "Analytical Engines", selectedSubscriptionPriceId: "price_1", leadConversionLeadId: "lead_1", leadConversionToken: "signed-lead-token", leadConversionPlanId: "plan_pro", ipAddress: "127.0.0.1", slug: "ada", }); expect(registrationMock.create).toHaveBeenCalledWith({ data: { email: "ada@example.test", firstName: "Ada", lastName: "Lovelace", token: "registration-token", company: "Analytical Engines", selectedSubscriptionPriceId: "price_1", leadConversionLeadId: "lead_1", leadConversionToken: "signed-lead-token", leadConversionPlanId: "plan_pro", ipAddress: "127.0.0.1", slug: "ada", }, }); }); it("clears missing lead conversion fields to null when creating registrations", async () => { registrationMock.create.mockResolvedValueOnce({ id: "registration_1" } as Awaited>); await createRegistration({ email: "ada@example.test", firstName: "Ada", lastName: "Lovelace", token: "registration-token", company: null, selectedSubscriptionPriceId: null, ipAddress: null, slug: null, }); expect(registrationMock.create).toHaveBeenCalledWith( expect.objectContaining({ data: expect.objectContaining({ leadConversionLeadId: null, leadConversionToken: null, leadConversionPlanId: null, }), }) ); }); it("updates lead conversion context and created tenant id together", async () => { registrationMock.update.mockResolvedValueOnce({ id: "registration_1" } as Awaited>); await updateRegistration("registration_1", { firstName: "Ada", lastName: "Lovelace", company: "Analytical Engines", createdTenantId: "tenant_1", token: "new-token", leadConversionLeadId: "lead_1", leadConversionToken: "signed-lead-token", leadConversionPlanId: "plan_pro", }); expect(registrationMock.update).toHaveBeenCalledWith({ where: { id: "registration_1" }, data: { firstName: "Ada", lastName: "Lovelace", company: "Analytical Engines", createdTenantId: "tenant_1", token: "new-token", leadConversionLeadId: "lead_1", leadConversionToken: "signed-lead-token", leadConversionPlanId: "plan_pro", }, }); }); });