feat: write initial unit test for apply

This commit is contained in:
Patrick Erichsen 2025-05-14 21:50:59 -07:00
parent 93cec27e28
commit f4a59dc185
5 changed files with 131 additions and 29 deletions

View file

@ -0,0 +1,15 @@
name: IntelliJ Plugin Test Execution
version: 0.0.1
schema: v1
rules:
- name: IntelliJ Plugin Test Execution
rule: |-
Run IntelliJ plugin tests using Gradle with the fully qualified test class or method name:
```bash
# Run test class
./gradlew test --tests "com.github.continuedev.continueintellijextension.unit.ApplyToFileHandlerTest"
# Run specific test method
./gradlew test --tests "com.github.continuedev.continueintellijextension.unit.ApplyToFileHandlerTest.should*"
```
globs: extensions/intellij/**/*Test.kt

View file

@ -42,10 +42,12 @@ dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.0")
testImplementation("com.intellij.remoterobot:remote-robot:$remoteRobotVersion")
testImplementation("com.intellij.remoterobot:remote-fixtures:$remoteRobotVersion")
testImplementation("io.mockk:mockk:1.14.2")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.10.0")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.9.2")
testImplementation("com.squareup.okhttp3:logging-interceptor:4.12.0")
implementation("com.automation-remarks:video-recorder-junit5:2.0")
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3")
// Exclude vulnerable Log4j from all dependencies
@ -62,6 +64,7 @@ dependencies {
// Add Log4j 2.x explicitly
implementation("org.apache.logging.log4j:log4j-core:2.20.0")
implementation("org.apache.logging.log4j:log4j-api:2.20.0")
testImplementation(kotlin("test"))
}
// Set the JVM language level used to build the project. Use Java 11 for 2020.3+, and Java 17 for

View file

