import { describe, expect, it, vi } from "vitest"; import { createAdminCalendarService, type AdminCalendarRepository, } from "./admin-calendar.server.js"; function repository(): AdminCalendarRepository { return { activate: vi.fn().mockResolvedValue({ id: "calendar-1" }), applySync: vi.fn().mockResolvedValue({ id: "cursor-1" }), requireRecovery: vi.fn().mockResolvedValue({ id: "cursor-1" }), disconnect: vi.fn().mockResolvedValue({ id: "calendar-1", status: "revoked", }), } as never; } const actor = { adminUserId: "admin-1", reason: "Persönlichen Kalender konfigurieren", }; describe("admin calendar service", () => { it("normalizes a personal calendar only after the booking permission", async () => { const repo = repository(); const authorize = vi.fn(); const service = createAdminCalendarService({ repository: repo, authorize, }); await service.activate({ adminUserId: " admin-1 ", dataConnectionId: " connection-1 ", externalCalendarId: " primary ", externalCalendarLabel: " Vertrieb ", timeZone: "Europe/Berlin", actor, }); expect(authorize).toHaveBeenCalledWith("admin_sales.booking.manage"); expect(repo.activate).toHaveBeenCalledWith({ adminUserId: "admin-1", dataConnectionId: "connection-1", externalCalendarId: "primary", externalCalendarLabel: "Vertrieb", timeZone: "Europe/Berlin", actor, }); }); it("rejects invalid timezones before persistence", async () => { const repo = repository(); const service = createAdminCalendarService({ repository: repo, authorize: vi.fn(), }); await expect( service.activate({ adminUserId: "admin-1", dataConnectionId: "connection-1", externalCalendarId: "primary", timeZone: "Mars/Olympus", actor, }) ).rejects.toMatchObject({ code: "invalid_time_zone" }); expect(repo.activate).not.toHaveBeenCalled(); }); it("accepts exactly one provider cursor and minimizes busy data", async () => { const repo = repository(); const service = createAdminCalendarService({ repository: repo, authorize: vi.fn(), }); await service.applySync({ adminUserId: "admin-1", calendarConnectionId: "calendar-1", externalCalendarId: "primary", busyIntervals: [ { externalEventId: " event-1 ", startsAt: "2026-07-28T10:00:00+02:00", endsAt: "2026-07-28T11:00:00+02:00", opaqueVersion: " v1 ", }, ], cursorToken: " sync-1 ", fullSync: true, syncedAt: new Date("2026-07-25T12:00:00Z"), }); expect(repo.applySync).toHaveBeenCalledWith( expect.objectContaining({ cursorToken: "sync-1", deltaLink: null, busyIntervals: [ { externalEventId: "event-1", startsAt: "2026-07-28T08:00:00.000Z", endsAt: "2026-07-28T09:00:00.000Z", opaqueVersion: "v1", }, ], }) ); }); it("fails closed for ambiguous or absent provider cursors", async () => { const service = createAdminCalendarService({ repository: repository(), authorize: vi.fn(), }); const base = { adminUserId: "admin-1", calendarConnectionId: "calendar-1", externalCalendarId: "primary", busyIntervals: [], fullSync: false, syncedAt: new Date(), }; await expect( service.applySync({ ...base, cursorToken: "google", deltaLink: "https://graph.microsoft.com/v1.0/delta", }) ).rejects.toMatchObject({ code: "invalid_provider_cursor" }); await expect(service.applySync(base)).rejects.toMatchObject({ code: "invalid_provider_cursor", }); }); it("marks invalid provider cursors for deterministic full-sync recovery", async () => { const repo = repository(); const service = createAdminCalendarService({ repository: repo, authorize: vi.fn(), }); await service.requireRecovery({ adminUserId: "admin-1", calendarConnectionId: "calendar-1", externalCalendarId: "primary", errorCode: "sync_token_invalid", requestedAt: new Date("2026-07-25T12:00:00Z"), }); expect(repo.requireRecovery).toHaveBeenCalledWith( expect.objectContaining({ errorCode: "sync_token_invalid", }) ); }); });