games-chess: restart the event stream when tests swap the API
- Commit
da7eb41c0611c72141ef988ca0d3796090768b2c- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 19:33 UTC (28 days ago)
- Parents
181ab11
init starts the event stream on the main dispatcher, which may run before a test assigns the fake-backed LichessApi to the seam (on a slow CI box it did): the stream then talked to the real API while the test pushed game events into the fake, and every online-screen flow timed out at 'playing'. The api setter now restarts the stream when already signed in, making the swap order-independent. The app never reassigns the API, so production behaviour is unchanged. Verified: :games:chess:testDevUnitTest green locally (full suite); the five CI failures were all in these online flows.
Diff
games/chess/src/main/java/com/vayunmathur/games/chess/platform/OnlineChessViewModel.kt
diff --git a/games/chess/src/main/java/com/vayunmathur/games/chess/platform/OnlineChessViewModel.kt b/games/chess/src/main/java/com/vayunmathur/games/chess/platform/OnlineChessViewModel.ktindex e054af0ab0..f61eea39ba 100644--- a/games/chess/src/main/java/com/vayunmathur/games/chess/platform/OnlineChessViewModel.kt+++ b/games/chess/src/main/java/com/vayunmathur/games/chess/platform/OnlineChessViewModel.kt@@ -48,8 +48,22 @@ class OnlineChessViewModel(application: Application) : AndroidViewModel(applicat private val tokenStore = LichessTokenStore(application) private val authManager = LichessAuthManager(application, tokenStore) - /** Test seam: tests swap in a [LichessApi] backed by a fake transport. */+ /**+ * Test seam: tests swap in a [LichessApi] backed by a fake transport.+ *+ * The swap restarts the event stream when already signed in: `init`+ * launches its stream start on the main dispatcher, which may run+ * before or after the swap depending on the test runtime's timing+ * (Robolectric on a slow CI box ran it first, and the stream then+ * talked to the real API while the test pushed into the fake).+ */ internal var api = LichessApi(tokenStore)+ set(value) {+ field = value+ if (_uiState.value.auth is OnlineAuth.SignedIn) {+ startEventStream()+ }+ } private val _uiState = MutableStateFlow(OnlineChessUiState()) val uiState: StateFlow<OnlineChessUiState> = _uiState.asStateFlow()