|
1 | 1 | import { describe, expect, it } from "vitest"; |
2 | 2 | import { normalizeWorkflowRunEvent } from "../src/webhooks/handlers/workflowRun"; |
| 3 | +import { normalizePushPayload, normalizeCheckSuitePayload } from "../src/trustsignal/github"; |
3 | 4 |
|
4 | 5 | describe("normalizeWorkflowRunEvent", () => { |
5 | 6 | it("normalizes a completed workflow run", () => { |
@@ -29,3 +30,107 @@ describe("normalizeWorkflowRunEvent", () => { |
29 | 30 | expect(job?.headSha).toBe("abc123"); |
30 | 31 | }); |
31 | 32 | }); |
| 33 | + |
| 34 | +describe("normalizePushPayload", () => { |
| 35 | + it("returns null for branch deletion events (zero SHA)", () => { |
| 36 | + const result = normalizePushPayload({ |
| 37 | + ref: "refs/heads/main", |
| 38 | + before: "abc1234def5678901234567890abcdef12345678", |
| 39 | + after: "0000000000000000000000000000000000000000", |
| 40 | + repository: { |
| 41 | + name: "repo", |
| 42 | + default_branch: "main", |
| 43 | + html_url: "https://github.com/acme/repo", |
| 44 | + owner: { login: "acme" }, |
| 45 | + }, |
| 46 | + }); |
| 47 | + |
| 48 | + expect(result).toBeNull(); |
| 49 | + }); |
| 50 | + |
| 51 | + it("returns null when after is missing", () => { |
| 52 | + const result = normalizePushPayload({ |
| 53 | + ref: "refs/heads/main", |
| 54 | + before: "abc1234def5678901234567890abcdef12345678", |
| 55 | + repository: { |
| 56 | + name: "repo", |
| 57 | + default_branch: "main", |
| 58 | + html_url: "https://github.com/acme/repo", |
| 59 | + owner: { login: "acme" }, |
| 60 | + }, |
| 61 | + }); |
| 62 | + |
| 63 | + expect(result).toBeNull(); |
| 64 | + }); |
| 65 | + |
| 66 | + it("returns an envelope for a default-branch push with a valid SHA", () => { |
| 67 | + const result = normalizePushPayload({ |
| 68 | + ref: "refs/heads/main", |
| 69 | + before: "abc1234def5678901234567890abcdef12345678", |
| 70 | + after: "def5678abc1234901234567890abcdef12345678", |
| 71 | + repository: { |
| 72 | + name: "repo", |
| 73 | + default_branch: "main", |
| 74 | + html_url: "https://github.com/acme/repo", |
| 75 | + owner: { login: "acme" }, |
| 76 | + }, |
| 77 | + }); |
| 78 | + |
| 79 | + expect(result).not.toBeNull(); |
| 80 | + expect(result?.headSha).toBe("def5678abc1234901234567890abcdef12345678"); |
| 81 | + expect(result?.externalId).toBe("push:def5678abc1234901234567890abcdef12345678"); |
| 82 | + }); |
| 83 | +}); |
| 84 | + |
| 85 | +describe("normalizeCheckSuitePayload", () => { |
| 86 | + it("uses html_url for detailsUrl when available", () => { |
| 87 | + const result = normalizeCheckSuitePayload({ |
| 88 | + action: "requested", |
| 89 | + check_suite: { |
| 90 | + id: 321, |
| 91 | + head_sha: "def5678abc1234901234567890abcdef12345678", |
| 92 | + status: "queued", |
| 93 | + conclusion: null, |
| 94 | + html_url: "https://github.com/acme/repo/actions/runs/321", |
| 95 | + url: "https://api.github.com/repos/acme/repo/check-suites/321", |
| 96 | + app: { slug: "some-app" }, |
| 97 | + pull_requests: [], |
| 98 | + }, |
| 99 | + repository: { |
| 100 | + id: 1, |
| 101 | + name: "repo", |
| 102 | + default_branch: "main", |
| 103 | + html_url: "https://github.com/acme/repo", |
| 104 | + owner: { login: "acme" }, |
| 105 | + }, |
| 106 | + }); |
| 107 | + |
| 108 | + expect(result).not.toBeNull(); |
| 109 | + expect(result?.detailsUrl).toBe("https://github.com/acme/repo/actions/runs/321"); |
| 110 | + }); |
| 111 | + |
| 112 | + it("falls back to commit URL for detailsUrl when html_url is absent", () => { |
| 113 | + const result = normalizeCheckSuitePayload({ |
| 114 | + action: "requested", |
| 115 | + check_suite: { |
| 116 | + id: 654, |
| 117 | + head_sha: "abc1234def5678901234567890abcdef12345678", |
| 118 | + status: "queued", |
| 119 | + conclusion: null, |
| 120 | + url: "https://api.github.com/repos/acme/repo/check-suites/654", |
| 121 | + app: { slug: "some-app" }, |
| 122 | + pull_requests: [], |
| 123 | + }, |
| 124 | + repository: { |
| 125 | + id: 1, |
| 126 | + name: "repo", |
| 127 | + default_branch: "main", |
| 128 | + html_url: "https://github.com/acme/repo", |
| 129 | + owner: { login: "acme" }, |
| 130 | + }, |
| 131 | + }); |
| 132 | + |
| 133 | + expect(result).not.toBeNull(); |
| 134 | + expect(result?.detailsUrl).toBe("https://github.com/acme/repo/commit/abc1234def5678901234567890abcdef12345678"); |
| 135 | + }); |
| 136 | +}); |
0 commit comments