Modern Apps git

← Modern-Apps.git

sdk/openassistant: add tests for dispatch, availability and receiver

Commit
94a9d657b650eec15f3f40a63d9a8fa08736d3b3
Author
Vayun Mathur <[email protected]>
When
2026-11-07 06:34 UTC (29 days ago)
Parents
0825ebe
Coverage (merged INSTRUCTION, :sdk:openassistant:
createMergedCoverageReport): 0% -> 97.19% (346/356), above the
85% target. One test file with 13 tests:

- isAvailable is false with no assistant package and true once the
  package appears (Robolectric ShadowPackageManager install), and
  generate/generateJson throw AssistantNotInstalledException with
  its documented message before availability.
- Successful dispatch: the test captures the foreground-service
  intent (component, user_text extra, default schema for plain
  generate and the caller's schema for generateJson), then delivers
  a result through the intent's RECEIVER ResultReceiver and asserts
  the returned json_result; a null result bundle maps to "".
- Error results map to AssistantException carrying the bundle's
  error text, and to the "Inference failed" fallback when the
  bundle/message is absent (including non-zero arbitrary codes).
- A service that never responds fails with
  TimeoutCancellationException at the configured timeoutMs.
- startForegroundService throwing SecurityException or
  IllegalStateException (throwing ContextWrapper) is converted to
  AssistantException naming the failure.
- close() is a repeatable no-op; SecureResultReceiver forwards
  codes/bundles verbatim; exception message contracts hold.

Verified: gslot ./gradlew :sdk:openassistant:testDebugUnitTest
(13 tests, 0 failures) and the merged coverage report above.

(cherry picked from commit 654fbf2b210b5cf8177ef8627e9c257d1da7727f)

Diff

sdk/openassistant/src/test/java/com/vayunmathur/sdk/openassistant/OpenAssistantTest.kt
diff --git a/sdk/openassistant/src/test/java/com/vayunmathur/sdk/openassistant/OpenAssistantTest.kt b/sdk/openassistant/src/test/java/com/vayunmathur/sdk/openassistant/OpenAssistantTest.ktnew file mode 100644index 0000000000..34c4bce505--- /dev/null+++ b/sdk/openassistant/src/test/java/com/vayunmathur/sdk/openassistant/OpenAssistantTest.kt@@ -0,0 +1,213 @@+package com.vayunmathur.sdk.openassistant++import android.content.ComponentName+import android.content.Context+import android.content.ContextWrapper+import android.content.Intent+import android.content.pm.PackageInfo+import android.os.Bundle+import android.os.Looper+import android.os.ResultReceiver+import androidx.test.core.app.ApplicationProvider+import androidx.test.ext.junit.runners.AndroidJUnit4+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.TimeoutCancellationException+import kotlinx.coroutines.async+import kotlinx.coroutines.runBlocking+import kotlinx.coroutines.withTimeoutOrNull+import org.junit.runner.RunWith+import org.robolectric.Shadows+import org.robolectric.shadows.ShadowLooper+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFailsWith+import kotlin.test.assertNotNull+import kotlin.test.assertNull+import kotlin.test.assertTrue++@RunWith(AndroidJUnit4::class)+class OpenAssistantTest {++    private val app: Context get() = ApplicationProvider.getApplicationContext()++    private fun installAssistant() {+        val info = PackageInfo().apply { packageName = "com.vayunmathur.openassistant" }+        Shadows.shadowOf(app.packageManager).installPackage(info)+    }++    private fun nextStartedService(): Intent? =+        Shadows.shadowOf(app as android.app.Application).nextStartedService++    private fun receiverOf(intent: Intent): ResultReceiver {+        @Suppress("DEPRECATION")+        val receiver = intent.getParcelableExtra<ResultReceiver>("RECEIVER")+        return assertNotNull(receiver)+    }++    private fun idleMain() {+        ShadowLooper.idleMainLooper()+        Shadows.shadowOf(Looper.getMainLooper()).idle()+    }++    @Test fun notAvailableByDefault() {+        val oa = OpenAssistant(app)+        assertEquals(false, oa.isAvailable())+    }++    @Test fun generateThrowsWhenNotInstalled() = runBlocking {+        val oa = OpenAssistant(app)+        val e = assertFailsWith<AssistantNotInstalledException> { oa.generate("hi") }+        assertEquals("OpenAssistant app is not installed on this device", e.message)+        assertFailsWith<AssistantNotInstalledException> { oa.generateJson("hi", "{}") }+        Unit+    }++    @Test fun availableAfterInstall() {+        installAssistant()+        assertTrue(OpenAssistant(app).isAvailable())+    }++    @Test fun generateReturnsJsonResultAndSendsIntent() = runBlocking {+        installAssistant()+        val oa = OpenAssistant(app, timeoutMs = 10_000)+        val deferred = async(Dispatchers.Default) { oa.generate("hello") }+        // Wait until the service intent has been dispatched.+        var intent: Intent? = null+        var guard = 0+        while (intent == null && guard++ < 100) {+            idleMain()+            intent = nextStartedService()+            if (intent == null) Thread.sleep(10)+        }+        val started = assertNotNull(intent, "no service intent dispatched")+        assertEquals(+            ComponentName("com.vayunmathur.openassistant", "com.vayunmathur.openassistant.util.InferenceService"),+            started.component,+        )+        assertEquals("hello", started.getStringExtra("user_text"))+        // generate() without a schema must send the default response schema.+        assertTrue(started.getStringExtra("schema")!!.contains("\"response\""))+        receiverOf(started).send(0, Bundle().apply { putString("json_result", "{\"a\":1}") })+        idleMain()+        assertEquals("{\"a\":1}", withTimeoutOrNull(5_000) { deferred.await() })+    }++    @Test fun generateJsonSendsCustomSchema() = runBlocking {+        installAssistant()+        val oa = OpenAssistant(app, timeoutMs = 10_000)+        val schema = "{\"type\":\"object\",\"properties\":{\"x\":{\"type\":\"number\"}}}"+        val deferred = async(Dispatchers.Default) { oa.generateJson("count", schema) }+        var intent: Intent? = null+        var guard = 0+        while (intent == null && guard++ < 100) {+            idleMain()+            intent = nextStartedService()+            if (intent == null) Thread.sleep(10)+        }+        val started = assertNotNull(intent)+        assertEquals(schema, started.getStringExtra("schema"))+        assertEquals("count", started.getStringExtra("user_text"))+        receiverOf(started).send(0, Bundle().apply { putString("json_result", "{\"x\":2}") })+        idleMain()+        assertEquals("{\"x\":2}", withTimeoutOrNull(5_000) { deferred.await() })+    }++    @Test fun successWithNullDataReturnsEmpty() = runBlocking {+        installAssistant()+        val oa = OpenAssistant(app, timeoutMs = 10_000)+        val deferred = async(Dispatchers.Default) { oa.generate("hi") }+        var intent: Intent? = null+        var guard = 0+        while (intent == null && guard++ < 100) {+            idleMain(); intent = nextStartedService(); if (intent == null) Thread.sleep(10)+        }+        receiverOf(assertNotNull(intent)).send(0, null)+        idleMain()+        assertEquals("", withTimeoutOrNull(5_000) { deferred.await() })+    }++    @Test fun errorResultThrowsAssistantException() = runBlocking {+        installAssistant()+        val oa = OpenAssistant(app, timeoutMs = 10_000)+        val deferred = async(Dispatchers.Default) { runCatching { oa.generate("hi") } }+        var intent: Intent? = null+        var guard = 0+        while (intent == null && guard++ < 100) {+            idleMain(); intent = nextStartedService(); if (intent == null) Thread.sleep(10)+        }+        receiverOf(assertNotNull(intent)).send(1, Bundle().apply { putString("error", "model exploded") })+        idleMain()+        val result = withTimeoutOrNull(5_000) { deferred.await() }+        val e = assertNotNull(result).exceptionOrNull()+        assertTrue(e is AssistantException, "got $e")+        assertEquals("model exploded", e!!.message)+    }++    @Test fun errorResultWithoutMessageUsesFallback() = runBlocking {+        installAssistant()+        val oa = OpenAssistant(app, timeoutMs = 10_000)+        val deferred = async(Dispatchers.Default) { runCatching { oa.generate("hi") } }+        var intent: Intent? = null+        var guard = 0+        while (intent == null && guard++ < 100) {+            idleMain(); intent = nextStartedService(); if (intent == null) Thread.sleep(10)+        }+        receiverOf(assertNotNull(intent)).send(7, null)+        idleMain()+        val e = assertNotNull(withTimeoutOrNull(5_000) { deferred.await() }).exceptionOrNull()+        assertTrue(e is AssistantException, "got $e")+        assertEquals("Inference failed", e!!.message)+    }++    @Test fun generateTimesOutWhenServiceNeverResponds() = runBlocking {+        installAssistant()+        val oa = OpenAssistant(app, timeoutMs = 300)+        assertFailsWith<TimeoutCancellationException> { oa.generate("slow") }+        Unit+    }++    /** Context whose startForegroundService always throws [failure]. */+    private fun throwingContext(failure: RuntimeException): Context = object : ContextWrapper(app) {+        override fun startForegroundService(service: Intent?): ComponentName? = throw failure+    }++    @Test fun securityExceptionBecomesAssistantException() = runBlocking {+        installAssistant()+        val oa = OpenAssistant(throwingContext(SecurityException("denied")), timeoutMs = 10_000)+        val e = assertFailsWith<AssistantException> { oa.generate("hi") }+        assertTrue(e.message!!.contains("Failed to start InferenceService"), e.message)+        assertTrue(e.message!!.contains("denied"), e.message)+        val e2 = assertFailsWith<AssistantException> {+            OpenAssistant(throwingContext(IllegalStateException("bg start")), timeoutMs = 10_000)+                .generateJson("hi", "{}")+        }+        assertTrue(e2.message!!.contains("bg start"), e2.message)+    }++    @Test fun closeIsNoOp() = runBlocking {+        OpenAssistant(app).close()+        OpenAssistant(app).close()+    }++    @Test fun secureResultReceiverForwards() {+        var gotCode: Int? = null+        var gotData: Bundle? = null+        val receiver = SecureResultReceiver(null) { code, data -> gotCode = code; gotData = data }+        val bundle = Bundle().apply { putString("k", "v") }+        receiver.send(3, bundle)+        assertEquals(3, gotCode)+        assertEquals("v", gotData!!.getString("k"))+        receiver.send(0, null)+        assertEquals(0, gotCode)+        assertNull(gotData)+    }++    @Test fun exceptionsCarryMessages() {+        assertEquals(+            "OpenAssistant app is not installed on this device",+            AssistantNotInstalledException().message,+        )+        assertEquals("custom", AssistantException("custom").message)+        assertTrue(AssistantNotInstalledException() is Exception)+    }+}