swadhinbiswas chore: update src/lib/integrations/webhooks.test.ts · 2/18/2026
9968b3b
76 lines · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { sendDiscordMessage, createPREmbed } from "./discord";
import { sendTeamsMessage, createPRCard } from "./teams";

// Mock fetch globally
const originalFetch = global.fetch;
const mockFetch = vi.fn((url: string | Request | URL, init?: RequestInit) => {
    return Promise.resolve(new Response("OK", { status: 200 }));
});

describe("Webhook Integrations", () => {
    beforeEach(() => {
        global.fetch = mockFetch;
        mockFetch.mockClear();
    });

    afterEach(() => {
        global.fetch = originalFetch;
    });

    describe("Discord", () => {
        it("should send a PR notification to Discord", async () => {
            const embed = createPREmbed({
                action: "opened",
                prTitle: "Test PR",
                prNumber: 123,
                prUrl: "http://example.com/pr/123",
                author: "testuser",
                repository: "owner/repo",
            });

            const success = await sendDiscordMessage("https://discord.com/api/webhooks/xyz", {
                username: "OpenCodeHub",
                embeds: [embed],
            });

            expect(success).toBe(true);
            expect(mockFetch).toHaveBeenCalledTimes(1);
            expect(mockFetch.mock.calls[0][0]).toBe("https://discord.com/api/webhooks/xyz");

            const body = JSON.parse(mockFetch.mock.calls[0][1]?.body as string);
            expect(body.username).toBe("OpenCodeHub");
            expect(body.embeds[0].title).toContain("Pull Request Opened");
        });

        it("should handle failures gracefully", async () => {
            global.fetch = vi.fn(() => Promise.resolve(new Response("Error", { status: 500 })));

            const success = await sendDiscordMessage("https://discord.com/api/webhooks/xyz", {});
            expect(success).toBe(false);
        });
    });

    describe("Teams", () => {
        it("should send a PR notification to Teams", async () => {
            const card = createPRCard({
                action: "opened",
                prTitle: "Test PR",
                prNumber: 123,
                prUrl: "http://example.com/pr/123",
                author: "testuser",
                repository: "owner/repo",
            });

            const success = await sendTeamsMessage("https://outlook.office.com/webhook/xyz", card);

            expect(success).toBe(true);
            expect(mockFetch).toHaveBeenCalledTimes(1);

            const body = JSON.parse(mockFetch.mock.calls[0][1]?.body as string);
            expect(body["@type"]).toBe("MessageCard");
            expect(body.summary).toContain("New Pull Request");
        });
    });
});