-
Notifications
You must be signed in to change notification settings - Fork 305
Expand file tree
/
Copy pathgithubPrParser.test.ts
More file actions
209 lines (179 loc) · 6.4 KB
/
Copy pathgithubPrParser.test.ts
File metadata and controls
209 lines (179 loc) · 6.4 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { expect, test, vi, describe } from "vitest";
import { Octokit } from "octokit";
import { githubPrParser } from "./githubPrParser";
import { GitHubPullRequest } from "../types";
vi.mock("@sourcebot/shared", () => ({
createLogger: () => ({
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
}),
}));
// Minimal shape satisfying the fields accessed by githubPrParser
function makePullRequest(
overrides: Partial<{
number: number;
title: string;
body: string | null;
head_sha: string;
owner: string;
repo: string;
diff_url: string;
}> = {},
): GitHubPullRequest {
const opts = {
number: 7,
title: "My PR title",
body: "My PR description",
head_sha: "sha_abc123",
owner: "my-org",
repo: "my-repo",
diff_url: "https://github.com/my-org/my-repo/pull/7.diff",
...overrides,
};
return {
number: opts.number,
title: opts.title,
body: opts.body,
diff_url: opts.diff_url,
head: { sha: opts.head_sha, repo: {} },
base: {
repo: {
name: opts.repo,
owner: { login: opts.owner },
},
},
} as unknown as GitHubPullRequest;
}
function makeMockOctokit(diffText: string) {
return {
request: vi.fn().mockResolvedValue({ data: diffText }),
} as unknown as Octokit;
}
describe("githubPrParser", () => {
test("maps pull request metadata correctly", async () => {
const octokit = makeMockOctokit("");
const pr = makePullRequest();
const result = await githubPrParser(octokit, pr);
expect(result.title).toBe("My PR title");
expect(result.description).toBe("My PR description");
expect(result.number).toBe(7);
expect(result.head_sha).toBe("sha_abc123");
expect(result.owner).toBe("my-org");
expect(result.repo).toBe("my-repo");
expect(result.hostDomain).toBe("github.com");
});
test("uses empty string when body is null", async () => {
const octokit = makeMockOctokit("");
const pr = makePullRequest({ body: null });
const result = await githubPrParser(octokit, pr);
expect(result.description).toBe("");
});
test("fetches diff using the REST API endpoint instead of the web diff_url", async () => {
const mockRequest = vi.fn().mockResolvedValue({ data: "" });
const octokit = { request: mockRequest } as unknown as Octokit;
const pr = makePullRequest({
owner: "my-org",
repo: "my-repo",
number: 7,
});
await githubPrParser(octokit, pr);
expect(mockRequest).toHaveBeenCalledWith(
"GET /repos/{owner}/{repo}/pulls/{pull_number}",
{
owner: "my-org",
repo: "my-repo",
pull_number: 7,
mediaType: { format: "diff" },
},
);
});
test("returns empty file_diffs for an empty diff", async () => {
const octokit = makeMockOctokit("");
const pr = makePullRequest();
const result = await githubPrParser(octokit, pr);
expect(result.file_diffs).toEqual([]);
});
test("parses a unified diff with added and context lines", async () => {
const unifiedDiff = [
"diff --git a/src/foo.ts b/src/foo.ts",
"--- a/src/foo.ts",
"+++ b/src/foo.ts",
"@@ -1,2 +1,3 @@",
" context line",
"+added line",
" another context",
].join("\n");
const octokit = makeMockOctokit(unifiedDiff);
const pr = makePullRequest();
const result = await githubPrParser(octokit, pr);
expect(result.file_diffs).toHaveLength(1);
expect(result.file_diffs[0].from).toBe("src/foo.ts");
expect(result.file_diffs[0].to).toBe("src/foo.ts");
expect(result.file_diffs[0].diffs).toHaveLength(1);
});
test("newSnippet contains added lines and context", async () => {
const unifiedDiff = [
"diff --git a/src/foo.ts b/src/foo.ts",
"--- a/src/foo.ts",
"+++ b/src/foo.ts",
"@@ -1,1 +1,2 @@",
" context",
"+new line here",
].join("\n");
const octokit = makeMockOctokit(unifiedDiff);
const pr = makePullRequest();
const result = await githubPrParser(octokit, pr);
const diff = result.file_diffs[0].diffs[0];
expect(diff.newSnippet).toContain("+new line here");
expect(diff.oldSnippet).not.toContain("+new line here");
});
test("oldSnippet contains deleted lines", async () => {
const unifiedDiff = [
"diff --git a/src/foo.ts b/src/foo.ts",
"--- a/src/foo.ts",
"+++ b/src/foo.ts",
"@@ -1,2 +1,1 @@",
" context",
"-removed line",
].join("\n");
const octokit = makeMockOctokit(unifiedDiff);
const pr = makePullRequest();
const result = await githubPrParser(octokit, pr);
const diff = result.file_diffs[0].diffs[0];
expect(diff.oldSnippet).toContain("-removed line");
expect(diff.newSnippet).not.toContain("-removed line");
});
test("parses multiple files from a diff", async () => {
const unifiedDiff = [
"diff --git a/src/a.ts b/src/a.ts",
"--- a/src/a.ts",
"+++ b/src/a.ts",
"@@ -1,1 +1,2 @@",
" ctx",
"+add",
"diff --git a/src/b.ts b/src/b.ts",
"--- a/src/b.ts",
"+++ b/src/b.ts",
"@@ -1,2 +1,1 @@",
" ctx",
"-remove",
].join("\n");
const octokit = makeMockOctokit(unifiedDiff);
const pr = makePullRequest();
const result = await githubPrParser(octokit, pr);
expect(result.file_diffs).toHaveLength(2);
expect(result.file_diffs[0].to).toBe("src/a.ts");
expect(result.file_diffs[1].to).toBe("src/b.ts");
});
test("throws when the diff request fails", async () => {
const octokit = {
request: vi.fn().mockRejectedValue(new Error("Network error")),
} as unknown as Octokit;
const pr = makePullRequest();
await expect(githubPrParser(octokit, pr)).rejects.toThrow(
"Network error",
);
});
});