|
| 1 | +/** |
| 2 | + * Trigger-event catalog drift guard. The catalog a consumer validates workflow |
| 3 | + * `provider_event` triggers against MUST agree with what `parse()` actually |
| 4 | + * emits — otherwise a real event gets rejected, or a dead one accepted. These |
| 5 | + * tests pin the two together: catalog shape invariants, the namespace holding |
| 6 | + * against live parse output, and the `closed` (Telegram) catalog being exactly |
| 7 | + * the set `parse` can produce. |
| 8 | + */ |
| 9 | + |
| 10 | +import { describe, expect, it } from 'vitest' |
| 11 | +import * as webhooks from '../src/webhooks/index' |
| 12 | +import { |
| 13 | + docusealWebhookProvider, |
| 14 | + hellosignWebhookProvider, |
| 15 | + slackWebhookProvider, |
| 16 | + stripeWebhookProvider, |
| 17 | + telegramWebhookProvider, |
| 18 | + type WebhookProvider, |
| 19 | +} from '../src/webhooks/index' |
| 20 | + |
| 21 | +/** A value from the webhooks barrel that is a WebhookProvider. */ |
| 22 | +function isWebhookProvider(v: unknown): v is WebhookProvider { |
| 23 | + return ( |
| 24 | + typeof v === 'object' && |
| 25 | + v !== null && |
| 26 | + typeof (v as { id?: unknown }).id === 'string' && |
| 27 | + typeof (v as { verifySignature?: unknown }).verifySignature === 'function' && |
| 28 | + typeof (v as { parse?: unknown }).parse === 'function' |
| 29 | + ) |
| 30 | +} |
| 31 | + |
| 32 | +// EVERY exported webhook provider, discovered dynamically so a new provider is |
| 33 | +// covered by the invariants below without editing this test. |
| 34 | +const ALL_PROVIDERS: WebhookProvider[] = Object.values(webhooks).filter(isWebhookProvider) |
| 35 | + |
| 36 | +// Providers the platform wires as workflow `provider_event` sources. These MUST |
| 37 | +// declare a catalog — without one their triggers can never be author-time |
| 38 | +// validated. Kept explicit so dropping a catalog from a wired provider fails |
| 39 | +// here loudly, while a non-event provider (gmail/gdrive push, generic HMAC) may |
| 40 | +// legitimately have none. |
| 41 | +const EVENT_SOURCE_PROVIDER_IDS = ['stripe', 'slack', 'docuseal', 'telegram', 'hellosign'] |
| 42 | + |
| 43 | +describe('event catalog — shape invariants', () => { |
| 44 | + // Assert every provider that DECLARES a catalog declares a well-formed one — |
| 45 | + // catalog is optional, so a provider without one is skipped, not failed. |
| 46 | + for (const provider of ALL_PROVIDERS) { |
| 47 | + const catalog = provider.eventCatalog |
| 48 | + if (!catalog) continue |
| 49 | + it(`${provider.id}: declares a well-formed catalog`, () => { |
| 50 | + expect(catalog.events.length).toBeGreaterThan(0) |
| 51 | + // No duplicate ids. |
| 52 | + const ids = catalog.events.map((e) => e.id) |
| 53 | + expect(new Set(ids).size).toBe(ids.length) |
| 54 | + // Every declared event carries the namespace (when the provider has one). |
| 55 | + if (catalog.namespace !== null) { |
| 56 | + for (const id of ids) { |
| 57 | + expect(id.startsWith(catalog.namespace)).toBe(true) |
| 58 | + } |
| 59 | + } |
| 60 | + }) |
| 61 | + } |
| 62 | + |
| 63 | + // Coverage guard: a provider the platform routes workflow events through must |
| 64 | + // carry a catalog, or its triggers silently skip validation. |
| 65 | + for (const id of EVENT_SOURCE_PROVIDER_IDS) { |
| 66 | + it(`${id}: is a wired event source and declares a catalog`, () => { |
| 67 | + const provider = ALL_PROVIDERS.find((p) => p.id === id) |
| 68 | + expect(provider, `no exported webhook provider with id "${id}"`).toBeDefined() |
| 69 | + expect( |
| 70 | + provider?.eventCatalog, |
| 71 | + `wired event-source provider "${id}" must declare an eventCatalog`, |
| 72 | + ).toBeDefined() |
| 73 | + }) |
| 74 | + } |
| 75 | +}) |
| 76 | + |
| 77 | +describe('event catalog — namespace holds against parse()', () => { |
| 78 | + async function eventTypeOf( |
| 79 | + provider: WebhookProvider, |
| 80 | + rawBody: string, |
| 81 | + ): Promise<string> { |
| 82 | + const [env] = await provider.parse({ rawBody, headers: {} }) |
| 83 | + return env.eventType |
| 84 | + } |
| 85 | + |
| 86 | + it('slack: parsed event carries the declared namespace', async () => { |
| 87 | + const eventType = await eventTypeOf( |
| 88 | + slackWebhookProvider, |
| 89 | + JSON.stringify({ type: 'event_callback', event: { type: 'app_mention' } }), |
| 90 | + ) |
| 91 | + expect(eventType).toBe('slack.app_mention') |
| 92 | + expect(eventType.startsWith(slackWebhookProvider.eventCatalog!.namespace!)).toBe(true) |
| 93 | + }) |
| 94 | + |
| 95 | + it('docuseal: parsed event carries the declared namespace', async () => { |
| 96 | + const eventType = await eventTypeOf( |
| 97 | + docusealWebhookProvider, |
| 98 | + JSON.stringify({ event_type: 'form.completed' }), |
| 99 | + ) |
| 100 | + expect(eventType).toBe('docuseal.form.completed') |
| 101 | + expect(eventType.startsWith(docusealWebhookProvider.eventCatalog!.namespace!)).toBe(true) |
| 102 | + }) |
| 103 | + |
| 104 | + it('hellosign: parsed event carries the declared namespace', async () => { |
| 105 | + const eventType = await eventTypeOf( |
| 106 | + hellosignWebhookProvider, |
| 107 | + JSON.stringify({ event: { event_type: 'signature_request_signed' } }), |
| 108 | + ) |
| 109 | + expect(eventType).toBe('hellosign.signature_request_signed') |
| 110 | + expect(eventType.startsWith(hellosignWebhookProvider.eventCatalog!.namespace!)).toBe(true) |
| 111 | + }) |
| 112 | + |
| 113 | + it('stripe: raw event type, no namespace to enforce', async () => { |
| 114 | + const eventType = await eventTypeOf( |
| 115 | + stripeWebhookProvider, |
| 116 | + JSON.stringify({ id: 'evt_1', type: 'charge.succeeded' }), |
| 117 | + ) |
| 118 | + expect(eventType).toBe('charge.succeeded') |
| 119 | + expect(stripeWebhookProvider.eventCatalog!.namespace).toBeNull() |
| 120 | + }) |
| 121 | +}) |
| 122 | + |
| 123 | +describe('telegram — closed catalog is exactly what parse can emit', () => { |
| 124 | + const catalog = telegramWebhookProvider.eventCatalog! |
| 125 | + const catalogIds = new Set(catalog.events.map((e) => e.id)) |
| 126 | + |
| 127 | + it('is closed and namespaced', () => { |
| 128 | + expect(catalog.closed).toBe(true) |
| 129 | + expect(catalog.namespace).toBe('telegram.') |
| 130 | + }) |
| 131 | + |
| 132 | + it('every update kind parse recognizes is a catalog member', async () => { |
| 133 | + // Drive parse with one update per declared kind; each must round-trip to a |
| 134 | + // catalog id. This is the drift guard: parse derives the kind from the same |
| 135 | + // TELEGRAM_UPDATE_KEYS the catalog is built from, so any divergence fails here. |
| 136 | + for (const { id } of catalog.events) { |
| 137 | + const kind = id.slice('telegram.'.length) |
| 138 | + const [env] = await telegramWebhookProvider.parse({ |
| 139 | + rawBody: JSON.stringify({ update_id: 1, [kind]: {} }), |
| 140 | + headers: {}, |
| 141 | + }) |
| 142 | + expect(env.eventType).toBe(id) |
| 143 | + expect(catalogIds.has(env.eventType)).toBe(true) |
| 144 | + } |
| 145 | + }) |
| 146 | + |
| 147 | + it('an unrecognized update falls back to telegram.unknown, which is NOT triggerable', async () => { |
| 148 | + const [env] = await telegramWebhookProvider.parse({ |
| 149 | + rawBody: JSON.stringify({ update_id: 1, not_a_real_update: {} }), |
| 150 | + headers: {}, |
| 151 | + }) |
| 152 | + expect(env.eventType).toBe('telegram.unknown') |
| 153 | + expect(catalogIds.has('telegram.unknown')).toBe(false) |
| 154 | + }) |
| 155 | +}) |
0 commit comments