feat: implement maintenance analytics state with team metrics and progress tracking in ViewModel and UI

This commit is contained in:
Jhony Cesar
2026-07-28 00:18:35 -04:00
parent cd17f02e21
commit a8d1ea9f4b
4 changed files with 257 additions and 127 deletions

View File

@@ -181,4 +181,82 @@ class ExampleRobolectricTest {
kotlinx.coroutines.Dispatchers.resetMain()
}
}
@OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)
@Test
fun `test analytics state calculations in ViewModel`() {
val testDispatcher = kotlinx.coroutines.test.UnconfinedTestDispatcher()
kotlinx.coroutines.Dispatchers.setMain(testDispatcher)
try {
val context = ApplicationProvider.getApplicationContext<Context>()
val db = androidx.room.Room.inMemoryDatabaseBuilder(context, AppDatabase::class.java)
.allowMainThreadQueries()
.setQueryExecutor { it.run() }
.setTransactionExecutor { it.run() }
.build()
val repository = com.example.data.repository.MaintenanceRepository(db.maintenanceDao(), context)
val act1 = com.example.data.model.MaintenanceActivity(
numeroOs = "OS-1",
descricao = "Inspection 1",
setor = "Geral",
equipe = "Mecânica",
recurso = "Sem Recurso",
dataInicio = "Sex 14/08/26 07:00",
dataFim = "Sex 14/08/26 17:00",
percentualExecucao = 100,
tag = "TAG-MEC",
synced = true
)
val act2 = com.example.data.model.MaintenanceActivity(
numeroOs = "OS-2",
descricao = "Repair 2",
setor = "Geral",
equipe = "Elétrica",
recurso = "Sem Recurso",
dataInicio = "14/08/2026 08:30",
dataFim = "15/08/2026 17:00",
percentualExecucao = 50,
tag = "TAG-ELE",
synced = true
)
kotlinx.coroutines.runBlocking {
db.maintenanceDao().insertActivities(listOf(act1, act2))
val viewModel = com.example.ui.viewmodel.MaintenanceViewModel(repository)
// Start collection to activate stateIn subscription
val collectionJob = viewModel.viewModelScope.launch {
viewModel.analyticsState.collect {}
}
// Allow Room flow to emit
Thread.sleep(200)
org.robolectric.shadows.ShadowLooper.idleMainLooper()
val state = viewModel.analyticsState.value
// 1. Verify team metrics
val mecProductivity = state.teamMetrics.find { it.first == "Mecânica" }?.second
val eleProductivity = state.teamMetrics.find { it.first == "Elétrica" }?.second
assertEquals(100, mecProductivity)
assertEquals(0, eleProductivity) // OS-2 is only 50%, so not completed (>= 100)
// 2. Verify timeline dates size (should be 5 steps)
assertEquals(5, state.timelineDates.size)
// 3. Verify planned progress points has correct count
assertEquals(5, state.plannedProgressPoints.size)
// 4. Verify realized progress points has correct count
assertEquals(5, state.realizedProgressPoints.size)
collectionJob.cancel()
}
db.close()
} finally {
kotlinx.coroutines.Dispatchers.resetMain()
}
}
}