|
| 1 | +import { assert, describe, it } from "@effect/vitest"; |
| 2 | + |
| 3 | +import { findFailedWebhookFilter } from "./webhookFilter.ts"; |
| 4 | +import type { WebhookRequest } from "./webhookTemplate.ts"; |
| 5 | + |
| 6 | +const issueComment: WebhookRequest = { |
| 7 | + method: "POST", |
| 8 | + path: "/api/hooks/task-1", |
| 9 | + query: "", |
| 10 | + headers: { "content-type": "application/json", "x-github-event": "issue_comment" }, |
| 11 | + bodyText: JSON.stringify({ |
| 12 | + action: "created", |
| 13 | + issue: { number: 7, labels: [{ name: "bug" }, { name: "needs more info" }], assignees: [] }, |
| 14 | + comment: { user: { login: "reporter", type: "User" } }, |
| 15 | + }), |
| 16 | +}; |
| 17 | + |
| 18 | +describe("findFailedWebhookFilter", () => { |
| 19 | + it("matches a wildcard path when any element matches", () => { |
| 20 | + assert.isNull( |
| 21 | + findFailedWebhookFilter( |
| 22 | + [ |
| 23 | + { path: "body.issue.labels[].name", op: "equals", value: "needs more info" }, |
| 24 | + { path: "body.issue.labels[].name", op: "in", value: ["needs more info", "triage"] }, |
| 25 | + { path: "body.issue.labels[].name", op: "startsWith", value: "needs" }, |
| 26 | + { path: "body.issue.labels[].name", op: "exists" }, |
| 27 | + ], |
| 28 | + issueComment, |
| 29 | + ), |
| 30 | + ); |
| 31 | + assert.deepEqual( |
| 32 | + findFailedWebhookFilter( |
| 33 | + [{ path: "body.issue.labels[].name", op: "equals", value: "wontfix" }], |
| 34 | + issueComment, |
| 35 | + ), |
| 36 | + { index: 0, path: "body.issue.labels[].name" }, |
| 37 | + ); |
| 38 | + }); |
| 39 | + |
| 40 | + it("fails notEquals on a wildcard path when any element equals the value", () => { |
| 41 | + assert.deepEqual( |
| 42 | + findFailedWebhookFilter( |
| 43 | + [{ path: "body.issue.labels[].name", op: "notEquals", value: "bug" }], |
| 44 | + issueComment, |
| 45 | + ), |
| 46 | + { index: 0, path: "body.issue.labels[].name" }, |
| 47 | + ); |
| 48 | + assert.isNull( |
| 49 | + findFailedWebhookFilter( |
| 50 | + [{ path: "body.issue.labels[].name", op: "notEquals", value: "wontfix" }], |
| 51 | + issueComment, |
| 52 | + ), |
| 53 | + ); |
| 54 | + }); |
| 55 | + |
| 56 | + it("treats an empty or missing wildcard list as nothing to match", () => { |
| 57 | + assert.deepEqual( |
| 58 | + findFailedWebhookFilter( |
| 59 | + [{ path: "body.issue.assignees[].login", op: "exists" }], |
| 60 | + issueComment, |
| 61 | + ), |
| 62 | + { index: 0, path: "body.issue.assignees[].login" }, |
| 63 | + ); |
| 64 | + assert.isNull( |
| 65 | + findFailedWebhookFilter( |
| 66 | + [{ path: "body.issue.assignees[].login", op: "notEquals", value: "anyone" }], |
| 67 | + issueComment, |
| 68 | + ), |
| 69 | + ); |
| 70 | + assert.deepEqual( |
| 71 | + findFailedWebhookFilter([{ path: "body.issue.number[].x", op: "exists" }], issueComment), |
| 72 | + { index: 0, path: "body.issue.number[].x" }, |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + it("still compares scalars without a wildcard", () => { |
| 77 | + assert.isNull( |
| 78 | + findFailedWebhookFilter( |
| 79 | + [ |
| 80 | + { path: "body.action", op: "equals", value: "created" }, |
| 81 | + { path: "body.comment.user.type", op: "notEquals", value: "Bot" }, |
| 82 | + ], |
| 83 | + issueComment, |
| 84 | + ), |
| 85 | + ); |
| 86 | + // A list without a wildcard in the path only satisfies exists. |
| 87 | + assert.deepEqual( |
| 88 | + findFailedWebhookFilter( |
| 89 | + [{ path: "body.issue.labels", op: "equals", value: "bug" }], |
| 90 | + issueComment, |
| 91 | + ), |
| 92 | + { index: 0, path: "body.issue.labels" }, |
| 93 | + ); |
| 94 | + }); |
| 95 | +}); |
0 commit comments