Path: blob/main/extensions/copilot/src/extension/review/node/test/reviewCommand.spec.ts
13405 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import assert from 'assert';6import { describe, suite, test } from 'vitest';7import { TextDocumentSnapshot } from '../../../../platform/editing/common/textDocumentSnapshot';8import { toCodeReviewResult } from '../../../../platform/review/common/reviewCommand';9import { ReviewComment, ReviewSuggestion } from '../../../../platform/review/common/reviewService';10import { createTextDocumentData } from '../../../../util/common/test/shims/textDocument';11import { URI } from '../../../../util/vs/base/common/uri';12import { Range } from '../../../../vscodeTypes';1314function createMockDocument(uri = URI.file('/test.ts'), content = 'test content') {15return TextDocumentSnapshot.create(createTextDocumentData(uri, content, 'typescript').document);16}1718function createTestComment(overrides: Partial<ReviewComment> = {}): ReviewComment {19return {20request: {21source: 'githubReviewAgent',22promptCount: -1,23messageId: 'test-id',24inputType: 'change',25inputRanges: [],26},27document: createMockDocument(),28uri: URI.file('/test.ts'),29languageId: 'typescript',30range: new Range(0, 0, 0, 10),31body: 'Test comment body',32kind: 'bug',33severity: 'medium',34originalIndex: 0,35actionCount: 0,36...overrides,37};38}3940suite('reviewCommand', () => {4142describe('toCodeReviewResult', () => {4344test('maps empty comments array to empty result', async () => {45const result = await toCodeReviewResult([]);4647assert.strictEqual(result.type, 'success');48assert.ok(result.type === 'success');49assert.strictEqual(result.comments.length, 0);50});5152test('maps string body correctly', async () => {53const comment = createTestComment({ body: 'plain text body' });5455const result = await toCodeReviewResult([comment]);5657assert.ok(result.type === 'success');58assert.strictEqual(result.comments.length, 1);59assert.strictEqual(result.comments[0].body, 'plain text body');60});6162test('maps MarkdownString body to its value', async () => {63const { MarkdownString } = await import('../../../../vscodeTypes');64const comment = createTestComment({ body: new MarkdownString('**bold** text') });6566const result = await toCodeReviewResult([comment]);6768assert.ok(result.type === 'success');69assert.strictEqual(result.comments[0].body, '**bold** text');70});7172test('preserves uri, range, kind, severity', async () => {73const uri = URI.file('/foo/bar.ts');74const range = new Range(5, 2, 5, 20);75const comment = createTestComment({ uri, range, kind: 'style', severity: 'high' });7677const result = await toCodeReviewResult([comment]);7879assert.ok(result.type === 'success');80const c = result.comments[0];81assert.strictEqual(c.uri.toString(), uri.toString());82assert.strictEqual(c.range.start.line, 5);83assert.strictEqual(c.range.start.character, 2);84assert.strictEqual(c.kind, 'style');85assert.strictEqual(c.severity, 'high');86});8788test('excludes internal fields like request, document, originalIndex, actionCount', async () => {89const comment = createTestComment();9091const result = await toCodeReviewResult([comment]);9293assert.ok(result.type === 'success');94const c = result.comments[0] as unknown as Record<string, unknown>;95assert.strictEqual('request' in c, false);96assert.strictEqual('document' in c, false);97assert.strictEqual('originalIndex' in c, false);98assert.strictEqual('actionCount' in c, false);99assert.strictEqual('languageId' in c, false);100});101102test('maps sync suggestion with edits', async () => {103const suggestion: ReviewSuggestion = {104markdown: '',105edits: [{106range: new Range(1, 0, 2, 0),107newText: 'fixed code\n',108oldText: 'broken code\n',109}],110};111const comment = createTestComment({ suggestion });112113const result = await toCodeReviewResult([comment]);114115assert.ok(result.type === 'success');116const s = result.comments[0].suggestion;117assert.ok(s);118assert.strictEqual(s.edits.length, 1);119assert.strictEqual(s.edits[0].newText, 'fixed code\n');120assert.strictEqual(s.edits[0].oldText, 'broken code\n');121});122123test('omits suggestion when edits array is empty', async () => {124const suggestion: ReviewSuggestion = { markdown: '', edits: [] };125const comment = createTestComment({ suggestion });126127const result = await toCodeReviewResult([comment]);128129assert.ok(result.type === 'success');130assert.strictEqual(result.comments[0].suggestion, undefined);131});132133test('omits suggestion when undefined', async () => {134const comment = createTestComment({ suggestion: undefined });135136const result = await toCodeReviewResult([comment]);137138assert.ok(result.type === 'success');139assert.strictEqual(result.comments[0].suggestion, undefined);140});141142test('resolves promise-based suggestion', async () => {143const suggestion: ReviewSuggestion = {144markdown: '',145edits: [{146range: new Range(0, 0, 1, 0),147newText: 'new\n',148oldText: 'old\n',149}],150};151const comment = createTestComment({ suggestion: Promise.resolve(suggestion) });152153const result = await toCodeReviewResult([comment]);154155assert.ok(result.type === 'success');156const s = result.comments[0].suggestion;157assert.ok(s);158assert.strictEqual(s.edits[0].newText, 'new\n');159});160161test('maps multiple comments', async () => {162const comments = [163createTestComment({ body: 'first', kind: 'bug' }),164createTestComment({ body: 'second', kind: 'style', uri: URI.file('/other.ts') }),165];166167const result = await toCodeReviewResult(comments);168169assert.ok(result.type === 'success');170assert.strictEqual(result.comments.length, 2);171assert.strictEqual(result.comments[0].body, 'first');172assert.strictEqual(result.comments[1].body, 'second');173assert.strictEqual(result.comments[1].kind, 'style');174});175});176});177178179