blob: 713e6dfc1a79a857ff597cf7d80a06e582529e50 [file] [log] [blame]
Dhruv Srivastava254cb722021-03-08 21:15:37 +01001/**
2 * @license
Ben Rohlfs94fcbbc2022-05-27 10:45:03 +02003 * Copyright 2020 Google LLC
4 * SPDX-License-Identifier: Apache-2.0
Dhruv Srivastava254cb722021-03-08 21:15:37 +01005 */
Frank Bordenbe9451a2022-09-12 15:44:29 +02006import '../test/common-test-setup';
Dhruv Srivastava254cb722021-03-08 21:15:37 +01007import {
8 isUnresolved,
9 getPatchRangeForCommentUrl,
10 createCommentThreads,
11 sortComments,
Milutin Kristofic1d219672022-06-21 14:57:25 +020012 USER_SUGGESTION_START_PATTERN,
13 hasUserSuggestion,
14 getUserSuggestion,
15 getContentInCommentRange,
16 createUserFixSuggestion,
Ben Rohlfsd0207e52022-08-04 22:02:00 +020017 PROVIDED_FIX_ID,
Dhruv Srivastava2a83d322022-08-09 16:49:01 +020018 getMentionedThreads,
Ben Rohlfs7c1f9d82023-04-20 10:28:28 +020019 isNewThread,
20 createNew,
Ben Rohlfsd56d7c92021-03-12 10:03:34 +010021} from './comment-util';
Dhruv Srivastava2a83d322022-08-09 16:49:01 +020022import {
23 createAccountWithEmail,
24 createComment,
25 createCommentThread,
26} from '../test/test-data-generators';
Ben Rohlfs05750b92021-10-29 08:23:08 +020027import {CommentSide} from '../constants/constants';
Dhruv Srivastava254cb722021-03-08 21:15:37 +010028import {
Ben Rohlfsba440822023-04-11 18:08:03 +020029 Comment,
30 DraftInfo,
Ben Rohlfs610bb4f2023-04-17 12:34:35 +020031 SavingState,
Ben Rohlfs58267b72022-05-27 15:59:18 +020032 PARENT,
Dhruv Srivastava1c41a1b72021-03-11 16:24:53 +010033 RevisionPatchSetNum,
Dhruv Srivastava254cb722021-03-08 21:15:37 +010034 Timestamp,
35 UrlEncodedCommentId,
Ben Rohlfsd56d7c92021-03-12 10:03:34 +010036} from '../types/common';
Frank Bordene1ba8212022-08-29 15:20:01 +020037import {assert} from '@open-wc/testing';
Ben Rohlfsb9956102023-05-12 17:07:06 +020038import {FILE} from '../api/diff';
Dhruv Srivastava254cb722021-03-08 21:15:37 +010039
40suite('comment-util', () => {
41 test('isUnresolved', () => {
42 const thread = createCommentThread([createComment()]);
43
Dhruv Srivastava254cb722021-03-08 21:15:37 +010044 assert.isFalse(isUnresolved(thread));
45
46 assert.isTrue(
47 isUnresolved({
48 ...thread,
49 comments: [{...createComment(), unresolved: true}],
50 })
51 );
52 assert.isFalse(
53 isUnresolved({
54 ...thread,
55 comments: [{...createComment(), unresolved: false}],
56 })
57 );
58 assert.isTrue(
59 isUnresolved({
60 ...thread,
61 comments: [
62 {...createComment(), unresolved: false},
63 {...createComment(), unresolved: true},
64 ],
65 })
66 );
67 assert.isFalse(
68 isUnresolved({
69 ...thread,
70 comments: [
71 {...createComment(), unresolved: true},
72 {...createComment(), unresolved: false},
73 ],
74 })
75 );
76 });
77
Ben Rohlfs7c1f9d82023-04-20 10:28:28 +020078 test('isNewThread', () => {
79 let thread = createCommentThread([createComment()]);
80 assert.isFalse(isNewThread(thread));
81
82 thread = createCommentThread([createComment(), createNew()]);
83 assert.isFalse(isNewThread(thread));
84
85 thread = createCommentThread([createNew()]);
86 assert.isTrue(isNewThread(thread));
87 });
88
Frank Borden795317d2022-08-31 15:17:51 +020089 suite('getPatchRangeForCommentUrl', () => {
Dhruv Srivastava254cb722021-03-08 21:15:37 +010090 test('comment created with side=PARENT does not navigate to latest ps', () => {
91 const comment = {
92 ...createComment(),
93 id: 'c4' as UrlEncodedCommentId,
94 line: 10,
Ben Rohlfsabaeacf2022-05-27 15:24:22 +020095 patch_set: 4 as RevisionPatchSetNum,
Dhruv Srivastava254cb722021-03-08 21:15:37 +010096 side: CommentSide.PARENT,
97 path: '/COMMIT_MSG',
98 };
Dhruv Srivastava1c41a1b72021-03-11 16:24:53 +010099 assert.deepEqual(
100 getPatchRangeForCommentUrl(comment, 11 as RevisionPatchSetNum),
101 {
Ben Rohlfs58267b72022-05-27 15:59:18 +0200102 basePatchNum: PARENT,
Ben Rohlfsabaeacf2022-05-27 15:24:22 +0200103 patchNum: 4 as RevisionPatchSetNum,
Dhruv Srivastava1c41a1b72021-03-11 16:24:53 +0100104 }
105 );
Dhruv Srivastava254cb722021-03-08 21:15:37 +0100106 });
107 });
108
Dhruv Srivastava2a83d322022-08-09 16:49:01 +0200109 test('getMentionedThreads', () => {
110 const account = createAccountWithEmail('abcd@def.com');
111 const threads = [
112 createCommentThread([
113 {
114 ...createComment(),
115 message: 'random text with no emails',
116 },
117 ]),
118 createCommentThread([
119 {
120 ...createComment(),
121 message: '@abcd@def.com please take a look',
122 },
123 {
124 ...createComment(),
125 message: '@abcd@def.com please take a look again at this',
126 },
127 ]),
128 createCommentThread([
129 {
130 ...createComment(),
131 message: '@abcd@def.com this is important',
132 },
133 ]),
134 ];
135 assert.deepEqual(getMentionedThreads(threads, account), [
136 threads[1],
137 threads[2],
138 ]);
139
140 assert.deepEqual(
141 getMentionedThreads(threads, createAccountWithEmail('xyz@def.com')),
142 []
143 );
144 });
145
Dhruv Srivastava254cb722021-03-08 21:15:37 +0100146 test('comments sorting', () => {
Ben Rohlfsba440822023-04-11 18:08:03 +0200147 const comments: Comment[] = [
Dhruv Srivastava254cb722021-03-08 21:15:37 +0100148 {
149 id: 'new_draft' as UrlEncodedCommentId,
150 message: 'i do not like either of you',
Ben Rohlfs610bb4f2023-04-17 12:34:35 +0200151 savingState: SavingState.OK,
Dhruv Srivastava254cb722021-03-08 21:15:37 +0100152 updated: '2015-12-20 15:01:20.396000000' as Timestamp,
Ben Rohlfsba440822023-04-11 18:08:03 +0200153 } as DraftInfo,
Dhruv Srivastava254cb722021-03-08 21:15:37 +0100154 {
155 id: 'sallys_confession' as UrlEncodedCommentId,
156 message: 'i like you, jack',
157 updated: '2015-12-23 15:00:20.396000000' as Timestamp,
158 line: 1,
Dhruv Srivastava254cb722021-03-08 21:15:37 +0100159 },
160 {
161 id: 'jacks_reply' as UrlEncodedCommentId,
162 message: 'i like you, too',
163 updated: '2015-12-24 15:01:20.396000000' as Timestamp,
Dhruv Srivastava254cb722021-03-08 21:15:37 +0100164 line: 1,
Ben Rohlfsba440822023-04-11 18:08:03 +0200165 in_reply_to: 'sallys_confession' as UrlEncodedCommentId,
Dhruv Srivastava254cb722021-03-08 21:15:37 +0100166 },
167 ];
168 const sortedComments = sortComments(comments);
169 assert.equal(sortedComments[0], comments[1]);
170 assert.equal(sortedComments[1], comments[2]);
171 assert.equal(sortedComments[2], comments[0]);
172 });
173
174 suite('createCommentThreads', () => {
175 test('creates threads from individual comments', () => {
Ben Rohlfs610bb4f2023-04-17 12:34:35 +0200176 const comments: Comment[] = [
Dhruv Srivastava254cb722021-03-08 21:15:37 +0100177 {
178 id: 'sallys_confession' as UrlEncodedCommentId,
179 message: 'i like you, jack',
180 updated: '2015-12-23 15:00:20.396000000' as Timestamp,
181 line: 1,
Ben Rohlfsabaeacf2022-05-27 15:24:22 +0200182 patch_set: 1 as RevisionPatchSetNum,
Dhruv Srivastava254cb722021-03-08 21:15:37 +0100183 path: 'some/path',
184 },
185 {
186 id: 'jacks_reply' as UrlEncodedCommentId,
187 message: 'i like you, too',
188 updated: '2015-12-24 15:01:20.396000000' as Timestamp,
189 line: 1,
190 in_reply_to: 'sallys_confession' as UrlEncodedCommentId,
Ben Rohlfsabaeacf2022-05-27 15:24:22 +0200191 patch_set: 1 as RevisionPatchSetNum,
Dhruv Srivastava254cb722021-03-08 21:15:37 +0100192 path: 'some/path',
193 },
194 {
195 id: 'new_draft' as UrlEncodedCommentId,
196 message: 'i do not like either of you' as UrlEncodedCommentId,
Ben Rohlfs610bb4f2023-04-17 12:34:35 +0200197 savingState: SavingState.OK,
Dhruv Srivastava254cb722021-03-08 21:15:37 +0100198 updated: '2015-12-20 15:01:20.396000000' as Timestamp,
Ben Rohlfsabaeacf2022-05-27 15:24:22 +0200199 patch_set: 1 as RevisionPatchSetNum,
Dhruv Srivastava254cb722021-03-08 21:15:37 +0100200 path: 'some/path',
201 },
202 ];
203
Ben Rohlfs05750b92021-10-29 08:23:08 +0200204 const actualThreads = createCommentThreads(comments);
Dhruv Srivastava254cb722021-03-08 21:15:37 +0100205
206 assert.equal(actualThreads.length, 2);
207
Dhruv Srivastava254cb722021-03-08 21:15:37 +0100208 assert.equal(actualThreads[0].comments.length, 2);
209 assert.deepEqual(actualThreads[0].comments[0], comments[0]);
210 assert.deepEqual(actualThreads[0].comments[1], comments[1]);
Ben Rohlfsabaeacf2022-05-27 15:24:22 +0200211 assert.equal(actualThreads[0].patchNum, 1 as RevisionPatchSetNum);
Dhruv Srivastava254cb722021-03-08 21:15:37 +0100212 assert.equal(actualThreads[0].line, 1);
213
Dhruv Srivastava254cb722021-03-08 21:15:37 +0100214 assert.equal(actualThreads[1].comments.length, 1);
215 assert.deepEqual(actualThreads[1].comments[0], comments[2]);
Ben Rohlfsabaeacf2022-05-27 15:24:22 +0200216 assert.equal(actualThreads[1].patchNum, 1 as RevisionPatchSetNum);
Ben Rohlfsb9956102023-05-12 17:07:06 +0200217 assert.equal(actualThreads[1].line, FILE);
Dhruv Srivastava254cb722021-03-08 21:15:37 +0100218 });
219
220 test('derives patchNum and range', () => {
221 const comments = [
222 {
223 id: 'betsys_confession' as UrlEncodedCommentId,
224 message: 'i like you, jack',
225 updated: '2015-12-24 15:00:10.396000000' as Timestamp,
226 range: {
227 start_line: 1,
228 start_character: 1,
229 end_line: 1,
230 end_character: 2,
231 },
Ben Rohlfsabaeacf2022-05-27 15:24:22 +0200232 patch_set: 5 as RevisionPatchSetNum,
Dhruv Srivastava254cb722021-03-08 21:15:37 +0100233 path: '/p',
234 line: 1,
235 },
236 ];
237
238 const expectedThreads = [
239 {
Dhruv Srivastava254cb722021-03-08 21:15:37 +0100240 commentSide: CommentSide.REVISION,
241 path: '/p',
242 rootId: 'betsys_confession' as UrlEncodedCommentId,
243 mergeParentNum: undefined,
244 comments: [
245 {
246 id: 'betsys_confession' as UrlEncodedCommentId,
247 path: '/p',
248 message: 'i like you, jack',
249 updated: '2015-12-24 15:00:10.396000000' as Timestamp,
250 range: {
251 start_line: 1,
252 start_character: 1,
253 end_line: 1,
254 end_character: 2,
255 },
Ben Rohlfsabaeacf2022-05-27 15:24:22 +0200256 patch_set: 5 as RevisionPatchSetNum,
Dhruv Srivastava254cb722021-03-08 21:15:37 +0100257 line: 1,
258 },
259 ],
Ben Rohlfsabaeacf2022-05-27 15:24:22 +0200260 patchNum: 5 as RevisionPatchSetNum,
Dhruv Srivastava254cb722021-03-08 21:15:37 +0100261 range: {
262 start_line: 1,
263 start_character: 1,
264 end_line: 1,
265 end_character: 2,
266 },
267 line: 1,
268 },
269 ];
270
Ben Rohlfs05750b92021-10-29 08:23:08 +0200271 assert.deepEqual(createCommentThreads(comments), expectedThreads);
Dhruv Srivastava254cb722021-03-08 21:15:37 +0100272 });
273
274 test('does not thread unrelated comments at same location', () => {
275 const comments = [
276 {
277 id: 'sallys_confession' as UrlEncodedCommentId,
278 message: 'i like you, jack',
279 updated: '2015-12-23 15:00:20.396000000' as Timestamp,
Dhruv Srivastava254cb722021-03-08 21:15:37 +0100280 path: '/p',
281 },
282 {
283 id: 'jacks_reply' as UrlEncodedCommentId,
284 message: 'i like you, too',
285 updated: '2015-12-24 15:01:20.396000000' as Timestamp,
Dhruv Srivastava254cb722021-03-08 21:15:37 +0100286 path: '/p',
287 },
288 ];
289 assert.equal(createCommentThreads(comments).length, 2);
290 });
291 });
Milutin Kristofic1d219672022-06-21 14:57:25 +0200292
293 test('hasUserSuggestion', () => {
294 const comment = {
295 ...createComment(),
296 message: `${USER_SUGGESTION_START_PATTERN}${'test'}${'\n```'}`,
297 };
298 assert.isTrue(hasUserSuggestion(comment));
299 });
300
301 test('getUserSuggestion', () => {
302 const suggestion = 'test';
303 const comment = {
304 ...createComment(),
305 message: `${USER_SUGGESTION_START_PATTERN}${suggestion}${'\n```'}`,
306 };
307 assert.equal(getUserSuggestion(comment), suggestion);
308 });
309
Milutin Kristoficd91d3122022-08-09 15:48:22 +0200310 suite('getContentInCommentRange', () => {
311 test('one line', () => {
312 const comment = {
313 ...createComment(),
314 line: 1,
315 };
316 const content = 'line1\nline2\nline3';
317 assert.equal(getContentInCommentRange(content, comment), 'line1');
318 });
319
320 test('multi line', () => {
321 const comment = {
322 ...createComment(),
323 line: 3,
324 range: {
325 start_line: 1,
326 start_character: 5,
327 end_line: 3,
328 end_character: 39,
329 },
330 };
331 const selectedText =
332 ' * Examples:\n' +
333 ' * Acknowledge/Dismiss, Delete, Report a bug, Report as not useful,\n' +
334 ' * Make blocking, Downgrade severity.';
335 const content = `${selectedText}\n`;
336 assert.equal(getContentInCommentRange(content, comment), selectedText);
337 });
Milutin Kristofic1d219672022-06-21 14:57:25 +0200338 });
339
Milutin Kristoficd91d3122022-08-09 15:48:22 +0200340 suite('createUserFixSuggestion', () => {
341 test('one line', () => {
342 const comment = {
343 ...createComment(),
344 line: 1,
345 path: 'abc.txt',
346 };
347 const line = 'lane1';
348 const replacement = 'line1';
349 assert.deepEqual(createUserFixSuggestion(comment, line, replacement), [
350 {
Ben Rohlfsd0207e52022-08-04 22:02:00 +0200351 fix_id: PROVIDED_FIX_ID,
Milutin Kristoficd91d3122022-08-09 15:48:22 +0200352 description: 'User suggestion',
353 replacements: [
354 {
355 path: 'abc.txt',
356 range: {
357 start_line: 1,
358 start_character: 0,
359 end_line: 1,
360 end_character: line.length,
361 },
362 replacement,
Milutin Kristofic1d219672022-06-21 14:57:25 +0200363 },
Milutin Kristoficd91d3122022-08-09 15:48:22 +0200364 ],
365 },
366 ]);
367 });
368
369 test('multiline', () => {
370 const comment = {
371 ...createComment(),
372 line: 3,
373 range: {
374 start_line: 1,
375 start_character: 5,
376 end_line: 3,
377 end_character: 39,
378 },
379 path: 'abc.txt',
380 };
381 const line =
382 ' * Examples:\n' +
383 ' * Acknowledge/Dismiss, Delete, Report a bug, Report as not useful,\n' +
384 ' * Make blocking, Downgrade severity.';
385 const replacement =
386 ' - Examples:\n' +
387 ' - Acknowledge/Dismiss, Delete, Report a bug, Report as not useful,\n' +
388 ' - Make blocking, Downgrade severity.';
389 assert.deepEqual(createUserFixSuggestion(comment, line, replacement), [
390 {
Ben Rohlfsd0207e52022-08-04 22:02:00 +0200391 fix_id: PROVIDED_FIX_ID,
Milutin Kristoficd91d3122022-08-09 15:48:22 +0200392 description: 'User suggestion',
393 replacements: [
394 {
395 path: 'abc.txt',
396 range: {
397 start_line: 1,
398 start_character: 0,
399 end_line: 3,
400 end_character: 42,
401 },
402 replacement,
403 },
404 ],
405 },
406 ]);
407 });
Milutin Kristofic1d219672022-06-21 14:57:25 +0200408 });
Dhruv Srivastava254cb722021-03-08 21:15:37 +0100409});