Skip to content

Commit c069d49

Browse files
mario-camposCopilot
andcommitted
Test sticky comments using observable state
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 36f220a commit c069d49

1 file changed

Lines changed: 93 additions & 74 deletions

File tree

‎pr-checks/post-comment.test.ts‎

Lines changed: 93 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,6 @@ describe("parseOptions", async () => {
7575
/Missing required flag --marker/,
7676
);
7777
});
78-
79-
await it("rejects unknown flags using parseArgs", () => {
80-
assert.throws(() => parseOptions([...validFlags, "--unknown", "value"]), {
81-
code: "ERR_PARSE_ARGS_UNKNOWN_OPTION",
82-
});
83-
});
84-
85-
await it("rejects flags without a value using parseArgs", () => {
86-
assert.throws(() => parseOptions([...validFlags, "--body"]), {
87-
code: "ERR_PARSE_ARGS_INVALID_OPTION_VALUE",
88-
});
89-
});
9078
});
9179

9280
describe("resolveToken", async () => {
@@ -108,27 +96,34 @@ describe("resolveToken", async () => {
10896

10997
/** A minimal fake of the subset of `ApiClient` used by post-comment.ts. */
11098
function fakeClient(existingComments: Array<{ id: number; body?: string }>) {
111-
const calls: {
112-
createComment: unknown[];
113-
updateComment: unknown[];
114-
} = { createComment: [], updateComment: [] };
99+
const comments = existingComments.map((comment) => ({ ...comment }));
100+
let nextId =
101+
comments.reduce((maxId, comment) => Math.max(maxId, comment.id), 0) + 1;
115102

116103
const client = {
117-
paginate: async () => existingComments,
104+
paginate: async () => comments,
118105
rest: {
119106
issues: {
120107
listComments: () => {},
121-
createComment: async (params: unknown) => {
122-
calls.createComment.push(params);
108+
createComment: async ({ body }: { body: string }) => {
109+
comments.push({ id: nextId++, body });
123110
},
124-
updateComment: async (params: unknown) => {
125-
calls.updateComment.push(params);
111+
updateComment: async ({
112+
comment_id,
113+
body,
114+
}: {
115+
comment_id: number;
116+
body: string;
117+
}) => {
118+
const comment = comments.find(({ id }) => id === comment_id);
119+
assert.ok(comment, `Comment ${comment_id} does not exist.`);
120+
comment.body = body;
126121
},
127122
},
128123
},
129124
};
130125

131-
return { client: client as unknown as ApiClient, calls };
126+
return { client: client as unknown as ApiClient, comments };
132127
}
133128

134129
describe("findExistingComment", async () => {
@@ -167,85 +162,109 @@ const baseOptions: Options = {
167162
repository: { owner: "owner", repo: "repo" },
168163
};
169164

165+
function assertCommentContent(
166+
comment: { body?: string },
167+
marker: string,
168+
body: string,
169+
): void {
170+
assert.ok(comment.body?.startsWith(marker));
171+
assert.ok(comment.body?.endsWith(body));
172+
}
173+
170174
describe("setComment", async () => {
171175
await it("uses the supplied marker without affecting other comments", async () => {
172176
const marker = "<!-- custom-warning -->";
173-
const { client, calls } = fakeClient([
174-
{ id: 1, body: "<!-- marker -->\nunrelated warning" },
177+
const { client, comments } = fakeClient([
178+
{ id: 1, body: `${baseOptions.marker}\nunrelated warning` },
175179
{ id: 2, body: `${marker}\nold warning` },
176180
]);
177181
const options = parseOptions(withFlag("--marker", marker));
182+
178183
await setComment(client, options);
179-
assert.deepEqual(calls, {
180-
createComment: [],
181-
updateComment: [
182-
{
183-
...options.repository,
184-
comment_id: 2,
185-
body: `${marker}\nhello`,
186-
},
187-
],
184+
185+
// Only the comment with the supplied marker changes.
186+
assert.equal(comments.length, 2);
187+
assert.deepEqual(comments[0], {
188+
id: 1,
189+
body: `${baseOptions.marker}\nunrelated warning`,
188190
});
191+
assert.equal(comments[1].id, 2);
192+
assertCommentContent(comments[1], marker, options.body);
189193
});
190194

191195
await it("creates a new comment with the marker prepended", async () => {
192-
const { client, calls } = fakeClient([]);
196+
const { client, comments } = fakeClient([]);
197+
// No comment has the marker before posting.
198+
assert.equal(comments.length, 0);
199+
assert.ok(
200+
!comments.some(({ body }) => body?.startsWith(baseOptions.marker)),
201+
);
202+
193203
await setComment(client, baseOptions);
194-
assert.deepEqual(calls.createComment, [
195-
{
196-
owner: "owner",
197-
repo: "repo",
198-
issue_number: 42,
199-
body: "<!-- marker -->\nhello",
200-
},
201-
]);
202-
assert.deepEqual(calls.updateComment, []);
204+
205+
// Posting adds exactly one comment with the marker before the body.
206+
assert.equal(comments.length, 1);
207+
assertCommentContent(comments[0], baseOptions.marker, baseOptions.body);
203208
});
204209

205210
await it("creates a new comment when no existing comment matches", async () => {
206-
const { client, calls } = fakeClient([{ id: 1, body: "unrelated" }]);
211+
const originalComments = [
212+
{ id: 7, body: "unrelated" },
213+
{ id: 2, body: "another unrelated comment" },
214+
];
215+
const { client, comments } = fakeClient(originalComments);
216+
// Existing comments do not match the marker.
217+
assert.ok(
218+
!comments.some(({ body }) => body?.startsWith(baseOptions.marker)),
219+
);
220+
207221
await setComment(client, baseOptions);
208-
assert.deepEqual(calls.createComment, [
209-
{
210-
owner: "owner",
211-
repo: "repo",
212-
issue_number: 42,
213-
body: "<!-- marker -->\nhello",
214-
},
215-
]);
216-
assert.deepEqual(calls.updateComment, []);
222+
223+
// A new comment is added without changing unrelated comments.
224+
assert.equal(comments.length, originalComments.length + 1);
225+
assert.deepEqual(
226+
comments.slice(0, originalComments.length),
227+
originalComments,
228+
);
229+
const markedComments = comments.filter(({ body }) =>
230+
body?.startsWith(baseOptions.marker),
231+
);
232+
// Exactly one comment contains the expected marker and body.
233+
assert.equal(markedComments.length, 1);
234+
assertCommentContent(
235+
markedComments[0],
236+
baseOptions.marker,
237+
baseOptions.body,
238+
);
239+
// The new comment's ID does not collide with existing IDs.
240+
assert.equal(new Set(comments.map(({ id }) => id)).size, comments.length);
217241
});
218242

219243
await it("updates the existing comment when one is found", async () => {
220-
const { client, calls } = fakeClient([
221-
{ id: 7, body: "<!-- marker -->\nold" },
244+
const { client, comments } = fakeClient([
245+
{ id: 7, body: `${baseOptions.marker}\nold` },
222246
]);
247+
223248
await setComment(client, baseOptions);
224-
assert.deepEqual(calls.updateComment, [
225-
{
226-
owner: "owner",
227-
repo: "repo",
228-
comment_id: 7,
229-
body: "<!-- marker -->\nhello",
230-
},
231-
]);
232-
assert.deepEqual(calls.createComment, []);
249+
250+
// Updating preserves the comment's ID without creating a duplicate.
251+
assert.equal(comments.length, 1);
252+
assert.equal(comments[0].id, 7);
253+
assertCommentContent(comments[0], baseOptions.marker, baseOptions.body);
233254
});
234255

235256
await it("replaces a warning with the resolved state using the same marker", async () => {
236-
const { client, calls } = fakeClient([
237-
{ id: 9, body: "<!-- marker -->\nold" },
257+
const { client, comments } = fakeClient([
258+
{ id: 9, body: `${baseOptions.marker}\nold` },
238259
]);
239260
const body =
240261
"`CHANGELOG.md` is not modified in this PR. No changelog action is needed.";
262+
241263
await setComment(client, { ...baseOptions, body });
242-
assert.deepEqual(calls.updateComment, [
243-
{
244-
...baseOptions.repository,
245-
comment_id: 9,
246-
body: `${baseOptions.marker}\n${body}`,
247-
},
248-
]);
249-
assert.deepEqual(calls.createComment, []);
264+
265+
// The resolved state replaces the warning on the same marked comment.
266+
assert.equal(comments.length, 1);
267+
assert.equal(comments[0].id, 9);
268+
assertCommentContent(comments[0], baseOptions.marker, body);
250269
});
251270
});

0 commit comments

Comments
 (0)