Skip to content

Commit

Permalink
more progress on integration test
Browse files Browse the repository at this point in the history
they still don't pass but they are getting closer
  • Loading branch information
steveyegge committed Jun 26, 2024
1 parent 540560d commit bda3bbe
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.sourcegraph.cody

import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.vfs.VirtualFile
import com.sourcegraph.cody.agent.CodyAgentService
import com.sourcegraph.cody.agent.protocol.GetDocumentsParams
import com.sourcegraph.cody.util.CodyIntegrationTestFixture
import org.junit.Test
import java.util.concurrent.CompletableFuture

class DocumentSynchronizationTest : CodyIntegrationTestFixture() {

Expand All @@ -16,59 +18,68 @@ class DocumentSynchronizationTest : CodyIntegrationTestFixture() {
console.log(\"hello there@\")
}
"""
.trimIndent()
.removePrefix("\n")
.trimIndent()
.removePrefix("\n")

val expectedContent =
"""
"""
class Foo {
console.log(\"hello there!\")
}
"""
.trimIndent()
.removePrefix("\n")
.trimIndent()
.removePrefix("\n")

val tempFile = myFixture.createFile("tempFile.java", beforeContent)
val myUri = tempFile.url
myFixture.configureByText("tempFile.java", beforeContent)

insertBeforeText(beforeContent)
configureFixtureWithFile(tempFile)
setCaretAndSelection()

val document = myFixture.editor.document
// Write our initial content to the Editor
WriteCommandAction.runWriteCommandAction(project) { document.setText(beforeContent) }
val editor = myFixture.editor // Will not be set until we configure the fixture above.
val document = editor.document

// Perform our editing action.
WriteCommandAction.runWriteCommandAction(project) {
val offset = document.text.indexOf('@')
document.replaceString(offset, offset + 1, "!")
// This is the test-specific editing operation to test.
// TODO: Move everything else here except before/after text, into the test fixture.
document.insertString(editor.caretModel.offset, "!")
}

// Ensure that the Editor's after-text matches expected
// Make sure our own copy of the document was edited properly.
assertEquals(expectedContent, document.text)

checkAgentResults(tempFile, expectedContent)
}

private fun checkAgentResults(tempFile: VirtualFile, expectedContent: String) {
// Verify that Agent has the correct content, caret, and optionally, selection.
val future = CompletableFuture<Void>()
CodyAgentService.withAgent(project) { agent ->
agent.server.awaitPendingPromises() // Wait for Agent to complete its computations.
agent.server.awaitPendingPromises()

val result =
agent.server.testingRequestWorkspaceDocuments(GetDocumentsParams(uris = listOf(myUri)))
agent.server.testingRequestWorkspaceDocuments(
GetDocumentsParams(uris = listOf(tempFile.url)))

result.thenAccept { response ->
// There should be one document in the response.
assertEquals(1, response.documents.size)
// It should have our URI.
val agentDocument = response.documents[0]
assertEquals(myUri, agentDocument.uri)
assertEquals(tempFile.url, agentDocument.uri)

// It should have the same content as the Editor's after-text.
assertEquals(expectedContent, agentDocument.content)
future.complete(null)
}.exceptionally { ex ->
future.completeExceptionally(ex)
null
}
}
future.get() // Wait for the CompletableFuture to complete
}

private fun insertBeforeText(content: String) {
private fun setCaretAndSelection() {
WriteCommandAction.runWriteCommandAction(project) {
var text = content
var text = myFixture.editor.document.text
var caretOffset = -1
var selectionStart = -1
var selectionEnd = -1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.intellij.openapi.editor.ex.EditorEx
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.project.DumbService
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.testFramework.EditorTestUtil
import com.intellij.testFramework.PlatformTestUtil
import com.intellij.testFramework.fixtures.BasePlatformTestCase
Expand All @@ -22,13 +23,13 @@ import com.sourcegraph.cody.edit.CodyInlineEditActionNotifier
import com.sourcegraph.cody.edit.FixupService
import com.sourcegraph.cody.edit.sessions.FixupSession
import com.sourcegraph.config.ConfigUtil
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import java.io.File
import java.nio.file.Paths
import java.util.concurrent.CompletableFuture
import java.util.concurrent.TimeUnit
import java.util.regex.Pattern
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@RunWith(JUnit4::class)
abstract class CodyIntegrationTestFixture : BasePlatformTestCase() {
Expand Down Expand Up @@ -91,6 +92,16 @@ abstract class CodyIntegrationTestFixture : BasePlatformTestCase() {
initCaretPosition()
}

protected fun configureFixtureWithFile(testFile: VirtualFile) {
val policy = System.getProperty("idea.test.execution.policy")
assertTrue(policy == "com.sourcegraph.cody.test.NonEdtIdeaTestExecutionPolicy")

myFixture.configureFromExistingVirtualFile(testFile)

initCredentialsAndAgent()
initCaretPosition()
}

override fun getTestDataPath(): String {
if (project == null) {
return super.getTestDataPath()
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/com/sourcegraph/cody/agent/CodyAgent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import com.intellij.util.system.CpuArch
import com.sourcegraph.cody.agent.protocol.*
import com.sourcegraph.cody.vscode.CancellationToken
import com.sourcegraph.config.ConfigUtil
import org.eclipse.lsp4j.jsonrpc.Launcher
import java.io.*
import java.net.Socket
import java.net.URI
import java.nio.file.*
import java.util.*
import java.util.concurrent.*
import org.eclipse.lsp4j.jsonrpc.Launcher

/**
* Orchestrator for the Cody agent, which is a Node.js program that implements the prompt logic for
Expand Down Expand Up @@ -192,7 +192,7 @@ private constructor(

logger.info("starting Cody agent ${command.joinToString(" ")}")
logger.info(
"Cody agent proxyUrl ${proxyUrl} PROXY_TYPE_IS_SOCKS ${proxy.PROXY_TYPE_IS_SOCKS}")
"Cody agent proxyUrl $proxyUrl PROXY_TYPE_IS_SOCKS ${proxy.PROXY_TYPE_IS_SOCKS}")

val process =
processBuilder
Expand Down

0 comments on commit bda3bbe

Please sign in to comment.