@ -7,9 +7,9 @@ import com.github.continuedev.continueintellijextension.ToastType
import com.github.continuedev.continueintellijextension.editor.DiffStreamHandler
import com.github.continuedev.continueintellijextension.editor.DiffStreamService
import com.github.continuedev.continueintellijextension.editor.EditorUtils
import com.github.continuedev.continueintellijextension.protocol.ApplyToFileParams
import com.github.continuedev.continueintellijextension.services.ContinuePluginService
import com.github.continuedev.continueintellijextension.utils.castNestedOrNull
import com.intellij.openapi.components.service
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import kotlinx.coroutines.suspendCancellableCoroutine
@ -23,18 +23,15 @@ class ApplyToFileHandler(
private val project: Project,
private val continuePluginService: ContinuePluginService,
private val ide: IDE,
private val streamId: String,
private val toolCallId: String,
private val text: String,
private val filepath: String?,
private val params: ApplyToFileParams,
private val editorUtils: EditorUtils?,
private val diffStreamService: DiffStreamService
) {
suspend fun handleApplyToFile() {
// Notify webview that we're starting to stream
notifyStreamStarted()
val editorUtils = EditorUtils.getOrOpenEditor(project, filepath)
if (editorUtils == null) {
ide.showToast(ToastType.ERROR, "No active editor to apply edits to")
notifyStreamClosed()
@ -42,7 +39,7 @@ class ApplyToFileHandler(
}
if (editorUtils.isDocumentEmpty()) {
editorUtils.insertTextIntoEmptyDocument(text)
editorUtils.insertTextIntoEmptyDocument(params.text)
notifyStreamClosed()
return
}
@ -70,12 +67,12 @@ class ApplyToFileHandler(
numDiffs: Int? = null
) {
val payload = UpdateApplyStatePayload(
streamId = streamId,
streamId = params.streamId,
status = status,
numDiffs = numDiffs,
filepath = filepath,
fileContent = text,
toolCallId = toolCallId
filepath = params.filepath,
fileContent = params.text,
toolCallId = params.toolCallId.toString()
)
continuePluginService.sendToWebview("updateApplyState", payload)
@ -117,8 +114,6 @@ class ApplyToFileHandler(
}
private fun setupAndStreamDiffs(editorUtils: EditorUtils, llm: Any) {
val diffStreamService = project.service<DiffStreamService>()
// Clear all diff blocks before running the diff stream
diffStreamService.reject(editorUtils.editor)
@ -144,7 +139,7 @@ class ApplyToFileHandler(
}
private fun buildApplyPrompt(): String {
return "The following code was suggested as an edit:\n```\n${text}\n```\nPlease apply it to the previous code."
return "The following code was suggested as an edit:\n```\n${params.text}\n```\nPlease apply it to the previous code."
}
private fun createDiffStreamHandler(
@ -159,8 +154,8 @@ class ApplyToFileHandler(
endLine,
{},
{},
streamId,
toolCallId
params.streamId,
params.toolCallId.toString()
)
}
@ -172,20 +167,18 @@ class ApplyToFileHandler(
project: Project,
continuePluginService: ContinuePluginService,
ide: IDE,
filepath: String?,
streamId: String,
toolCallId: String,
text: String
params: ApplyToFileParams
) {
val editorUtils = EditorUtils.getOrOpenEditor(project, params.filepath)
val diffStreamService = project.getService(DiffStreamService::class.java)
val handler = ApplyToFileHandler(
project,
continuePluginService,
ide,
streamId,
toolCallId,
text,
filepath,
params,
editorUtils,
diffStreamService
)
handler.handleApplyToFile()

View file

@ -463,10 +463,7 @@ class IdeProtocolClient(
project,
continuePluginService,
ide,
params.filepath,
params.streamId,
params.toolCallId.toString(),
params.text
params
)
respond(null)
}

View file

@ -0,0 +1,94 @@
package com.github.continuedev.continueintellijextension.unit
import com.github.continuedev.continueintellijextension.ApplyStateStatus
import com.github.continuedev.continueintellijextension.IDE
import com.github.continuedev.continueintellijextension.UpdateApplyStatePayload
import com.github.continuedev.continueintellijextension.`continue`.ApplyToFileHandler
import com.github.continuedev.continueintellijextension.`continue`.CoreMessenger
import com.github.continuedev.continueintellijextension.editor.DiffStreamService
import com.github.continuedev.continueintellijextension.editor.EditorUtils
import com.github.continuedev.continueintellijextension.protocol.ApplyToFileParams
import com.github.continuedev.continueintellijextension.services.ContinuePluginService
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import io.mockk.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
@ExperimentalCoroutinesApi
class ApplyToFileHandlerTest {
// Mock all dependencies
private val mockProject = mockk<Project>(relaxed = true)
private val mockContinuePluginService = mockk<ContinuePluginService>(relaxed = true)
private val mockIde = mockk<IDE>(relaxed = true)
private val mockEditorUtils = mockk<EditorUtils>(relaxed = true)
private val mockDiffStreamService = mockk<DiffStreamService>(relaxed = true)
private val mockEditor = mockk<Editor>(relaxed = true)
private val mockCoreMessenger = mockk<CoreMessenger>(relaxed = true)
// Test subject
private lateinit var handler: ApplyToFileHandler
// Constants for testing
private val testParams = ApplyToFileParams(
"Sample text to apply",
"stream123",
"test/file.kt",
"tool-call-123"
)
@BeforeEach
fun setUp() {
// Common setup
every { mockEditorUtils.editor } returns mockEditor
every { mockContinuePluginService.coreMessenger } returns mockCoreMessenger
// Create the handler with mocked dependencies
handler = ApplyToFileHandler(
mockProject,
mockContinuePluginService,
mockIde,
testParams,
mockEditorUtils,
mockDiffStreamService
)
}
@Test
fun `should insert text directly when document is empty`() = runTest {
// Given
every { mockEditorUtils.isDocumentEmpty() } returns true
// When
handler.handleApplyToFile()
// Then
verify { mockEditorUtils.insertTextIntoEmptyDocument(testParams.text) }
verify(exactly = 0) { mockDiffStreamService.register(any(), any()) } // Ensure no diff streaming happened
// Verify notifications sent
verify {
mockContinuePluginService.sendToWebview(
eq("updateApplyState"),
withArg { payload ->
assert(payload is UpdateApplyStatePayload)
assert((payload as UpdateApplyStatePayload).status == ApplyStateStatus.STREAMING)
},
any()
)
}
verify {
mockContinuePluginService.sendToWebview(
eq("updateApplyState"),
withArg { payload ->
assert(payload is UpdateApplyStatePayload)
assert((payload as UpdateApplyStatePayload).status == ApplyStateStatus.CLOSED)
},
any()
)
}
}
}