Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/review/node/test/reviewCommand.spec.ts
13405 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import assert from 'assert';
7
import { describe, suite, test } from 'vitest';
8
import { TextDocumentSnapshot } from '../../../../platform/editing/common/textDocumentSnapshot';
9
import { toCodeReviewResult } from '../../../../platform/review/common/reviewCommand';
10
import { ReviewComment, ReviewSuggestion } from '../../../../platform/review/common/reviewService';
11
import { createTextDocumentData } from '../../../../util/common/test/shims/textDocument';
12
import { URI } from '../../../../util/vs/base/common/uri';
13
import { Range } from '../../../../vscodeTypes';
14
15
function createMockDocument(uri = URI.file('/test.ts'), content = 'test content') {
16
return TextDocumentSnapshot.create(createTextDocumentData(uri, content, 'typescript').document);
17
}
18
19
function createTestComment(overrides: Partial<ReviewComment> = {}): ReviewComment {
20
return {
21
request: {
22
source: 'githubReviewAgent',
23
promptCount: -1,
24
messageId: 'test-id',
25
inputType: 'change',
26
inputRanges: [],
27
},
28
document: createMockDocument(),
29
uri: URI.file('/test.ts'),
30
languageId: 'typescript',
31
range: new Range(0, 0, 0, 10),
32
body: 'Test comment body',
33
kind: 'bug',
34
severity: 'medium',
35
originalIndex: 0,
36
actionCount: 0,
37
...overrides,
38
};
39
}
40
41
suite('reviewCommand', () => {
42
43
describe('toCodeReviewResult', () => {
44
45
test('maps empty comments array to empty result', async () => {
46
const result = await toCodeReviewResult([]);
47
48
assert.strictEqual(result.type, 'success');
49
assert.ok(result.type === 'success');
50
assert.strictEqual(result.comments.length, 0);
51
});
52
53
test('maps string body correctly', async () => {
54
const comment = createTestComment({ body: 'plain text body' });
55
56
const result = await toCodeReviewResult([comment]);
57
58
assert.ok(result.type === 'success');
59
assert.strictEqual(result.comments.length, 1);
60
assert.strictEqual(result.comments[0].body, 'plain text body');
61
});
62
63
test('maps MarkdownString body to its value', async () => {
64
const { MarkdownString } = await import('../../../../vscodeTypes');
65
const comment = createTestComment({ body: new MarkdownString('**bold** text') });
66
67
const result = await toCodeReviewResult([comment]);
68
69
assert.ok(result.type === 'success');
70
assert.strictEqual(result.comments[0].body, '**bold** text');
71
});
72
73
test('preserves uri, range, kind, severity', async () => {
74
const uri = URI.file('/foo/bar.ts');
75
const range = new Range(5, 2, 5, 20);
76
const comment = createTestComment({ uri, range, kind: 'style', severity: 'high' });
77
78
const result = await toCodeReviewResult([comment]);
79
80
assert.ok(result.type === 'success');
81
const c = result.comments[0];
82
assert.strictEqual(c.uri.toString(), uri.toString());
83
assert.strictEqual(c.range.start.line, 5);
84
assert.strictEqual(c.range.start.character, 2);
85
assert.strictEqual(c.kind, 'style');
86
assert.strictEqual(c.severity, 'high');
87
});
88
89
test('excludes internal fields like request, document, originalIndex, actionCount', async () => {
90
const comment = createTestComment();
91
92
const result = await toCodeReviewResult([comment]);
93
94
assert.ok(result.type === 'success');
95
const c = result.comments[0] as unknown as Record<string, unknown>;
96
assert.strictEqual('request' in c, false);
97
assert.strictEqual('document' in c, false);
98
assert.strictEqual('originalIndex' in c, false);
99
assert.strictEqual('actionCount' in c, false);
100
assert.strictEqual('languageId' in c, false);
101
});
102
103
test('maps sync suggestion with edits', async () => {
104
const suggestion: ReviewSuggestion = {
105
markdown: '',
106
edits: [{
107
range: new Range(1, 0, 2, 0),
108
newText: 'fixed code\n',
109
oldText: 'broken code\n',
110
}],
111
};
112
const comment = createTestComment({ suggestion });
113
114
const result = await toCodeReviewResult([comment]);
115
116
assert.ok(result.type === 'success');
117
const s = result.comments[0].suggestion;
118
assert.ok(s);
119
assert.strictEqual(s.edits.length, 1);
120
assert.strictEqual(s.edits[0].newText, 'fixed code\n');
121
assert.strictEqual(s.edits[0].oldText, 'broken code\n');
122
});
123
124
test('omits suggestion when edits array is empty', async () => {
125
const suggestion: ReviewSuggestion = { markdown: '', edits: [] };
126
const comment = createTestComment({ suggestion });
127
128
const result = await toCodeReviewResult([comment]);
129
130
assert.ok(result.type === 'success');
131
assert.strictEqual(result.comments[0].suggestion, undefined);
132
});
133
134
test('omits suggestion when undefined', async () => {
135
const comment = createTestComment({ suggestion: undefined });
136
137
const result = await toCodeReviewResult([comment]);
138
139
assert.ok(result.type === 'success');
140
assert.strictEqual(result.comments[0].suggestion, undefined);
141
});
142
143
test('resolves promise-based suggestion', async () => {
144
const suggestion: ReviewSuggestion = {
145
markdown: '',
146
edits: [{
147
range: new Range(0, 0, 1, 0),
148
newText: 'new\n',
149
oldText: 'old\n',
150
}],
151
};
152
const comment = createTestComment({ suggestion: Promise.resolve(suggestion) });
153
154
const result = await toCodeReviewResult([comment]);
155
156
assert.ok(result.type === 'success');
157
const s = result.comments[0].suggestion;
158
assert.ok(s);
159
assert.strictEqual(s.edits[0].newText, 'new\n');
160
});
161
162
test('maps multiple comments', async () => {
163
const comments = [
164
createTestComment({ body: 'first', kind: 'bug' }),
165
createTestComment({ body: 'second', kind: 'style', uri: URI.file('/other.ts') }),
166
];
167
168
const result = await toCodeReviewResult(comments);
169
170
assert.ok(result.type === 'success');
171
assert.strictEqual(result.comments.length, 2);
172
assert.strictEqual(result.comments[0].body, 'first');
173
assert.strictEqual(result.comments[1].body, 'second');
174
assert.strictEqual(result.comments[1].kind, 'style');
175
});
176
});
177
});
178
179