Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: test (edit) command & add tests code lenses #2959

Merged
merged 17 commits into from
Feb 1, 2024
Merged

Conversation

abeatrix
Copy link
Contributor

@abeatrix abeatrix commented Jan 30, 2024

CLOSE #1475 & #1761

PR to introduce /unit command that is a smarter version of the /test command.

/unit command

  • /unit command generates unit tests in chat (old /test)
  • /test command generates unit tests in line
    • if a test file exists, the new unit test suit will be appended to the end of the current test file
    • if a test file does not exist, cody will suggest a file name and we will create a temporary test file with the new test suit appended along with the import statements (if any)

unit-test-file

Add Tests code lenses

add-tests is a command that can be executed via the Cody Command CodeLenses (when enabled) in test files. It is not available via the command menu or context menu.

Behind internal unstable flag.

unit-test-case

Summary

This PR includes the following change:

  • Refactor the /unit command, rename it to /test, and move it out of unstable
  • Rename the old /test command to /unit
  • Add test-case command
  • Add executeUnitCaseCommand to execute unit test cases
  • Added support for specifying a destination file in the executeEditArguments interface in the edit/execute.ts file.
  • Added support for specifying a destination file in the executeEdit method in the edit/manager.ts file.
  • Removed the NewFixupFileMap import and added support for specifying a destination file in the EditProvider class in the edit/provider.ts file.
  • Removed the NewFixupFileMap import in the FixupController class in the non-stop/FixupController.ts file.

Test plan

Added e2e test.

Test

Run the Test command to confirm the newly generated tests are added to a new / exisiting test file automatically for you.

image

Add Tests

Enable Code Lenses in your Cody Setting

image

Locate the Add Tests code lenses in a test file to check if new test cases have been inserted in line for you

image

Follow-up

  • option to append to current selection instead of insert
  • context improvement for test

@abeatrix abeatrix requested review from toolmantim, umpox and a team January 30, 2024 23:39
Copy link
Contributor

@toolmantim toolmantim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woo!

  • Why the rename to /unit? I don't see a strong argument for renaming — I'd recommend we keep a single /test command (the edit version) and replace the old /test (chat version) with this one
  • I couldn't get this to work well with Python or Typescript… it seems to replace the code with the tests (see videos below)
  • We might need to add some kind of progress that isn't just a spinner, because tests can take quite a while to generate. Maybe something like \Generating tests… (${n} lines)``?
  • Is the "unit-test-case" command — it's an interesting idea, but I don't fully understand what problem it's trying to solve. Could you expand more on how it fits into the overall flow?
CleanShot.2024-01-31.at.16.38.16.mp4
CleanShot.2024-01-31.at.16.31.13.mp4

@abeatrix
Copy link
Contributor Author

Why the rename to /unit? I don't see a strong argument for renaming — I'd recommend we keep a single /test command (the edit version) and replace the old /test (chat version) with this one

Because jetBrains is using the old one so if I was worried switching it would cause issue for them, but I think I can find a workaround for that.

@abeatrix
Copy link
Contributor Author

Is the "unit-test-case" command — it's an interesting idea, but I don't fully understand what problem it's trying to solve. Could you expand more on how it fits into the overall flow?

That's for adding more tests to an existing test suite inside the test file, vs create a new test suite with tests using other test context as reference, adding more tests to an existing test suite should be easier for the llm and the result should have a higher quality from what I've tested so far.

@abeatrix
Copy link
Contributor Author

I couldn't get this to work well with Python or Typescript… it seems to replace the code with the tests (see videos below)

Let me try to reproduce it. Can you try it on non-react typescript code in the meantime?

@toolmantim
Copy link
Contributor

That's for adding more tests to an existing test suite inside the test file, vs create a new test suite with tests using other test context as reference, adding more tests to an existing test suite should be easier for the llm and the result should have a higher quality from what I've tested so far.

That's interesting about higher quality — is that higher quality output for the same user problem? (i.e. same source and test file, but running the command from the test file first rather than the source file first).

I still don't quite understand what "Add Tests" quite means, or how it fits into their overall flow though. When would someone know to hit and not hit the "Add Tests" button?

@abeatrix
Copy link
Contributor Author

