-
Notifications
You must be signed in to change notification settings - Fork 501
Expand file tree
/
Copy pathhide_comment.test.cjs
More file actions
271 lines (209 loc) · 9.28 KB
/
Copy pathhide_comment.test.cjs
File metadata and controls
271 lines (209 loc) · 9.28 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// @ts-check
import { describe, it, expect, beforeEach, vi } from "vitest";
const mockCore = {
info: vi.fn(),
warning: vi.fn(),
error: vi.fn(),
setFailed: vi.fn(),
setOutput: vi.fn(),
};
const mockGithub = {
graphql: vi.fn(),
rest: {
issues: {
getComment: vi.fn(),
},
},
};
const mockContext = {
eventName: "issue_comment",
repo: { owner: "testowner", repo: "testrepo" },
payload: { issue: { number: 42 } },
};
global.core = mockCore;
global.github = mockGithub;
global.context = mockContext;
async function loadModule() {
const { main } = await import("./hide_comment.cjs?" + Date.now());
return { main };
}
describe("hide_comment.cjs", () => {
beforeEach(() => {
vi.resetAllMocks();
delete process.env.GH_AW_SAFE_OUTPUTS_STAGED;
// Default successful graphql mock
mockGithub.graphql.mockResolvedValue({
minimizeComment: { minimizedComment: { isMinimized: true } },
});
mockGithub.rest.issues.getComment.mockResolvedValue({
data: { node_id: "IC_kwDOABCD123456" },
});
});
describe("main factory", () => {
it("should return a handler function", async () => {
const { main } = await loadModule();
const handler = await main();
expect(typeof handler).toBe("function");
});
it("should log configuration on initialization", async () => {
const { main } = await loadModule();
await main({ max: 3 });
expect(mockCore.info).toHaveBeenCalledWith(expect.stringContaining("max=3"));
});
it("should log allowed reasons when configured", async () => {
const { main } = await loadModule();
await main({ allowed_reasons: ["SPAM", "ABUSE"] });
expect(mockCore.info).toHaveBeenCalledWith(expect.stringContaining("SPAM, ABUSE"));
});
});
describe("handleHideComment", () => {
it("should hide a comment successfully", async () => {
const { main } = await loadModule();
const handler = await main();
const result = await handler({ comment_id: "IC_kwDOABCD123456", reason: "SPAM" }, {});
expect(result.success).toBe(true);
expect(result.comment_id).toBe("IC_kwDOABCD123456");
expect(result.is_hidden).toBe(true);
});
it("should use SPAM as default reason when not provided", async () => {
const { main } = await loadModule();
const handler = await main();
await handler({ comment_id: "IC_kwDOABCD123456" }, {});
expect(mockGithub.graphql).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({ classifier: "SPAM" }));
});
it("should normalize reason to uppercase", async () => {
const { main } = await loadModule();
const handler = await main();
await handler({ comment_id: "IC_kwDOABCD123456", reason: "abuse" }, {});
expect(mockGithub.graphql).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({ classifier: "ABUSE" }));
});
it("should pass through LOW_QUALITY reason", async () => {
const { main } = await loadModule();
const handler = await main();
await handler({ comment_id: "IC_kwDOABCD123456", reason: "low_quality" }, {});
expect(mockGithub.graphql).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({ classifier: "LOW_QUALITY" }));
});
it("should fail when comment_id is missing", async () => {
const { main } = await loadModule();
const handler = await main();
const result = await handler({ reason: "SPAM" }, {});
expect(result.success).toBe(false);
expect(result.error).toContain("comment_id is required");
expect(mockGithub.graphql).not.toHaveBeenCalled();
});
it("should resolve numeric comment_id to a GraphQL node ID", async () => {
const { main } = await loadModule();
const handler = await main();
const result = await handler({ comment_id: 12345, reason: "SPAM" }, {});
expect(result.success).toBe(true);
expect(mockGithub.rest.issues.getComment).toHaveBeenCalledWith({
owner: "testowner",
repo: "testrepo",
comment_id: 12345,
});
expect(mockGithub.graphql).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({ nodeId: "IC_kwDOABCD123456" }));
expect(result.comment_id).toBe("IC_kwDOABCD123456");
});
it("should resolve numeric string comment_id to a GraphQL node ID", async () => {
const { main } = await loadModule();
const handler = await main();
const result = await handler({ comment_id: "12345", reason: "SPAM" }, {});
expect(result.success).toBe(true);
expect(mockGithub.rest.issues.getComment).toHaveBeenCalledWith({
owner: "testowner",
repo: "testrepo",
comment_id: 12345,
});
expect(result.comment_id).toBe("IC_kwDOABCD123456");
});
it("should fail when comment_id is an empty string", async () => {
const { main } = await loadModule();
const handler = await main();
const result = await handler({ comment_id: "", reason: "SPAM" }, {});
expect(result.success).toBe(false);
expect(result.error).toContain("comment_id is required");
expect(mockGithub.graphql).not.toHaveBeenCalled();
});
it("should fail when comment_id is a whitespace-only string", async () => {
const { main } = await loadModule();
const handler = await main();
const result = await handler({ comment_id: " ", reason: "SPAM" }, {});
expect(result.success).toBe(false);
expect(result.error).toContain("comment_id is required");
expect(mockGithub.graphql).not.toHaveBeenCalled();
});
it("should fail for invalid numeric comment_id", async () => {
const { main } = await loadModule();
const handler = await main();
const result = await handler({ comment_id: 0, reason: "SPAM" }, {});
expect(result.success).toBe(false);
expect(result.error).toBe("ERR_VALIDATION: comment_id must be a GraphQL node ID string or a positive numeric REST comment ID");
expect(mockGithub.graphql).not.toHaveBeenCalled();
});
it("should fail when repo context is unavailable for numeric comment_id", async () => {
global.context = { eventName: "issue_comment", payload: {} };
const { main } = await loadModule();
const handler = await main();
const result = await handler({ comment_id: 12345, reason: "SPAM" }, {});
global.context = mockContext;
expect(result.success).toBe(false);
expect(result.error).toContain("repository context");
expect(mockGithub.rest.issues.getComment).not.toHaveBeenCalled();
expect(mockGithub.graphql).not.toHaveBeenCalled();
});
it("should enforce max count limit", async () => {
const { main } = await loadModule();
const handler = await main({ max: 2 });
await handler({ comment_id: "IC_1" }, {});
await handler({ comment_id: "IC_2" }, {});
const result = await handler({ comment_id: "IC_3" }, {});
expect(result.success).toBe(false);
expect(result.error).toContain("Max count");
expect(mockGithub.graphql).toHaveBeenCalledTimes(2);
});
it("should reject reason not in allowed-reasons list", async () => {
const { main } = await loadModule();
const handler = await main({ allowed_reasons: ["SPAM", "ABUSE"] });
const result = await handler({ comment_id: "IC_kwDOABCD123456", reason: "OFF_TOPIC" }, {});
expect(result.success).toBe(false);
expect(result.error).toContain("not in allowed-reasons list");
expect(mockGithub.graphql).not.toHaveBeenCalled();
});
it("should accept allowed reason case-insensitively", async () => {
const { main } = await loadModule();
const handler = await main({ allowed_reasons: ["spam", "abuse"] });
const result = await handler({ comment_id: "IC_kwDOABCD123456", reason: "SPAM" }, {});
expect(result.success).toBe(true);
});
it("should handle GraphQL API errors gracefully", async () => {
const { main } = await loadModule();
mockGithub.graphql.mockRejectedValue(new Error("GraphQL error: Forbidden"));
const handler = await main();
const result = await handler({ comment_id: "IC_kwDOABCD123456" }, {});
expect(result.success).toBe(false);
expect(result.error).toContain("GraphQL error");
expect(mockCore.error).toHaveBeenCalled();
});
it("should return failure when comment is not minimized", async () => {
const { main } = await loadModule();
mockGithub.graphql.mockResolvedValue({
minimizeComment: { minimizedComment: { isMinimized: false } },
});
const handler = await main();
const result = await handler({ comment_id: "IC_kwDOABCD123456" }, {});
expect(result.success).toBe(false);
expect(result.error).toContain("Failed to hide comment");
});
it("should return staged preview in staged mode", async () => {
process.env.GH_AW_SAFE_OUTPUTS_STAGED = "true";
const { main } = await loadModule();
const handler = await main();
const result = await handler({ comment_id: "IC_kwDOABCD123456", reason: "ABUSE" }, {});
expect(result.success).toBe(true);
expect(result.staged).toBe(true);
expect(result.previewInfo?.commentId).toBe("IC_kwDOABCD123456");
expect(result.previewInfo?.reason).toBe("ABUSE");
expect(mockGithub.graphql).not.toHaveBeenCalled();
});
});
});