Skip to content

Commit 7f7f1f0

Browse files
committed
tests
1 parent e8f2f78 commit 7f7f1f0

File tree

2 files changed

+45
-19
lines changed

2 files changed

+45
-19
lines changed

plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/workspace/WorkspaceServiceHandler.kt

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -139,25 +139,27 @@ class WorkspaceServiceHandler(
139139
val oldUri = toUriString(parentFile)?.let { parentUri -> "$parentUri/$oldFileName" }
140140
val newUri = toUriString(renamedFile)
141141

142-
oldUri?.let { uri ->
143-
languageServer.textDocumentService.didClose(
144-
DidCloseTextDocumentParams().apply {
145-
textDocument = TextDocumentIdentifier().apply {
146-
this.uri = uri
142+
if (!renamedFile.isDirectory) {
143+
oldUri?.let { uri ->
144+
languageServer.textDocumentService.didClose(
145+
DidCloseTextDocumentParams().apply {
146+
textDocument = TextDocumentIdentifier().apply {
147+
this.uri = uri
148+
}
147149
}
148-
}
149-
)
150-
}
150+
)
151+
}
151152

152-
newUri?.let { uri ->
153-
languageServer.textDocumentService.didOpen(
154-
DidOpenTextDocumentParams().apply {
155-
textDocument = TextDocumentItem().apply {
156-
this.uri = uri
157-
text = renamedFile.inputStream.readAllBytes().decodeToString()
153+
newUri?.let { uri ->
154+
languageServer.textDocumentService.didOpen(
155+
DidOpenTextDocumentParams().apply {
156+
textDocument = TextDocumentItem().apply {
157+
this.uri = uri
158+
text = renamedFile.inputStream.readAllBytes().decodeToString()
159+
}
158160
}
159-
}
160-
)
161+
)
162+
}
161163
}
162164

163165
FileRename().apply {

plugins/amazonq/shared/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/amazonq/lsp/workspace/WorkspaceServiceHandlerTest.kt

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@ import org.eclipse.lsp4j.CreateFilesParams
3030
import org.eclipse.lsp4j.DeleteFilesParams
3131
import org.eclipse.lsp4j.DidChangeWatchedFilesParams
3232
import org.eclipse.lsp4j.DidChangeWorkspaceFoldersParams
33+
import org.eclipse.lsp4j.DidCloseTextDocumentParams
34+
import org.eclipse.lsp4j.DidOpenTextDocumentParams
3335
import org.eclipse.lsp4j.FileChangeType
3436
import org.eclipse.lsp4j.RenameFilesParams
3537
import org.eclipse.lsp4j.WorkspaceFolder
3638
import org.eclipse.lsp4j.jsonrpc.messages.ResponseMessage
39+
import org.eclipse.lsp4j.services.TextDocumentService
3740
import org.eclipse.lsp4j.services.WorkspaceService
3841
import org.junit.jupiter.api.Assertions.assertEquals
3942
import org.junit.jupiter.api.BeforeEach
@@ -50,13 +53,15 @@ class WorkspaceServiceHandlerTest {
5053
private lateinit var project: Project
5154
private lateinit var mockLanguageServer: AmazonQLanguageServer
5255
private lateinit var mockWorkspaceService: WorkspaceService
56+
private lateinit var mockTextDocumentService: TextDocumentService
5357
private lateinit var sut: WorkspaceServiceHandler
5458
private lateinit var mockApplication: Application
5559

5660
@BeforeEach
5761
fun setup() {
5862
project = mockk<Project>()
5963
mockWorkspaceService = mockk<WorkspaceService>()
64+
mockTextDocumentService = mockk<TextDocumentService>()
6065
mockLanguageServer = mockk<AmazonQLanguageServer>()
6166

6267
mockApplication = mockk<Application>()
@@ -89,6 +94,11 @@ class WorkspaceServiceHandlerTest {
8994
every { mockWorkspaceService.didChangeWatchedFiles(any()) } returns Unit
9095
every { mockWorkspaceService.didChangeWorkspaceFolders(any()) } returns Unit
9196

97+
// Mock textDocument service (for didRename calls)
98+
every { mockLanguageServer.textDocumentService } returns mockTextDocumentService
99+
every { mockTextDocumentService.didOpen(any()) } returns Unit
100+
every { mockTextDocumentService.didClose(any()) } returns Unit
101+
92102
// Mock message bus
93103
val messageBus = mockk<MessageBus>()
94104
every { project.messageBus } returns messageBus
@@ -389,10 +399,19 @@ class WorkspaceServiceHandlerTest {
389399
// Act
390400
sut.after(listOf(propertyEvent))
391401

402+
val closeParams = slot<DidCloseTextDocumentParams>()
403+
verify { mockTextDocumentService.didClose(capture(closeParams)) }
404+
assertEquals(normalizeFileUri("file:///testDir/$oldName"), closeParams.captured.textDocument.uri)
405+
406+
val openParams = slot<DidOpenTextDocumentParams>()
407+
verify { mockTextDocumentService.didOpen(capture(openParams)) }
408+
assertEquals(normalizeFileUri("file:///testDir/$newName"), openParams.captured.textDocument.uri)
409+
assertEquals(normalizeFileUri("content"), openParams.captured.textDocument.text)
410+
392411
// Assert
393-
val paramsSlot = slot<RenameFilesParams>()
394-
verify { mockWorkspaceService.didRenameFiles(capture(paramsSlot)) }
395-
with(paramsSlot.captured.files[0]) {
412+
val renameParams = slot<RenameFilesParams>()
413+
verify { mockWorkspaceService.didRenameFiles(capture(renameParams)) }
414+
with(renameParams.captured.files[0]) {
396415
assertEquals(normalizeFileUri("file:///testDir/$oldName"), oldUri)
397416
assertEquals(normalizeFileUri("file:///testDir/$newName"), newUri)
398417
}
@@ -411,6 +430,8 @@ class WorkspaceServiceHandlerTest {
411430
sut.after(listOf(propertyEvent))
412431

413432
// Assert
433+
verify(exactly = 0) { mockTextDocumentService.didClose(any()) }
434+
verify(exactly = 0) { mockTextDocumentService.didOpen(any()) }
414435
verify(exactly = 0) { mockWorkspaceService.didRenameFiles(any()) }
415436
}
416437

@@ -427,6 +448,8 @@ class WorkspaceServiceHandlerTest {
427448
sut.after(listOf(propertyEvent))
428449

429450
// Assert
451+
verify(exactly = 0) { mockTextDocumentService.didClose(any()) }
452+
verify(exactly = 0) { mockTextDocumentService.didOpen(any()) }
430453
val paramsSlot = slot<RenameFilesParams>()
431454
verify { mockWorkspaceService.didRenameFiles(capture(paramsSlot)) }
432455
with(paramsSlot.captured.files[0]) {
@@ -624,6 +647,7 @@ class WorkspaceServiceHandlerTest {
624647
every { fileSystem } returns mockk {
625648
every { protocol } returns "file"
626649
}
650+
every { this@mockk.inputStream } returns "content".byteInputStream()
627651
}
628652
}
629653

0 commit comments

Comments
 (0)