feat: enhance date parsing with timestamp support, refine CSV import column mapping, and update overdue activity logic.

This commit is contained in:
Jhony Cesar
2026-07-29 11:44:23 -04:00
parent 2466250851
commit 646c6abc6b
4 changed files with 119 additions and 20 deletions

View File

@@ -4,8 +4,10 @@ import android.content.Context
import androidx.test.core.app.ApplicationProvider
import com.example.data.local.AppDatabase
import com.example.data.remote.GoogleSheetsSyncService
import com.example.data.repository.MaintenanceRepository
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
@@ -424,4 +426,90 @@ class ExampleRobolectricTest {
kotlinx.coroutines.Dispatchers.resetMain()
}
}
@OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)
@Test
fun `test analytics calculation and interactions with project CSV file`() {
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).build()
val repository = MaintenanceRepository(db.maintenanceDao(), context)
val csvFile = java.io.File("/home/cesarjhony/dev/projects/gestao_de_manutencao/documentacao/exemplo_testes/GestaoOS_Parada_project.csv")
assertTrue(csvFile.exists())
val inputStream = csvFile.inputStream()
kotlinx.coroutines.runBlocking {
val result = repository.importCsvFromInputStream(inputStream)
assertTrue("Import CSV failed: ${result.exceptionOrNull()?.message}", result.isSuccess)
val activitiesList = db.maintenanceDao().getAllActivitiesList()
println("IMPORTED ACTIVITIES COUNT: ${activitiesList.size}")
if (activitiesList.isNotEmpty()) {
println("FIRST ACTIVITY: ${activitiesList.first()}")
}
val viewModel = com.example.ui.viewmodel.MaintenanceViewModel(repository)
val collectionJob = viewModel.viewModelScope.launch {
viewModel.analyticsState.collect {}
}
val historyJob = viewModel.viewModelScope.launch {
viewModel.allHistoryState.collect {}
}
Thread.sleep(500)
org.robolectric.shadows.ShadowLooper.idleMainLooper()
Thread.sleep(200)
val state = viewModel.analyticsState.value
println("ANALYTICS STATE TIMELINE DATES: ${state.timelineDates.size}")
assertNotNull(state)
assertEquals(5, state.timelineDates.size)
assertEquals(5, state.plannedProgressPoints.size)
assertEquals(5, state.realizedProgressPoints.size)
// 1. Current Date (July 2026): 0 overdue OSs because all CSV activities are in Aug/Sep 2026
val overdueJuly = state.overdueBySector.sumOf { it.second }
assertEquals(0, overdueJuly)
// 2. Simulated Future Date (August 20, 2026): OSs due before Aug 20 start appearing as overdue
val aug20 = com.example.data.model.parseFullDateTime("20/08/26 18:00")!!
val stateAug20 = viewModel.calculateAnalyticsState(activitiesList, emptyList(), referenceDateOverride = aug20)
val overdueAug20 = stateAug20.overdueBySector.sumOf { it.second }
assertTrue("Expected overdue activities on Aug 20, 2026, got: $overdueAug20", overdueAug20 > 0)
// 3. Simulated Future Date (September 15, 2026): All incomplete activities in project are overdue
val sep15 = com.example.data.model.parseFullDateTime("15/09/26 18:00")!!
val stateSep15 = viewModel.calculateAnalyticsState(activitiesList, emptyList(), referenceDateOverride = sep15)
val overdueSep15 = stateSep15.overdueBySector.sumOf { it.second }
assertTrue("Expected most CSV activities to be overdue on Sep 15, got: $overdueSep15", overdueSep15 > 400)
val targetOs = db.maintenanceDao().getAllActivitiesList().firstOrNull()?.numeroOs ?: "1024"
repository.updateProgress(
numeroOs = targetOs,
novoPercentual = 100,
observacao = "Test OS completed",
fotoPath = null,
recurso = "GTEC 1",
emailEditor = "test@empresa.com"
)
Thread.sleep(200)
org.robolectric.shadows.ShadowLooper.idleMainLooper()
val historyList = db.maintenanceDao().getAllHistoryList()
assertTrue("History list size: ${historyList.size}", historyList.any { it.numeroOs == targetOs && it.percentualNovo == 100 })
collectionJob.cancel()
historyJob.cancel()
}
db.close()
} finally {
kotlinx.coroutines.Dispatchers.resetMain()
}
}
}