|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { convertIssueToLink } from "./issue-sync"; |
| 3 | +import type { GitHubIssueComment } from "./github"; |
| 4 | + |
| 5 | +function makeIssue(overrides: Record<string, unknown> = {}) { |
| 6 | + return { |
| 7 | + number: 42, |
| 8 | + title: "A bug", |
| 9 | + body: "The description.", |
| 10 | + html_url: "https://github.com/acme/web/issues/42", |
| 11 | + created_at: "2026-07-01T00:00:00Z", |
| 12 | + closed_at: null, |
| 13 | + user: { id: 1, login: "octocat" }, |
| 14 | + assignees: [], |
| 15 | + assignee: null, |
| 16 | + ...overrides, |
| 17 | + }; |
| 18 | +} |
| 19 | + |
| 20 | +const comment = (id: number): GitHubIssueComment => ({ |
| 21 | + id, |
| 22 | + body: `comment ${id}`, |
| 23 | + created_at: "2026-07-02T00:00:00Z", |
| 24 | + updated_at: "2026-07-02T00:00:00Z", |
| 25 | + user: { id: 9, login: "commenter" }, |
| 26 | + html_url: "", |
| 27 | +}); |
| 28 | + |
| 29 | +/** A page of `n` comments with ids starting at `base`. */ |
| 30 | +const commentPage = (n: number, base: number) => |
| 31 | + Array.from({ length: n }, (_, i) => comment(base + i)); |
| 32 | + |
| 33 | +/** |
| 34 | + * Fake source serving a preconfigured list of comment pages keyed by the |
| 35 | + * `page=` query param, recording every requested path. |
| 36 | + */ |
| 37 | +function makeIssueSource(commentPages: GitHubIssueComment[][]): { |
| 38 | + source: any; |
| 39 | + paths: string[]; |
| 40 | +} { |
| 41 | + const paths: string[] = []; |
| 42 | + const source = { |
| 43 | + userToContact: (user: { id: number; login: string }) => ({ |
| 44 | + email: `${user.id}+${user.login}@users.noreply.github.com`, |
| 45 | + name: user.login, |
| 46 | + source: { accountId: String(user.id) }, |
| 47 | + }), |
| 48 | + githubFetch: async (_token: string, path: string) => { |
| 49 | + paths.push(path); |
| 50 | + const pageNum = Number(new URLSearchParams(path.split("?")[1]).get("page")); |
| 51 | + const items = commentPages[pageNum - 1] ?? []; |
| 52 | + return { ok: true, json: async () => items } as unknown as Response; |
| 53 | + }, |
| 54 | + }; |
| 55 | + return { source, paths }; |
| 56 | +} |
| 57 | + |
| 58 | +const commentNotes = (link: any) => |
| 59 | + link.notes.filter((n: any) => typeof n.key === "string" && n.key.startsWith("comment-")); |
| 60 | + |
| 61 | +describe("convertIssueToLink comment fetching", () => { |
| 62 | + it("always emits a description note", async () => { |
| 63 | + const { source } = makeIssueSource([[]]); |
| 64 | + const link = await convertIssueToLink(source, "tok", "acme", "web", makeIssue(), "acme/web", false); |
| 65 | + const description = link?.notes?.find((n: any) => n.key === "description"); |
| 66 | + expect(description?.content).toBe("The description."); |
| 67 | + }); |
| 68 | + |
| 69 | + it("fetches every comment in a single request when under the page cap", async () => { |
| 70 | + const { source, paths } = makeIssueSource([commentPage(30, 0)]); |
| 71 | + const link = await convertIssueToLink(source, "tok", "acme", "web", makeIssue(), "acme/web", false); |
| 72 | + expect(commentNotes(link)).toHaveLength(30); |
| 73 | + expect(paths).toHaveLength(1); |
| 74 | + }); |
| 75 | + |
| 76 | + it("caps at MAX_COMMENT_PAGES and never issues a request past the cap", async () => { |
| 77 | + const { source, paths } = makeIssueSource([ |
| 78 | + commentPage(100, 0), |
| 79 | + commentPage(100, 100), |
| 80 | + commentPage(100, 200), |
| 81 | + commentPage(100, 300), |
| 82 | + ]); |
| 83 | + const link = await convertIssueToLink(source, "tok", "acme", "web", makeIssue(), "acme/web", false); |
| 84 | + // Keeps 3 pages (300 comments), never fetches the 4th full page. |
| 85 | + expect(commentNotes(link)).toHaveLength(300); |
| 86 | + expect(paths).toHaveLength(3); |
| 87 | + }); |
| 88 | + |
| 89 | + it("requests comments newest-first so the retained pages are the most recent", async () => { |
| 90 | + const { source, paths } = makeIssueSource([commentPage(10, 0)]); |
| 91 | + await convertIssueToLink(source, "tok", "acme", "web", makeIssue(), "acme/web", false); |
| 92 | + expect(paths[0]).toContain("/repos/acme/web/issues/42/comments"); |
| 93 | + expect(paths[0]).toContain("sort=created"); |
| 94 | + expect(paths[0]).toContain("direction=desc"); |
| 95 | + }); |
| 96 | +}); |
0 commit comments