import { beforeEach, describe, expect, it, vi } from "vitest"; const mocks = vi.hoisted(() => ({ tx: { form: { upsert: vi.fn(), findFirst: vi.fn(), }, formSubmission: { create: vi.fn(), }, hrRequestTemplate: { findFirst: vi.fn(), }, }, })); import { createSystemHrSubmissionWithTx, createTemplateHrRequestSubmissionWithTx, } from "./hrM05Submission.server"; import { buildHrRequestFormDefinition } from "./hrM05Forms"; describe("internal HR M05 submission contract", () => { beforeEach(() => { vi.clearAllMocks(); mocks.tx.form.upsert.mockResolvedValue({ id: "system-form" }); mocks.tx.formSubmission.create.mockResolvedValue({ id: "submission-1" }); mocks.tx.hrRequestTemplate.findFirst.mockResolvedValue({ linkedFormId: "template-form", }); mocks.tx.form.findFirst.mockResolvedValue({ id: "template-form", definition: buildHrRequestFormDefinition(), }); }); it("upserts a private internal form and links a validated leave submission", async () => { const submissionId = await createSystemHrSubmissionWithTx( mocks.tx as never, { tenantId: "tenant-1", actorUserId: "employee-1", process: "leave", linkedEntityType: "hr_leave_request", linkedEntityId: "leave-1", values: { requestKind: "vacation", startDate: "2026-07-28", endDate: "2026-07-31", note: "", approverUserId: "manager-1", }, }, ); expect(submissionId).toBe("submission-1"); expect(mocks.tx.form.upsert).toHaveBeenCalledWith( expect.objectContaining({ where: { tenantId_slug: { tenantId: "tenant-1", slug: "__hivegrid_hr_leave", }, }, update: expect.objectContaining({ subtype: "internal", status: "published", isPublic: false, }), }), ); expect(mocks.tx.formSubmission.create).toHaveBeenCalledWith({ data: { formId: "system-form", tenantId: "tenant-1", createdByUserId: "employee-1", payload: { values: { requestKind: "vacation", startDate: "2026-07-28", endDate: "2026-07-31", note: "", }, }, status: "received", linkedEntityType: "hr_leave_request", linkedEntityId: "leave-1", }, select: { id: true }, }); }); it("rejects an incomplete payload before persisting a submission", async () => { await expect( createSystemHrSubmissionWithTx(mocks.tx as never, { tenantId: "tenant-1", actorUserId: "employee-1", process: "sick", linkedEntityType: "hr_sick_leave", linkedEntityId: "sick-1", values: { expectedReturnDate: "2026-07-31" }, }), ).rejects.toThrow("hr.m05.validation.FIELD_REQUIRED"); expect(mocks.tx.formSubmission.create).not.toHaveBeenCalled(); }); it("requires an active published hr_request form for templates", async () => { mocks.tx.hrRequestTemplate.findFirst.mockResolvedValue({ linkedFormId: "missing-form", }); mocks.tx.form.findFirst.mockResolvedValue(null); await expect( createTemplateHrRequestSubmissionWithTx(mocks.tx as never, { tenantId: "tenant-1", actorUserId: "employee-1", templateId: "template-1", linkedEntityId: "request-1", values: { requestKind: "hardware", title: "Notebook", details: "", }, }), ).rejects.toThrow("hr.m05.templateFormUnavailable"); expect(mocks.tx.form.findFirst).toHaveBeenCalledWith( expect.objectContaining({ where: expect.objectContaining({ tenantId: "tenant-1", subtype: "hr_request", status: "published", isEnabled: true, }), }), ); }); });