gencay / vscode-chatgpt

An unofficial Visual Studio Code - OpenAI ChatGPT integration
ISC License
3.5k stars 760 forks source link

Promptcrafting: have the extension send the correct prompt to chatGPT #2

Closed ayim closed 1 year ago

ayim commented 1 year ago

Give an input of:

Question 1: What are some test cases for this code? Question 2: Using chai, write some unit tests for this code:

public async search(prompt) {
        const isSignedIn = await this._chatGPTAPI.getIsSignedIn();
        if (!isSignedIn) {
            await this._chatGPTAPI.init();
        }

        const languageId = vscode.window.activeTextEditor?.document.languageId;
        const surroundingText = vscode.window.activeTextEditor?.document.getText();

        // get the selected text
        const selection = vscode.window.activeTextEditor?.selection;
        const selectedText = vscode.window.activeTextEditor?.document.getText(selection);
        let searchPrompt = '';

        if (selection && selectedText) {
            searchPrompt = `${selectedText}

    ${prompt}`;
        } else {
            searchPrompt = `This is the ${languageId} file I'm working on:
    ${surroundingText}

    ${prompt}`;
        }

        const response = await this._chatGPTAPI.sendMessage(searchPrompt);

        if (this._view) {
            this._view.show?.(true);
            this._view.webview.postMessage({ type: 'addResponse', value: response });
        }
    }

The following response (or something similar) is acquired:

Here are some test cases for the code:

Test the code when the user is signed in. Test the code when the user is not signed in. Test the code when there is an active text editor with a selected text. Test the code when there is an active text editor without a selected text. Test the code when there is no active text editor. Here is an example of how you can write some unit tests for this code using Chai:



describe('search()', function() { let chatGPTAPIStub; let viewStub;

beforeEach(function() { // Set up stubs for dependencies chatGPTAPIStub = sinon.stub({ getIsSignedIn: () => {}, init: () => {}, sendMessage: () => {} }); viewStub = sinon.stub({ show: () => {}, webview: { postMessage: () => {} } }); });

it('should check if the user is signed in', async function() { // Set up test case const searchPrompt = 'What is the weather like today?'; const isSignedIn = true; chatGPTAPIStub.getIsSignedIn.resolves(isSignedIn);

// Call search()
await search(searchPrompt);

// Assert that getIsSignedIn() was called
expect(chatGPTAPIStub.getIsSignedIn.calledOnce).to.be.true;
});

it('should initialize the chat GPT API if the user is not signed in', async function() { // Set up test case const searchPrompt = 'What is the weather like today?'; const isSignedIn = false; chatGPTAPIStub.getIsSignedIn.resolves(isSignedIn);

// Call search()
await search(searchPrompt);

// Assert that init() was called
expect(chatGPTAPIStub.init.calledOnce).to.be.true;
});

it('should get the language ID of the active text editor', async function() { // Set up test case const searchPrompt = 'What is the weather like today?'; const isSignedIn = true; const languageId = 'python'; chatGPTAPIStub.getIsSignedIn.resolves(isSignedIn); // Stub activeTextEditor to return an object with a languageId property vscode.window.activeTextEditor = { document: { languageId } };

// Call search()
await search(searchPrompt);

// Assert that the correct language ID was retrieved
expect(languageId).to.equal('python');
});

it('should get the surrounding text of the active text editor', async function() { // Set up test case const searchPrompt = 'What is the weather like today?'; const isSignedIn = true; const surroundingText = 'This