-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ba5a763
commit e050427
Showing
5 changed files
with
122 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
src/integrationTest/kotlin/com/sourcegraph/cody/DocumentSynchronizationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
src/main/kotlin/com/sourcegraph/cody/agent/protocol/GetDocumentParams.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>?) |
3 changes: 3 additions & 0 deletions
3
src/main/kotlin/com/sourcegraph/cody/agent/protocol/GetDocumentsResult.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>) |