Repository navigation
Expand file tree
/
Copy pathpullRequestDiffComments.test.ts
More file actions
132 lines (131 loc) · 4.06 KB
/
Copy pathpullRequestDiffComments.test.ts
File metadata and controls
132 lines (131 loc) · 4.06 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
import { describe, expect, it } from "vite-plus/test";
import type { PullRequestReviewThread } from "@t3tools/contracts";
import type { NativeReviewDiffData } from "../review/nativeReviewDiffAdapter";
import { annotatePullRequestDiff, reviewPositionLabel } from "./pullRequestDiffComments";
const data: NativeReviewDiffData = {
rows: [
{ kind: "file", id: "header", fileId: "f", filePath: "a.ts" },
{ kind: "line", id: "old", fileId: "f", change: "delete", oldLineNumber: 2 },
{ kind: "line", id: "new", fileId: "f", change: "add", newLineNumber: 2 },
],
files: [{ id: "f", path: "a.ts", language: "typescript", additions: 1, deletions: 1 }],
commentTargetsByRowId: new Map(),
rowIdByCommentLineId: new Map(),
additions: 1,
deletions: 1,
};
const thread: PullRequestReviewThread = {
id: "t",
path: "a.ts",
line: 2,
side: "left",
isResolved: false,
isOutdated: false,
comments: [],
};
describe("PR comments in continuous native diffs", () => {
it("returns unannotated data unchanged without reading or indexing rows", () => {
const unannotated = {
...data,
get rows(): NativeReviewDiffData["rows"] {
throw new Error("Empty annotation must not read rows");
},
};
expect(annotatePullRequestDiff(unannotated, [], [], undefined)).toBe(unannotated);
expect(annotatePullRequestDiff(data, [], [], undefined).rows).toBe(data.rows);
});
it("anchors old/new lines separately despite identical line numbers", () => {
const annotated = annotatePullRequestDiff(
data,
[thread, { ...thread, id: "right", side: "right" }],
[],
"abc",
);
expect(annotated.rows.map((row) => row.id)).toEqual([
"header",
"old",
"discussion:t",
"new",
"discussion:right",
]);
});
it("keeps outdated and unloaded anchors off the current code", () => {
const annotated = annotatePullRequestDiff(
data,
[
{ ...thread, isOutdated: true },
{ ...thread, id: "missing", path: "b.ts" },
],
[
{
id: "draft",
path: "a.ts",
position: { kind: "added", newLine: 2 },
body: "Feedback",
code: "",
headSha: "old",
},
],
"new",
);
expect(annotated.rows).toEqual(data.rows);
});
it("supports file-level threads and current private drafts without changing code IDs", () => {
const annotated = annotatePullRequestDiff(
data,
[{ ...thread, line: null }],
[
{
id: "draft",
path: "a.ts",
position: { kind: "added", newLine: 2 },
body: "Feedback",
code: "",
headSha: "abc",
},
],
"abc",
);
expect(annotated.rows.map((row) => row.id)).toEqual([
"header",
"discussion:t",
"old",
"new",
"draft:draft",
]);
expect(annotated.commentTargetsByRowId).toBe(data.commentTargetsByRowId);
});
it("counts a single-comment discussion in the singular", () => {
const annotated = annotatePullRequestDiff(
data,
[
{ ...thread, commentCount: 1 },
{ ...thread, id: "right", side: "right", commentCount: 2 },
],
[],
"abc",
);
expect(
annotated.rows
.filter((row) => row.kind === "comment")
.map((row) => row.commentText?.split("\n").at(-1)),
).toEqual(["1 comment · Tap to open", "2 comments · Tap to open"]);
});
it("uses the old line number for left-side context when the line numbers differ", () => {
const position = { kind: "context" as const, side: "left" as const, oldLine: 7, newLine: 8 };
const annotated = annotatePullRequestDiff(
{
...data,
rows: [
...data.rows,
{ kind: "line", id: "context", fileId: "f", oldLineNumber: 7, newLineNumber: 8 },
],
},
[],
[{ id: "left", path: "a.ts", position, headSha: "abc", code: "", body: "Feedback" }],
"abc",
);
expect(annotated.rows.at(-1)?.id).toBe("draft:left");
expect(reviewPositionLabel(position)).toBe("Old line 7");
});
});