Skip to content

Commit 15cfef7

Browse files
committed
Add tests for skipReason computed properties and sync type
1 parent 4314a71 commit 15cfef7

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

Modules/Tests/YosemiteTests/Tools/POS/POSCatalogSyncCoordinatorTests.swift

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,7 @@ extension POSCatalogSyncCoordinatorTests {
13311331
let syncSkipped = mockAnalytics.trackedEvents.first { $0.eventName == "local_catalog_sync_skipped" }
13321332
#expect(syncSkipped != nil)
13331333
#expect(syncSkipped?.properties?["reason"] as? String == "no_full_sync")
1334+
#expect(syncSkipped?.properties?["sync_type"] as? String == "incremental")
13341335
}
13351336

13361337
@Test func performFullSyncIfApplicable_tracks_sync_skipped_when_not_stale() async throws {
@@ -1354,6 +1355,87 @@ extension POSCatalogSyncCoordinatorTests {
13541355
let syncSkipped = mockAnalytics.trackedEvents.first { $0.eventName == "local_catalog_sync_skipped" }
13551356
#expect(syncSkipped != nil)
13561357
#expect(syncSkipped?.properties?["reason"] as? String == "catalog_not_stale")
1358+
#expect(syncSkipped?.properties?["sync_type"] as? String == "full")
1359+
}
1360+
1361+
// MARK: - Sync Type Analytics
1362+
1363+
@Test("POSCatalogSyncType enum has correct raw values")
1364+
func testPOSCatalogSyncTypeRawValues() {
1365+
#expect(POSCatalogSyncType.full.rawValue == "full")
1366+
#expect(POSCatalogSyncType.incremental.rawValue == "incremental")
1367+
}
1368+
1369+
// MARK: - POS Not Opened 30 Days Skip Reason Tests
1370+
1371+
@Test func performFullSyncIfApplicable_tracks_pos_not_opened_30_days_when_not_opened_recently() async throws {
1372+
// Given - Store with first sync > 30 days ago and last opened > 30 days ago
1373+
let mockAnalytics = MockAnalytics()
1374+
let mockSiteSettings = MockSiteSpecificAppSettingsStoreMethods()
1375+
1376+
// Set first sync date to 40 days ago
1377+
let firstSyncDate = Calendar.current.date(byAdding: .day, value: -40, to: Date())!
1378+
mockSiteSettings.setFirstPOSCatalogSyncDate(siteID: sampleSiteID, date: firstSyncDate)
1379+
1380+
// Set last opened date to 35 days ago (more than 30 days)
1381+
let lastOpenedDate = Calendar.current.date(byAdding: .day, value: -35, to: Date())!
1382+
mockSiteSettings.setPOSLastOpenedDate(siteID: sampleSiteID, date: lastOpenedDate)
1383+
1384+
// Create site in database with full sync date
1385+
try createSiteInDatabase(siteID: sampleSiteID, lastFullSyncDate: firstSyncDate)
1386+
1387+
let sut = POSCatalogSyncCoordinator(
1388+
fullSyncService: mockSyncService,
1389+
incrementalSyncService: mockIncrementalSyncService,
1390+
grdbManager: grdbManager,
1391+
catalogEligibilityChecker: mockEligibilityChecker,
1392+
siteSettings: mockSiteSettings,
1393+
analytics: mockAnalytics,
1394+
connectivityObserver: nil
1395+
)
1396+
1397+
// When - Try to perform sync
1398+
try? await sut.performFullSyncIfApplicable(for: sampleSiteID, maxAge: .zero)
1399+
1400+
// Then - Should track pos_not_opened_30_days
1401+
let syncSkipped = mockAnalytics.trackedEvents.first { $0.eventName == "local_catalog_sync_skipped" }
1402+
#expect(syncSkipped != nil)
1403+
#expect(syncSkipped?.properties?["reason"] as? String == "pos_not_opened_30_days")
1404+
#expect(syncSkipped?.properties?["sync_type"] as? String == "full")
1405+
}
1406+
1407+
@Test func performIncrementalSyncIfApplicable_tracks_pos_not_opened_30_days_when_never_opened_and_past_grace_period() async throws {
1408+
// Given - Store with first sync > 30 days ago and never opened POS
1409+
let mockAnalytics = MockAnalytics()
1410+
let mockSiteSettings = MockSiteSpecificAppSettingsStoreMethods()
1411+
1412+
// Set first sync date to 40 days ago
1413+
let firstSyncDate = Calendar.current.date(byAdding: .day, value: -40, to: Date())!
1414+
mockSiteSettings.setFirstPOSCatalogSyncDate(siteID: sampleSiteID, date: firstSyncDate)
1415+
1416+
// Don't set last opened date (nil = never opened)
1417+
1418+
// Create site in database with full sync date
1419+
try createSiteInDatabase(siteID: sampleSiteID, lastFullSyncDate: firstSyncDate)
1420+
1421+
let sut = POSCatalogSyncCoordinator(
1422+
fullSyncService: mockSyncService,
1423+
incrementalSyncService: mockIncrementalSyncService,
1424+
grdbManager: grdbManager,
1425+
catalogEligibilityChecker: mockEligibilityChecker,
1426+
siteSettings: mockSiteSettings,
1427+
analytics: mockAnalytics,
1428+
connectivityObserver: nil
1429+
)
1430+
1431+
// When - Try to perform incremental sync
1432+
try await sut.performIncrementalSyncIfApplicable(for: sampleSiteID, maxAge: .zero)
1433+
1434+
// Then - Should track pos_not_opened_30_days
1435+
let syncSkipped = mockAnalytics.trackedEvents.first { $0.eventName == "local_catalog_sync_skipped" }
1436+
#expect(syncSkipped != nil)
1437+
#expect(syncSkipped?.properties?["reason"] as? String == "pos_not_opened_30_days")
1438+
#expect(syncSkipped?.properties?["sync_type"] as? String == "incremental")
13571439
}
13581440
}
13591441

