|
| 1 | +import { describe, it, expect, beforeEach, vi } from "vitest"; |
| 2 | + |
| 3 | +const mockCore = { |
| 4 | + debug: vi.fn(), |
| 5 | + info: vi.fn(), |
| 6 | + warning: vi.fn(), |
| 7 | + error: vi.fn(), |
| 8 | + setFailed: vi.fn(), |
| 9 | + setOutput: vi.fn(), |
| 10 | + summary: { |
| 11 | + addRaw: vi.fn().mockReturnThis(), |
| 12 | + write: vi.fn().mockResolvedValue(), |
| 13 | + }, |
| 14 | +}; |
| 15 | + |
| 16 | +global.core = mockCore; |
| 17 | + |
| 18 | +const mockGraphql = vi.fn(); |
| 19 | +const mockGithub = { |
| 20 | + graphql: mockGraphql, |
| 21 | +}; |
| 22 | + |
| 23 | +global.github = mockGithub; |
| 24 | + |
| 25 | +const mockContext = { |
| 26 | + repo: { owner: "test-owner", repo: "test-repo" }, |
| 27 | + runId: 12345, |
| 28 | + eventName: "pull_request", |
| 29 | + payload: { |
| 30 | + pull_request: { number: 42 }, |
| 31 | + repository: { html_url: "https://github.com/test-owner/test-repo" }, |
| 32 | + }, |
| 33 | +}; |
| 34 | + |
| 35 | +global.context = mockContext; |
| 36 | + |
| 37 | +describe("resolve_pr_review_thread", () => { |
| 38 | + let handler; |
| 39 | + |
| 40 | + beforeEach(async () => { |
| 41 | + vi.clearAllMocks(); |
| 42 | + |
| 43 | + mockGraphql.mockResolvedValue({ |
| 44 | + resolveReviewThread: { |
| 45 | + thread: { |
| 46 | + id: "PRRT_kwDOABCD123456", |
| 47 | + isResolved: true, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }); |
| 51 | + |
| 52 | + const { main } = require("./resolve_pr_review_thread.cjs"); |
| 53 | + handler = await main({ max: 10 }); |
| 54 | + }); |
| 55 | + |
| 56 | + it("should return a function from main()", async () => { |
| 57 | + const { main } = require("./resolve_pr_review_thread.cjs"); |
| 58 | + const result = await main({}); |
| 59 | + expect(typeof result).toBe("function"); |
| 60 | + }); |
| 61 | + |
| 62 | + it("should successfully resolve a review thread", async () => { |
| 63 | + const message = { |
| 64 | + type: "resolve_pull_request_review_thread", |
| 65 | + thread_id: "PRRT_kwDOABCD123456", |
| 66 | + }; |
| 67 | + |
| 68 | + const result = await handler(message, {}); |
| 69 | + |
| 70 | + expect(result.success).toBe(true); |
| 71 | + expect(result.thread_id).toBe("PRRT_kwDOABCD123456"); |
| 72 | + expect(result.is_resolved).toBe(true); |
| 73 | + expect(mockGraphql).toHaveBeenCalledWith( |
| 74 | + expect.stringContaining("resolveReviewThread"), |
| 75 | + expect.objectContaining({ |
| 76 | + threadId: "PRRT_kwDOABCD123456", |
| 77 | + }) |
| 78 | + ); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should fail when thread_id is missing", async () => { |
| 82 | + const message = { |
| 83 | + type: "resolve_pull_request_review_thread", |
| 84 | + }; |
| 85 | + |
| 86 | + const result = await handler(message, {}); |
| 87 | + |
| 88 | + expect(result.success).toBe(false); |
| 89 | + expect(result.error).toContain("thread_id"); |
| 90 | + }); |
| 91 | + |
| 92 | + it("should fail when thread_id is empty string", async () => { |
| 93 | + const message = { |
| 94 | + type: "resolve_pull_request_review_thread", |
| 95 | + thread_id: "", |
| 96 | + }; |
| 97 | + |
| 98 | + const result = await handler(message, {}); |
| 99 | + |
| 100 | + expect(result.success).toBe(false); |
| 101 | + expect(result.error).toContain("thread_id"); |
| 102 | + }); |
| 103 | + |
| 104 | + it("should fail when thread_id is whitespace only", async () => { |
| 105 | + const message = { |
| 106 | + type: "resolve_pull_request_review_thread", |
| 107 | + thread_id: " ", |
| 108 | + }; |
| 109 | + |
| 110 | + const result = await handler(message, {}); |
| 111 | + |
| 112 | + expect(result.success).toBe(false); |
| 113 | + expect(result.error).toContain("thread_id"); |
| 114 | + }); |
| 115 | + |
| 116 | + it("should fail when thread_id is not a string", async () => { |
| 117 | + const message = { |
| 118 | + type: "resolve_pull_request_review_thread", |
| 119 | + thread_id: 12345, |
| 120 | + }; |
| 121 | + |
| 122 | + const result = await handler(message, {}); |
| 123 | + |
| 124 | + expect(result.success).toBe(false); |
| 125 | + expect(result.error).toContain("thread_id"); |
| 126 | + }); |
| 127 | + |
| 128 | + it("should respect max count limit", async () => { |
| 129 | + const { main } = require("./resolve_pr_review_thread.cjs"); |
| 130 | + const limitedHandler = await main({ max: 2 }); |
| 131 | + |
| 132 | + const message = { |
| 133 | + type: "resolve_pull_request_review_thread", |
| 134 | + thread_id: "PRRT_kwDOABCD123456", |
| 135 | + }; |
| 136 | + |
| 137 | + const result1 = await limitedHandler(message, {}); |
| 138 | + const result2 = await limitedHandler(message, {}); |
| 139 | + const result3 = await limitedHandler(message, {}); |
| 140 | + |
| 141 | + expect(result1.success).toBe(true); |
| 142 | + expect(result2.success).toBe(true); |
| 143 | + expect(result3.success).toBe(false); |
| 144 | + expect(result3.error).toContain("Max count of 2 reached"); |
| 145 | + }); |
| 146 | + |
| 147 | + it("should handle API errors gracefully", async () => { |
| 148 | + mockGraphql.mockRejectedValue(new Error("Could not resolve. Thread not found.")); |
| 149 | + |
| 150 | + const message = { |
| 151 | + type: "resolve_pull_request_review_thread", |
| 152 | + thread_id: "PRRT_invalid", |
| 153 | + }; |
| 154 | + |
| 155 | + const result = await handler(message, {}); |
| 156 | + |
| 157 | + expect(result.success).toBe(false); |
| 158 | + expect(result.error).toContain("Could not resolve"); |
| 159 | + }); |
| 160 | + |
| 161 | + it("should handle unexpected resolve failure", async () => { |
| 162 | + mockGraphql.mockResolvedValue({ |
| 163 | + resolveReviewThread: { |
| 164 | + thread: { |
| 165 | + id: "PRRT_kwDOABCD123456", |
| 166 | + isResolved: false, |
| 167 | + }, |
| 168 | + }, |
| 169 | + }); |
| 170 | + |
| 171 | + const message = { |
| 172 | + type: "resolve_pull_request_review_thread", |
| 173 | + thread_id: "PRRT_kwDOABCD123456", |
| 174 | + }; |
| 175 | + |
| 176 | + const result = await handler(message, {}); |
| 177 | + |
| 178 | + expect(result.success).toBe(false); |
| 179 | + expect(result.error).toContain("Failed to resolve"); |
| 180 | + }); |
| 181 | + |
| 182 | + it("should default max to 10", async () => { |
| 183 | + const { main } = require("./resolve_pr_review_thread.cjs"); |
| 184 | + const defaultHandler = await main({}); |
| 185 | + |
| 186 | + const message = { |
| 187 | + type: "resolve_pull_request_review_thread", |
| 188 | + thread_id: "PRRT_kwDOABCD123456", |
| 189 | + }; |
| 190 | + |
| 191 | + // Process 10 messages successfully |
| 192 | + for (let i = 0; i < 10; i++) { |
| 193 | + const result = await defaultHandler(message, {}); |
| 194 | + expect(result.success).toBe(true); |
| 195 | + } |
| 196 | + |
| 197 | + // 11th should fail |
| 198 | + const result = await defaultHandler(message, {}); |
| 199 | + expect(result.success).toBe(false); |
| 200 | + expect(result.error).toContain("Max count of 10 reached"); |
| 201 | + }); |
| 202 | +}); |
0 commit comments