|
| 1 | +// packages/integrations/github/src/issue-writes.test.ts |
| 2 | +import { describe, expect, test } from "bun:test" |
| 3 | +import type { Octokit } from "@octokit/rest" |
| 4 | +import { |
| 5 | + addAssignees, |
| 6 | + removeAssignees, |
| 7 | + updateIssueMilestone, |
| 8 | + updateIssueState, |
| 9 | + updateIssueTitle, |
| 10 | +} from "./issue-writes" |
| 11 | + |
| 12 | +/** Build a minimal fake Octokit that records calls */ |
| 13 | +function makeFakeOctokit() { |
| 14 | + const calls: { |
| 15 | + update: Array<Record<string, unknown>> |
| 16 | + addAssignees: Array<Record<string, unknown>> |
| 17 | + removeAssignees: Array<Record<string, unknown>> |
| 18 | + } = { update: [], addAssignees: [], removeAssignees: [] } |
| 19 | + |
| 20 | + const octokit = { |
| 21 | + rest: { |
| 22 | + issues: { |
| 23 | + update: async (args: Record<string, unknown>) => { |
| 24 | + calls.update.push(args) |
| 25 | + return { data: {} } |
| 26 | + }, |
| 27 | + addAssignees: async (args: Record<string, unknown>) => { |
| 28 | + calls.addAssignees.push(args) |
| 29 | + return { data: {} } |
| 30 | + }, |
| 31 | + removeAssignees: async (args: Record<string, unknown>) => { |
| 32 | + calls.removeAssignees.push(args) |
| 33 | + return { data: {} } |
| 34 | + }, |
| 35 | + }, |
| 36 | + }, |
| 37 | + } as unknown as Octokit |
| 38 | + |
| 39 | + return { octokit, calls } |
| 40 | +} |
| 41 | + |
| 42 | +describe("updateIssueTitle", () => { |
| 43 | + test("calls update with owner, repo, issue_number, title", async () => { |
| 44 | + const { octokit, calls } = makeFakeOctokit() |
| 45 | + await updateIssueTitle(octokit, "acme", "frontend", 42, "New title") |
| 46 | + expect(calls.update.length).toBe(1) |
| 47 | + expect(calls.update[0]).toMatchObject({ |
| 48 | + owner: "acme", |
| 49 | + repo: "frontend", |
| 50 | + issue_number: 42, |
| 51 | + title: "New title", |
| 52 | + }) |
| 53 | + }) |
| 54 | +}) |
| 55 | + |
| 56 | +describe("updateIssueMilestone", () => { |
| 57 | + test("calls update with milestone number", async () => { |
| 58 | + const { octokit, calls } = makeFakeOctokit() |
| 59 | + await updateIssueMilestone(octokit, "acme", "frontend", 42, 7) |
| 60 | + expect(calls.update.length).toBe(1) |
| 61 | + expect(calls.update[0]).toMatchObject({ |
| 62 | + owner: "acme", |
| 63 | + repo: "frontend", |
| 64 | + issue_number: 42, |
| 65 | + milestone: 7, |
| 66 | + }) |
| 67 | + }) |
| 68 | + |
| 69 | + test("calls update with null to clear milestone", async () => { |
| 70 | + const { octokit, calls } = makeFakeOctokit() |
| 71 | + await updateIssueMilestone(octokit, "acme", "frontend", 42, null) |
| 72 | + expect(calls.update.length).toBe(1) |
| 73 | + expect(calls.update[0]).toMatchObject({ |
| 74 | + owner: "acme", |
| 75 | + repo: "frontend", |
| 76 | + issue_number: 42, |
| 77 | + milestone: null, |
| 78 | + }) |
| 79 | + }) |
| 80 | +}) |
| 81 | + |
| 82 | +describe("addAssignees", () => { |
| 83 | + test("calls addAssignees with provided logins", async () => { |
| 84 | + const { octokit, calls } = makeFakeOctokit() |
| 85 | + await addAssignees(octokit, "acme", "frontend", 42, ["alice", "bob"]) |
| 86 | + expect(calls.addAssignees.length).toBe(1) |
| 87 | + expect(calls.addAssignees[0]).toMatchObject({ |
| 88 | + owner: "acme", |
| 89 | + repo: "frontend", |
| 90 | + issue_number: 42, |
| 91 | + assignees: ["alice", "bob"], |
| 92 | + }) |
| 93 | + }) |
| 94 | + |
| 95 | + test("no-op on empty logins", async () => { |
| 96 | + const { octokit, calls } = makeFakeOctokit() |
| 97 | + await addAssignees(octokit, "acme", "frontend", 42, []) |
| 98 | + expect(calls.addAssignees.length).toBe(0) |
| 99 | + }) |
| 100 | +}) |
| 101 | + |
| 102 | +describe("removeAssignees", () => { |
| 103 | + test("calls removeAssignees with provided logins", async () => { |
| 104 | + const { octokit, calls } = makeFakeOctokit() |
| 105 | + await removeAssignees(octokit, "acme", "frontend", 42, ["carol"]) |
| 106 | + expect(calls.removeAssignees.length).toBe(1) |
| 107 | + expect(calls.removeAssignees[0]).toMatchObject({ |
| 108 | + owner: "acme", |
| 109 | + repo: "frontend", |
| 110 | + issue_number: 42, |
| 111 | + assignees: ["carol"], |
| 112 | + }) |
| 113 | + }) |
| 114 | + |
| 115 | + test("no-op on empty logins", async () => { |
| 116 | + const { octokit, calls } = makeFakeOctokit() |
| 117 | + await removeAssignees(octokit, "acme", "frontend", 42, []) |
| 118 | + expect(calls.removeAssignees.length).toBe(0) |
| 119 | + }) |
| 120 | +}) |
| 121 | + |
| 122 | +describe("updateIssueState", () => { |
| 123 | + test("closed with state_reason passed through", async () => { |
| 124 | + const { octokit, calls } = makeFakeOctokit() |
| 125 | + await updateIssueState(octokit, "acme", "frontend", 42, { |
| 126 | + state: "closed", |
| 127 | + stateReason: "not_planned", |
| 128 | + }) |
| 129 | + expect(calls.update.length).toBe(1) |
| 130 | + expect(calls.update[0]).toMatchObject({ |
| 131 | + owner: "acme", |
| 132 | + repo: "frontend", |
| 133 | + issue_number: 42, |
| 134 | + state: "closed", |
| 135 | + state_reason: "not_planned", |
| 136 | + }) |
| 137 | + }) |
| 138 | + |
| 139 | + test("open with reopened reason", async () => { |
| 140 | + const { octokit, calls } = makeFakeOctokit() |
| 141 | + await updateIssueState(octokit, "acme", "frontend", 42, { |
| 142 | + state: "open", |
| 143 | + stateReason: "reopened", |
| 144 | + }) |
| 145 | + expect(calls.update.length).toBe(1) |
| 146 | + expect(calls.update[0]).toMatchObject({ |
| 147 | + owner: "acme", |
| 148 | + repo: "frontend", |
| 149 | + issue_number: 42, |
| 150 | + state: "open", |
| 151 | + state_reason: "reopened", |
| 152 | + }) |
| 153 | + }) |
| 154 | + |
| 155 | + test("open with null stateReason omits state_reason from call", async () => { |
| 156 | + const { octokit, calls } = makeFakeOctokit() |
| 157 | + await updateIssueState(octokit, "acme", "frontend", 42, { |
| 158 | + state: "open", |
| 159 | + stateReason: null, |
| 160 | + }) |
| 161 | + expect(calls.update.length).toBe(1) |
| 162 | + expect(calls.update[0]?.state).toBe("open") |
| 163 | + expect(calls.update[0]?.state_reason).toBeUndefined() |
| 164 | + }) |
| 165 | +}) |
0 commit comments