|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + extractBranchName, |
| 4 | + extractCommitMessage, |
| 5 | + extractHash, |
| 6 | + getProviderByHeader, |
| 7 | +} from "@/pages/api/deploy/[refreshToken]"; |
| 8 | + |
| 9 | +describe("Soft Serve Webhook", () => { |
| 10 | + const mockSoftServeHeaders = { |
| 11 | + "x-softserve-event": "push", |
| 12 | + }; |
| 13 | + |
| 14 | + const createMockBody = (message: string, hash: string, branch: string) => ({ |
| 15 | + event: "push", |
| 16 | + ref: `refs/heads/${branch}`, |
| 17 | + after: hash, |
| 18 | + commits: [{ message: message }], |
| 19 | + }); |
| 20 | + const message: string = "feat: add new feature"; |
| 21 | + const hash: string = "3c91c24ef9560bddc695bce138bf8a7094ec3df5"; |
| 22 | + const branch: string = "feat/add-new"; |
| 23 | + const goodWebhook = createMockBody(message, hash, branch); |
| 24 | + |
| 25 | + it("should properly extract the provider name", () => { |
| 26 | + expect(getProviderByHeader(mockSoftServeHeaders)).toBe("soft-serve"); |
| 27 | + }); |
| 28 | + |
| 29 | + it("should properly extract the commit message", () => { |
| 30 | + expect(extractCommitMessage(mockSoftServeHeaders, goodWebhook)).toBe( |
| 31 | + message, |
| 32 | + ); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should properly extract hash", () => { |
| 36 | + expect(extractHash(mockSoftServeHeaders, goodWebhook)).toBe(hash); |
| 37 | + }); |
| 38 | + |
| 39 | + it("should properly extract branch name", () => { |
| 40 | + expect(extractBranchName(mockSoftServeHeaders, goodWebhook)).toBe(branch); |
| 41 | + }); |
| 42 | + |
| 43 | + it("should gracefully handle invalid webhook", () => { |
| 44 | + expect(getProviderByHeader({})).toBeNull(); |
| 45 | + expect(extractCommitMessage(mockSoftServeHeaders, {})).toBe("NEW COMMIT"); |
| 46 | + expect(extractHash(mockSoftServeHeaders, {})).toBe("NEW COMMIT"); |
| 47 | + expect(extractBranchName(mockSoftServeHeaders, {})).toBeNull(); |
| 48 | + }); |
| 49 | +}); |
0 commit comments