|
| 1 | +import { createLogger } from '@sim/logger' |
| 2 | +import { createWorkflowRecord } from '@sim/testing' |
| 3 | +import { jsonResponse } from '@sim/testing/helpers/http' |
| 4 | +import { |
| 5 | + webhooksProcessorMock, |
| 6 | + webhooksProcessorMockFns, |
| 7 | +} from '@sim/testing/mocks/webhooks-processor.mock' |
| 8 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 9 | + |
| 10 | +const { mockUpdateConfig, mockMarkFailed } = vi.hoisted(() => ({ |
| 11 | + mockUpdateConfig: vi.fn(), |
| 12 | + mockMarkFailed: vi.fn(), |
| 13 | +})) |
| 14 | + |
| 15 | +vi.mock('@/lib/core/idempotency/service', () => ({ |
| 16 | + pollingIdempotency: { |
| 17 | + executeWithIdempotency: vi.fn( |
| 18 | + async (_provider: string, _key: string, execute: () => Promise<unknown>) => execute() |
| 19 | + ), |
| 20 | + }, |
| 21 | +})) |
| 22 | + |
| 23 | +vi.mock('@/lib/webhooks/processor', () => webhooksProcessorMock) |
| 24 | + |
| 25 | +vi.mock('@/lib/webhooks/polling/utils', async (importOriginal) => ({ |
| 26 | + ...(await importOriginal<typeof import('@/lib/webhooks/polling/utils')>()), |
| 27 | + resolveOAuthCredential: vi.fn().mockResolvedValue('access-token'), |
| 28 | + markWebhookSuccess: vi.fn(), |
| 29 | + markWebhookFailed: mockMarkFailed, |
| 30 | + updateWebhookProviderConfig: mockUpdateConfig, |
| 31 | +})) |
| 32 | + |
| 33 | +import { ADMISSION_REJECTION_CODE } from '@/lib/core/admission/rejection' |
| 34 | +import { googleCalendarPollingHandler } from '@/lib/webhooks/polling/google-calendar' |
| 35 | +import type { PollWebhookContext, WebhookRecord } from '@/lib/webhooks/polling/types' |
| 36 | + |
| 37 | +const mockProcessEvent = webhooksProcessorMockFns.mockProcessPolledWebhookEvent |
| 38 | + |
| 39 | +function context(): PollWebhookContext { |
| 40 | + const webhookData: WebhookRecord = { |
| 41 | + id: 'calendar-webhook', |
| 42 | + workflowId: 'calendar-listener', |
| 43 | + deploymentVersionId: null, |
| 44 | + registrationStatus: null, |
| 45 | + registrationGeneration: null, |
| 46 | + configFingerprint: null, |
| 47 | + preparedAt: null, |
| 48 | + blockId: null, |
| 49 | + path: 'calendar-listener', |
| 50 | + routingKey: null, |
| 51 | + provider: 'google-calendar', |
| 52 | + providerConfig: { |
| 53 | + calendarId: 'primary', |
| 54 | + lastCheckedTimestamp: '2026-10-09T11:00:00.000Z', |
| 55 | + }, |
| 56 | + isActive: true, |
| 57 | + failedCount: 0, |
| 58 | + lastFailedAt: null, |
| 59 | + archivedAt: null, |
| 60 | + createdAt: new Date('2026-10-01T00:00:00.000Z'), |
| 61 | + updatedAt: new Date('2026-10-09T11:00:00.000Z'), |
| 62 | + } |
| 63 | + return { |
| 64 | + webhookData, |
| 65 | + workflowData: createWorkflowRecord({ |
| 66 | + id: 'calendar-listener', |
| 67 | + }) as PollWebhookContext['workflowData'], |
| 68 | + requestId: 'calendar-request', |
| 69 | + logger: createLogger('GoogleCalendarTest'), |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +describe('Google Calendar polling when execution admission refuses events', () => { |
| 74 | + beforeEach(() => { |
| 75 | + const events = ['event-1', 'event-2'].map((id) => ({ |
| 76 | + id, |
| 77 | + status: 'confirmed', |
| 78 | + created: '2026-10-09T11:30:00.000Z', |
| 79 | + updated: '2026-10-09T11:30:00.000Z', |
| 80 | + })) |
| 81 | + vi.stubGlobal('fetch', vi.fn().mockResolvedValue(jsonResponse({ items: events }, 200))) |
| 82 | + mockProcessEvent.mockResolvedValue({ |
| 83 | + success: false, |
| 84 | + statusCode: 402, |
| 85 | + error: 'Usage limit exceeded', |
| 86 | + code: ADMISSION_REJECTION_CODE.USAGE_LIMIT_EXCEEDED, |
| 87 | + retryable: false, |
| 88 | + }) |
| 89 | + }) |
| 90 | + |
| 91 | + it('stops the batch without advancing its cursor or counting a failure', async () => { |
| 92 | + expect(await googleCalendarPollingHandler.pollWebhook(context())).toBe('skipped') |
| 93 | + |
| 94 | + expect(mockProcessEvent).toHaveBeenCalledOnce() |
| 95 | + const cursorUpdates = mockUpdateConfig.mock.calls.filter( |
| 96 | + ([, update]) => 'lastCheckedTimestamp' in (update as Record<string, unknown>) |
| 97 | + ) |
| 98 | + expect(cursorUpdates).toEqual([]) |
| 99 | + expect(mockMarkFailed).not.toHaveBeenCalled() |
| 100 | + }) |
| 101 | +}) |
0 commit comments