|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import test from "node:test"; |
| 3 | +import { bodyOf, markerOf, upsertComment } from "./upsertComment.cjs"; |
| 4 | + |
| 5 | +const bot = "github-actions[bot]"; |
| 6 | + |
| 7 | +/** |
| 8 | + * A fake of the comments API that records the calls that change comments. |
| 9 | + * |
| 10 | + * @param {{ id: number, body?: string, user: { login: string } | null }[]} comments |
| 11 | + */ |
| 12 | +function fakeClient(comments) { |
| 13 | + /** @type {unknown[]} */ |
| 14 | + const calls = []; |
| 15 | + return { |
| 16 | + calls, |
| 17 | + listComments: async () => comments, |
| 18 | + createComment: async (/** @type {any} */ params) => { |
| 19 | + calls.push({ create: params }); |
| 20 | + }, |
| 21 | + updateComment: async (/** @type {any} */ params) => { |
| 22 | + calls.push({ update: params }); |
| 23 | + }, |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +const options = { |
| 28 | + owner: "rescript-lang", |
| 29 | + repo: "rescript", |
| 30 | + issueNumber: 42, |
| 31 | + tag: "compiler-benchmarks", |
| 32 | + text: "## Report\n\nAll good.\n\n", |
| 33 | + author: bot, |
| 34 | +}; |
| 35 | + |
| 36 | +test("creates the comment when there is none", async () => { |
| 37 | + const client = fakeClient([ |
| 38 | + { id: 1, body: "Looks good to me.", user: { login: "someone" } }, |
| 39 | + ]); |
| 40 | + assert.equal(await upsertComment(client, options), "created"); |
| 41 | + assert.deepEqual(client.calls, [ |
| 42 | + { |
| 43 | + create: { |
| 44 | + owner: "rescript-lang", |
| 45 | + repo: "rescript", |
| 46 | + issue_number: 42, |
| 47 | + body: `## Report\n\nAll good.\n\n${markerOf("compiler-benchmarks")}\n`, |
| 48 | + }, |
| 49 | + }, |
| 50 | + ]); |
| 51 | +}); |
| 52 | + |
| 53 | +test("updates the workflow's comment with the same tag", async () => { |
| 54 | + const client = fakeClient([ |
| 55 | + { id: 1, body: bodyOf("Old", "semantic-diff"), user: { login: bot } }, |
| 56 | + { id: 2, body: bodyOf("Old", "compiler-benchmarks"), user: { login: bot } }, |
| 57 | + ]); |
| 58 | + assert.equal(await upsertComment(client, options), "updated"); |
| 59 | + assert.deepEqual(client.calls, [ |
| 60 | + { |
| 61 | + update: { |
| 62 | + owner: "rescript-lang", |
| 63 | + repo: "rescript", |
| 64 | + comment_id: 2, |
| 65 | + body: bodyOf(options.text, "compiler-benchmarks"), |
| 66 | + }, |
| 67 | + }, |
| 68 | + ]); |
| 69 | +}); |
| 70 | + |
| 71 | +test("leaves an unchanged comment alone", async () => { |
| 72 | + const client = fakeClient([ |
| 73 | + { |
| 74 | + id: 2, |
| 75 | + body: bodyOf(options.text, "compiler-benchmarks"), |
| 76 | + user: { login: bot }, |
| 77 | + }, |
| 78 | + ]); |
| 79 | + assert.equal(await upsertComment(client, options), "unchanged"); |
| 80 | + assert.deepEqual(client.calls, []); |
| 81 | +}); |
| 82 | + |
| 83 | +test("ignores a person's comment that quotes the marker", async () => { |
| 84 | + const client = fakeClient([ |
| 85 | + { id: 3, body: bodyOf("Quoted", "compiler-benchmarks"), user: null }, |
| 86 | + { |
| 87 | + id: 4, |
| 88 | + body: bodyOf("Quoted", "compiler-benchmarks"), |
| 89 | + user: { login: "someone" }, |
| 90 | + }, |
| 91 | + ]); |
| 92 | + assert.equal(await upsertComment(client, options), "created"); |
| 93 | +}); |
| 94 | + |
| 95 | +test("ignores a tag that only starts like this one", async () => { |
| 96 | + const client = fakeClient([ |
| 97 | + { |
| 98 | + id: 5, |
| 99 | + body: bodyOf("Old", "compiler-benchmarks-old"), |
| 100 | + user: { login: bot }, |
| 101 | + }, |
| 102 | + ]); |
| 103 | + assert.equal(await upsertComment(client, options), "created"); |
| 104 | +}); |
| 105 | + |
| 106 | +test("rejects tags that aren't words and missing pull request numbers", async () => { |
| 107 | + const client = fakeClient([]); |
| 108 | + await assert.rejects( |
| 109 | + upsertComment(client, { ...options, tag: 'a" -->' }), |
| 110 | + /isn't a word/, |
| 111 | + ); |
| 112 | + await assert.rejects( |
| 113 | + upsertComment(client, { ...options, issueNumber: Number("") }), |
| 114 | + /isn't a pull request number/, |
| 115 | + ); |
| 116 | + assert.deepEqual(client.calls, []); |
| 117 | +}); |
0 commit comments