Skip to content

Commit 992cef1

Browse files
committed
Improve and simplify remote flag tests
1 parent 9aee94a commit 992cef1

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

Modules/Sources/Yosemite/Tools/POS/POSLocalCatalogEligibilityService.swift

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ public actor POSLocalCatalogEligibilityService: POSLocalCatalogEligibilityServic
3737
self.isLocalCatalogFeatureFlagEnabled = isLocalCatalogFeatureFlagEnabled
3838
self.remoteFeatureFlagProvider = remoteFeatureFlagProvider
3939
self.catalogSizeLimit = catalogSizeLimit ?? Constants.defaultCatalogSizeLimit
40+
// Eagerly start fetching the remote flag in the background
4041
Task {
41-
_ = await isRemoteCatalogFeatureFlagEnabled()
42+
await self.fetchRemoteFlag()
4243
}
4344
}
4445

@@ -53,22 +54,15 @@ public actor POSLocalCatalogEligibilityService: POSLocalCatalogEligibilityServic
5354
}
5455

5556
/// Fetch and cache the remote feature flag value
56-
/// Returns cached value if available, otherwise returns true without waiting for network
57+
/// Returns cached value if available, otherwise returns true (assumes eligible)
5758
private func isRemoteCatalogFeatureFlagEnabled() async -> Bool {
58-
if let cached = cachedRemoteFeatureFlag {
59-
return cached
60-
}
61-
// No cached value yet - assume eligible (true) without waiting for network
62-
// Kick off the fetch in the background to cache for next time
63-
Task { [weak self] in
64-
let value = await self?.remoteFeatureFlagProvider() ?? true
65-
await self?.cacheRemoteFeatureFlag(value)
66-
}
67-
return true
59+
// Return cached value if we have one
60+
return cachedRemoteFeatureFlag ?? true
6861
}
6962

70-
/// Cache the remote feature flag value (actor-isolated)
71-
private func cacheRemoteFeatureFlag(_ value: Bool) {
63+
/// Fetch the remote feature flag value and cache it (actor-isolated)
64+
private func fetchRemoteFlag() async {
65+
let value = await remoteFeatureFlagProvider()
7266
cachedRemoteFeatureFlag = value
7367
}
7468

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ struct POSLocalCatalogEligibilityServiceTests {
242242
#expect(sizeChecker.checkCatalogSizeCallCount == 0)
243243
}
244244

245-
@Test("Remote feature flag disabled returns ineligible")
245+
@Test("Remote feature flag disabled returns ineligible after refresh")
246246
func testRemoteFeatureFlagDisabledReturnsIneligible() async throws {
247247
let sizeChecker = MockPOSCatalogSizeChecker(
248248
sizeToReturn: .success(POSCatalogSize(productCount: 500, variationCount: 400))
@@ -257,10 +257,14 @@ struct POSLocalCatalogEligibilityServiceTests {
257257
)
258258
try await service.updatePOSEligibility(isEligible: true, for: siteID)
259259

260-
let state = try await service.catalogEligibility(for: siteID)
260+
// First refresh might check catalog (using default true before fetch completes)
261+
_ = try? await service.refreshEligibilityState(for: siteID)
262+
263+
// Second refresh should use the fetched remote flag value (false)
264+
let state = try await service.refreshEligibilityState(for: siteID)
261265

262266
guard case .ineligible(let reason) = state else {
263-
Issue.record("Expected ineligible state")
267+
Issue.record("Expected ineligible state after remote flag fetch")
264268
return
265269
}
266270

@@ -269,8 +273,9 @@ struct POSLocalCatalogEligibilityServiceTests {
269273
return
270274
}
271275

272-
// Should not have checked catalog size
273-
#expect(sizeChecker.checkCatalogSizeCallCount == 0)
276+
// Second refresh should not have checked catalog size (short-circuited by flag)
277+
// First refresh might have checked it (count could be 0 or 1)
278+
#expect(sizeChecker.checkCatalogSizeCallCount <= 1)
274279
}
275280

276281
@Test("Both feature flags required for eligibility")

0 commit comments

Comments
 (0)