using ZKTecoSyncService.Services; using ZKTecoSyncService.Models; namespace ZKTecoSyncService.Tests; /// /// Regression tests for circuit breaker fix (Fix 1). /// Tests that _retryCount increments on connection failure. /// Note: Full integration testing requires a real device or mock ZKTecoProtocol. /// These tests verify the logic at the OfflineBufferService level. /// public class OfflineBufferTests : IDisposable { private readonly string _testDb; private readonly OfflineBufferService _buffer; public OfflineBufferTests() { _testDb = Path.Combine(Path.GetTempPath(), $"test_buffer_{Guid.NewGuid():N}.db"); _buffer = new OfflineBufferService(_testDb); } public void Dispose() { try { if (File.Exists(_testDb)) File.Delete(_testDb); if (File.Exists(_testDb + "-wal")) File.Delete(_testDb + "-wal"); if (File.Exists(_testDb + "-shm")) File.Delete(_testDb + "-shm"); } catch { /* SQLite may still hold the file */ } } [Fact] public void BufferLog_InsertsRecordWithZeroAttemptCount() { var log = CreateTestLog("1001", DateTime.UtcNow, "in"); _buffer.BufferLog(log); var (pending, _) = _buffer.GetPendingLogs("192.168.1.1", 4370, 10); Assert.Single(pending); } [Fact] public void GetPendingLogs_IncrementsAttemptCount() { var log = CreateTestLog("1001", DateTime.UtcNow, "in"); _buffer.BufferLog(log); var (pending, _) = _buffer.GetPendingLogs("192.168.1.1", 4370, 10); Assert.Single(pending); // Get again — should return empty because status is now SENDING var (pending2, _) = _buffer.GetPendingLogs("192.168.1.1", 4370, 10); Assert.Empty(pending2); } [Fact] public void ResetAttemptCount_ResetsToZero() { var log = CreateTestLog("1001", DateTime.UtcNow, "in"); _buffer.BufferLog(log); var (pending, _) = _buffer.GetPendingLogs("192.168.1.1", 4370, 10); var rowIds = pending.Select(p => p.RowId).ToList(); // Reset attempt count (simulates transient failure) _buffer.ResetAttemptCount(rowIds); // Should be pickable again var (pending2, _) = _buffer.GetPendingLogs("192.168.1.1", 4370, 10); Assert.Single(pending2); } [Fact] public void GetRecordsOverMaxRetry_DetectsHighRetryCount() { // Buffer 3 records for (int i = 0; i < 3; i++) { var log = CreateTestLog($"100{i}", DateTime.UtcNow.AddMinutes(i), "in"); _buffer.BufferLog(log); } // Get and fail 5 times for (int attempt = 0; attempt < 5; attempt++) { var (pending, _) = _buffer.GetPendingLogs("192.168.1.1", 4370, 10); if (pending.Count > 0) { var rowIds = pending.Select(p => p.RowId).ToList(); _buffer.MarkAsFailed(rowIds, "test error"); } } // Now check — records should be at or over max retry var (finalPending, _) = _buffer.GetPendingLogs("192.168.1.1", 4370, 10); if (finalPending.Count > 0) { var rowIds = finalPending.Select(p => p.RowId).ToList(); var overRetry = _buffer.GetRecordsOverMaxRetry(rowIds, 5); Assert.True(overRetry.Count > 0, "Records should exceed max retry after 5 failures"); } } [Fact] public void MoveToDeadLetter_TransfersRecordCorrectly() { var log = CreateTestLog("1001", DateTime.UtcNow, "in"); _buffer.BufferLog(log); var (pending, _) = _buffer.GetPendingLogs("192.168.1.1", 4370, 10); var rowIds = pending.Select(p => p.RowId).ToList(); _buffer.MoveToDeadLetter(rowIds, "test_rejection"); var dlqCount = _buffer.GetDeadLetterCount(); Assert.Equal(1, dlqCount); // Buffer should be empty var (remaining, _) = _buffer.GetPendingLogs("192.168.1.1", 4370, 10); Assert.Empty(remaining); } [Fact] public void GetOldestPendingAt_ReturnsNonNullForBufferedData() { var log = CreateTestLog("1001", DateTime.UtcNow, "in"); _buffer.BufferLog(log); var oldest = _buffer.GetOldestPendingAt(); Assert.NotNull(oldest); } [Fact] public void GetOldestPendingAt_ReturnsNullForEmptyBuffer() { var oldest = _buffer.GetOldestPendingAt(); Assert.Null(oldest); } private static AttendanceLog CreateTestLog(string personnelCode, DateTime punchTime, string punchType) { return new AttendanceLog { DeviceIp = "192.168.1.1", DevicePort = 4370, PersonnelCode = personnelCode, PunchTime = punchTime, PunchType = punchType, VerifyMode = "fingerprint", WorkCode = 0, }; } }