Modern Apps git

← Modern-Apps.git

games-chess: pass 3 nav entries, DTO surface, sign-in tests

Commit
04d68093c41f15ab72d59e849cc3ca3b1d05b345
Author
Vayun Mathur <[email protected]>
When
2026-11-07 13:05 UTC (29 days ago)
Parents
3a82b06
MainActivity test now also opens the Game Center route and a Learn
stage entry. LichessDtoPass3Test pins the generated data-class
surface of GameFull/GameState/envelope/player DTOs.
OnlineChessSignInPass3Test covers signIn entering SigningIn (repeat
ignored), a state-mismatched OAuth redirect failing sign-in via the
AuthBridge collector, and redirects being dropped while signed out.

Verified: all three classes pass.
(cherry picked from commit 18cf93135ffa62c7eec04a4acdbb55f619c2e221)

Diff

games/chess/src/test/java/com/vayunmathur/games/chess/ChessPass3MainActivityTest.kt
diff --git a/games/chess/src/test/java/com/vayunmathur/games/chess/ChessPass3MainActivityTest.kt b/games/chess/src/test/java/com/vayunmathur/games/chess/ChessPass3MainActivityTest.ktindex e619c2cf64..899fa355eb 100644--- a/games/chess/src/test/java/com/vayunmathur/games/chess/ChessPass3MainActivityTest.kt+++ b/games/chess/src/test/java/com/vayunmathur/games/chess/ChessPass3MainActivityTest.kt@@ -3,6 +3,7 @@ package com.vayunmathur.games.chess import android.content.Context import androidx.compose.ui.test.junit4.createAndroidComposeRule import androidx.compose.ui.test.onAllNodesWithText+import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick import androidx.test.core.app.ApplicationProvider@@ -38,4 +39,25 @@ class ChessPass3MainActivityTest {         rule.onNodeWithText(str(R.string.tab_offline)).performClick()         rule.waitForIdle()     }++    @Test fun gameCenterAndLearnStageEntriesOpen() {+        // Achievements (star) action on the game entry opens the Game Center route.+        rule.onNodeWithContentDescription("Achievements").performClick()+        rule.waitForIdle()+        rule.onNodeWithText(str(com.vayunmathur.library.ui.R.string.title_game_center)).assertExists()+        // Back via the top-bar navigation icon.+        rule.onNodeWithContentDescription("Back", substring = true).let { node ->+            runCatching { node.performClick() }+        }+        rule.waitForIdle()++        // Learn tab -> first stage row opens the LearnStage entry.+        rule.onNodeWithText(str(R.string.tab_learn)).performClick()+        rule.waitForIdle()+        val stageTitle = com.vayunmathur.games.chess.data.LearnRepository+            .apply { ensureLoaded(ctx) }.categories.first().stages.first().title+        rule.onNodeWithText(stageTitle, substring = true).performClick()+        rule.waitForIdle()+        rule.onNodeWithText(str(R.string.learn_lets_go)).assertExists()+    } }
games/chess/src/test/java/com/vayunmathur/games/chess/network/LichessDtoPass3Test.kt
diff --git a/games/chess/src/test/java/com/vayunmathur/games/chess/network/LichessDtoPass3Test.kt b/games/chess/src/test/java/com/vayunmathur/games/chess/network/LichessDtoPass3Test.ktnew file mode 100644index 0000000000..6c74cd6885--- /dev/null+++ b/games/chess/src/test/java/com/vayunmathur/games/chess/network/LichessDtoPass3Test.kt@@ -0,0 +1,71 @@+package com.vayunmathur.games.chess.network++import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNotEquals+import kotlin.test.assertTrue++/** Pass 3: generated data-class surface (copy/equals/hash) of the Lichess DTOs. */+class LichessDtoPass3Test {++    @Test fun gameFullDataClassSurface() {+        val full = GameFull(+            id = "g1", rated = true,+            variant = LichessVariant("standard"),+            clock = LichessClock(300000, 3000),+            white = LichessPlayer("a", "Alice"),+            black = LichessPlayer("b", "Bob"),+            initialFen = "startpos",+            state = GameState(moves = "e2e4", wtime = 290000, status = "started"),+        )+        assertEquals(full, full.copy())+        assertEquals(full.hashCode(), full.copy().hashCode())+        assertNotEquals(full, full.copy(id = "g2"))+        assertEquals("g2", full.copy(id = "g2").id)+        val (id, rated) = full+        assertEquals("g1", id)+        assertTrue(rated)+        assertTrue(full.toString().contains("g1"))+        assertEquals("standard", full.variant.key)+        assertEquals(3000, full.clock!!.increment)+        assertEquals(GameFull(), GameFull(id = "", rated = false))+    }++    @Test fun envelopeAndPlayerDataClassSurface() {+        val game = LichessEventGame("g1", "white", LichessOpponent("opp"), isMyTurn = true)+        assertEquals(game, game.copy())+        assertEquals("opp", game.opponent!!.username)+        assertNotEquals(game, game.copy(color = "black"))+        assertEquals(LichessEventGame(), LichessEventGame("", "", null, false))++        val challenge = LichessChallenge("c1", LichessOpponent("carol"), LichessOpponent("dave"))+        assertEquals(challenge, challenge.copy())+        assertEquals("dave", challenge.destUser!!.username)+        assertEquals(LichessOpponent(), LichessOpponent(""))++        val envelope = LichessEventEnvelope("gameStart", game, challenge)+        assertEquals(envelope, envelope.copy())+        assertEquals("gameStart", envelope.type)+        assertEquals(LichessEventEnvelope(), LichessEventEnvelope("", null, null))++        val player = LichessPlayer("id1", "Name")+        assertEquals(player, player.copy())+        assertEquals("id1", player.component1())+        assertEquals(LichessProfile("x", "y"), LichessProfile("x", "y").copy())+    }++    @Test fun gameStateDataClassSurface() {+        val state = GameState(+            type = "gameState", moves = "e2e4 e7e5", wtime = 1000, btime = 2000,+            winc = 0, binc = 0, status = "mate", winner = "white",+        )+        assertEquals(state, state.copy())+        assertEquals(state.hashCode(), state.copy().hashCode())+        assertNotEquals(state, state.copy(winner = "black"))+        assertEquals("black", state.copy(winner = "black").winner)+        assertEquals(GameState(), GameState("", "", 0, 0, 0, 0, "started", null))+        val clock = LichessClock(1000, 50)+        assertEquals(clock, clock.copy())+        assertEquals(50, clock.component2())+    }+}
games/chess/src/test/java/com/vayunmathur/games/chess/platform/OnlineChessSignInPass3Test.kt
diff --git a/games/chess/src/test/java/com/vayunmathur/games/chess/platform/OnlineChessSignInPass3Test.kt b/games/chess/src/test/java/com/vayunmathur/games/chess/platform/OnlineChessSignInPass3Test.ktnew file mode 100644index 0000000000..a880c06110--- /dev/null+++ b/games/chess/src/test/java/com/vayunmathur/games/chess/platform/OnlineChessSignInPass3Test.kt@@ -0,0 +1,87 @@+package com.vayunmathur.games.chess.platform++import android.app.Application+import android.content.Intent+import androidx.core.net.toUri+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.games.chess.auth.LichessAuthBridge+import com.vayunmathur.games.chess.auth.LichessTokenStore+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.ExperimentalCoroutinesApi+import kotlinx.coroutines.runBlocking+import kotlinx.coroutines.test.resetMain+import kotlinx.coroutines.test.setMain+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import kotlin.test.AfterTest+import kotlin.test.BeforeTest+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNull+import kotlin.test.assertTrue++/** Pass 3: OnlineChessViewModel sign-in state and OAuth redirect handling. */+@OptIn(ExperimentalCoroutinesApi::class)+@RunWith(RobolectricTestRunner::class)+class OnlineChessSignInPass3Test {++    private val app get() = ApplicationProvider.getApplicationContext<Application>()++    @BeforeTest fun setUp() {+        Dispatchers.setMain(Dispatchers.Unconfined)+        runBlocking { LichessTokenStore(app).clear() }+    }++    @AfterTest fun tearDown() {+        runBlocking { LichessTokenStore(app).clear() }+        Dispatchers.resetMain()+    }++    private fun await(desc: String, cond: () -> Boolean) {+        val deadline = System.currentTimeMillis() + 15_000+        while (System.currentTimeMillis() < deadline) {+            if (cond()) return+            Thread.sleep(25)+        }+        assertTrue(cond(), "timed out: $desc")+    }++    @Test fun signInEntersSigningInAndIgnoresRepeat() {+        val vm = OnlineChessViewModel(app)+        assertEquals(OnlineAuth.SignedOut, vm.uiState.value.auth)+        // The Custom Tab launch may not resolve under Robolectric; the state+        // transition happens before it either way.+        runCatching { vm.signIn() }+        assertEquals(OnlineAuth.SigningIn, vm.uiState.value.auth)+        runCatching { vm.signIn() }+        assertEquals(OnlineAuth.SigningIn, vm.uiState.value.auth, "repeat sign-in while pending is ignored")+        assertNull(vm.uiState.value.authError)+    }++    @Test fun redirectWithMismatchedStateFailsSignIn() {+        val vm = OnlineChessViewModel(app)+        runCatching { vm.signIn() }+        assertEquals(OnlineAuth.SigningIn, vm.uiState.value.auth)+        LichessAuthBridge.onRedirect(+            Intent().setData("modernapps://lichess-callback?code=abc&state=not-the-state".toUri())+        )+        await("sign-in failed") { vm.uiState.value.auth is OnlineAuth.SignedOut }+        assertEquals(OnlineAuth.Error.SignInFailed, vm.uiState.value.authError)+        assertEquals(+            app.getString(com.vayunmathur.games.chess.R.string.online_sign_in_failed),+            vm.authErrorText(OnlineAuth.Error.SignInFailed),+        )+        vm.dismissLobbyMessage()+        assertNull(vm.uiState.value.authError)+    }++    @Test fun redirectWhileSignedOutIsDropped() {+        val vm = OnlineChessViewModel(app)+        LichessAuthBridge.onRedirect(+            Intent().setData("modernapps://lichess-callback?code=abc&state=xyz".toUri())+        )+        Thread.sleep(300)+        assertEquals(OnlineAuth.SignedOut, vm.uiState.value.auth)+        assertNull(vm.uiState.value.authError)+    }+}