|
| 1 | +import { execute } from "@infrastructure/discord/commands/myIssues"; |
| 2 | +import { GithubAPI } from "@infrastructure/github"; |
| 3 | +import { can } from "@infrastructure/discord/authz"; |
| 4 | +import { buildIssueButtonRow } from "@infrastructure/discord/builders"; |
| 5 | +import { formatDiscordDate } from "@infrastructure/discord/webhookMessages"; |
| 6 | + |
| 7 | +jest.mock("@infrastructure/github", () => ({ |
| 8 | + GithubAPI: { |
| 9 | + fetchProjectItems: jest.fn(), |
| 10 | + }, |
| 11 | +})); |
| 12 | + |
| 13 | +jest.mock("@infrastructure/discord/authz", () => ({ |
| 14 | + can: jest.fn(), |
| 15 | +})); |
| 16 | + |
| 17 | +jest.mock("@infrastructure/discord/builders", () => ({ |
| 18 | + buildIssueButtonRow: jest.fn(() => "[buttons]"), |
| 19 | +})); |
| 20 | + |
| 21 | +jest.mock("@infrastructure/discord/webhookMessages", () => ({ |
| 22 | + formatDiscordDate: jest.fn((date) => `Formatted(${date})`), |
| 23 | +})); |
| 24 | + |
| 25 | +describe("my-issues command", () => { |
| 26 | + const mockReply = jest.fn(); |
| 27 | + const mockEditReply = jest.fn(); |
| 28 | + const mockDeferReply = jest.fn(); |
| 29 | + const mockFollowUp = jest.fn(); |
| 30 | + const mockLogger = { info: jest.fn(), error: jest.fn() }; |
| 31 | + |
| 32 | + const makeInteraction = (options = {}) => ({ |
| 33 | + user: { id: "user-123" }, |
| 34 | + options: { getInteger: jest.fn(() => null), ...options }, |
| 35 | + reply: mockReply, |
| 36 | + deferReply: mockDeferReply, |
| 37 | + editReply: mockEditReply, |
| 38 | + followUp: mockFollowUp, |
| 39 | + }); |
| 40 | + |
| 41 | + const defaultItem = (overrides = {}) => ({ |
| 42 | + title: "Issue Title", |
| 43 | + url: "https://github.com/test", |
| 44 | + githubIssueId: "123", |
| 45 | + assignedUsers: ["https://github.com/test-user"], |
| 46 | + createdAt: new Date().toISOString(), |
| 47 | + dueDate: "2025-05-20", |
| 48 | + status: "Open", |
| 49 | + ...overrides, |
| 50 | + }); |
| 51 | + |
| 52 | + beforeEach(() => { |
| 53 | + jest.clearAllMocks(); |
| 54 | + }); |
| 55 | + |
| 56 | + it("will block unauthorized users", async () => { |
| 57 | + (can as jest.Mock).mockReturnValue(false); |
| 58 | + const interaction = makeInteraction(); |
| 59 | + |
| 60 | + await execute(interaction as any); |
| 61 | + |
| 62 | + expect(mockReply).toHaveBeenCalledWith({ |
| 63 | + content: "You do not have permission to create an issue.", |
| 64 | + ephemeral: true, |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + it("will show error if user is not linked to a GitHub account", async () => { |
| 69 | + (can as jest.Mock).mockReturnValue(true); |
| 70 | + const interaction = makeInteraction(); |
| 71 | + interaction.user.id = "not-in-map"; |
| 72 | + jest.spyOn(Object, 'values').mockReturnValueOnce([{ |
| 73 | + githubUsername: "test-user", |
| 74 | + discordId: "someone-else", |
| 75 | + githubId: "123", |
| 76 | + }]); |
| 77 | + |
| 78 | + await execute(interaction as any); |
| 79 | + |
| 80 | + expect(mockReply).toHaveBeenCalledWith({ |
| 81 | + content: expect.stringContaining("linked to a GitHub account"), |
| 82 | + ephemeral: true, |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + it("will show error if GitHub API fails", async () => { |
| 87 | + (can as jest.Mock).mockReturnValue(true); |
| 88 | + jest.spyOn(Object, 'values').mockReturnValue([{ githubUsername: "test-user", discordId: "user-123" }]); |
| 89 | + (GithubAPI.fetchProjectItems as jest.Mock).mockResolvedValue({ err: true, val: { message: "Boom" } }); |
| 90 | + |
| 91 | + const interaction = makeInteraction(); |
| 92 | + |
| 93 | + await execute(interaction as any); |
| 94 | + |
| 95 | + expect(mockEditReply).toHaveBeenCalledWith({ |
| 96 | + content: expect.stringContaining("Failed to fetch issues"), |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + it("will show message if no assigned issues are found", async () => { |
| 101 | + (can as jest.Mock).mockReturnValue(true); |
| 102 | + jest.spyOn(Object, 'values').mockReturnValue([{ githubUsername: "test-user", discordId: "user-123" }]); |
| 103 | + (GithubAPI.fetchProjectItems as jest.Mock).mockResolvedValue({ err: false, val: [] }); |
| 104 | + |
| 105 | + const interaction = makeInteraction(); |
| 106 | + |
| 107 | + await execute(interaction as any); |
| 108 | + |
| 109 | + expect(mockEditReply).toHaveBeenCalledWith({ |
| 110 | + content: expect.stringContaining("no assigned issues"), |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + it("will show a specific issue by index", async () => { |
| 115 | + (can as jest.Mock).mockReturnValue(true); |
| 116 | + jest.spyOn(Object, 'values').mockReturnValue([{ githubUsername: "test-user", discordId: "user-123" }]); |
| 117 | + (GithubAPI.fetchProjectItems as jest.Mock).mockResolvedValue({ |
| 118 | + err: false, |
| 119 | + val: [defaultItem({ assignedUsers: ["https://github.com/test-user"] })], |
| 120 | + }); |
| 121 | + |
| 122 | + const interaction = makeInteraction({ getInteger: () => 0 }); |
| 123 | + |
| 124 | + await execute(interaction as any); |
| 125 | + |
| 126 | + expect(mockEditReply).toHaveBeenCalledWith({ |
| 127 | + content: expect.stringContaining("**Issue #0**"), |
| 128 | + components: ["[buttons]"], |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + it("will show index list when no index is provided", async () => { |
| 133 | + (can as jest.Mock).mockReturnValue(true); |
| 134 | + jest.spyOn(Object, 'values').mockReturnValue([{ githubUsername: "test-user", discordId: "user-123" }]); |
| 135 | + (GithubAPI.fetchProjectItems as jest.Mock).mockResolvedValue({ |
| 136 | + err: false, |
| 137 | + val: [defaultItem({ assignedUsers: ["https://github.com/test-user"] })], |
| 138 | + }); |
| 139 | + |
| 140 | + const interaction = makeInteraction(); |
| 141 | + |
| 142 | + await execute(interaction as any); |
| 143 | + |
| 144 | + expect(mockEditReply).toHaveBeenCalledWith({ |
| 145 | + content: expect.stringContaining("assigned issue(s):\n\n\`0\`"), |
| 146 | + }); |
| 147 | + }); |
| 148 | +}); |
0 commit comments