|
| 1 | +import type * as github from "@actions/github"; |
| 2 | + |
| 3 | +import { describe, expect, it, vi } from "vitest"; |
| 4 | + |
| 5 | +import { runCommentCleanup } from "./runCommentCleanup"; |
| 6 | + |
| 7 | +const mockCore = { |
| 8 | + info: vi.fn(), |
| 9 | +}; |
| 10 | + |
| 11 | +vi.mock("@actions/core", () => ({ |
| 12 | + get info() { |
| 13 | + return mockCore.info; |
| 14 | + }, |
| 15 | +})); |
| 16 | + |
| 17 | +const mockOctokitFromAuth = vi.fn(); |
| 18 | + |
| 19 | +vi.mock("octokit-from-auth", () => ({ |
| 20 | + get octokitFromAuth() { |
| 21 | + return mockOctokitFromAuth; |
| 22 | + }, |
| 23 | +})); |
| 24 | + |
| 25 | +const mockCreateActor = vi.fn(); |
| 26 | + |
| 27 | +vi.mock("../actors/createActor.js", () => ({ |
| 28 | + get createActor() { |
| 29 | + return mockCreateActor; |
| 30 | + }, |
| 31 | +})); |
| 32 | + |
| 33 | +const mockGetExistingComment = vi.fn(); |
| 34 | + |
| 35 | +vi.mock("./comments/getExistingComment.js", () => ({ |
| 36 | + get getExistingComment() { |
| 37 | + return mockGetExistingComment; |
| 38 | + }, |
| 39 | +})); |
| 40 | + |
| 41 | +const auth = "gho_..."; |
| 42 | +const locator = { owner: "owner", repository: "repo" }; |
| 43 | +const url = "/repos/owner/repo/issues/1"; |
| 44 | + |
| 45 | +describe(runCommentCleanup, () => { |
| 46 | + it("does nothing when the payload does not contain a comment", async () => { |
| 47 | + const payload = { |
| 48 | + issue: {}, |
| 49 | + } as typeof github.context.payload; |
| 50 | + |
| 51 | + await runCommentCleanup({ auth, payload, url }); |
| 52 | + |
| 53 | + expect(mockOctokitFromAuth).not.toHaveBeenCalled(); |
| 54 | + }); |
| 55 | + |
| 56 | + it("throws an error when the actor cannot be resolved", async () => { |
| 57 | + const payload = { |
| 58 | + comment: {}, |
| 59 | + } as typeof github.context.payload; |
| 60 | + |
| 61 | + mockCreateActor.mockReturnValueOnce({}); |
| 62 | + |
| 63 | + await expect(runCommentCleanup({ auth, payload, url })).rejects.toThrow( |
| 64 | + "Could not resolve GitHub entity actor.", |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + it("logs info without throwing when an existing comment cannot be found", async () => { |
| 69 | + const payload = { |
| 70 | + comment: {}, |
| 71 | + } as typeof github.context.payload; |
| 72 | + |
| 73 | + mockCreateActor.mockReturnValueOnce({ actor: {}, locator }); |
| 74 | + mockGetExistingComment.mockResolvedValueOnce(undefined); |
| 75 | + |
| 76 | + await runCommentCleanup({ auth, payload, url }); |
| 77 | + expect(mockCore.info).toHaveBeenCalledWith( |
| 78 | + "No existing comment found. Nothing to clean up.", |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + it("deletes the comment when it exists as a discussion comment", async () => { |
| 83 | + const payload = { |
| 84 | + comment: { id: 123 }, |
| 85 | + discussion: {}, |
| 86 | + } as typeof github.context.payload; |
| 87 | + const nodeId = "abc123"; |
| 88 | + |
| 89 | + mockOctokitFromAuth.mockResolvedValueOnce({ |
| 90 | + graphql: vi.fn(), |
| 91 | + }); |
| 92 | + mockCreateActor.mockReturnValueOnce({ actor: {}, locator }); |
| 93 | + mockGetExistingComment.mockResolvedValueOnce({ |
| 94 | + node_id: nodeId, |
| 95 | + }); |
| 96 | + |
| 97 | + await runCommentCleanup({ auth, payload, url }); |
| 98 | + expect(mockCore.info).toHaveBeenCalledWith( |
| 99 | + `Deleting discussion comment with node id: ${nodeId}`, |
| 100 | + ); |
| 101 | + }); |
| 102 | + |
| 103 | + it("deletes the comment when it exists as an issue-like comment", async () => { |
| 104 | + const id = 123; |
| 105 | + const payload = { |
| 106 | + comment: { id }, |
| 107 | + issue: {}, |
| 108 | + } as typeof github.context.payload; |
| 109 | + |
| 110 | + mockOctokitFromAuth.mockResolvedValueOnce({ |
| 111 | + rest: { |
| 112 | + issues: { |
| 113 | + deleteComment: vi.fn(), |
| 114 | + }, |
| 115 | + }, |
| 116 | + }); |
| 117 | + mockCreateActor.mockReturnValueOnce({ actor: {}, locator }); |
| 118 | + mockGetExistingComment.mockResolvedValueOnce({ id }); |
| 119 | + |
| 120 | + await runCommentCleanup({ auth, payload, url }); |
| 121 | + expect(mockCore.info).toHaveBeenCalledWith( |
| 122 | + `Deleting issue-like comment with id: ${id.toString()}`, |
| 123 | + ); |
| 124 | + }); |
| 125 | +}); |
0 commit comments