Skip to content

StreamVault is a TV-first free IPTV player for Android TV built with Kotlin, Jetpack Compose, Room, Hilt, and Media3.

feat(data): ajoute SyncProgressBus singleton + tests unitaires

Celtioree293430a6890
diff --git a/data/src/main/java/com/streamvault/data/sync/SyncProgressBus.kt b/data/src/main/java/com/streamvault/data/sync/SyncProgressBus.kt
new file mode 100644
index 00000000..f4f580d3
--- /dev/null
+++ b/data/src/main/java/com/streamvault/data/sync/SyncProgressBus.kt
@@ -0,0 +1,35 @@
+package com.streamvault.data.sync
+
+import com.streamvault.domain.sync.SyncProgress
+import javax.inject.Inject
+import javax.inject.Singleton
+import kotlinx.coroutines.flow.MutableStateFlow
+import kotlinx.coroutines.flow.StateFlow
+import kotlinx.coroutines.flow.asStateFlow
+
+/**
+ * Bus singleton de progression du sync catalogue.
+ *
+ * Publie le dernier [SyncProgress] émis par `SyncManager` et ses stratégies, consommé
+ * par `WelcomeViewModel` côté `:app`. Pattern miroir de `SyncStateTracker` (StateFlow
+ * privé mutable + façade publique read-only).
+ *
+ * Valeur initiale `null` = aucun sync en cours / écran fallback. `reset()` ramène
+ * explicitement le flow à `null` à la fin de chaque cycle (succès, échec ou abort
+ * low-memory) pour éviter qu'un écran ultérieur n'hérite d'un état obsolète.
+ */
+@Singleton
+class SyncProgressBus @Inject constructor() {
+
+ private val _flow = MutableStateFlow<SyncProgress?>(null)
+
+ val flow: StateFlow<SyncProgress?> = _flow.asStateFlow()
+
+ fun emit(progress: SyncProgress) {
+ _flow.value = progress
+ }
+
+ fun reset() {
+ _flow.value = null
+ }
+}
diff --git a/data/src/test/java/com/streamvault/data/sync/SyncProgressBusTest.kt b/data/src/test/java/com/streamvault/data/sync/SyncProgressBusTest.kt
new file mode 100644
index 00000000..736c34ed
--- /dev/null
+++ b/data/src/test/java/com/streamvault/data/sync/SyncProgressBusTest.kt
@@ -0,0 +1,101 @@
+package com.streamvault.data.sync
+
+import com.google.common.truth.Truth.assertThat
+import com.streamvault.domain.sync.Section
+import com.streamvault.domain.sync.SyncProgress
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.flow.toList
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.test.UnconfinedTestDispatcher
+import kotlinx.coroutines.test.runTest
+import org.junit.Test
+
+/**
+ * Tests unitaires de [SyncProgressBus].
+ *
+ * Couvre les 4 cas du test plan §7 du SCOPE M1 :
+ * 1. `emit` met à jour la valeur courante du flow.
+ * 2. `reset` remet le flow à `null`.
+ * 3. Deux collectors observent la même séquence d'émissions.
+ * 4. Un collector qui souscrit tardivement reçoit la dernière valeur (contrat StateFlow).
+ */
+@OptIn(ExperimentalCoroutinesApi::class)
+class SyncProgressBusTest {
+
+ @Test
+ fun emit_updatesFlowValue() = runTest {
+ val bus = SyncProgressBus()
+ val progress = SyncProgress(
+ section = Section.LIVE,
+ current = 3,
+ total = 10,
+ currentLabel = "Sport",
+ itemsIndexed = 42
+ )
+
+ bus.emit(progress)
+
+ assertThat(bus.flow.value).isEqualTo(progress)
+ }
+
+ @Test
+ fun reset_setsFlowToNull() = runTest {
+ val bus = SyncProgressBus()
+ bus.emit(
+ SyncProgress(
+ section = Section.VOD,
+ current = 1,
+ total = 5,
+ currentLabel = "Movies",
+ itemsIndexed = 100
+ )
+ )
+ assertThat(bus.flow.value).isNotNull()
+
+ bus.reset()
+
+ assertThat(bus.flow.value).isNull()
+ }
+
+ @Test
+ fun twoCollectors_receiveSameSequence() = runTest(UnconfinedTestDispatcher()) {
+ val bus = SyncProgressBus()
+ val first = mutableListOf<SyncProgress?>()
+ val second = mutableListOf<SyncProgress?>()
+
+ val firstJob = launch { bus.flow.toList(first) }
+ val secondJob = launch { bus.flow.toList(second) }
+
+ val progressLive = SyncProgress(Section.LIVE, 1, 4, "Live", 10)
+ val progressVod = SyncProgress(Section.VOD, 2, 4, "VOD", 20)
+ bus.emit(progressLive)
+ bus.emit(progressVod)
+ bus.reset()
+
+ firstJob.cancel()
+ secondJob.cancel()
+
+ assertThat(first).containsExactly(null, progressLive, progressVod, null).inOrder()
+ assertThat(second).containsExactly(null, progressLive, progressVod, null).inOrder()
+ }
+
+ @Test
+ fun lateCollector_receivesReplayValue() = runTest(UnconfinedTestDispatcher()) {
+ val bus = SyncProgressBus()
+ val progress = SyncProgress(
+ section = Section.SERIES,
+ current = 7,
+ total = 12,
+ currentLabel = "Drama",
+ itemsIndexed = 350
+ )
+
+ bus.emit(progress)
+
+ val received = mutableListOf<SyncProgress?>()
+ val job = launch { bus.flow.toList(received) }
+ job.cancel()
+
+ assertThat(received).containsExactly(progress)
+ }
+}