i.e. same source and test file, but running the command from the test file first rather than the source file first

Yes. You will only see this code lense in test files. You will not see this in source file.

I still don't quite understand what "Add Tests" quite means, or how it fits into their overall flow though. When would someone know to hit and not hit the "Add Tests" button?

it only shows up on top of each test suite, and clicking "Add Tests" adds new tests to the associated test suite. You don't see them in non-test files.

image

@abeatrix
Copy link
Contributor Author

For example, i started the suite with only 1 test:

image

Press Add tests:

image

Ended up with more tests:

image

@abeatrix
Copy link
Contributor Author

@toolmantim Can you share the link to the repo you tested with to see if I can reproduce the issue? I can't reproduce the issue when testing it on the langchain python repository.

python.mov

@abeatrix
Copy link
Contributor Author

  • rename the new unit test command to /test
  • replace the old /test (chat version) with /unit

@abeatrix abeatrix changed the title feat: unit test command & unit test case command feat: test (edit) command & add tests code lenses Jan 31, 2024
@umpox
Copy link
Contributor

umpox commented Jan 31, 2024

Noticed one thing, it seems to often not stream the text into the new file for me. It's a bit off putting because it feels like nothing is working, but actually the file seems to be just created at the end.

I got a good video of it doing this:

TestStreaming.mov

It looks like the file name isn't properly returned by the LLM. WDYT about maybe firing two LLM requests for this command:

  1. The edit one
  2. A separate one to get the file name, if it doesn't exist. Then we maybe have a better chance of getting a valid response?

We had something similar for the intent detector: https://sourcegraph.com/github.com/sourcegraph/cody@bee/new-commands/-/blob/lib/shared/src/intent-detector/client.ts?L66%3A1-125%3A6

We could do the same here?

Copy link
Contributor

@umpox umpox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some minor comments. Looking really good!

vscode/src/commands/execute/test-case.ts Outdated Show resolved Hide resolved
vscode/src/commands/execute/test-case.ts Outdated Show resolved Hide resolved
vscode/src/commands/execute/test-file.ts Outdated Show resolved Hide resolved
vscode/src/commands/services/CommandCodeLenses.ts Outdated Show resolved Hide resolved
vscode/src/commands/execute/test-case.ts Outdated Show resolved Hide resolved
Comment on lines 91 to 102
const symbols =
allSymbols?.filter(
symbol =>
symbol.kind === vscode.SymbolKind.Function ||
symbol.kind === vscode.SymbolKind.Class ||
symbol.kind === vscode.SymbolKind.Method ||
symbol.kind === vscode.SymbolKind.Constructor
) ?? []

// Add code lenses for each symbol
if (symbols) {
// For test files, adds code lenses for each symbol
if (isTest) {
for (const symbol of symbols) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the downside here is that this will show for random functions too, e.g.
image

I guess we don't really have a way to determine which symbols are actually used for tests here :/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea we should update the code lenses logic in a follow up PR!

vscode/src/non-stop/FixupController.ts Outdated Show resolved Hide resolved
vscode/src/non-stop/FixupTask.ts Show resolved Hide resolved
abeatrix and others added 2 commits January 31, 2024 07:24
Co-authored-by: Tom Ross <tom@umpox.com>
Co-authored-by: Tom Ross <tom@umpox.com>
@abeatrix
Copy link
Contributor Author

@toolmantim @umpox i've updated the prompt so that it will always generate the file name. Before this change, if cody doesn't reply with a file name suggestion, we would wait for the test to finish before adding the code, causing a delay in showing the response.
The updated prompt now reminds the LLM provider to start the response with a file name suggestion.

@abeatrix
Copy link
Contributor Author

@toolmantim also put the Add Tests code lense behind the unstable flag so the new /test command won't get blocked by it.

Copy link
Contributor

@toolmantim toolmantim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works great with the examples I found now! Let's just go with a single /test command (edit) for now though.

@abeatrix abeatrix merged commit 0513f7a into main Feb 1, 2024
15 checks passed
@abeatrix abeatrix deleted the bee/new-commands branch February 1, 2024 00:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

VSCE: Improve Generate Unit Tests command
3 participants