|
| 1 | +import Foundation |
| 2 | +import Testing |
| 3 | +@testable import Yosemite |
| 4 | +import Storage |
| 5 | + |
| 6 | +struct POSCatalogSyncCoordinatorTests { |
| 7 | + private let mockSyncService: MockPOSCatalogFullSyncService |
| 8 | + private let mockSettingsStore: MockSiteSpecificAppSettingsStoreMethods |
| 9 | + private let sut: POSCatalogSyncCoordinator |
| 10 | + private let sampleSiteID: Int64 = 134 |
| 11 | + |
| 12 | + init() { |
| 13 | + self.mockSyncService = MockPOSCatalogFullSyncService() |
| 14 | + self.mockSettingsStore = MockSiteSpecificAppSettingsStoreMethods() |
| 15 | + self.sut = POSCatalogSyncCoordinator( |
| 16 | + syncService: mockSyncService, |
| 17 | + settingsStore: mockSettingsStore |
| 18 | + ) |
| 19 | + } |
| 20 | + |
| 21 | + // MARK: - Full Sync Tests |
| 22 | + |
| 23 | + @Test func performFullSync_delegates_to_sync_service() async throws { |
| 24 | + // Given |
| 25 | + let expectedCatalog = POSCatalog( |
| 26 | + products: [POSProduct.fake()], |
| 27 | + variations: [POSProductVariation.fake()] |
| 28 | + ) |
| 29 | + mockSyncService.startFullSyncResult = expectedCatalog |
| 30 | + |
| 31 | + // When |
| 32 | + let result = try await sut.performFullSync(for: sampleSiteID) |
| 33 | + |
| 34 | + // Then |
| 35 | + #expect(result.products.count == expectedCatalog.products.count) |
| 36 | + #expect(result.variations.count == expectedCatalog.variations.count) |
| 37 | + #expect(mockSyncService.startFullSyncCallCount == 1) |
| 38 | + #expect(mockSyncService.lastSyncSiteID == sampleSiteID) |
| 39 | + } |
| 40 | + |
| 41 | + @Test func performFullSync_stores_sync_timestamp() async throws { |
| 42 | + // Given |
| 43 | + let beforeSync = Date() |
| 44 | + let expectedCatalog = POSCatalog(products: [], variations: []) |
| 45 | + mockSyncService.startFullSyncResult = expectedCatalog |
| 46 | + |
| 47 | + // When |
| 48 | + _ = try await sut.performFullSync(for: sampleSiteID) |
| 49 | + let afterSync = Date() |
| 50 | + |
| 51 | + // Then |
| 52 | + #expect(mockSettingsStore.setPOSLastFullSyncDateCallCount == 1) |
| 53 | + #expect(mockSettingsStore.lastSetSiteID == sampleSiteID) |
| 54 | + |
| 55 | + let storedDate = mockSettingsStore.lastSetDate |
| 56 | + #expect(storedDate != nil) |
| 57 | + #expect(storedDate! >= beforeSync) |
| 58 | + #expect(storedDate! <= afterSync) |
| 59 | + } |
| 60 | + |
| 61 | + @Test func performFullSync_propagates_errors() async throws { |
| 62 | + // Given |
| 63 | + let expectedError = NSError(domain: "sync", code: 500, userInfo: [NSLocalizedDescriptionKey: "Sync failed"]) |
| 64 | + mockSyncService.startFullSyncError = expectedError |
| 65 | + |
| 66 | + // When/Then |
| 67 | + await #expect(throws: expectedError) { |
| 68 | + _ = try await sut.performFullSync(for: sampleSiteID) |
| 69 | + } |
| 70 | + |
| 71 | + // Should not store timestamp on failure |
| 72 | + #expect(mockSettingsStore.setPOSLastFullSyncDateCallCount == 0) |
| 73 | + } |
| 74 | + |
| 75 | + // MARK: - Should Sync Decision Tests |
| 76 | + |
| 77 | + @Test func shouldPerformFullSync_returns_true_when_no_previous_sync() { |
| 78 | + // Given - no previous sync date stored |
| 79 | + mockSettingsStore.storedDates = [:] |
| 80 | + |
| 81 | + // When |
| 82 | + let shouldSync = sut.shouldPerformFullSync(for: sampleSiteID, maxAge: 3600) |
| 83 | + |
| 84 | + // Then |
| 85 | + #expect(shouldSync == true) |
| 86 | + #expect(mockSettingsStore.getPOSLastFullSyncDateCallCount == 1) |
| 87 | + } |
| 88 | + |
| 89 | + @Test func shouldPerformFullSync_returns_true_when_sync_is_stale() { |
| 90 | + // Given - previous sync was 2 hours ago |
| 91 | + let twoHoursAgo = Date().addingTimeInterval(-2 * 60 * 60) |
| 92 | + mockSettingsStore.storedDates[sampleSiteID] = twoHoursAgo |
| 93 | + |
| 94 | + // When - max age is 1 hour |
| 95 | + let shouldSync = sut.shouldPerformFullSync(for: sampleSiteID, maxAge: 60 * 60) |
| 96 | + |
| 97 | + // Then |
| 98 | + #expect(shouldSync == true) |
| 99 | + } |
| 100 | + |
| 101 | + @Test func shouldPerformFullSync_returns_false_when_sync_is_fresh() { |
| 102 | + // Given - previous sync was 30 minutes ago |
| 103 | + let thirtyMinutesAgo = Date().addingTimeInterval(-30 * 60) |
| 104 | + mockSettingsStore.storedDates[sampleSiteID] = thirtyMinutesAgo |
| 105 | + |
| 106 | + // When - max age is 1 hour |
| 107 | + let shouldSync = sut.shouldPerformFullSync(for: sampleSiteID, maxAge: 60 * 60) |
| 108 | + |
| 109 | + // Then |
| 110 | + #expect(shouldSync == false) |
| 111 | + } |
| 112 | + |
| 113 | + @Test func shouldPerformFullSync_handles_different_sites_independently() { |
| 114 | + // Given |
| 115 | + let siteA: Int64 = 123 |
| 116 | + let siteB: Int64 = 456 |
| 117 | + let oneHourAgo = Date().addingTimeInterval(-60 * 60) |
| 118 | + |
| 119 | + mockSettingsStore.storedDates[siteA] = oneHourAgo // Has previous sync |
| 120 | + // siteB has no previous sync |
| 121 | + |
| 122 | + // When |
| 123 | + let shouldSyncA = sut.shouldPerformFullSync(for: siteA, maxAge: 2 * 60 * 60) // 2 hours |
| 124 | + let shouldSyncB = sut.shouldPerformFullSync(for: siteB, maxAge: 2 * 60 * 60) // 2 hours |
| 125 | + |
| 126 | + // Then |
| 127 | + #expect(shouldSyncA == false) // Recent sync exists |
| 128 | + #expect(shouldSyncB == true) // No previous sync |
| 129 | + } |
| 130 | + |
| 131 | + @Test func shouldPerformFullSync_with_zero_maxAge_always_returns_true() { |
| 132 | + // Given - previous sync was just now |
| 133 | + let justNow = Date() |
| 134 | + mockSettingsStore.storedDates[sampleSiteID] = justNow |
| 135 | + |
| 136 | + // When - max age is 0 (always sync) |
| 137 | + let shouldSync = sut.shouldPerformFullSync(for: sampleSiteID, maxAge: 0) |
| 138 | + |
| 139 | + // Then |
| 140 | + #expect(shouldSync == true) |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +// MARK: - Mock Services |
| 145 | + |
| 146 | +final class MockPOSCatalogFullSyncService: POSCatalogFullSyncServiceProtocol { |
| 147 | + var startFullSyncResult: POSCatalog = POSCatalog(products: [], variations: []) |
| 148 | + var startFullSyncError: Error? |
| 149 | + |
| 150 | + private(set) var startFullSyncCallCount = 0 |
| 151 | + private(set) var lastSyncSiteID: Int64? |
| 152 | + |
| 153 | + func startFullSync(for siteID: Int64) async throws -> POSCatalog { |
| 154 | + startFullSyncCallCount += 1 |
| 155 | + lastSyncSiteID = siteID |
| 156 | + |
| 157 | + if let error = startFullSyncError { |
| 158 | + throw error |
| 159 | + } |
| 160 | + |
| 161 | + return startFullSyncResult |
| 162 | + } |
| 163 | +} |
| 164 | + |
0 commit comments