-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Issues with context/secret trigger error (#137)
* fix: Missing context emits an error local-event * fix: Non-authenticated fetches trigger error * refactor: no OF context init is allowed * refactor: Renaming test classes * test: [OF] Test errors on init * fix: todo comments * feat: Add docs for public functions * fix: Remove unnecessary check * fix: Rename test class * fix: Formatting
- Loading branch information
1 parent
d295ffa
commit 54a674e
Showing
5 changed files
with
111 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
Tests/ConfidenceProviderTests/ConfidenceProviderTest.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import Foundation | ||
import ConfidenceProvider | ||
import Combine | ||
import OpenFeature | ||
import XCTest | ||
|
||
@testable import Confidence | ||
|
||
class ConfidenceProviderTest: XCTestCase { | ||
private var readyExpectation = XCTestExpectation(description: "Ready") | ||
private var errorExpectation = XCTestExpectation(description: "Error") | ||
|
||
func testErrorFetchOnInit() async throws { | ||
class FakeClient: ConfidenceResolveClient { | ||
func resolve(ctx: ConfidenceStruct) async throws -> ResolvesResult { | ||
throw ConfidenceError.internalError(message: "test") | ||
} | ||
} | ||
|
||
let client = FakeClient() | ||
let confidence = Confidence.Builder(clientSecret: "test") | ||
.withContext(initialContext: ["targeting_key": .init(string: "user1")]) | ||
.withFlagResolverClient(flagResolver: client) | ||
.build() | ||
|
||
let provider = ConfidenceFeatureProvider(confidence: confidence, initializationStrategy: .activateAndFetchAsync) | ||
OpenFeatureAPI.shared.setProvider(provider: provider) | ||
|
||
let cancellable = OpenFeatureAPI.shared.observe().sink { event in | ||
if event == .ready { | ||
self.readyExpectation.fulfill() | ||
} else { | ||
print(event) | ||
} | ||
} | ||
await fulfillment(of: [readyExpectation], timeout: 5.0) | ||
cancellable.cancel() | ||
} | ||
|
||
func testErrorStorageOnInit() async throws { | ||
class FakeStorage: Storage { | ||
func save(data: Encodable) throws { | ||
// no-op | ||
} | ||
|
||
func load<T>(defaultValue: T) throws -> T where T: Decodable { | ||
throw ConfidenceError.internalError(message: "test") | ||
} | ||
|
||
func clear() throws { | ||
// no-op | ||
} | ||
|
||
func isEmpty() -> Bool { | ||
return false | ||
} | ||
} | ||
|
||
let confidence = Confidence.Builder(clientSecret: "test") | ||
.withContext(initialContext: ["targeting_key": .init(string: "user1")]) | ||
.withStorage(storage: FakeStorage()) | ||
.build() | ||
|
||
let provider = ConfidenceFeatureProvider(confidence: confidence, initializationStrategy: .activateAndFetchAsync) | ||
OpenFeatureAPI.shared.setProvider(provider: provider) | ||
|
||
let cancellable = OpenFeatureAPI.shared.observe().sink { event in | ||
if event == .error { | ||
self.errorExpectation.fulfill() | ||
} else { | ||
print(event) | ||
} | ||
} | ||
await fulfillment(of: [errorExpectation], timeout: 5.0) | ||
cancellable.cancel() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters