feat: implement remote database epoch sync to purge stale local data and add status tracking for synced history logs
This commit is contained in:
@@ -578,4 +578,99 @@ class ExampleRobolectricTest {
|
||||
kotlinx.coroutines.Dispatchers.resetMain()
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)
|
||||
@Test
|
||||
fun `test database epoch reconciliation and synced flag purge rules`() {
|
||||
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 dao = db.maintenanceDao()
|
||||
|
||||
kotlinx.coroutines.runBlocking {
|
||||
dao.clearAllActivities()
|
||||
dao.clearAllHistory()
|
||||
dao.clearAllFindings()
|
||||
|
||||
// 1. Create active activity OS-100 and OS-200
|
||||
val act1 = com.example.data.model.MaintenanceActivity(
|
||||
numeroOs = "OS-100", descricao = "Tarefa 100", setor = "Mecânica", equipe = "Mecânica",
|
||||
recurso = "Eng 1", dataInicio = "01/08/2026", dataFim = "02/08/2026", percentualExecucao = 50, synced = true
|
||||
)
|
||||
dao.insertActivity(act1)
|
||||
|
||||
// 2. Insert synced history for OS-999 (which is stale/removed remotely)
|
||||
val staleSyncedHistory = com.example.data.model.OSUpdateHistory(
|
||||
numeroOs = "OS-999", timestamp = 1000L, percentualAnterior = 0, percentualNovo = 50,
|
||||
observacao = "Histórico antigo sincronizado", recurso = "Eng 1", synced = true
|
||||
)
|
||||
dao.insertHistory(staleSyncedHistory)
|
||||
|
||||
// 3. Insert unsynced history for OS-100 (pending offline change created by user)
|
||||
val offlinePendingHistory = com.example.data.model.OSUpdateHistory(
|
||||
numeroOs = "OS-100", timestamp = 2000L, percentualAnterior = 50, percentualNovo = 100,
|
||||
observacao = "Histórico novo offline", recurso = "Eng 1", synced = false
|
||||
)
|
||||
dao.insertHistory(offlinePendingHistory)
|
||||
|
||||
// 4. Insert synced finding for OS-999 (stale) and unsynced finding for OS-100 (pending)
|
||||
val staleSyncedFinding = com.example.data.model.OSFinding(
|
||||
id = "finding-stale", numeroOs = "OS-999", tipo = "OBS", descricao = "Achado antigo",
|
||||
timestamp = 1000L, responsavel = "Eng 1", synced = true
|
||||
)
|
||||
val offlinePendingFinding = com.example.data.model.OSFinding(
|
||||
id = "finding-offline", numeroOs = "OS-100", tipo = "OBS", descricao = "Achado novo offline",
|
||||
timestamp = 2000L, responsavel = "Eng 1", synced = false
|
||||
)
|
||||
dao.insertFinding(staleSyncedFinding)
|
||||
dao.insertFinding(offlinePendingFinding)
|
||||
|
||||
// Verify initial state before epoch purge
|
||||
assertEquals(2, dao.getAllHistoryList().size)
|
||||
assertEquals(2, dao.getAllFindingsList().size)
|
||||
assertEquals(1, dao.getUnsyncedHistory().size)
|
||||
|
||||
// 5. Execute Epoch purge for active OS list ["OS-100"]
|
||||
dao.deleteStaleSyncedHistory(listOf("OS-100"))
|
||||
dao.deleteStaleSyncedFindings(listOf("OS-100"))
|
||||
|
||||
// 6. Verify that stale synced OS-999 entries were purged, while unsynced OS-100 entries were preserved
|
||||
val historyAfterPurge = dao.getAllHistoryList()
|
||||
assertEquals(1, historyAfterPurge.size)
|
||||
assertEquals("OS-100", historyAfterPurge.first().numeroOs)
|
||||
assertEquals(false, historyAfterPurge.first().synced)
|
||||
|
||||
val findingsAfterPurge = dao.getAllFindingsList()
|
||||
assertEquals(1, findingsAfterPurge.size)
|
||||
assertEquals("OS-100", findingsAfterPurge.first().numeroOs)
|
||||
assertEquals(false, findingsAfterPurge.first().synced)
|
||||
|
||||
// 7. Test marking history as synced
|
||||
val unsyncedHist = historyAfterPurge.first()
|
||||
dao.markHistorySyncedByOsAndTimestamp(unsyncedHist.numeroOs, unsyncedHist.timestamp)
|
||||
|
||||
val historyFinal = dao.getAllHistoryList()
|
||||
assertEquals(true, historyFinal.first().synced)
|
||||
assertEquals(0, dao.getUnsyncedHistory().size)
|
||||
}
|
||||
db.close()
|
||||
} finally {
|
||||
kotlinx.coroutines.Dispatchers.resetMain()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test fetchDatabaseEpoch graceful fallback`() {
|
||||
val context = ApplicationProvider.getApplicationContext<Context>()
|
||||
val service = GoogleSheetsSyncService(context)
|
||||
val epoch = service.fetchDatabaseEpoch()
|
||||
assertTrue("Epoch default should be >= 1, got $epoch", epoch >= 1)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user