|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift OpenFeature open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 the Swift OpenFeature project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// |
| 10 | +// SPDX-License-Identifier: Apache-2.0 |
| 11 | +// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +import Foundation |
| 15 | +import OFREP |
| 16 | +import OpenFeature |
| 17 | +import ServiceLifecycle |
| 18 | +import Testing |
| 19 | + |
| 20 | +@testable import Logging |
| 21 | + |
| 22 | +@Suite("OFREP Integration Tests") |
| 23 | +struct OFREPIntegrationTests { |
| 24 | + @Suite("Bool Flag Resolution") |
| 25 | + struct BoolResolutionTests { |
| 26 | + @Test("Static", arguments: [("static-on", true), ("static-off", false)]) |
| 27 | + func staticBool(flag: String, expectedValue: Bool) async throws { |
| 28 | + let provider = OFREPProvider(serverURL: URL(string: "http://localhost:8016")!) |
| 29 | + |
| 30 | + try await withOFREPProvider(provider) { |
| 31 | + let resolution = OpenFeatureResolution( |
| 32 | + value: expectedValue, |
| 33 | + error: nil, |
| 34 | + reason: .static, |
| 35 | + variant: expectedValue ? "on" : "off", |
| 36 | + flagMetadata: [:] |
| 37 | + ) |
| 38 | + await #expect(provider.resolution(of: flag, defaultValue: !expectedValue, context: nil) == resolution) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + @Test("Targeting Match") |
| 43 | + func targetingMatch() async throws { |
| 44 | + let provider = OFREPProvider(serverURL: URL(string: "http://localhost:8016")!) |
| 45 | + |
| 46 | + try await withOFREPProvider(provider) { |
| 47 | + let resolution = OpenFeatureResolution( |
| 48 | + value: true, |
| 49 | + error: nil, |
| 50 | + reason: .targetingMatch, |
| 51 | + variant: "on", |
| 52 | + flagMetadata: [:] |
| 53 | + ) |
| 54 | + let flag = "targeting-on" |
| 55 | + let context = OpenFeatureEvaluationContext(targetingKey: "swift") |
| 56 | + await #expect(provider.resolution(of: flag, defaultValue: false, context: context) == resolution) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Test("No Targeting Match") |
| 61 | + func noTargetingMatch() async throws { |
| 62 | + let provider = OFREPProvider(serverURL: URL(string: "http://localhost:8016")!) |
| 63 | + |
| 64 | + try await withOFREPProvider(provider) { |
| 65 | + let resolution = OpenFeatureResolution( |
| 66 | + value: false, |
| 67 | + error: nil, |
| 68 | + reason: .default, |
| 69 | + variant: "off", |
| 70 | + flagMetadata: [:] |
| 71 | + ) |
| 72 | + let flag = "targeting-on" |
| 73 | + await #expect(provider.resolution(of: flag, defaultValue: true, context: nil) == resolution) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Test("Type mismatch", arguments: [true, false]) |
| 78 | + func typeMismatch(defaultValue: Bool) async throws { |
| 79 | + let provider = OFREPProvider(serverURL: URL(string: "http://localhost:8016")!) |
| 80 | + |
| 81 | + try await withOFREPProvider(provider) { |
| 82 | + let resolution = OpenFeatureResolution( |
| 83 | + value: defaultValue, |
| 84 | + error: OpenFeatureResolutionError( |
| 85 | + code: .typeMismatch, |
| 86 | + message: #"Expected flag value of type "Bool" but received "String"."# |
| 87 | + ), |
| 88 | + reason: .error, |
| 89 | + variant: "a" |
| 90 | + ) |
| 91 | + let flag = "static-a-b" |
| 92 | + await #expect(provider.resolution(of: flag, defaultValue: defaultValue, context: nil) == resolution) |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @Test("Flag not found", arguments: [true, false]) |
| 98 | + func flagNotFound(defaultValue: Bool) async throws { |
| 99 | + let provider = OFREPProvider(serverURL: URL(string: "http://localhost:8016")!) |
| 100 | + |
| 101 | + try await withOFREPProvider(provider) { |
| 102 | + let resolution = OpenFeatureResolution( |
| 103 | + value: defaultValue, |
| 104 | + error: OpenFeatureResolutionError(code: .flagNotFound, message: "flag `💩` does not exist"), |
| 105 | + reason: .error |
| 106 | + ) |
| 107 | + await #expect(provider.resolution(of: "💩", defaultValue: defaultValue, context: nil) == resolution) |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +private func withOFREPProvider<Transport: OFREPClientTransport>( |
| 113 | + _ provider: OFREPProvider<Transport>, |
| 114 | + perform integrationTest: @escaping @Sendable () async throws -> Void |
| 115 | +) async throws { |
| 116 | + let integrationTestService = IntegrationTestService(test: integrationTest) |
| 117 | + let group = ServiceGroup( |
| 118 | + configuration: ServiceGroupConfiguration( |
| 119 | + services: [ |
| 120 | + .init(service: provider), |
| 121 | + .init(service: integrationTestService, successTerminationBehavior: .gracefullyShutdownGroup), |
| 122 | + ], |
| 123 | + logger: Logger(label: #function) |
| 124 | + ) |
| 125 | + ) |
| 126 | + |
| 127 | + try await group.run() |
| 128 | +} |
| 129 | + |
| 130 | +private struct IntegrationTestService: Service { |
| 131 | + let test: @Sendable () async throws -> Void |
| 132 | + |
| 133 | + func run() async throws { |
| 134 | + try await test() |
| 135 | + } |
| 136 | +} |
0 commit comments