|
| 1 | +// packages/integrations/github/src/comments.test.ts |
| 2 | +import { describe, test, expect, mock } from "bun:test" |
| 3 | +import { |
| 4 | + createIssueComment, |
| 5 | + updateIssueComment, |
| 6 | + deleteIssueComment, |
| 7 | + listIssueComments, |
| 8 | +} from "./comments" |
| 9 | +import type { Octokit } from "@octokit/rest" |
| 10 | + |
| 11 | +function makeComment(id: number, body: string) { |
| 12 | + return { |
| 13 | + id, |
| 14 | + body, |
| 15 | + user: { id: 42, login: "test-user", avatar_url: "https://example.com/avatar.png" }, |
| 16 | + created_at: "2026-01-01T00:00:00Z", |
| 17 | + updated_at: "2026-01-02T00:00:00Z", |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +describe("createIssueComment", () => { |
| 22 | + test("returns normalized shape from API response", async () => { |
| 23 | + const createComment = mock(async () => ({ data: makeComment(100, "Hello!") })) |
| 24 | + const client = { |
| 25 | + rest: { issues: { createComment } }, |
| 26 | + } as unknown as Octokit |
| 27 | + |
| 28 | + const result = await createIssueComment(client, "owner", "repo", 5, "Hello!") |
| 29 | + |
| 30 | + expect(createComment).toHaveBeenCalledWith({ |
| 31 | + owner: "owner", |
| 32 | + repo: "repo", |
| 33 | + issue_number: 5, |
| 34 | + body: "Hello!", |
| 35 | + }) |
| 36 | + expect(result).toEqual({ |
| 37 | + id: 100, |
| 38 | + body: "Hello!", |
| 39 | + user: { id: 42, login: "test-user", avatar_url: "https://example.com/avatar.png" }, |
| 40 | + createdAt: "2026-01-01T00:00:00Z", |
| 41 | + updatedAt: "2026-01-02T00:00:00Z", |
| 42 | + }) |
| 43 | + }) |
| 44 | + |
| 45 | + test("handles null user gracefully", async () => { |
| 46 | + const createComment = mock(async () => ({ |
| 47 | + data: { |
| 48 | + id: 200, |
| 49 | + body: "test", |
| 50 | + user: null, |
| 51 | + created_at: "2026-01-01T00:00:00Z", |
| 52 | + updated_at: "2026-01-01T00:00:00Z", |
| 53 | + }, |
| 54 | + })) |
| 55 | + const client = { |
| 56 | + rest: { issues: { createComment } }, |
| 57 | + } as unknown as Octokit |
| 58 | + |
| 59 | + const result = await createIssueComment(client, "owner", "repo", 1, "test") |
| 60 | + expect(result.user).toEqual({ id: 0, login: "", avatar_url: null }) |
| 61 | + }) |
| 62 | +}) |
| 63 | + |
| 64 | +describe("updateIssueComment", () => { |
| 65 | + test("calls updateComment with comment_id + body", async () => { |
| 66 | + const updateComment = mock(async () => ({ data: {} })) |
| 67 | + const client = { |
| 68 | + rest: { issues: { updateComment } }, |
| 69 | + } as unknown as Octokit |
| 70 | + |
| 71 | + await updateIssueComment(client, "owner", "repo", 999, "New body") |
| 72 | + |
| 73 | + expect(updateComment).toHaveBeenCalledWith({ |
| 74 | + owner: "owner", |
| 75 | + repo: "repo", |
| 76 | + comment_id: 999, |
| 77 | + body: "New body", |
| 78 | + }) |
| 79 | + }) |
| 80 | +}) |
| 81 | + |
| 82 | +describe("deleteIssueComment", () => { |
| 83 | + test("calls deleteComment with comment_id", async () => { |
| 84 | + const deleteComment = mock(async () => ({ data: {} })) |
| 85 | + const client = { |
| 86 | + rest: { issues: { deleteComment } }, |
| 87 | + } as unknown as Octokit |
| 88 | + |
| 89 | + await deleteIssueComment(client, "owner", "repo", 888) |
| 90 | + |
| 91 | + expect(deleteComment).toHaveBeenCalledWith({ |
| 92 | + owner: "owner", |
| 93 | + repo: "repo", |
| 94 | + comment_id: 888, |
| 95 | + }) |
| 96 | + }) |
| 97 | +}) |
| 98 | + |
| 99 | +describe("listIssueComments", () => { |
| 100 | + test("flattens paginated results", async () => { |
| 101 | + const page1 = [makeComment(1, "First"), makeComment(2, "Second")] |
| 102 | + const page2 = [makeComment(3, "Third")] |
| 103 | + |
| 104 | + // Build an async iterator that yields two pages |
| 105 | + const pages = [{ data: page1 }, { data: page2 }] |
| 106 | + let pageIdx = 0 |
| 107 | + const asyncIterator = { |
| 108 | + [Symbol.asyncIterator]() { |
| 109 | + return { |
| 110 | + async next() { |
| 111 | + if (pageIdx < pages.length) { |
| 112 | + return { value: pages[pageIdx++], done: false } |
| 113 | + } |
| 114 | + return { value: undefined, done: true } |
| 115 | + }, |
| 116 | + } |
| 117 | + }, |
| 118 | + } |
| 119 | + |
| 120 | + const paginateIterator = mock(() => asyncIterator) |
| 121 | + const listComments = mock(async () => ({ data: [] })) |
| 122 | + const client = { |
| 123 | + rest: { issues: { listComments } }, |
| 124 | + paginate: { iterator: paginateIterator }, |
| 125 | + } as unknown as Octokit |
| 126 | + |
| 127 | + const results = await listIssueComments(client, "owner", "repo", 7) |
| 128 | + |
| 129 | + expect(results).toHaveLength(3) |
| 130 | + expect(results[0].id).toBe(1) |
| 131 | + expect(results[1].id).toBe(2) |
| 132 | + expect(results[2].id).toBe(3) |
| 133 | + expect(paginateIterator).toHaveBeenCalledWith(listComments, { |
| 134 | + owner: "owner", |
| 135 | + repo: "repo", |
| 136 | + issue_number: 7, |
| 137 | + per_page: 100, |
| 138 | + }) |
| 139 | + }) |
| 140 | + |
| 141 | + test("returns empty array when no comments", async () => { |
| 142 | + const asyncIterator = { |
| 143 | + [Symbol.asyncIterator]() { |
| 144 | + return { |
| 145 | + async next() { |
| 146 | + return { value: undefined, done: true } |
| 147 | + }, |
| 148 | + } |
| 149 | + }, |
| 150 | + } |
| 151 | + |
| 152 | + const paginateIterator = mock(() => asyncIterator) |
| 153 | + const listComments = mock(async () => ({ data: [] })) |
| 154 | + const client = { |
| 155 | + rest: { issues: { listComments } }, |
| 156 | + paginate: { iterator: paginateIterator }, |
| 157 | + } as unknown as Octokit |
| 158 | + |
| 159 | + const results = await listIssueComments(client, "owner", "repo", 1) |
| 160 | + expect(results).toHaveLength(0) |
| 161 | + }) |
| 162 | +}) |
0 commit comments