|
| 1 | +import vscode from '../mock/vscode'; |
| 2 | +import sinon from 'sinon'; |
| 3 | +import { expect } from 'chai'; |
| 4 | +import PickCopilotModelCommand from '../../../src/commands/pickCopilotModel'; |
| 5 | + |
| 6 | +describe('pickCopilotModel', () => { |
| 7 | + describe('execute', () => { |
| 8 | + let models: Record<string, unknown>[]; |
| 9 | + let executeCommandStub: sinon.SinonStub; |
| 10 | + let showQuickPickStub: sinon.SinonStub; |
| 11 | + let showErrorMessageStub: sinon.SinonStub; |
| 12 | + const chosenModel = 'claude-3.5-sonnet'; |
| 13 | + beforeEach(() => { |
| 14 | + models = [ |
| 15 | + { id: 'gpt-4o', name: 'GPT-4o', maxInputTokens: 325, family: 'copilot' }, |
| 16 | + { |
| 17 | + id: 'claude-3.5-sonnet', |
| 18 | + name: 'Claude 3.5 Sonnet', |
| 19 | + maxInputTokens: 325, |
| 20 | + family: 'copilot', |
| 21 | + }, |
| 22 | + ]; |
| 23 | + showQuickPickStub = sinon.stub(vscode.window, 'showQuickPick').callsFake(() => ({ |
| 24 | + details: chosenModel, |
| 25 | + })); |
| 26 | + showErrorMessageStub = sinon.stub(vscode.window, 'showErrorMessage').resolves(); |
| 27 | + executeCommandStub = sinon.stub(vscode.commands, 'executeCommand').resolves(); |
| 28 | + sinon.stub(vscode.lm, 'selectChatModels').callsFake(() => models as any); // eslint-disable-line @typescript-eslint/no-explicit-any |
| 29 | + }); |
| 30 | + |
| 31 | + afterEach(() => { |
| 32 | + sinon.restore(); |
| 33 | + }); |
| 34 | + |
| 35 | + const setPreferredModel = (model: string | undefined) => |
| 36 | + vscode.workspace.getConfiguration('appMap').update('copilot.preferredModel', model); |
| 37 | + const getPreferredModel = () => |
| 38 | + vscode.workspace.getConfiguration('appMap').get('copilot.preferredModel'); |
| 39 | + |
| 40 | + it('sets the appMap.copilot.preferredModel setting', async () => { |
| 41 | + setPreferredModel(undefined); |
| 42 | + await PickCopilotModelCommand.execute(); |
| 43 | + expect(getPreferredModel()).to.equal(chosenModel); |
| 44 | + }); |
| 45 | + |
| 46 | + it('shows an error message if no models are available', async () => { |
| 47 | + models = []; |
| 48 | + await PickCopilotModelCommand.execute(); |
| 49 | + expect(showErrorMessageStub.called).to.be.true; |
| 50 | + expect(showQuickPickStub.called).to.be.false; |
| 51 | + expect(executeCommandStub.called).to.be.false; |
| 52 | + }); |
| 53 | + |
| 54 | + it('restarts the RPC server', async () => { |
| 55 | + await PickCopilotModelCommand.execute(); |
| 56 | + expect(executeCommandStub.calledWith('appmap.rpc.restart')).to.be.true; |
| 57 | + }); |
| 58 | + |
| 59 | + it('does nothing if the user cancels the quick pick', async () => { |
| 60 | + showQuickPickStub.resolves(undefined); |
| 61 | + setPreferredModel(chosenModel); |
| 62 | + await PickCopilotModelCommand.execute(); |
| 63 | + expect(getPreferredModel()).to.equal(chosenModel); |
| 64 | + expect(executeCommandStub.called).to.be.false; |
| 65 | + }); |
| 66 | + }); |
| 67 | +}); |
0 commit comments