11package com.sourcegraph.cody
22
33import com.intellij.openapi.command.WriteCommandAction
4+ import com.intellij.openapi.vfs.VirtualFile
45import com.sourcegraph.cody.agent.CodyAgentService
56import com.sourcegraph.cody.agent.protocol.GetDocumentsParams
67import com.sourcegraph.cody.util.CodyIntegrationTestFixture
78import org.junit.Test
9+ import java.util.concurrent.CompletableFuture
810
911class DocumentSynchronizationTest : CodyIntegrationTestFixture () {
1012
@@ -16,59 +18,68 @@ class DocumentSynchronizationTest : CodyIntegrationTestFixture() {
1618 console.log(\"hello there@\")
1719 }
1820 """
19- .trimIndent()
20- .removePrefix(" \n " )
21+ .trimIndent()
22+ .removePrefix(" \n " )
2123
2224 val expectedContent =
23- """
25+ """
2426 class Foo {
2527 console.log(\"hello there!\")
2628 }
2729 """
28- .trimIndent()
29- .removePrefix(" \n " )
30+ .trimIndent()
31+ .removePrefix(" \n " )
3032
3133 val tempFile = myFixture.createFile(" tempFile.java" , beforeContent)
32- val myUri = tempFile.url
33- myFixture.configureByText(" tempFile.java" , beforeContent)
34-
35- insertBeforeText(beforeContent)
34+ configureFixtureWithFile(tempFile)
35+ setCaretAndSelection()
3636
37- val document = myFixture.editor.document
38- // Write our initial content to the Editor
39- WriteCommandAction .runWriteCommandAction(project) { document.setText(beforeContent) }
37+ val editor = myFixture.editor // Will not be set until we configure the fixture above.
38+ val document = editor.document
4039
41- // Perform our editing action.
4240 WriteCommandAction .runWriteCommandAction(project) {
43- val offset = document.text.indexOf(' @' )
44- document.replaceString(offset, offset + 1 , " !" )
41+ // This is the test-specific editing operation to test.
42+ // TODO: Move everything else here except before/after text, into the test fixture.
43+ document.insertString(editor.caretModel.offset, " !" )
4544 }
4645
47- // Ensure that the Editor's after-text matches expected
46+ // Make sure our own copy of the document was edited properly.
4847 assertEquals(expectedContent, document.text)
4948
49+ checkAgentResults(tempFile, expectedContent)
50+ }
51+
52+ private fun checkAgentResults (tempFile : VirtualFile , expectedContent : String ) {
53+ // Verify that Agent has the correct content, caret, and optionally, selection.
54+ val future = CompletableFuture <Void >()
5055 CodyAgentService .withAgent(project) { agent ->
51- agent.server.awaitPendingPromises() // Wait for Agent to complete its computations.
56+ agent.server.awaitPendingPromises()
5257
5358 val result =
54- agent.server.testingRequestWorkspaceDocuments(GetDocumentsParams (uris = listOf (myUri)))
59+ agent.server.testingRequestWorkspaceDocuments(
60+ GetDocumentsParams (uris = listOf (tempFile.url)))
5561
5662 result.thenAccept { response ->
5763 // There should be one document in the response.
5864 assertEquals(1 , response.documents.size)
5965 // It should have our URI.
6066 val agentDocument = response.documents[0 ]
61- assertEquals(myUri , agentDocument.uri)
67+ assertEquals(tempFile.url , agentDocument.uri)
6268
6369 // It should have the same content as the Editor's after-text.
6470 assertEquals(expectedContent, agentDocument.content)
71+ future.complete(null )
72+ }.exceptionally { ex ->
73+ future.completeExceptionally(ex)
74+ null
6575 }
6676 }
77+ future.get() // Wait for the CompletableFuture to complete
6778 }
6879
69- private fun insertBeforeText ( content : String ) {
80+ private fun setCaretAndSelection ( ) {
7081 WriteCommandAction .runWriteCommandAction(project) {
71- var text = content
82+ var text = myFixture.editor.document.text
7283 var caretOffset = - 1
7384 var selectionStart = - 1
7485 var selectionEnd = - 1
0 commit comments