diff --git a/plugins/AI-Code-Suggestions/build.gradle.kts b/plugins/AI-Code-Suggestions/build.gradle.kts index 3e83b739d..c6b25f30a 100644 --- a/plugins/AI-Code-Suggestions/build.gradle.kts +++ b/plugins/AI-Code-Suggestions/build.gradle.kts @@ -15,8 +15,8 @@ android { applicationId = "com.itsaky.androidide.plugins.aicodesuggestions" minSdk = 33 targetSdk = 36 - versionCode = 2 - versionName = "1.1.0" + versionCode = 3 + versionName = "1.1.1" } buildTypes { @@ -25,6 +25,10 @@ android { } } + testOptions { + unitTests.isReturnDefaultValues = true + } + compileOptions { sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 @@ -48,6 +52,10 @@ dependencies { implementation("com.google.android.material:material:1.10.0") implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.8.7") implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.7") + + testImplementation(files("../../libs/plugin-api.jar")) + testImplementation("junit:junit:4.13.2") + testImplementation("io.mockk:mockk:1.13.8") } tasks.matching { diff --git a/plugins/AI-Code-Suggestions/src/main/AndroidManifest.xml b/plugins/AI-Code-Suggestions/src/main/AndroidManifest.xml index 528491ebd..afc85fc1f 100644 --- a/plugins/AI-Code-Suggestions/src/main/AndroidManifest.xml +++ b/plugins/AI-Code-Suggestions/src/main/AndroidManifest.xml @@ -43,7 +43,7 @@ + android:value="26.99" /> () + private val shown = LinkedBlockingQueue() + private val plugin = CodeSuggestionsPlugin() + private lateinit var listener: EditorContentChangeListener + + @Before + fun setUp() { + val captured = slot() + val editor = mockk(relaxed = true) + every { editor.addContentChangeListener(capture(captured)) } just Runs + every { editor.showInlineSuggestion(any()) } answers { shown.add(firstArg()) } + + val llm = mockk() + every { llm.generateCompletion(any(), any()) } answers { + prompts.add(firstArg()) + CompletableFuture.completedFuture(LlmInferenceService.LlmResponse.success(COMPLETION, 1, 1L)) + } + + val services = mockk() + every { services.get(IdeEditorService::class.java) } returns editor + every { services.get(LlmInferenceService::class.java) } returns llm + val context = mockk() + every { context.services } returns services + + assertTrue(plugin.initialize(context)) + assertTrue(plugin.activate()) + listener = captured.captured + } + + @After + fun tearDown() { + plugin.deactivate() + plugin.dispose() + } + + @Test + fun aWordOfTwoLettersOrMoreGetsASuggestion() { + type("fun") + + assertEquals(COMPLETION, shown.poll(5, TimeUnit.SECONDS)) + } + + @Test + fun aSpaceAfterAWordCancelsThatWordsPendingSuggestion() { + type("fun") + type("fun ") + + assertNull("the LLM was asked to complete the word left behind", prompts.poll(2, TimeUnit.SECONDS)) + assertTrue("a suggestion appeared after the space: $shown", shown.isEmpty()) + } + + @Test + fun aOneLetterWordCancelsThePreviousWordsPendingSuggestion() { + type("fun") + type("fun x") + + assertNull("the LLM was asked to complete the word left behind", prompts.poll(2, TimeUnit.SECONDS)) + assertTrue("a suggestion appeared after the one-letter word: $shown", shown.isEmpty()) + } + + /** Reports [text] as a one-line Kotlin file with the cursor at its end. */ + private fun type(text: String) = listener.onContentChanged(text, 0, text.length, "kotlin") + + private companion object { + const val COMPLETION = "ction main() {" + } +}