Skip to content

Your phone, on your command. Gotcha is world's first fully on-device AI copilot for Android that turns natural language into real device actions — safely.

fix: instrumented chat tests - bounded drain, deterministic request sync, no modal dialogs

Rishabh Bajpai4448d6b39f03
diff --git a/app/src/androidTest/java/com/gotcha/ChatRoundTripTest.kt b/app/src/androidTest/java/com/gotcha/ChatRoundTripTest.kt
index face419..93c9dbe 100644
--- a/app/src/androidTest/java/com/gotcha/ChatRoundTripTest.kt
+++ b/app/src/androidTest/java/com/gotcha/ChatRoundTripTest.kt
@@ -29,6 +29,7 @@ import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
+import java.util.concurrent.TimeUnit
@RunWith(AndroidJUnit4::class)
class ChatRoundTripTest {
@@ -69,11 +70,20 @@ class ChatRoundTripTest {
/** Consumes and returns the request bodies recorded since [previousCount]. */
private fun drainSince(previousCount: Int): List<String> {
- val bodies = mutableListOf<String>()
- while (mockLlm.server.requestCount > previousCount) {
- mockLlm.server.takeRequest()?.let { bodies.add(it.body.readUtf8()) }
+ // Take everything currently queued (bounded waits so an empty queue
+ // ends the drain instead of wedging the run), then keep only the
+ // requests that arrived after previousCount. MockWebServer is FIFO,
+ // so dropping the first previousCount entries is exactly "since".
+ // NOTE: a plain `while (requestCount > previousCount) takeRequest()`
+ // never terminates the takes and re-includes pre-revert requests —
+ // that wedged this test for 12+ minutes (unbounded take) and, once
+ // bounded, failed it on send#2's own legitimate request.
+ val all = mutableListOf<String>()
+ while (true) {
+ val req = mockLlm.server.takeRequest(2, TimeUnit.SECONDS) ?: break
+ all.add(req.body.readUtf8())
}
- return bodies
+ return all.drop(previousCount)
}
@Test
@@ -177,10 +187,12 @@ class ChatRoundTripTest {
waitForReply()
sendText("second prompt")
waitForReply()
- // run2's request + the run1 title-generation request must both have landed
- // and the final run cleaned up before we snapshot, so the edit's
- // regeneration is the only new request.
- composeRule.waitUntil(timeoutMillis = 15_000) { mockLlm.server.requestCount >= 3 }
+ // Both sends must have landed and the final run cleaned up before we
+ // snapshot, so the edit's regeneration is the only new request. Count
+ // the two sends, not a fixed 3: the run-1 title-generation request
+ // fires only once per process, so in a full-suite run an earlier
+ // test may already have consumed it and >= 3 would wait out 15 s.
+ composeRule.waitUntil(timeoutMillis = 15_000) { mockLlm.server.requestCount >= 2 }
composeRule.waitForIdle()
val since = mockLlm.server.requestCount
diff --git a/app/src/androidTest/java/com/gotcha/testutil/TestSeed.kt b/app/src/androidTest/java/com/gotcha/testutil/TestSeed.kt
index e330043..c86d6fe 100644
--- a/app/src/androidTest/java/com/gotcha/testutil/TestSeed.kt
+++ b/app/src/androidTest/java/com/gotcha/testutil/TestSeed.kt
@@ -2,6 +2,7 @@ package com.gotcha.testutil
import android.content.Context
import com.gotcha.MainActivity
+import com.gotcha.agent.ChatViewModel
import com.gotcha.data.LEGAL_VERSION
import com.gotcha.data.LlmProvider
import com.gotcha.data.Settings
@@ -49,6 +50,7 @@ object TestSeed {
repository.prefs.edit()
.putBoolean(MainActivity.KEY_SUPPRESS_MEDIA_PROJECTION_PROMPT, true)
.putBoolean(SUPPRESS_TOUR_KEY, true)
+ .putBoolean(ChatViewModel.KEY_NOTIFICATION_PERMISSION_ASKED, true)
.apply()
}
}
diff --git a/app/src/debug/java/com/gotcha/debug/TestHooksReceiver.kt b/app/src/debug/java/com/gotcha/debug/TestHooksReceiver.kt
index ea5c989..f108859 100644
--- a/app/src/debug/java/com/gotcha/debug/TestHooksReceiver.kt
+++ b/app/src/debug/java/com/gotcha/debug/TestHooksReceiver.kt
@@ -5,6 +5,7 @@ import android.content.Context
import android.content.Intent
import android.util.Log
import com.gotcha.MainActivity
+import com.gotcha.agent.ChatViewModel
import com.gotcha.data.LEGAL_VERSION
import com.gotcha.data.LlmProvider
import com.gotcha.data.Settings
@@ -114,5 +115,9 @@ fun seedTestSettings(
repository.prefs.edit()
.putBoolean(MainActivity.KEY_SUPPRESS_MEDIA_PROJECTION_PROMPT, true)
.putBoolean(SUPPRESS_TOUR_KEY, true)
+ // The one-time notification-permission ask fires on the first send;
+ // no test covers the ask itself, and its modal would otherwise sit
+ // over every multi-send chat test on fresh installs.
+ .putBoolean(ChatViewModel.KEY_NOTIFICATION_PERMISSION_ASKED, true)
.apply()
}
diff --git a/app/src/main/java/com/gotcha/agent/ChatViewModel.kt b/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
index 5d37fa3..1eb6b4d 100644
--- a/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
+++ b/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
@@ -1453,8 +1453,13 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), C
return synthesizeRunSummariesFromHistory(chat.history, settings.model, chat.agentName)
}
- private companion object {
- /** Set once the one-time notification-permission ask has been shown. */
+ internal companion object {
+ /**
+ * Set once the one-time notification-permission ask has been shown.
+ * Internal (not private) so instrumented-test seeding can pre-mark
+ * it: no test covers the ask itself, and its modal dialog would
+ * otherwise overlay every multi-send chat test on fresh installs.
+ */
const val KEY_NOTIFICATION_PERMISSION_ASKED = "chat_notification_permission_asked"
}
}