Modules/Tests/YosemiteTests/Tools/POS/POSLocalCatalogEligibilityServiceTests.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,4 +536,31 @@ struct POSLocalCatalogEligibilityServiceTests {
536536
#expect(reason == expectedReason)
537537
#expect(sizeChecker.checkCatalogSizeCallCount == 0)
538538
}
539+
540+
// MARK: - Skip Reason Analytics
541+
542+
@Test("POSLocalCatalogIneligibleReason skipReason returns correct analytics string")
543+
func testSkipReasonReturnsCorrectAnalyticsString() {
544+
#expect(POSLocalCatalogIneligibleReason.posTabNotEligible.skipReason == "pos_not_eligible")
545+
#expect(POSLocalCatalogIneligibleReason.featureFlagDisabled.skipReason == "feature_flag_disabled")
546+
#expect(POSLocalCatalogIneligibleReason.unsupportedWooCommerceVersion(minimumVersion: "10.3.0").skipReason == "unsupported_woocommerce_version")
547+
#expect(POSLocalCatalogIneligibleReason.catalogSizeTooLarge(totalCount: 1500, limit: 1000).skipReason == "catalog_too_large")
548+
#expect(POSLocalCatalogIneligibleReason.catalogSizeCheckFailed(underlyingError: "error").skipReason == "catalog_size_check_failed")
549+
}
550+
551+
@Test("Skip reason strings are consistent regardless of associated values")
552+
func testSkipReasonConsistentRegardlessOfAssociatedValues() {
553+
// Test that associated values don't affect the skip reason string
554+
let version1 = POSLocalCatalogIneligibleReason.unsupportedWooCommerceVersion(minimumVersion: "10.3.0")
555+
let version2 = POSLocalCatalogIneligibleReason.unsupportedWooCommerceVersion(minimumVersion: "11.0.0")
556+
#expect(version1.skipReason == version2.skipReason)
557+
558+
let sizeTooLarge1 = POSLocalCatalogIneligibleReason.catalogSizeTooLarge(totalCount: 1500, limit: 1000)
559+
let sizeTooLarge2 = POSLocalCatalogIneligibleReason.catalogSizeTooLarge(totalCount: 2000, limit: 1000)
560+
#expect(sizeTooLarge1.skipReason == sizeTooLarge2.skipReason)
561+
562+
let checkFailed1 = POSLocalCatalogIneligibleReason.catalogSizeCheckFailed(underlyingError: "error1")
563+
let checkFailed2 = POSLocalCatalogIneligibleReason.catalogSizeCheckFailed(underlyingError: "error2")
564+
#expect(checkFailed1.skipReason == checkFailed2.skipReason)
565+
}
539566
}

0 commit comments

Comments
 (0)