4.0 KB140 lines
Blame
1/**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8import type {Repository} from 'isl-server/src/Repository';
9import type {CommitInfo} from 'isl/src/types';
10
11import {GitHubCodeReviewProvider} from 'isl-server/src/github/githubCodeReviewProvider';
12import {mockLogger} from 'shared/testUtils';
13import {getDiffBlameHoverMarkup} from '../blameHover';
14import {getRealignedBlameInfo, shortenAuthorName} from '../blameUtils';
15
16describe('blame', () => {
17 describe('getRealignedBlameInfo', () => {
18 it('realigns blame', () => {
19 const person1 = {author: 'person1', date: new Date('2020-01-01'), hash: 'A'} as CommitInfo;
20 const person2 = {author: 'person2', date: new Date('2021-01-01'), hash: 'B'} as CommitInfo;
21 const person3 = {author: 'person3', date: new Date('2022-01-01'), hash: 'C'} as CommitInfo;
22
23 const blame: Array<[string, CommitInfo]> = [
24 /* A - person1 */ ['A\n', person1],
25 /* B - person2 */ ['B\n', person2],
26 /* C - person3 */ ['C\n', person3],
27 /* D - person1 */ ['D\n', person1],
28 /* E - person2 */ ['E\n', person2],
29 /* F - person3 */ ['F\n', person3],
30 /* G - person1 */ ['G\n', person1],
31 ];
32
33 const after = `\
34A
35C
36D
37hi
38hey
39E
40F!
41G
42`;
43
44 const expected = [
45 /* A - person1 */ [expect.anything(), person1],
46 /* C - person3 */ [expect.anything(), person3],
47 /* D - person1 */ [expect.anything(), person1],
48 /* hi - (you) */ [expect.anything(), undefined],
49 /* hey - (you) */ [expect.anything(), undefined],
50 /* E - person2 */ [expect.anything(), person2],
51 /* F! - (you) */ [expect.anything(), undefined],
52 /* G - person1 */ [expect.anything(), person1],
53 ];
54
55 const result = getRealignedBlameInfo(blame, after);
56 expect(result).toEqual(expected);
57 });
58 });
59
60 describe('blame hover', () => {
61 const mockRepo = {
62 codeReviewProvider: new GitHubCodeReviewProvider(
63 {type: 'github', owner: 'facebook', repo: 'sapling', hostname: 'github.com'},
64 mockLogger,
65 ),
66 } as unknown as Repository;
67 const mockCommit = {
68 hash: 'a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2',
69 date: new Date(),
70 author: 'person',
71 title: 'My cool PR',
72 } as unknown as CommitInfo;
73 it('renders attachec PR links', () => {
74 expect(
75 getDiffBlameHoverMarkup(mockRepo, {
76 ...mockCommit,
77 date: new Date(),
78 description: 'added some stuff',
79 diffId: '1234',
80 } as unknown as CommitInfo),
81 ).toEqual(
82 `\
83**person** - [#1234](https://github.com/facebook/sapling/pull/1234) (just now)
84
85**My cool PR**
86
87
88added some stuff`,
89 );
90 });
91
92 it('renders detected PR links', () => {
93 expect(
94 getDiffBlameHoverMarkup(mockRepo, {
95 ...mockCommit,
96 description: 'added some stuff in #1234',
97 } as unknown as CommitInfo),
98 ).toEqual(
99 `\
100**person** - [#1234](https://github.com/facebook/sapling/pull/1234) (just now)
101
102**My cool PR**
103
104
105added some stuff in #1234`,
106 );
107 });
108
109 it('falls back to commit hash', () => {
110 expect(
111 getDiffBlameHoverMarkup(mockRepo, {
112 ...mockCommit,
113 description: 'added some stuff',
114 } as unknown as CommitInfo),
115 ).toEqual(
116 `\
117**person** - [\`a1b2c3d4e5f6\`](https://github.com/facebook/sapling/commit/a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2) (just now)
118
119**My cool PR**
120
121
122added some stuff`,
123 );
124 });
125 });
126});
127
128describe('blame utils', () => {
129 describe('shortenAuthorName', () => {
130 it('removes email for inline display', () => {
131 expect(shortenAuthorName('John Smith john@example.com')).toEqual('John Smith');
132 expect(shortenAuthorName('John Smith <john@example.com>')).toEqual('John Smith');
133 });
134
135 it('shows email if no name is given', () => {
136 expect(shortenAuthorName('john@example.com')).toEqual('john@example.com');
137 });
138 });
139});
140