@@ -9,6 +9,7 @@ import com.intellij.openapi.application.ApplicationManager
9
9
import com.intellij.openapi.components.serviceIfCreated
10
10
import com.intellij.openapi.editor.Document
11
11
import com.intellij.openapi.fileEditor.FileDocumentManager
12
+ import com.intellij.openapi.fileEditor.FileEditorManager
12
13
import com.intellij.openapi.project.Project
13
14
import com.intellij.openapi.vfs.VirtualFile
14
15
import com.intellij.openapi.vfs.newvfs.events.VFileContentChangeEvent
@@ -24,13 +25,13 @@ import io.mockk.runs
24
25
import io.mockk.slot
25
26
import io.mockk.verify
26
27
import kotlinx.coroutines.test.runTest
28
+ import org.assertj.core.api.Assertions.assertThat
27
29
import org.eclipse.lsp4j.DidChangeTextDocumentParams
28
30
import org.eclipse.lsp4j.DidCloseTextDocumentParams
29
31
import org.eclipse.lsp4j.DidOpenTextDocumentParams
30
32
import org.eclipse.lsp4j.DidSaveTextDocumentParams
31
33
import org.eclipse.lsp4j.jsonrpc.messages.ResponseMessage
32
34
import org.eclipse.lsp4j.services.TextDocumentService
33
- import org.junit.Assert.assertEquals
34
35
import org.junit.Before
35
36
import org.junit.Test
36
37
import software.aws.toolkits.jetbrains.services.amazonq.lsp.AmazonQLanguageServer
@@ -43,6 +44,7 @@ import java.util.concurrent.CompletableFuture
43
44
44
45
class TextDocumentServiceHandlerTest {
45
46
private lateinit var project: Project
47
+ private lateinit var mockFileEditorManager: FileEditorManager
46
48
private lateinit var mockLanguageServer: AmazonQLanguageServer
47
49
private lateinit var mockTextDocumentService: TextDocumentService
48
50
private lateinit var sut: TextDocumentServiceHandler
@@ -90,6 +92,11 @@ class TextDocumentServiceHandlerTest {
90
92
every { messageBus.connect(any<Disposable >()) } returns mockConnection
91
93
every { mockConnection.subscribe(any(), any()) } just runs
92
94
95
+ // Mock FileEditorManager
96
+ mockFileEditorManager = mockk<FileEditorManager >()
97
+ every { mockFileEditorManager.openFiles } returns emptyArray()
98
+ every { project.getService(FileEditorManager ::class .java) } returns mockFileEditorManager
99
+
93
100
sut = TextDocumentServiceHandler (project, mockk())
94
101
}
95
102
@@ -120,12 +127,31 @@ class TextDocumentServiceHandlerTest {
120
127
verify { mockTextDocumentService.didSave(capture(paramsSlot)) }
121
128
122
129
with (paramsSlot.captured) {
123
- assertEquals( normalizeFileUri(uri.toString()), textDocument.uri )
124
- assertEquals( " test content" , text )
130
+ assertThat(textDocument.uri).isEqualTo( normalizeFileUri(uri.toString()))
131
+ assertThat(text).isEqualTo( " test content" )
125
132
}
126
133
}
127
134
}
128
135
136
+ @Test
137
+ fun `didOpen runs on service init` () = runTest {
138
+ val uri = URI .create(" file:///test/path/file.txt" )
139
+ val content = " test content"
140
+ val file = createMockVirtualFile(uri, content)
141
+
142
+ every { mockFileEditorManager.openFiles } returns arrayOf(file)
143
+
144
+ sut = TextDocumentServiceHandler (project, mockk())
145
+
146
+ val paramsSlot = slot<DidOpenTextDocumentParams >()
147
+ verify { mockTextDocumentService.didOpen(capture(paramsSlot)) }
148
+
149
+ with (paramsSlot.captured.textDocument) {
150
+ assertThat(this .uri).isEqualTo(normalizeFileUri(uri.toString()))
151
+ assertThat(text).isEqualTo(content)
152
+ }
153
+ }
154
+
129
155
@Test
130
156
fun `didOpen runs on fileOpened` () = runTest {
131
157
// Create test file
@@ -142,8 +168,8 @@ class TextDocumentServiceHandlerTest {
142
168
verify { mockTextDocumentService.didOpen(capture(paramsSlot)) }
143
169
144
170
with (paramsSlot.captured.textDocument) {
145
- assertEquals( normalizeFileUri(uri.toString()), this .uri )
146
- assertEquals(content, text)
171
+ assertThat( this .uri).isEqualTo( normalizeFileUri(uri.toString()))
172
+ assertThat( text).isEqualTo(content )
147
173
}
148
174
}
149
175
@@ -157,7 +183,7 @@ class TextDocumentServiceHandlerTest {
157
183
val paramsSlot = slot<DidCloseTextDocumentParams >()
158
184
verify { mockTextDocumentService.didClose(capture(paramsSlot)) }
159
185
160
- assertEquals( normalizeFileUri(uri.toString()), paramsSlot.captured.textDocument.uri )
186
+ assertThat(paramsSlot.captured.textDocument.uri).isEqualTo( normalizeFileUri(uri.toString()))
161
187
}
162
188
163
189
@Test
@@ -191,9 +217,9 @@ class TextDocumentServiceHandlerTest {
191
217
verify { mockTextDocumentService.didChange(capture(paramsSlot)) }
192
218
193
219
with (paramsSlot.captured) {
194
- assertEquals( normalizeFileUri(uri.toString()), textDocument.uri )
195
- assertEquals( 123 , textDocument.version)
196
- assertEquals( " changed content " , contentChanges[0 ].text)
220
+ assertThat(textDocument.uri).isEqualTo( normalizeFileUri(uri.toString()))
221
+ assertThat( textDocument.version).isEqualTo( 123 )
222
+ assertThat( contentChanges[0 ].text).isEqualTo( " changed content " )
197
223
}
198
224
}
199
225
0 commit comments