|
| 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 { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 7 | +import type * as vscode from 'vscode'; |
| 8 | +import { IFileSystemService } from '../../../../../platform/filesystem/common/fileSystemService'; |
| 9 | +import { MockFileSystemService } from '../../../../../platform/filesystem/node/test/mockFileSystemService'; |
| 10 | +import { ILogService } from '../../../../../platform/log/common/logService'; |
| 11 | +import { CancellationToken } from '../../../../../util/vs/base/common/cancellation'; |
| 12 | +import { DisposableStore } from '../../../../../util/vs/base/common/lifecycle'; |
| 13 | +import * as path from '../../../../../util/vs/base/common/path'; |
| 14 | +import { URI } from '../../../../../util/vs/base/common/uri'; |
| 15 | +import { ChatReferenceDiagnostic, Diagnostic, DiagnosticSeverity, FileType, Range } from '../../../../../vscodeTypes'; |
| 16 | +import { createExtensionUnitTestingServices } from '../../../../test/node/services'; |
| 17 | +import { TestChatRequest } from '../../../../test/node/testHelpers'; |
| 18 | +import { CopilotCLIPromptResolver } from '../copilotcliPromptResolver'; |
| 19 | + |
| 20 | +function makeDiagnostic(line: number, message: string, severity: DiagnosticSeverity = DiagnosticSeverity.Error, code?: string): Diagnostic { |
| 21 | + const diag = new Diagnostic( |
| 22 | + new Range(line, 0, line, 0), |
| 23 | + message, |
| 24 | + severity |
| 25 | + ); |
| 26 | + diag.code = code; |
| 27 | + return diag; |
| 28 | +} |
| 29 | + |
| 30 | +// Helper to create a ChatRequest with references array patched |
| 31 | +function withReferences(req: TestChatRequest, refs: unknown[]): TestChatRequest { |
| 32 | + // vitest doesn't prevent mutation; emulate the readonly property by assignment cast |
| 33 | + req.references = refs as vscode.ChatPromptReference[]; |
| 34 | + return req; |
| 35 | +} |
| 36 | + |
| 37 | +describe('CopilotCLIPromptResolver', () => { |
| 38 | + const store = new DisposableStore(); |
| 39 | + let resolver: CopilotCLIPromptResolver; |
| 40 | + let fileSystemService: IFileSystemService; |
| 41 | + let logService: ILogService; |
| 42 | + |
| 43 | + beforeEach(() => { |
| 44 | + const services = store.add(createExtensionUnitTestingServices()); |
| 45 | + const accessor = services.createTestingAccessor(); |
| 46 | + fileSystemService = new MockFileSystemService(); |
| 47 | + logService = accessor.get(ILogService); |
| 48 | + resolver = new CopilotCLIPromptResolver(logService, fileSystemService); |
| 49 | + }); |
| 50 | + |
| 51 | + afterEach(() => { |
| 52 | + store.clear(); |
| 53 | + vi.resetAllMocks(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('returns original prompt unchanged for slash command', async () => { |
| 57 | + const req = new TestChatRequest('/help something'); |
| 58 | + const { prompt, attachments } = await resolver.resolvePrompt(req as unknown as vscode.ChatRequest, CancellationToken.None); |
| 59 | + expect(prompt).toBe('/help something'); |
| 60 | + expect(attachments).toHaveLength(0); |
| 61 | + }); |
| 62 | + |
| 63 | + it('collects file references and produces attachments plus reminder block', async () => { |
| 64 | + // Spy on stat to simulate file type |
| 65 | + const statSpy = vi.spyOn(fileSystemService, 'stat').mockResolvedValue({ type: FileType.File, size: 10 } as any); |
| 66 | + |
| 67 | + const fileA = URI.file(path.join('tmp', 'a.ts')); |
| 68 | + const fileB = URI.file(path.join('tmp', 'b.ts')); |
| 69 | + |
| 70 | + const req = withReferences(new TestChatRequest('Explain a and b'), [ |
| 71 | + { id: 'file-a', value: fileA, name: 'a.ts', range: [8, 9] }, // 'a' |
| 72 | + { id: 'file-b', value: fileB, name: 'b.ts', range: [14, 15] } // 'b' |
| 73 | + ]); |
| 74 | + |
| 75 | + const { prompt, attachments } = await resolver.resolvePrompt(req as unknown as vscode.ChatRequest, CancellationToken.None); |
| 76 | + |
| 77 | + // Should have reminder block |
| 78 | + expect(prompt).toMatch(/<reminder>/); |
| 79 | + expect(prompt).toMatch(/The user provided the following references:/); |
| 80 | + expect(prompt).toContain(`- a → ${fileA.fsPath}`); |
| 81 | + expect(prompt).toContain(`- b → ${fileB.fsPath}`); |
| 82 | + |
| 83 | + // Attachments reflect both files |
| 84 | + expect(attachments.map(a => a.displayName).sort()).toEqual(['a.ts', 'b.ts']); |
| 85 | + expect(attachments.every(a => a.type === 'file')).toBe(true); |
| 86 | + // Stat called for each file |
| 87 | + expect(statSpy).toHaveBeenCalledTimes(2); |
| 88 | + }); |
| 89 | + |
| 90 | + it('includes diagnostics in reminder block with severity and line', async () => { |
| 91 | + const statSpy = vi.spyOn(fileSystemService, 'stat').mockResolvedValue({ type: FileType.File, size: 10 } as any); |
| 92 | + const fileUri = URI.file(path.join('workspace', 'src', 'index.ts')); |
| 93 | + |
| 94 | + const diagnostics = [ |
| 95 | + makeDiagnostic(4, 'Unexpected any', 0, 'TS7005'), |
| 96 | + makeDiagnostic(9, 'Possible undefined', 1) |
| 97 | + ]; |
| 98 | + |
| 99 | + // ChatReferenceDiagnostic requires a Map of uri -> diagnostics array |
| 100 | + const chatRefDiag: ChatReferenceDiagnostic = { diagnostics: [[fileUri, diagnostics]] }; |
| 101 | + const req = withReferences(new TestChatRequest('Fix issues'), [ |
| 102 | + { id: 'diag-1', value: chatRefDiag } |
| 103 | + ]); |
| 104 | + |
| 105 | + const { prompt, attachments } = await resolver.resolvePrompt(req as unknown as vscode.ChatRequest, CancellationToken.None); |
| 106 | + |
| 107 | + expect(prompt).toMatch(/Fix issues/); |
| 108 | + expect(prompt).toMatch(/The user provided the following diagnostics:/); |
| 109 | + expect(prompt).toContain(`- error [TS7005] at ${fileUri.fsPath}:5: Unexpected any`); |
| 110 | + expect(prompt).toContain(`- warning at ${fileUri.fsPath}:10: Possible undefined`); |
| 111 | + // File should be attached once |
| 112 | + expect(attachments).toHaveLength(1); |
| 113 | + expect(attachments[0].path).toBe(fileUri.fsPath); |
| 114 | + expect(statSpy).toHaveBeenCalledTimes(1); |
| 115 | + }); |
| 116 | + |
| 117 | + it('attaches directories correctly', async () => { |
| 118 | + const statSpy = vi.spyOn(fileSystemService, 'stat').mockResolvedValueOnce({ type: FileType.Directory, size: 0 } as any); |
| 119 | + const dirUri = URI.file('/workspace/src'); |
| 120 | + const req = withReferences(new TestChatRequest('List src'), [ |
| 121 | + { id: 'src-dir', value: dirUri, name: 'src', range: [5, 8] } |
| 122 | + ]); |
| 123 | + |
| 124 | + const { attachments } = await resolver.resolvePrompt(req as unknown as vscode.ChatRequest, CancellationToken.None); |
| 125 | + expect(attachments).toHaveLength(1); |
| 126 | + expect(attachments[0].type).toBe('directory'); |
| 127 | + expect(attachments[0].displayName).toBe('src'); |
| 128 | + expect(statSpy).toHaveBeenCalledTimes(1); |
| 129 | + }); |
| 130 | + |
| 131 | + it('logs and ignores non file/directory stat types', async () => { |
| 132 | + // Simulate an unknown type (e.g., FileType.SymbolicLink or other) |
| 133 | + const statSpy = vi.spyOn(fileSystemService, 'stat').mockResolvedValue({ type: 99, size: 0 } as any); |
| 134 | + const logSpy = vi.spyOn(logService, 'error').mockImplementation(() => { }); |
| 135 | + const badUri = URI.file('/workspace/unknown'); |
| 136 | + const req = withReferences(new TestChatRequest('Check unknown'), [ |
| 137 | + { id: 'bad', value: badUri, name: 'unknown', range: [6, 13] } |
| 138 | + ]); |
| 139 | + |
| 140 | + const { attachments } = await resolver.resolvePrompt(req as unknown as vscode.ChatRequest, CancellationToken.None); |
| 141 | + expect(attachments).toHaveLength(0); // ignored |
| 142 | + expect(statSpy).toHaveBeenCalledTimes(1); |
| 143 | + expect(logSpy).toHaveBeenCalled(); |
| 144 | + }); |
| 145 | + |
| 146 | + it('handles stat failure gracefully and logs error', async () => { |
| 147 | + const error = new Error('stat failed'); |
| 148 | + const statSpy = vi.spyOn(fileSystemService, 'stat').mockRejectedValue(error); |
| 149 | + const logSpy = vi.spyOn(logService, 'error').mockImplementation(() => { }); |
| 150 | + const fileUri = URI.file('/workspace/src/index.ts'); |
| 151 | + const req = withReferences(new TestChatRequest('Read file'), [ |
| 152 | + { id: 'file', value: fileUri, name: 'index.ts', range: [5, 10] } |
| 153 | + ]); |
| 154 | + |
| 155 | + const { attachments } = await resolver.resolvePrompt(req as unknown as vscode.ChatRequest, CancellationToken.None); |
| 156 | + expect(attachments).toHaveLength(0); |
| 157 | + expect(statSpy).toHaveBeenCalledTimes(1); |
| 158 | + expect(logSpy).toHaveBeenCalled(); |
| 159 | + }); |
| 160 | + |
| 161 | + it('no reminder block when there are no references or diagnostics', async () => { |
| 162 | + const req = new TestChatRequest('Just a question'); |
| 163 | + const { prompt } = await resolver.resolvePrompt(req as unknown as vscode.ChatRequest, CancellationToken.None); |
| 164 | + expect(prompt).toBe('Just a question'); |
| 165 | + }); |
| 166 | +}); |
0 commit comments