Skip to content

Commit

Permalink
Chat: Fix "Ask Cody to Explain" (#3015)
Browse files Browse the repository at this point in the history
## Description

- Pass the correct arguments to the `cody.action.chat` command
- Add type safety to avoid this mistake again

closes #2970

## Test plan

1. Try "Ask Cody to Explain"
2. Check there is no error

<!-- Required. See
https://sourcegraph.com/docs/dev/background-information/testing_principles.
-->

---------

Co-authored-by: Beatrix <[email protected]>
  • Loading branch information
umpox and abeatrix authored Feb 5, 2024
1 parent c2b9e9e commit 4cba481
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 4 deletions.
6 changes: 5 additions & 1 deletion lib/shared/src/chat/transcript/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
1 change: 1 addition & 0 deletions vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
- Custom Command: Fixed an issue where custom commands could fail to load due to an invalid entry (e.g. missing prompt). [pull/3012](https://github.com/sourcegraph/cody/pull/3012)

### Changed
Expand Down
9 changes: 8 additions & 1 deletion vscode/src/code-actions/explain.ts
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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:explain',
submitType: 'user-newchat',
} satisfies ExecuteChatArguments,
],
title: 'Ask Cody to Explain',
}
action.diagnostics = diagnostics
Expand Down
77 changes: 77 additions & 0 deletions vscode/test/e2e/code-actions.test.ts
Original file line number Diff line number Diff line change
@@ -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<DotcomUrlOverride>({ 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)
})
4 changes: 2 additions & 2 deletions vscode/test/e2e/custom-command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,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()
Expand Down
9 changes: 9 additions & 0 deletions vscode/test/fixtures/workspace/error.ts
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 4cba481

Please sign in to comment.