import { beforeEach, describe, expect, it, vi } from "vitest"; const mocks = vi.hoisted(() => ({ tx: { employeeVacationRequest: { findFirst: vi.fn(), update: vi.fn() }, calendarEvent: { create: vi.fn() }, outboxEvent: { findUnique: vi.fn(), create: vi.fn() }, }, })); vi.mock("~/utils/db/.server/tenantRls.server", () => ({ withTenantRLS: async (_tenantId: string, callback: (tx: typeof mocks.tx) => Promise) => callback(mocks.tx), })); vi.mock("@hivegrid/db/audit-events.server", () => ({ createAuditEvent: vi.fn().mockResolvedValue(undefined) })); vi.mock("@hivegrid/db/notifications.server", () => ({ createNotificationDelivery: vi.fn().mockResolvedValue(undefined) })); import { approveRequest, rejectRequest } from "./hrLeaveService.server"; const baseRow = { id: "req-1", tenantId: "tenant-a", employeeUserId: "user-employee", requestKind: "vacation", startDate: new Date("2026-08-03T00:00:00.000Z"), endDate: new Date("2026-08-14T00:00:00.000Z"), note: null, status: "submitted", assignedApproverUserId: "user-approver", isDemoData: false, }; describe("leave approval calendar sync (P16/M25)", () => { beforeEach(() => { vi.clearAllMocks(); mocks.tx.employeeVacationRequest.findFirst.mockResolvedValue({ ...baseRow }); mocks.tx.employeeVacationRequest.update.mockResolvedValue({}); mocks.tx.calendarEvent.create.mockResolvedValue({ id: "event-1" }); mocks.tx.outboxEvent.findUnique.mockResolvedValue(null); mocks.tx.outboxEvent.create.mockResolvedValue({ id: "outbox-1" }); }); it("creates an internal all-day absence event and links it on approval", async () => { await approveRequest({ tenantId: "tenant-a", requestId: "req-1", deciderUserId: "user-approver", canApproveAny: false, absenceTitleByKind: { vacation: "Urlaub" }, }); expect(mocks.tx.calendarEvent.create).toHaveBeenCalledWith({ data: { tenantId: "tenant-a", userId: "user-employee", source: "hivegrid_internal", title: "Urlaub", startsAt: baseRow.startDate, endsAt: baseRow.endDate, isAllDay: true, isBusyForAvailability: true, internalEntityLink: { type: "hr_vacation_request", id: "req-1" }, isDemoData: false, }, select: { id: true }, }); expect(mocks.tx.employeeVacationRequest.update).toHaveBeenCalledWith({ where: { id: "req-1" }, data: expect.objectContaining({ status: "approved", calendarEventId: "event-1" }), }); expect(mocks.tx.outboxEvent.create).toHaveBeenCalledWith({ data: expect.objectContaining({ tenantId: "tenant-a", eventType: "workflow.trigger.event", dispatchKey: "workflow-trigger:calendar:event-1:created", payload: expect.objectContaining({ event: "calendar.event_created", data: expect.objectContaining({ calendarKey: "urlaubskalender" }) }), }), }); }); it("falls back to a title-less event when no resolved title is passed", async () => { await approveRequest({ tenantId: "tenant-a", requestId: "req-1", deciderUserId: "user-approver", canApproveAny: false }); expect(mocks.tx.calendarEvent.create).toHaveBeenCalledWith(expect.objectContaining({ data: expect.objectContaining({ title: null }) })); }); it("does not create a calendar event on rejection", async () => { await rejectRequest({ tenantId: "tenant-a", requestId: "req-1", deciderUserId: "user-approver", canApproveAny: false }); expect(mocks.tx.calendarEvent.create).not.toHaveBeenCalled(); expect(mocks.tx.employeeVacationRequest.update).toHaveBeenCalledWith({ where: { id: "req-1" }, data: expect.objectContaining({ status: "rejected" }), }); }); });