-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcomment.unit.test.ts
More file actions
119 lines (96 loc) · 3.86 KB
/
comment.unit.test.ts
File metadata and controls
119 lines (96 loc) · 3.86 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
import ansis from 'ansis';
import { vol } from 'memfs';
import { writeFile } from 'node:fs/promises';
import path from 'node:path';
import { MEMFS_VOLUME } from '@code-pushup/test-utils';
import { logger } from '@code-pushup/utils';
import { commentOnPR } from './comment.js';
import type { Comment, ProviderAPIClient } from './models.js';
describe('commentOnPR', () => {
const diffText = '# Code PushUp\n\nNo changes to report.\n';
const diffFile = 'report-diff.md';
const diffPath = path.join(MEMFS_VOLUME, diffFile);
const comment: Comment = {
id: 42,
body: `${diffText}\n\n<!-- generated by @code-pushup/ci -->\n`,
url: 'https://fake.git.repo/comments/42',
};
const otherComment: Comment = {
id: 666,
body: 'LGTM!',
url: 'https://fake.git.repo/comments/666',
};
const api = {
maxCommentChars: 1_000_000,
createComment: vi.fn().mockResolvedValue(comment),
updateComment: vi.fn().mockResolvedValue(comment),
listComments: vi.fn(),
} satisfies ProviderAPIClient;
const settings = { jobId: null };
beforeEach(() => {
vol.fromJSON({ [diffFile]: diffText }, MEMFS_VOLUME);
api.listComments.mockResolvedValue([]);
});
it('should create new comment if none existing', async () => {
api.listComments.mockResolvedValue([]);
await expect(commentOnPR(diffPath, api, settings)).resolves.toBe(
comment.id,
);
expect(api.listComments).toHaveBeenCalled();
expect(api.createComment).toHaveBeenCalledWith(comment.body);
expect(api.updateComment).not.toHaveBeenCalled();
});
it("should create new comment if existing comments don't match", async () => {
api.listComments.mockResolvedValue([otherComment]);
await expect(commentOnPR(diffPath, api, settings)).resolves.toBe(
comment.id,
);
expect(api.listComments).toHaveBeenCalled();
expect(api.createComment).toHaveBeenCalledWith(comment.body);
expect(api.updateComment).not.toHaveBeenCalled();
});
it('should update previous comment if it matches', async () => {
api.listComments.mockResolvedValue([comment]);
await expect(commentOnPR(diffPath, api, settings)).resolves.toBe(
comment.id,
);
expect(api.listComments).toHaveBeenCalled();
expect(api.createComment).not.toHaveBeenCalled();
expect(api.updateComment).toHaveBeenCalledWith(comment.id, comment.body);
});
it('should not match comment with different jobId', async () => {
api.listComments.mockResolvedValue([comment]);
await expect(
commentOnPR(diffPath, api, { jobId: 'monorepo-mode' }),
).resolves.toBe(comment.id);
expect(api.listComments).toHaveBeenCalled();
expect(api.createComment).toHaveBeenCalledWith(
`${diffText}\n\n<!-- generated by @code-pushup/ci [jobId=monorepo-mode] -->\n`,
);
expect(api.updateComment).not.toHaveBeenCalled();
});
it('should update previous comment which matches and ignore other comments', async () => {
api.listComments.mockResolvedValue([otherComment, comment]);
await expect(commentOnPR(diffPath, api, settings)).resolves.toBe(
comment.id,
);
expect(api.listComments).toHaveBeenCalled();
expect(api.createComment).not.toHaveBeenCalled();
expect(api.updateComment).toHaveBeenCalledWith(comment.id, comment.body);
});
it('should truncate comment body if it exceeds character limit', async () => {
const longDiffText = Array.from({ length: 100_000 })
.map((_, i) => `- Audit #${i} failed`)
.join('\n');
await writeFile(diffPath, longDiffText);
await expect(commentOnPR(diffPath, api, settings)).resolves.toBe(
comment.id,
);
expect(api.createComment).toHaveBeenCalledWith(
expect.stringContaining('...*[Comment body truncated]*'),
);
expect(logger.warn).toHaveBeenCalledWith(
`${ansis.bold.blue('<✓>')} Comment body is too long. Truncating to 1000000 characters.\n`,
);
});
});