|
| 1 | +import { db } from '@sim/db' |
| 2 | +import { user, webhook, workflow, workspace } from '@sim/db/schema' |
| 3 | +import { deleteWorkspaceFixture, insertWorkspaceFixture } from '@sim/db/testing/workspace-fixtures' |
| 4 | +import { createLogger } from '@sim/logger' |
| 5 | +import { generateId } from '@sim/utils/id' |
| 6 | +import { eq } from 'drizzle-orm' |
| 7 | +import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest' |
| 8 | + |
| 9 | +const { mockNotifyWebhookAutoDisabled } = vi.hoisted(() => ({ |
| 10 | + mockNotifyWebhookAutoDisabled: vi.fn(async () => {}), |
| 11 | +})) |
| 12 | +vi.mock('@/lib/webhooks/polling/disable-notifications', () => ({ |
| 13 | + notifyWebhookAutoDisabled: mockNotifyWebhookAutoDisabled, |
| 14 | +})) |
| 15 | + |
| 16 | +import { CredentialRevokedError } from '@/lib/oauth/credential-revoked' |
| 17 | +import { |
| 18 | + markWebhookFailed, |
| 19 | + markWebhookSuccess, |
| 20 | + recordPollSourceFailure, |
| 21 | +} from '@/lib/webhooks/polling/utils' |
| 22 | +import { MAX_CONSECUTIVE_FAILURES } from '@/triggers/constants' |
| 23 | + |
| 24 | +const logger = createLogger('PollAutoDisableIntegration') |
| 25 | +const owner = generateId() |
| 26 | +const workspaceId = generateId() |
| 27 | +const workflowId = generateId() |
| 28 | + |
| 29 | +const CONFIG = { credentialId: 'credential-1', historyId: '4100' } |
| 30 | + |
| 31 | +/** One active Gmail webhook on the fixture workflow; returns its id. */ |
| 32 | +async function insertWebhook(values: { |
| 33 | + failedCount?: number |
| 34 | + providerConfig?: Record<string, unknown> | null |
| 35 | +}): Promise<string> { |
| 36 | + const id = generateId() |
| 37 | + await db.insert(webhook).values({ |
| 38 | + id, |
| 39 | + workflowId, |
| 40 | + blockId: 'trigger', |
| 41 | + path: `poll-auto-disable-${id}`, |
| 42 | + provider: 'gmail', |
| 43 | + isActive: true, |
| 44 | + failedCount: values.failedCount ?? 0, |
| 45 | + providerConfig: values.providerConfig === undefined ? CONFIG : values.providerConfig, |
| 46 | + }) |
| 47 | + return id |
| 48 | +} |
| 49 | + |
| 50 | +async function readWebhook(id: string) { |
| 51 | + const [row] = await db |
| 52 | + .select({ |
| 53 | + isActive: webhook.isActive, |
| 54 | + failedCount: webhook.failedCount, |
| 55 | + providerConfig: webhook.providerConfig, |
| 56 | + }) |
| 57 | + .from(webhook) |
| 58 | + .where(eq(webhook.id, id)) |
| 59 | + return row |
| 60 | +} |
| 61 | + |
| 62 | +beforeAll(async () => { |
| 63 | + const now = new Date() |
| 64 | + await db.insert(user).values({ |
| 65 | + id: owner, |
| 66 | + name: 'Poll auto-disable fixture', |
| 67 | + email: `${owner}@poll-auto-disable.test`, |
| 68 | + emailVerified: true, |
| 69 | + createdAt: now, |
| 70 | + updatedAt: now, |
| 71 | + }) |
| 72 | + await insertWorkspaceFixture(db, [ |
| 73 | + { |
| 74 | + id: workspaceId, |
| 75 | + name: 'Poll auto-disable fixture', |
| 76 | + ownerId: owner, |
| 77 | + billedAccountUserId: owner, |
| 78 | + }, |
| 79 | + ]) |
| 80 | + await db.insert(workflow).values({ |
| 81 | + id: workflowId, |
| 82 | + userId: owner, |
| 83 | + workspaceId, |
| 84 | + name: 'Poll auto-disable fixture', |
| 85 | + lastSynced: now, |
| 86 | + createdAt: now, |
| 87 | + updatedAt: now, |
| 88 | + isDeployed: true, |
| 89 | + }) |
| 90 | +}) |
| 91 | + |
| 92 | +beforeEach(() => { |
| 93 | + mockNotifyWebhookAutoDisabled.mockClear() |
| 94 | +}) |
| 95 | + |
| 96 | +afterAll(async () => { |
| 97 | + await deleteWorkspaceFixture(db, eq(workspace.id, workspaceId)) |
| 98 | + await db.delete(user).where(eq(user.id, owner)) |
| 99 | + await db.$client.end() |
| 100 | +}) |
| 101 | + |
| 102 | +describe('markWebhookSuccess', () => { |
| 103 | + it('drops a recorded source backoff and resets the count, keeping the rest of the config', async () => { |
| 104 | + const id = await insertWebhook({ |
| 105 | + failedCount: 3, |
| 106 | + providerConfig: { |
| 107 | + ...CONFIG, |
| 108 | + pollSourceFailures: 3, |
| 109 | + pollBackoffUntil: new Date(Date.now() + 60_000).toISOString(), |
| 110 | + }, |
| 111 | + }) |
| 112 | + |
| 113 | + await markWebhookSuccess(id, logger) |
| 114 | + |
| 115 | + expect(await readWebhook(id)).toMatchObject({ failedCount: 0, providerConfig: CONFIG }) |
| 116 | + }) |
| 117 | + |
| 118 | + it('resets the count of a webhook with no config without touching it', async () => { |
| 119 | + const id = await insertWebhook({ failedCount: 2, providerConfig: null }) |
| 120 | + |
| 121 | + await markWebhookSuccess(id, logger) |
| 122 | + |
| 123 | + expect(await readWebhook(id)).toMatchObject({ failedCount: 0, providerConfig: null }) |
| 124 | + }) |
| 125 | +}) |
| 126 | + |
| 127 | +describe('auto-disable', () => { |
| 128 | + const revoked = new CredentialRevokedError('The google credential was revoked by the provider') |
| 129 | + |
| 130 | + it('turns a webhook off at once when its credential was revoked, telling its people once', async () => { |
| 131 | + const id = await insertWebhook({ failedCount: 1 }) |
| 132 | + const webhookData = { id, providerConfig: CONFIG } |
| 133 | + |
| 134 | + await recordPollSourceFailure(webhookData, Date.now(), revoked, 'poll failed', logger) |
| 135 | + await recordPollSourceFailure(webhookData, Date.now(), revoked, 'poll failed', logger) |
| 136 | + |
| 137 | + expect(await readWebhook(id)).toMatchObject({ isActive: false, failedCount: 1 }) |
| 138 | + expect(mockNotifyWebhookAutoDisabled).toHaveBeenCalledTimes(1) |
| 139 | + expect(mockNotifyWebhookAutoDisabled).toHaveBeenCalledWith({ |
| 140 | + webhookId: id, |
| 141 | + reason: 'credential_revoked', |
| 142 | + }) |
| 143 | + }) |
| 144 | + |
| 145 | + it('tells its people once when consecutive failures turn it off', async () => { |
| 146 | + const id = await insertWebhook({ failedCount: MAX_CONSECUTIVE_FAILURES - 2 }) |
| 147 | + |
| 148 | + await markWebhookFailed(id, logger) |
| 149 | + expect(mockNotifyWebhookAutoDisabled).not.toHaveBeenCalled() |
| 150 | + |
| 151 | + await markWebhookFailed(id, logger) |
| 152 | + await markWebhookFailed(id, logger) |
| 153 | + |
| 154 | + expect(await readWebhook(id)).toMatchObject({ isActive: false }) |
| 155 | + expect(mockNotifyWebhookAutoDisabled).toHaveBeenCalledTimes(1) |
| 156 | + expect(mockNotifyWebhookAutoDisabled).toHaveBeenCalledWith({ |
| 157 | + webhookId: id, |
| 158 | + reason: 'consecutive_failures', |
| 159 | + failedCount: MAX_CONSECUTIVE_FAILURES, |
| 160 | + }) |
| 161 | + }) |
| 162 | +}) |
0 commit comments