From 90d1832493d524027a76c9f31b4b3e94f6ac7c73 Mon Sep 17 00:00:00 2001 From: Tom Ross Date: Fri, 2 Feb 2024 13:39:49 +0000 Subject: [PATCH 1/6] Chat: Fix Ask Cody to Explain --- vscode/src/code-actions/explain.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/vscode/src/code-actions/explain.ts b/vscode/src/code-actions/explain.ts index 753bb6c27e..192ccd48a9 100644 --- a/vscode/src/code-actions/explain.ts +++ b/vscode/src/code-actions/explain.ts @@ -1,4 +1,5 @@ import * as vscode from 'vscode' +import type { ExecuteChatArguments } from '../commands/execute/ask' export class ExplainCodeAction implements vscode.CodeActionProvider { public static readonly providedCodeActionKinds = [vscode.CodeActionKind.QuickFix] @@ -24,7 +25,13 @@ export class ExplainCodeAction implements vscode.CodeActionProvider { const instruction = this.getCodeActionInstruction(diagnostics) action.command = { command: 'cody.action.chat', - arguments: [instruction, { source: 'code-action:explain' }], + arguments: [ + { + text: instruction, + source: 'code-action', + submitType: 'user-newchat', + } satisfies ExecuteChatArguments, + ], title: 'Ask Cody to Explain', } action.diagnostics = diagnostics From 91f0b6b5245696e52f2d3b24fe9cd45767265545 Mon Sep 17 00:00:00 2001 From: Tom Ross Date: Fri, 2 Feb 2024 14:01:16 +0000 Subject: [PATCH 2/6] fix event source --- lib/shared/src/chat/transcript/messages.ts | 6 +++++- vscode/src/code-actions/explain.ts | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/shared/src/chat/transcript/messages.ts b/lib/shared/src/chat/transcript/messages.ts index 780977177c..1109141655 100644 --- a/lib/shared/src/chat/transcript/messages.ts +++ b/lib/shared/src/chat/transcript/messages.ts @@ -69,7 +69,11 @@ export type ChatEventSource = | 'chat' | 'editor' | 'menu' - | 'code-action' + | 'code-action:explain' + | 'code-action:document' + | 'code-action:edit' + | 'code-action:fix' + | 'code-action:generate' | 'custom-commands' | 'test' | 'code-lens' diff --git a/vscode/src/code-actions/explain.ts b/vscode/src/code-actions/explain.ts index 192ccd48a9..383d8352d4 100644 --- a/vscode/src/code-actions/explain.ts +++ b/vscode/src/code-actions/explain.ts @@ -28,7 +28,7 @@ export class ExplainCodeAction implements vscode.CodeActionProvider { arguments: [ { text: instruction, - source: 'code-action', + source: 'code-action:explain', submitType: 'user-newchat', } satisfies ExecuteChatArguments, ], From f294ec7efa5395d33b2f27492cd0d253ca70a89f Mon Sep 17 00:00:00 2001 From: Tom Ross Date: Fri, 2 Feb 2024 15:22:42 +0000 Subject: [PATCH 3/6] add code action e2e tests --- vscode/test/e2e/code-actions.test.ts | 77 +++++++++++++++++++++++++ vscode/test/fixtures/workspace/error.ts | 9 +++ 2 files changed, 86 insertions(+) create mode 100644 vscode/test/e2e/code-actions.test.ts create mode 100644 vscode/test/fixtures/workspace/error.ts diff --git a/vscode/test/e2e/code-actions.test.ts b/vscode/test/e2e/code-actions.test.ts new file mode 100644 index 0000000000..be8ec9f7de --- /dev/null +++ b/vscode/test/e2e/code-actions.test.ts @@ -0,0 +1,77 @@ +import * as mockServer from '../fixtures/mock-server' + +import { sidebarExplorer, sidebarSignin } from './common' +import { type DotcomUrlOverride, test as baseTest, assertEvents } from './helpers' + +const test = baseTest.extend({ dotcomUrl: mockServer.SERVER_URL }) + +test.beforeEach(() => { + mockServer.resetLoggedEvents() +}) + +const ERROR_DECORATION_SELECTOR = 'div.view-overlays[role="presentation"] div[class*="squiggly-error"]' + +test('code action: explain', async ({ page, sidebar }) => { + // Sign into Cody + await sidebarSignin(page, sidebar) + + // Open the Explorer view from the sidebar + await sidebarExplorer(page).click() + // Open the error.ts file from the tree view + await page.getByRole('treeitem', { name: 'error.ts' }).locator('a').click() + // Wait for error.ts to fully open + await page.getByRole('tab', { name: 'error.ts' }).hover() + + // Remove the comment that suppresses the type error + await page.getByText('// @ts-nocheck').click({ clickCount: 3 }) + await page.keyboard.press('Backspace') + + // Activate the code action on the erred text + const erredText = page.getByText('logNumber').nth(1) + await page.waitForSelector(ERROR_DECORATION_SELECTOR) + await erredText.click() + await erredText.hover() + await page.getByRole('button', { name: 'Quick Fix... (⌘.)' }).click() + // Get by text takes a very long time, it's faster to type and let the quick fix item be focused + await page.keyboard.type('Explain') + await page.keyboard.press('Enter') + + const expectedEvents = [ + 'CodyVSCodeExtension:chat-question:submitted', + 'CodyVSCodeExtension:chat-question:executed', + ] + await assertEvents(mockServer.loggedEvents, expectedEvents) +}) + +test('code action: fix', async ({ page, sidebar }) => { + // Sign into Cody + await sidebarSignin(page, sidebar) + + // Open the Explorer view from the sidebar + await sidebarExplorer(page).click() + // Open the error.ts file from the tree view + await page.getByRole('treeitem', { name: 'error.ts' }).locator('a').click() + // Wait for error.ts to fully open + await page.getByRole('tab', { name: 'error.ts' }).hover() + + // Remove the comment that suppresses the type error + await page.getByText('// @ts-nocheck').click({ clickCount: 3 }) + await page.keyboard.press('Backspace') + + // Activate the code action on the erred text + const erredText = page.getByText('logNumber').nth(1) + await page.waitForSelector(ERROR_DECORATION_SELECTOR) + await erredText.click() + await erredText.hover() + await page.getByRole('button', { name: 'Quick Fix... (⌘.)' }).click() + // Get by text takes a very long time, it's faster to type and let the quick fix item be focused + await page.keyboard.type('Fix') + await page.keyboard.press('Enter') + + const expectedEvents = [ + 'CodyVSCodeExtension:command:edit:executed', + 'CodyVSCodeExtension:fixupResponse:hasCode', + 'CodyVSCodeExtension:fixup:applied', + ] + await assertEvents(mockServer.loggedEvents, expectedEvents) +}) diff --git a/vscode/test/fixtures/workspace/error.ts b/vscode/test/fixtures/workspace/error.ts new file mode 100644 index 0000000000..f3dfeb6d44 --- /dev/null +++ b/vscode/test/fixtures/workspace/error.ts @@ -0,0 +1,9 @@ +// @ts-nocheck +const logNumber = (number: number) => { + console.log('What a great number!', number) +} + +const hasError = () => { + const numbers = [1, 2, '3', 4, 5] + numbers.forEach(logNumber) +} From 01626c5eb46969c156de2f43e96090c2c10efb6f Mon Sep 17 00:00:00 2001 From: Tom Ross Date: Fri, 2 Feb 2024 15:53:02 +0000 Subject: [PATCH 4/6] more reliable test --- vscode/test/e2e/code-actions.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vscode/test/e2e/code-actions.test.ts b/vscode/test/e2e/code-actions.test.ts index be8ec9f7de..ddc18e060e 100644 --- a/vscode/test/e2e/code-actions.test.ts +++ b/vscode/test/e2e/code-actions.test.ts @@ -31,7 +31,7 @@ test('code action: explain', async ({ page, sidebar }) => { await page.waitForSelector(ERROR_DECORATION_SELECTOR) await erredText.click() await erredText.hover() - await page.getByRole('button', { name: 'Quick Fix... (⌘.)' }).click() + await page.getByRole('button', { name: /Quick Fix/ }).click() // Get by text takes a very long time, it's faster to type and let the quick fix item be focused await page.keyboard.type('Explain') await page.keyboard.press('Enter') @@ -63,7 +63,7 @@ test('code action: fix', async ({ page, sidebar }) => { await page.waitForSelector(ERROR_DECORATION_SELECTOR) await erredText.click() await erredText.hover() - await page.getByRole('button', { name: 'Quick Fix... (⌘.)' }).click() + await page.getByRole('button', { name: /Quick Fix/ }).click() // Get by text takes a very long time, it's faster to type and let the quick fix item be focused await page.keyboard.type('Fix') await page.keyboard.press('Enter') From 1d3d739c05898831a64b02bcc4ac04dd5c386357 Mon Sep 17 00:00:00 2001 From: Tom Ross Date: Fri, 2 Feb 2024 16:00:36 +0000 Subject: [PATCH 5/6] changelog --- vscode/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/vscode/CHANGELOG.md b/vscode/CHANGELOG.md index 3804e070eb..23315f862a 100644 --- a/vscode/CHANGELOG.md +++ b/vscode/CHANGELOG.md @@ -20,6 +20,7 @@ This is a log of all notable changes to Cody for VS Code. [Unreleased] changes a ### Fixed - Chat: Messages without enhanced context should not include the sparkle emoji in context list. [pull/3006](https://github.com/sourcegraph/cody/pull/3006) +- Edit: Fixed an issue where "Ask Cody to Explain" would result in an error. [pull/3015](https://github.com/sourcegraph/cody/pull/3015) ### Changed From db799a12096ae8a3b580fc5fedcbd4942d6380e4 Mon Sep 17 00:00:00 2001 From: Beatrix Date: Fri, 2 Feb 2024 09:59:43 -0800 Subject: [PATCH 6/6] update e2e test --- vscode/test/e2e/custom-command.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vscode/test/e2e/custom-command.test.ts b/vscode/test/e2e/custom-command.test.ts index 7c10de11a8..0b7330b2ad 100644 --- a/vscode/test/e2e/custom-command.test.ts +++ b/vscode/test/e2e/custom-command.test.ts @@ -123,8 +123,8 @@ test('execute custom commands with context defined in cody.json', async ({ page, await expect(chatPanel.getByText('Add four context files from the current directory.')).toBeVisible() // Show the current file numbers used as context - await expect(chatPanel.getByText('✨ Context: 55 lines from 4 files')).toBeVisible() - await chatPanel.getByText('✨ Context: 55 lines from 4 files').click() + await expect(chatPanel.getByText('✨ Context: 66 lines from 5 files')).toBeVisible() + await chatPanel.getByText('✨ Context: 66 lines from 5 files').click() // Display the context files to confirm no hidden files are included await expect(chatPanel.locator('span').filter({ hasText: '@Main.java:1-9' })).toBeVisible() await expect(chatPanel.locator('span').filter({ hasText: '@buzz.test.ts:1-12' })).toBeVisible()