@@ -12,6 +12,7 @@ import com.intellij.openapi.fileEditor.FileDocumentManager
1212import com.intellij.openapi.project.Project
1313import com.intellij.openapi.vfs.VirtualFile
1414import com.intellij.openapi.vfs.newvfs.events.VFileContentChangeEvent
15+ import com.intellij.openapi.vfs.newvfs.events.VFileEvent
1516import com.intellij.util.messages.MessageBus
1617import com.intellij.util.messages.MessageBusConnection
1718import io.mockk.coEvery
@@ -222,4 +223,79 @@ class TextDocumentServiceHandlerTest {
222223 assertEquals(" changed content" , contentChanges[0 ].text)
223224 }
224225 }
226+
227+ @Test
228+ fun `didSave does not run when URI is empty` () = runTest {
229+ val document = mockk<Document >()
230+ val path = mockk<Path > {
231+ every { toUri() } returns URI .create(" " )
232+ }
233+ val file = mockk<VirtualFile > {
234+ every { toNioPath() } returns path
235+ }
236+
237+ val fileDocumentManager = mockk<FileDocumentManager > {
238+ every { getFile(document) } returns file
239+ }
240+
241+ mockkStatic(FileDocumentManager ::class ) {
242+ every { FileDocumentManager .getInstance() } returns fileDocumentManager
243+
244+ sut.beforeDocumentSaving(document)
245+
246+ verify(exactly = 0 ) { mockTextDocumentService.didSave(any()) }
247+ }
248+ }
249+
250+ @Test
251+ fun `didSave does not run when file is null` () = runTest {
252+ val document = mockk<Document >()
253+
254+ val fileDocumentManager = mockk<FileDocumentManager > {
255+ every { getFile(document) } returns null
256+ }
257+
258+ mockkStatic(FileDocumentManager ::class ) {
259+ every { FileDocumentManager .getInstance() } returns fileDocumentManager
260+
261+ sut.beforeDocumentSaving(document)
262+
263+ verify(exactly = 0 ) { mockTextDocumentService.didSave(any()) }
264+ }
265+ }
266+
267+ @Test
268+ fun `didChange ignores non-content change events` () = runTest {
269+ val nonContentEvent = mockk<VFileEvent >() // Some other type of VFileEvent
270+
271+ sut.after(mutableListOf (nonContentEvent))
272+
273+ verify(exactly = 0 ) { mockTextDocumentService.didChange(any()) }
274+ }
275+
276+ @Test
277+ fun `didChange skips files without cached documents` () = runTest {
278+ val uri = URI .create(" file:///test/path/file.txt" )
279+ val path = mockk<Path > {
280+ every { toUri() } returns uri
281+ }
282+ val file = mockk<VirtualFile > {
283+ every { toNioPath() } returns path
284+ }
285+ val changeEvent = mockk<VFileContentChangeEvent > {
286+ every { this @mockk.file } returns file
287+ }
288+
289+ val fileDocumentManager = mockk<FileDocumentManager > {
290+ every { getCachedDocument(file) } returns null
291+ }
292+
293+ mockkStatic(FileDocumentManager ::class ) {
294+ every { FileDocumentManager .getInstance() } returns fileDocumentManager
295+
296+ sut.after(mutableListOf (changeEvent))
297+
298+ verify(exactly = 0 ) { mockTextDocumentService.didChange(any()) }
299+ }
300+ }
225301}
0 commit comments