Skip to content

Commit

Permalink
more work on doc sync tests
Browse files Browse the repository at this point in the history
  • Loading branch information
steveyegge committed Jun 25, 2024
1 parent ba5a763 commit e050427
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/integrationTest/kotlin/com/sourcegraph/cody/AllSuites.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package com.sourcegraph.cody

import com.intellij.openapi.diagnostic.Logger
import com.sourcegraph.cody.agent.CodyAgentService
import com.sourcegraph.cody.edit.DocumentCodeTest
import com.sourcegraph.cody.util.CodyIntegrationTestFixture
import java.util.concurrent.TimeUnit
import org.junit.AfterClass
import org.junit.runner.RunWith
import org.junit.runners.Suite
import java.util.concurrent.TimeUnit

/**
* We need a single tearDown() method running after all tests are complete, so agent can be closed
Expand All @@ -22,7 +21,7 @@ import org.junit.runners.Suite
* and define unique CODY_RECORDING_NAME.
*/
@RunWith(Suite::class)
@Suite.SuiteClasses(DocumentCodeTest::class)
@Suite.SuiteClasses(/* DocumentCodeTest::class, */DocumentSynchronizationTest::class)
class AllSuites {
companion object {
private val logger = Logger.getInstance(AllSuites::class.java)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.sourcegraph.cody

import com.intellij.openapi.command.WriteCommandAction
import com.sourcegraph.cody.agent.CodyAgentService
import com.sourcegraph.cody.agent.protocol.GetDocumentsParams
import com.sourcegraph.cody.util.CodyIntegrationTestFixture
import org.junit.Test

class DocumentSynchronizationTest : CodyIntegrationTestFixture() {

@Test
fun testInsertCharacter() {
val beforeContent =
"""
class Foo {
console.log(\"hello there@\")
}
"""
.trimIndent()
.removePrefix("\n")

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

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

insertBeforeText(beforeContent)

val document = myFixture.editor.document
// Write our initial content to the Editor
WriteCommandAction.runWriteCommandAction(project) { document.setText(beforeContent) }

// Perform our editing action.
WriteCommandAction.runWriteCommandAction(project) {
val offset = document.text.indexOf('@')
document.replaceString(offset, offset + 1, "!")
}

// Ensure that the Editor's after-text matches expected
assertEquals(expectedContent, document.text)

CodyAgentService.withAgent(project) { agent ->
agent.server.awaitPendingPromises() // Wait for Agent to complete its computations.

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

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)

// It should have the same content as the Editor's after-text.
assertEquals(expectedContent, agentDocument.content)
}
}
}

private fun insertBeforeText(content: String) {
WriteCommandAction.runWriteCommandAction(project) {
var text = content
var caretOffset = -1
var selectionStart = -1
var selectionEnd = -1

// Find and remove caret position marker "@"
val caretIndex = text.indexOf("@")
if (caretIndex != -1) {
caretOffset = caretIndex
text = text.removeRange(caretIndex, caretIndex + 1)
}

// Find and remove selection range marker "!"
val selectionIndex = text.indexOf("!")
if (caretIndex != -1 && selectionIndex != -1) {
selectionStart = caretOffset
selectionEnd = selectionIndex - 1 // Adjust for the removal of "!"
text = text.removeRange(selectionIndex, selectionIndex + 1)
}

myFixture.editor.document.setText(text)

// Set caret position if specified
if (caretOffset != -1) {
myFixture.editor.caretModel.moveToOffset(caretOffset)
}

// Set selection range if specified
if (selectionStart != -1 && selectionEnd != -1) {
myFixture.editor.selectionModel.setSelection(selectionStart, selectionEnd)
}
}
}
}
11 changes: 10 additions & 1 deletion src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import com.sourcegraph.cody.agent.protocol.CompletionItemParams
import com.sourcegraph.cody.agent.protocol.CurrentUserCodySubscription
import com.sourcegraph.cody.agent.protocol.EditTask
import com.sourcegraph.cody.agent.protocol.Event
import com.sourcegraph.cody.agent.protocol.GetDocumentsParams
import com.sourcegraph.cody.agent.protocol.GetDocumentsResult
import com.sourcegraph.cody.agent.protocol.GetFeatureFlag
import com.sourcegraph.cody.agent.protocol.GetFoldingRangeParams
import com.sourcegraph.cody.agent.protocol.GetFoldingRangeResult
Expand All @@ -33,9 +35,9 @@ import com.sourcegraph.cody.agent.protocol.ServerInfo
import com.sourcegraph.cody.agent.protocol.TaskIdParam
import com.sourcegraph.cody.agent.protocol.TelemetryEvent
import com.sourcegraph.cody.chat.ConnectionId
import java.util.concurrent.CompletableFuture
import org.eclipse.lsp4j.jsonrpc.services.JsonNotification
import org.eclipse.lsp4j.jsonrpc.services.JsonRequest
import java.util.concurrent.CompletableFuture

/**
* Interface for the server-part of the Cody agent protocol. The implementation of this interface is
Expand All @@ -57,6 +59,7 @@ interface CodyAgentServer {

@JsonRequest("graphql/logEvent") fun logEvent(event: Event): CompletableFuture<Void?>

@Suppress("unused")
@JsonRequest("graphql/currentUserId") fun currentUserId(): CompletableFuture<String>

@JsonRequest("graphql/getRepoIds")
Expand Down Expand Up @@ -157,4 +160,10 @@ interface CodyAgentServer {

@JsonRequest("testing/requestErrors")
fun testingRequestErrors(): CompletableFuture<List<NetworkRequest>>

@JsonRequest("testing/requestWorkspaceDocuments")
fun testingRequestWorkspaceDocuments(params: GetDocumentsParams): CompletableFuture<GetDocumentsResult>

@JsonRequest("testing/awaitPendingPromises")
fun awaitPendingPromises(): CompletableFuture<Unit>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.sourcegraph.cody.agent.protocol

data class GetDocumentsParams(val uris: List<String>?)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.sourcegraph.cody.agent.protocol

data class GetDocumentsResult(val documents: List<ProtocolTextDocument>)

0 comments on commit e050427

Please sign in to comment.