2023-05-31 16:10:53 +08:00
|
|
|
|
import { expect } from 'chai';
|
2024-03-10 16:36:28 +08:00
|
|
|
|
// import { describe, it } from 'mocha';
|
2023-05-31 16:10:53 +08:00
|
|
|
|
import sinon from 'sinon';
|
|
|
|
|
import DevChat, { ChatOptions } from '../../src/toolwrapper/devchat';
|
|
|
|
|
import { CommandRun } from '../../src/util/commonUtil';
|
|
|
|
|
import { UiUtilWrapper } from '../../src/util/uiUtil';
|
2024-03-10 16:36:28 +08:00
|
|
|
|
import { ApiKeyManager } from '../../src/util/apiKey';
|
|
|
|
|
|
2024-07-08 22:20:37 +08:00
|
|
|
|
// TODO: 删除devchat.js时,删除此测试文件
|
|
|
|
|
// TODO: 同时为 DevChatCLI & DevChatClient 添加测试
|
2023-05-31 16:10:53 +08:00
|
|
|
|
describe('DevChat', () => {
|
|
|
|
|
let devChat: DevChat;
|
|
|
|
|
let spawnAsyncStub: sinon.SinonStub;
|
|
|
|
|
let workspaceFoldersFirstPathStub: sinon.SinonStub;
|
2024-03-10 16:36:28 +08:00
|
|
|
|
let apiKeyManagerStub: sinon.SinonStub;
|
2023-05-31 16:10:53 +08:00
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
devChat = new DevChat();
|
|
|
|
|
spawnAsyncStub = sinon.stub(CommandRun.prototype, 'spawnAsync');
|
|
|
|
|
workspaceFoldersFirstPathStub = sinon.stub(UiUtilWrapper, 'workspaceFoldersFirstPath');
|
2024-03-10 16:36:28 +08:00
|
|
|
|
apiKeyManagerStub = sinon.stub(ApiKeyManager, 'llmModel');
|
2023-05-31 16:10:53 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
spawnAsyncStub.restore();
|
|
|
|
|
workspaceFoldersFirstPathStub.restore();
|
2024-03-10 16:36:28 +08:00
|
|
|
|
apiKeyManagerStub.restore();
|
2023-05-31 16:10:53 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
describe('chat', () => {
|
|
|
|
|
it('should return a ChatResponse object with isError false when the chat is successful', async () => {
|
|
|
|
|
const content = 'Test chat content';
|
|
|
|
|
const options: ChatOptions = {
|
|
|
|
|
// Provide mock values for the options
|
|
|
|
|
parent: 'parent_value',
|
|
|
|
|
reference: ['ref1', 'ref2'],
|
|
|
|
|
header: ['header1', 'header2'],
|
|
|
|
|
context: ['context1', 'context2'],
|
|
|
|
|
};
|
|
|
|
|
const mockResponse = {
|
|
|
|
|
exitCode: 0,
|
2024-03-10 16:36:28 +08:00
|
|
|
|
stdout: 'User: Test user\nDate: 2022-01-01\nTest chat response\nprompt-hash: 12345',
|
2023-05-31 16:10:53 +08:00
|
|
|
|
stderr: '',
|
|
|
|
|
};
|
|
|
|
|
const mockWorkspacePath = './';
|
2024-03-10 16:36:28 +08:00
|
|
|
|
const llmModelResponse = {
|
|
|
|
|
"model": "gpt-3.5-turbo",
|
|
|
|
|
"api_key": "DC.1234567890"
|
|
|
|
|
}
|
2023-05-31 16:10:53 +08:00
|
|
|
|
|
|
|
|
|
spawnAsyncStub.resolves(mockResponse);
|
|
|
|
|
workspaceFoldersFirstPathStub.returns(mockWorkspacePath);
|
2024-03-10 16:36:28 +08:00
|
|
|
|
apiKeyManagerStub.resolves(llmModelResponse);
|
2023-05-31 16:10:53 +08:00
|
|
|
|
|
2024-03-10 16:36:28 +08:00
|
|
|
|
const response = await devChat.chat(content, options, (data)=>{}, false);
|
2023-05-31 16:10:53 +08:00
|
|
|
|
|
2024-03-10 16:36:28 +08:00
|
|
|
|
expect(response).to.have.property('prompt-hash', '');
|
|
|
|
|
expect(response).to.have.property('user', '');
|
|
|
|
|
expect(response).to.have.property('date', '');
|
2023-05-31 16:10:53 +08:00
|
|
|
|
expect(response).to.have.property('response', 'Test chat response');
|
|
|
|
|
expect(response).to.have.property('isError', false);
|
|
|
|
|
expect(spawnAsyncStub.calledOnce).to.be.true;
|
|
|
|
|
expect(workspaceFoldersFirstPathStub.calledOnce).to.be.true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Add more test cases for the chat method here
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// ... other test cases
|
|
|
|
|
});
|