|
| 1 | +import { createHmac } from "node:crypto"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { WebhookError, webhooks } from "./webhooks.js"; |
| 4 | + |
| 5 | +const SECRET = "test-alert-webhook-secret"; |
| 6 | + |
| 7 | +function sign(payload: string, secret = SECRET) { |
| 8 | + return createHmac("sha256", secret).update(payload).digest("hex"); |
| 9 | +} |
| 10 | + |
| 11 | +// Matches the payload the webapp builds in deliverAlertEvent.server.ts (deliverWebhookChannel). |
| 12 | +const alertRaisedPayload = { |
| 13 | + id: "alert-event:alev_123:channel:ch_456", |
| 14 | + created: "2026-11-30T12:00:00.000Z", |
| 15 | + webhookVersion: "2026-09-17", |
| 16 | + type: "alert.raised", |
| 17 | + object: { |
| 18 | + alert: { id: "alert_123", name: null, capabilityKey: "limits.api" }, |
| 19 | + event: { |
| 20 | + id: "alev_123", |
| 21 | + type: "raised", |
| 22 | + at: "2026-11-30T12:00:00.000Z", |
| 23 | + facts: { component: "limits.api", signal: "approaching", usedPercent: 84 }, |
| 24 | + }, |
| 25 | + environment: { id: "env_123" }, |
| 26 | + organization: { id: "org_123" }, |
| 27 | + project: { id: "proj_123" }, |
| 28 | + }, |
| 29 | +}; |
| 30 | + |
| 31 | +const deploymentSuccessPayload = { |
| 32 | + id: "wh_deploy_1", |
| 33 | + created: "2026-11-30T12:00:00.000Z", |
| 34 | + webhookVersion: "2025-07-10", |
| 35 | + type: "alert.deployment.success", |
| 36 | + object: { |
| 37 | + deployment: { |
| 38 | + id: "deploy_1", |
| 39 | + status: "DEPLOYED", |
| 40 | + version: "20261130.1", |
| 41 | + shortCode: "abc123", |
| 42 | + deployedAt: "2026-11-30T12:00:00.000Z", |
| 43 | + }, |
| 44 | + tasks: [{ id: "hello-world", filePath: "src/trigger/example.ts", triggerSource: "STANDARD" }], |
| 45 | + environment: { id: "env_123", type: "PRODUCTION", slug: "prod" }, |
| 46 | + organization: { id: "org_123", slug: "acme", name: "Acme" }, |
| 47 | + project: { id: "proj_123", ref: "proj_abc", slug: "hello", name: "Hello" }, |
| 48 | + }, |
| 49 | +}; |
| 50 | + |
| 51 | +describe("webhooks.constructEvent", () => { |
| 52 | + it("accepts an alert.raised webhook from a Limits alert", async () => { |
| 53 | + const payload = JSON.stringify(alertRaisedPayload); |
| 54 | + |
| 55 | + const event = await webhooks.constructEvent({ payload, header: sign(payload) }, SECRET); |
| 56 | + |
| 57 | + expect(event.type).toBe("alert.raised"); |
| 58 | + if (event.type !== "alert.raised") throw new Error("expected alert.raised"); |
| 59 | + |
| 60 | + // Narrowed to the alert.raised shape |
| 61 | + expect(event.object.alert.name).toBeNull(); |
| 62 | + expect(event.object.alert.capabilityKey).toBe("limits.api"); |
| 63 | + expect(event.object.event.type).toBe("raised"); |
| 64 | + expect(event.object.event.at).toBeInstanceOf(Date); |
| 65 | + expect(event.object.event.at.toISOString()).toBe("2026-11-30T12:00:00.000Z"); |
| 66 | + expect(event.object.event.facts).toEqual(alertRaisedPayload.object.event.facts); |
| 67 | + expect(event.object.event.summary).toBeUndefined(); |
| 68 | + expect(event.object.project.id).toBe("proj_123"); |
| 69 | + expect(event.created).toBeInstanceOf(Date); |
| 70 | + }); |
| 71 | + |
| 72 | + it("accepts an alert.raised webhook passed as a Request, with a name and summary", async () => { |
| 73 | + const payload = JSON.stringify({ |
| 74 | + ...alertRaisedPayload, |
| 75 | + object: { |
| 76 | + ...alertRaisedPayload.object, |
| 77 | + alert: { id: "alert_456", name: "Slow queue", capabilityKey: "custom.query" }, |
| 78 | + event: { ...alertRaisedPayload.object.event, summary: "p95 duration is 12.4s" }, |
| 79 | + }, |
| 80 | + }); |
| 81 | + |
| 82 | + const request = new Request("https://example.com/webhooks/alerts", { |
| 83 | + method: "POST", |
| 84 | + headers: { "x-trigger-signature-hmacsha256": sign(payload) }, |
| 85 | + body: payload, |
| 86 | + }); |
| 87 | + |
| 88 | + const event = await webhooks.constructEvent(request, SECRET); |
| 89 | + |
| 90 | + if (event.type !== "alert.raised") throw new Error("expected alert.raised"); |
| 91 | + expect(event.object.alert.name).toBe("Slow queue"); |
| 92 | + expect(event.object.event.summary).toBe("p95 duration is 12.4s"); |
| 93 | + }); |
| 94 | + |
| 95 | + it("still parses existing alert webhook types", async () => { |
| 96 | + const payload = JSON.stringify(deploymentSuccessPayload); |
| 97 | + |
| 98 | + const event = await webhooks.constructEvent({ payload, header: sign(payload) }, SECRET); |
| 99 | + |
| 100 | + if (event.type !== "alert.deployment.success") { |
| 101 | + throw new Error("expected alert.deployment.success"); |
| 102 | + } |
| 103 | + expect(event.object.deployment.deployedAt).toBeInstanceOf(Date); |
| 104 | + expect(event.object.tasks[0]?.id).toBe("hello-world"); |
| 105 | + }); |
| 106 | + |
| 107 | + it("still rejects a bad signature and an unknown type", async () => { |
| 108 | + const payload = JSON.stringify(alertRaisedPayload); |
| 109 | + await expect( |
| 110 | + webhooks.constructEvent({ payload, header: sign(payload, "wrong-secret") }, SECRET) |
| 111 | + ).rejects.toThrow("Invalid signature"); |
| 112 | + |
| 113 | + const unknown = JSON.stringify({ ...alertRaisedPayload, type: "alert.unknown" }); |
| 114 | + await expect( |
| 115 | + webhooks.constructEvent({ payload: unknown, header: sign(unknown) }, SECRET) |
| 116 | + ).rejects.toBeInstanceOf(WebhookError); |
| 117 | + }); |
| 118 | +}); |
0 commit comments