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.

16.1 KB
Raw Blame
package com.gotcha

import androidx.compose.ui.test.SemanticsMatcher
import androidx.compose.ui.test.assert
import androidx.compose.ui.test.assertCountEquals
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.hasTestTag
import androidx.compose.ui.test.junit4.createEmptyComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performScrollTo
import androidx.compose.ui.test.performTextReplacement
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.gotcha.data.SettingsRepository
import com.gotcha.i18n.Language
import com.gotcha.ui.SettingsHighlighted
import com.gotcha.testutil.TestSeed
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class SettingsFlowTest {

    @get:Rule
    val composeRule = createEmptyComposeRule()

    private var scenario: ActivityScenario<MainActivity>? = null
    private val testBaseUrl = "http://localhost:8080/v1/"
    private val testModel = "flow-test-model"

    @After
    fun tearDown() {
        scenario?.close()
    }

    @Test
    fun enteredSettings_persistAcrossRelaunch() {
        val context = ApplicationProvider.getApplicationContext<android.content.Context>()
        TestSeed.seedUnconfigured(context)

        scenario = ActivityScenario.launch(MainActivity::class.java)
        composeRule.waitForIdle()

        // An unconfigured install opens on the AI Configuration page itself, so
        // there is no category row to click through first.
        composeRule.onNodeWithTag("settings_base_url").performTextReplacement(testBaseUrl)
        composeRule.onNodeWithTag("settings_model").performTextReplacement(testModel)
        // The Save button sits below the fold in the scrollable Settings column;
        // performClick() dispatches at the node's laid-out coordinates without
        // scrolling it into view first, so an off-screen click silently no-ops.
        composeRule.onNodeWithTag("settings_save").performScrollTo().performClick()
        composeRule.waitForIdle()

        scenario?.close()

        val persisted = SettingsRepository(context).load()
        assertEquals(testBaseUrl, persisted.baseUrl)
        assertEquals(testModel, persisted.model)

        // Relaunch and confirm the configured route is reached with the saved settings intact.
        scenario = ActivityScenario.launch(MainActivity::class.java)
        composeRule.waitForIdle()
        composeRule.onNodeWithTag("chat_input").assertExists()
    }

    @Test
    fun personalInfo_persistsAcrossRelaunch() {
        val context = ApplicationProvider.getApplicationContext<android.content.Context>()
        TestSeed.seedUnconfigured(context)

        scenario = ActivityScenario.launch(MainActivity::class.java)
        composeRule.waitForIdle()

        // First run opens on AI Configuration, which sits inside the AI hub, so
        // reaching the category list takes two Backs.
        composeRule.onNodeWithTag("settings_back").performClick()
        composeRule.waitForIdle()
        composeRule.onNodeWithTag("settings_back").performClick()
        composeRule.waitForIdle()
        composeRule.onNodeWithTag("settings_personal_info_row").performScrollTo().performClick()
        composeRule.waitForIdle()

        composeRule.onNodeWithTag("settings_user_name").performTextReplacement("Ada")
        composeRule.onNodeWithTag("settings_user_response_style")
            .performScrollTo()
            .performTextReplacement("No bullet lists.")
        composeRule.onNodeWithTag("settings_save_personal_info").performScrollTo().performClick()
        composeRule.waitForIdle()

        scenario?.close()

        val persisted = SettingsRepository(context).load()
        assertEquals("Ada", persisted.userName)
        assertEquals("No bullet lists.", persisted.userResponseStyle)
    }

    @Test
    fun languagePage_showsTheThreeLanguagesApartAndPersistsThem() {
        val context = ApplicationProvider.getApplicationContext<android.content.Context>()
        TestSeed.seedUnconfigured(context)

        scenario = ActivityScenario.launch(MainActivity::class.java)
        composeRule.waitForIdle()

        // First run opens on AI Configuration inside the AI hub — two Backs to the list.
        composeRule.onNodeWithTag("settings_back").performClick()
        composeRule.waitForIdle()
        composeRule.onNodeWithTag("settings_back").performClick()
        composeRule.waitForIdle()
        composeRule.onNodeWithTag("settings_language_row").performScrollTo().performClick()
        composeRule.waitForIdle()

        // The point of the page (issue #74): all three named and visible at once.
        composeRule.onNodeWithText("App display language").assertExists()
        composeRule.onNodeWithTag("settings_voice_language").performScrollTo().assertExists()
        composeRule.onNodeWithTag("settings_reply_language").performScrollTo().assertExists()

        // Untouched, the voice follows the reply language — what a pre-#74 install did.
        composeRule.onNodeWithText("Same as AI reply language").assertExists()

        // The whole point of the split: answers written in one language, spoken in
        // another. Pick Hindi for the voice and leave the reply language English.
        composeRule.onNodeWithTag("settings_voice_language").performScrollTo().performClick()
        composeRule.waitForIdle()
        composeRule.onNodeWithText("Hindi").performClick()
        composeRule.waitForIdle()

        composeRule.onNodeWithTag("settings_save_language").performScrollTo().performClick()
        composeRule.waitForIdle()
        scenario?.close()

        // Round-trips through EncryptedSharedPreferences, which no JVM test reaches.
        val persisted = SettingsRepository(context).load()
        assertEquals("Hindi", persisted.voiceLanguage)
        assertEquals("English", persisted.preferredLanguage)
        // TTS and STT follow the voice; the prompt still follows preferredLanguage.
        assertEquals(Language.HINDI, persisted.effectiveVoiceLanguage)
        assertEquals("hi-IN", persisted.effectiveVoiceLanguage.bcp47)
        assertEquals("hi", persisted.effectiveVoiceLanguage.iso639)
    }

    @Test
    fun settingsSubPage_opensFromTheListAndBackReturnsToIt() {
        val context = ApplicationProvider.getApplicationContext<android.content.Context>()
        TestSeed.seedUnconfigured(context)

        scenario = ActivityScenario.launch(MainActivity::class.java)
        composeRule.waitForIdle()

        // First run opens on AI Configuration; Back from a sub-page lands on the
        // hub it hangs off rather than leaving Settings...
        composeRule.onNodeWithTag("settings_base_url").assertExists()
        composeRule.onNodeWithTag("settings_back").performClick()
        composeRule.waitForIdle()
        composeRule.onNodeWithTag("settings_ai_config_row").assertExists()

        // ...and Back again from the hub lands on the category list.
        composeRule.onNodeWithTag("settings_back").performClick()
        composeRule.waitForIdle()
        composeRule.onNodeWithTag("settings_ai_row").assertExists()
        composeRule.onNodeWithTag("settings_ai_config_row").assertDoesNotExist()

        // Opening a category replaces the list with that page.
        composeRule.onNodeWithTag("settings_ai_row").performScrollTo().performClick()
        composeRule.waitForIdle()
        composeRule.onNodeWithTag("settings_speech_row").performScrollTo().performClick()
        composeRule.waitForIdle()
        composeRule.onNodeWithTag("settings_ai_config_row").assertDoesNotExist()
        composeRule.onNodeWithText("TTS Provider").assertExists()
    }

    @Test
    fun settingsSearch_findsAPageByAControlInsideIt() {
        val context = ApplicationProvider.getApplicationContext<android.content.Context>()
        TestSeed.seedUnconfigured(context)

        scenario = ActivityScenario.launch(MainActivity::class.java)
        composeRule.waitForIdle()

        // First run opens on AI Configuration inside the AI hub — two Backs to the list.
        composeRule.onNodeWithTag("settings_back").performClick()
        composeRule.waitForIdle()
        composeRule.onNodeWithTag("settings_back").performClick()
        composeRule.waitForIdle()

        // "wake word" is a control inside the ball's page, not a row on the list.
        composeRule.onNodeWithTag("settings_search").performTextReplacement("wake word")
        composeRule.waitForIdle()
        composeRule.onNodeWithTag("settings_assistive_ball_row").assertExists()
        composeRule.onNodeWithTag("settings_termux_row").assertDoesNotExist()
        // The tour and feedback rows are not pages, so they drop out of a search.
        composeRule.onNodeWithTag("settings_feature_tour_row").assertDoesNotExist()

        // Nothing matching says so rather than showing an empty list.
        composeRule.onNodeWithTag("settings_search").performTextReplacement("qwertyuiop")
        composeRule.waitForIdle()
        composeRule.onNodeWithTag("settings_search_empty").assertExists()
        composeRule.onNodeWithTag("settings_assistive_ball_row").assertDoesNotExist()

        // Clearing restores the full list, tour row included.
        composeRule.onNodeWithTag("settings_search_clear").performClick()
        composeRule.waitForIdle()
        composeRule.onNodeWithTag("settings_search_empty").assertDoesNotExist()
        composeRule.onNodeWithTag("settings_feature_tour_row").performScrollTo().assertExists()
        composeRule.onNodeWithTag("settings_termux_row").performScrollTo().assertExists()
    }

    @Test
    fun settingsSearch_opensAPageNestedInsideAHub() {
        val context = ApplicationProvider.getApplicationContext<android.content.Context>()
        TestSeed.seedUnconfigured(context)

        scenario = ActivityScenario.launch(MainActivity::class.java)
        composeRule.waitForIdle()

        composeRule.onNodeWithTag("settings_back").performClick()
        composeRule.waitForIdle()
        composeRule.onNodeWithTag("settings_back").performClick()
        composeRule.waitForIdle()

        // Speech lives inside the AI hub, so the home list never shows this row —
        // the search result is the only way to reach it in one tap.
        composeRule.onNodeWithTag("settings_search").performTextReplacement("read aloud")
        composeRule.waitForIdle()
        composeRule.onNodeWithTag("settings_speech_row").performScrollTo().performClick()
        composeRule.waitForIdle()
        composeRule.onNodeWithText("TTS Provider").assertExists()

        // Back from a nested page lands on its hub, and the query is gone: the
        // list that comes back is the whole list.
        composeRule.onNodeWithTag("settings_back").performClick()
        composeRule.waitForIdle()
        composeRule.onNodeWithTag("settings_ai_config_row").assertExists()
        composeRule.onNodeWithTag("settings_back").performClick()
        composeRule.waitForIdle()
        composeRule.onNodeWithTag("settings_termux_row").performScrollTo().assertExists()
    }

    @Test
    fun aiConfig_keepsTheAdvancedKnobsCollapsedUntilAsked() {
        val context = ApplicationProvider.getApplicationContext<android.content.Context>()
        TestSeed.seedUnconfigured(context)

        scenario = ActivityScenario.launch(MainActivity::class.java)
        composeRule.waitForIdle()

        // First run opens on AI Configuration. The agent-loop limits are one tap
        // away, not in the way of the fields that make the app work.
        composeRule.onNodeWithTag("settings_model").assertExists()
        composeRule.onNodeWithText("Max tool rounds").assertDoesNotExist()

        composeRule.onNodeWithTag("settings_ai_advanced").performScrollTo().performClick()
        composeRule.waitForIdle()
        composeRule.onNodeWithText("Max tool rounds").performScrollTo().assertExists()
    }

    @Test
    fun settingsSearch_opensOnTheMatchedFieldInsideACollapsedSection() {
        val context = ApplicationProvider.getApplicationContext<android.content.Context>()
        TestSeed.seedUnconfigured(context)

        scenario = ActivityScenario.launch(MainActivity::class.java)
        composeRule.waitForIdle()

        composeRule.onNodeWithTag("settings_back").performClick()
        composeRule.waitForIdle()
        composeRule.onNodeWithTag("settings_back").performClick()
        composeRule.waitForIdle()

        // The result says where it will land, down to the field.
        composeRule.onNodeWithTag("settings_search").performTextReplacement("max tool rounds")
        composeRule.waitForIdle()
        composeRule.onNodeWithText("AI › AI Configuration › Max tool rounds").assertExists()

        // Hold the clock, so the highlight can be caught before it fades.
        composeRule.mainClock.autoAdvance = false
        composeRule.onNodeWithTag("settings_ai_config_row").performClick()
        composeRule.mainClock.advanceTimeBy(HIGHLIGHT_CHECK_MS)

        // Advanced opened by itself, and the field is on screen and marked —
        // no performScrollTo: the page did the scrolling.
        composeRule.onNodeWithTag("settings_max_tool_rounds")
            .assertIsDisplayed()
            .assert(SemanticsMatcher.expectValue(SettingsHighlighted, true))

        // The tint fades and the highlight is spent; the field stays where it is.
        // Being spent is not the same as being gone: the page clears the mark
        // after a real FOLLOW_MS delay plus the fade, and waitForIdle() only
        // waits for the composition to settle — which it does long before that.
        // So wait for the mark to actually leave rather than racing it.
        composeRule.mainClock.autoAdvance = true
        composeRule.waitUntil(timeoutMillis = HIGHLIGHT_CLEAR_MS) {
            composeRule.onAllNodes(
                hasTestTag("settings_max_tool_rounds") and SemanticsMatcher.keyIsDefined(SettingsHighlighted)
            ).fetchSemanticsNodes().isEmpty()
        }
        composeRule.onNodeWithTag("settings_max_tool_rounds")
            .assert(SemanticsMatcher.keyNotDefined(SettingsHighlighted))

        // Back to the hub and in again is an ordinary visit: nothing highlighted,
        // and Advanced folded as it always starts.
        composeRule.onNodeWithTag("settings_back").performClick()
        composeRule.waitForIdle()
        composeRule.onNodeWithTag("settings_ai_config_row").performClick()
        composeRule.waitForIdle()
        composeRule.onNodeWithTag("settings_max_tool_rounds").assertDoesNotExist()
    }

    @Test
    fun settingsSearch_aPagesOwnTitleOpensItWithNothingHighlighted() {
        val context = ApplicationProvider.getApplicationContext<android.content.Context>()
        TestSeed.seedUnconfigured(context)

        scenario = ActivityScenario.launch(MainActivity::class.java)
        composeRule.waitForIdle()

        composeRule.onNodeWithTag("settings_back").performClick()
        composeRule.waitForIdle()
        composeRule.onNodeWithTag("settings_back").performClick()
        composeRule.waitForIdle()

        composeRule.onNodeWithTag("settings_search").performTextReplacement("AI Configuration")
        composeRule.waitForIdle()
        composeRule.mainClock.autoAdvance = false
        composeRule.onNodeWithTag("settings_ai_config_row").performClick()
        composeRule.mainClock.advanceTimeBy(HIGHLIGHT_CHECK_MS)

        composeRule.onNodeWithTag("settings_model").assertExists()
        composeRule.onNodeWithTag("settings_max_tool_rounds").assertDoesNotExist()
        composeRule.onAllNodes(SemanticsMatcher.keyIsDefined(SettingsHighlighted)).assertCountEquals(0)
        composeRule.mainClock.autoAdvance = true
    }

    private companion object {
        /** Long enough for the page to open and scroll, well short of the fade. */
        const val HIGHLIGHT_CHECK_MS = 700L

        /**
         * How long to wait for the highlight to be given up. The page holds the
         * mark while it can still be pulled back into view and only then clears
         * it, so this has to cover that window and the fade, not just one frame.
         */
        const val HIGHLIGHT_CLEAR_MS = 10_000L
    }
}