Repository navigation
Expand file tree
/
Copy pathsync-issue.test.ts
More file actions
144 lines (126 loc) · 4.25 KB
/
Copy pathsync-issue.test.ts
File metadata and controls
144 lines (126 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { describe, it } from "node:test";
import assert from "node:assert/strict";
import { syncIssue } from "../src/sync-issue.js";
import type { GitHubApi } from "../src/api.js";
import type { Issue, PullRequest } from "../src/types.js";
// ---------------------------------------------------------------------------
// Mock API
// ---------------------------------------------------------------------------
function makeMockApi(issue: Issue): GitHubApi & {
added: string[][];
removed: string[];
comments: string[];
} {
const added: string[][] = [];
const removed: string[] = [];
const comments: string[] = [];
return {
added,
removed,
comments,
async getIssue() {
return issue;
},
async getPullRequest(): Promise<PullRequest> {
throw new Error("not used");
},
async listIssuesWithLabelPrefix(): Promise<Issue[]> {
throw new Error("not used");
},
async addLabels(_target, labels) {
added.push(labels);
},
async removeLabel(_target, label) {
removed.push(label);
},
async addComment(_target, body) {
comments.push(body);
},
};
}
function makeIssue(overrides: Partial<Issue> & { number: number }): Issue {
return {
title: "Test issue",
html_url: `https://github.com/owner/repo/issues/${overrides.number}`,
state: "open",
labels: [],
assignees: [],
...overrides,
};
}
function labelNames(...names: string[]): { name: string }[] {
return names.map((name) => ({ name }));
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
describe("syncIssue", () => {
it("returns empty patch and makes no API calls when issue is already in sync", async () => {
const issue = makeIssue({
number: 42,
labels: labelNames("bounty: 💰 $500", "bounty", "bounty: claimed"),
assignees: [{ login: "alice" }],
});
const api = makeMockApi(issue);
const patch = await syncIssue({ issueNumber: 42, api });
assert.deepEqual(patch.ops, []);
assert.deepEqual(api.added, []);
assert.deepEqual(api.removed, []);
});
it("adds generic bounty label when value label is present but generic is missing", async () => {
const issue = makeIssue({
number: 42,
labels: labelNames("bounty: 💰 $300"),
});
const api = makeMockApi(issue);
const patch = await syncIssue({ issueNumber: 42, api });
assert.equal(patch.ops.length, 1);
assert.deepEqual(api.added, [["bounty"]]);
assert.deepEqual(api.removed, []);
});
it("adds both generic and claimed when value label present and issue assigned", async () => {
const issue = makeIssue({
number: 42,
labels: labelNames("bounty: 💰 $800"),
assignees: [{ login: "bob" }],
});
const api = makeMockApi(issue);
await syncIssue({ issueNumber: 42, api });
assert.equal(api.added.length, 1);
assert.deepEqual(api.added[0]!.sort(), ["bounty", "bounty: claimed"].sort());
});
it("removes claimed when last assignee is removed", async () => {
const issue = makeIssue({
number: 42,
labels: labelNames("bounty: 💰 $500", "bounty", "bounty: claimed"),
assignees: [],
});
const api = makeMockApi(issue);
await syncIssue({ issueNumber: 42, api });
assert.deepEqual(api.removed, ["bounty: claimed"]);
assert.deepEqual(api.added, []);
});
it("removes both generic and claimed when value label is removed", async () => {
const issue = makeIssue({
number: 42,
labels: labelNames("bounty", "bounty: claimed"),
assignees: [{ login: "alice" }],
});
const api = makeMockApi(issue);
await syncIssue({ issueNumber: 42, api });
assert.deepEqual(api.removed.sort(), ["bounty", "bounty: claimed"].sort());
assert.deepEqual(api.added, []);
});
it("does a single batched addLabels call for multiple adds", async () => {
const issue = makeIssue({
number: 42,
labels: labelNames("bounty: 💰 $100"),
assignees: [{ login: "carol" }],
});
const api = makeMockApi(issue);
await syncIssue({ issueNumber: 42, api });
// One call, two labels
assert.equal(api.added.length, 1);
assert.equal(api.added[0]!.length, 2);
});
});