import { beforeEach, describe, expect, it, vi } from "vitest"; const mocks = vi.hoisted(() => ({ resolveCalendarWebhookRlsIdentity: vi.fn(), getGoogleWebhookConnection: vi.fn(), recordWebhookEvent: vi.fn(), })); vi.mock("@hivegrid/db/rls-resolvers.server", () => ({ resolveCalendarWebhookRlsIdentity: mocks.resolveCalendarWebhookRlsIdentity, })); vi.mock("@hivegrid/db/rls-context.server", () => ({ runWithRlsContext: (_context: unknown, callback: () => unknown) => callback(), })); vi.mock("~/modules/calendar/db/calendar.db.server", () => ({ getGoogleWebhookConnection: mocks.getGoogleWebhookConnection, recordWebhookEvent: mocks.recordWebhookEvent, })); import { action } from "./webhooks.calendar.google"; beforeEach(() => { vi.clearAllMocks(); mocks.resolveCalendarWebhookRlsIdentity.mockResolvedValue({ tenantId: "tenant_1", resourceId: "connection_1", userId: "user_1" }); mocks.getGoogleWebhookConnection.mockResolvedValue({ id: "connection_1", tenantId: "tenant_1", userId: "user_1" }); }); function webhookRequest(options: { channelId?: string; channelToken?: string; resourceState?: string } = {}): Request { const headers = new Headers(); if (options.channelId !== undefined) headers.set("x-goog-channel-id", options.channelId); if (options.channelToken !== undefined) headers.set("x-goog-channel-token", options.channelToken); if (options.resourceState !== undefined) headers.set("x-goog-resource-state", options.resourceState); return new Request("https://app.example.test/webhooks/calendar/google", { method: "POST", headers }); } describe("Google Calendar webhook", () => { it("acks incomplete or mismatched channel credentials without writing", async () => { const incomplete = await action({ request: webhookRequest({ channelId: "channel_1" }) } as never); expect(incomplete.status).toBe(200); expect(mocks.resolveCalendarWebhookRlsIdentity).not.toHaveBeenCalled(); mocks.resolveCalendarWebhookRlsIdentity.mockResolvedValue(null); const mismatched = await action({ request: webhookRequest({ channelId: "channel_1", channelToken: "wrong", resourceState: "exists" }) } as never); expect(mismatched.status).toBe(200); expect(mocks.recordWebhookEvent).not.toHaveBeenCalled(); }); it("re-reads the exact connection before persisting a notification", async () => { const response = await action({ request: webhookRequest({ channelId: "channel_1", channelToken: "connection_1", resourceState: "exists" }), } as never); expect(response.status).toBe(200); expect(mocks.resolveCalendarWebhookRlsIdentity).toHaveBeenCalledWith("channel_1", "connection_1"); expect(mocks.getGoogleWebhookConnection).toHaveBeenCalledWith({ tenantId: "tenant_1", userId: "user_1", connectionId: "connection_1", channelId: "channel_1", channelToken: "connection_1", }); expect(mocks.recordWebhookEvent).toHaveBeenCalledWith(expect.objectContaining({ tenantId: "tenant_1", connectionId: "connection_1" })); }); it("acks a stale exact re-read and Google's initial sync ping without writing", async () => { mocks.getGoogleWebhookConnection.mockResolvedValueOnce(null); await action({ request: webhookRequest({ channelId: "channel_1", channelToken: "connection_1", resourceState: "exists" }) } as never); expect(mocks.recordWebhookEvent).not.toHaveBeenCalled(); await action({ request: webhookRequest({ channelId: "channel_1", channelToken: "connection_1", resourceState: "sync" }) } as never); expect(mocks.recordWebhookEvent).not.toHaveBeenCalled(); }); });