|
| 1 | +/* |
| 2 | + * Copyright 2025 LiveKit |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +@testable import LiveKit |
| 18 | +#if canImport(LiveKitTestSupport) |
| 19 | +import LiveKitTestSupport |
| 20 | +#endif |
| 21 | + |
| 22 | +class ProtoConverterTests: LKTestCase { |
| 23 | + func testParticipantPermissions() { |
| 24 | + let errors = Comparator.compareStructures( |
| 25 | + proto: Livekit_ParticipantPermission(), |
| 26 | + sdk: ParticipantPermissions(), |
| 27 | + excludedFields: ["agent"], // deprecated |
| 28 | + allowedTypeMismatches: ["canPublishSources"] // Array vs Set |
| 29 | + ) |
| 30 | + |
| 31 | + XCTAssert(errors.isEmpty, errors.description) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +enum Comparator { |
| 36 | + enum ComparisonError: Error, CustomStringConvertible { |
| 37 | + case missingField(String) |
| 38 | + case extraField(String) |
| 39 | + case typeMismatch(field: String, proto: String, sdk: String) |
| 40 | + |
| 41 | + var description: String { |
| 42 | + switch self { |
| 43 | + case let .missingField(field): |
| 44 | + "Missing field: '\(field)'" |
| 45 | + case let .extraField(field): |
| 46 | + "Extra field: '\(field)'" |
| 47 | + case let .typeMismatch(field, proto, sdk): |
| 48 | + "Type mismatch for '\(field)': proto has \(proto), sdk has \(sdk)" |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + struct FieldInfo { |
| 54 | + let name: String |
| 55 | + let type: String |
| 56 | + let nonOptionalType: String |
| 57 | + } |
| 58 | + |
| 59 | + static func extractFields(from instance: some Any, excludedFields: Set<String> = []) -> [FieldInfo] { |
| 60 | + let mirror = Mirror(reflecting: instance) |
| 61 | + var fields: [FieldInfo] = [] |
| 62 | + var backingFields: Set<String> = [] |
| 63 | + |
| 64 | + // Collect all backing fields |
| 65 | + for child in mirror.children { |
| 66 | + guard let label = child.label, label.hasPrefix("_") else { continue } |
| 67 | + backingFields.insert(String(label.dropFirst())) // Remove the underscore |
| 68 | + } |
| 69 | + |
| 70 | + for child in mirror.children { |
| 71 | + guard let label = child.label else { continue } |
| 72 | + |
| 73 | + // Skip excluded/unknown fields |
| 74 | + if excludedFields.contains(label) || label == "unknownFields" { |
| 75 | + continue |
| 76 | + } |
| 77 | + |
| 78 | + // Skip private backing fields (they have public computed properties) |
| 79 | + if label.hasPrefix("_"), backingFields.contains(String(label.dropFirst())) { |
| 80 | + // But add the public version instead |
| 81 | + let publicName = String(label.dropFirst()) |
| 82 | + let typeString = String(describing: type(of: child.value)) |
| 83 | + let nonOptional = extractNonOptionalType(from: typeString) |
| 84 | + |
| 85 | + if !fields.contains(where: { $0.name == publicName }) { |
| 86 | + fields.append(FieldInfo(name: publicName, type: typeString, nonOptionalType: nonOptional)) |
| 87 | + } |
| 88 | + continue |
| 89 | + } |
| 90 | + |
| 91 | + // Skip other private fields |
| 92 | + if label.hasPrefix("_") { |
| 93 | + continue |
| 94 | + } |
| 95 | + |
| 96 | + let typeString = String(describing: type(of: child.value)) |
| 97 | + let nonOptional = extractNonOptionalType(from: typeString) |
| 98 | + |
| 99 | + fields.append(FieldInfo(name: label, type: typeString, nonOptionalType: nonOptional)) |
| 100 | + } |
| 101 | + |
| 102 | + return fields.sorted { $0.name < $1.name } |
| 103 | + } |
| 104 | + |
| 105 | + static func extractNonOptionalType(from typeString: String) -> String { |
| 106 | + if typeString.hasPrefix("Optional<"), typeString.hasSuffix(">") { |
| 107 | + let start = typeString.index(typeString.startIndex, offsetBy: 9) |
| 108 | + let end = typeString.index(before: typeString.endIndex) |
| 109 | + return String(typeString[start ..< end]) |
| 110 | + } |
| 111 | + return typeString |
| 112 | + } |
| 113 | + |
| 114 | + static func compareStructures( |
| 115 | + proto: some Any, |
| 116 | + sdk: some Any, |
| 117 | + excludedFields: Set<String> = [], |
| 118 | + allowedTypeMismatches: Set<String> = [] |
| 119 | + ) -> [ComparisonError] { |
| 120 | + let protoFields = extractFields(from: proto, excludedFields: excludedFields) |
| 121 | + let sdkFields = extractFields(from: sdk, excludedFields: excludedFields) |
| 122 | + |
| 123 | + var errors: [ComparisonError] = [] |
| 124 | + |
| 125 | + let protoFieldMap = Dictionary(uniqueKeysWithValues: protoFields.map { ($0.name, $0) }) |
| 126 | + let sdkFieldMap = Dictionary(uniqueKeysWithValues: sdkFields.map { ($0.name, $0) }) |
| 127 | + |
| 128 | + for protoField in protoFields { |
| 129 | + guard let sdkField = sdkFieldMap[protoField.name] else { |
| 130 | + errors.append(.missingField(protoField.name)) |
| 131 | + continue |
| 132 | + } |
| 133 | + |
| 134 | + if protoField.nonOptionalType != sdkField.nonOptionalType, !allowedTypeMismatches.contains(protoField.name) { |
| 135 | + errors.append(.typeMismatch( |
| 136 | + field: protoField.name, |
| 137 | + proto: protoField.type, |
| 138 | + sdk: sdkField.type |
| 139 | + )) |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + for sdkField in sdkFields { |
| 144 | + if protoFieldMap[sdkField.name] == nil { |
| 145 | + errors.append(.extraField(sdkField.name)) |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return errors |
| 150 | + } |
| 151 | +} |
0 commit comments