Skip to content

Commit 1214429

Browse files
committed
feat(plan-77): add priority validation and document error types
- Add priority field validation (high/medium/low, case-insensitive) - Add ConfigurationError.invalidPriority case - Document error types in README-vsock-relays.md - Add priority validation tests (valid, invalid, nil cases) - Fix test imports to use ContainerComposeCore module - Update Plan 77 with quality assessment findings
1 parent 387de22 commit 1214429

5 files changed

Lines changed: 83 additions & 3 deletions

File tree

Sources/Container-Compose/Networking/RelayConfigurationLoader.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public struct RelayConfigurationLoader {
4343
case invalidPort(UInt32)
4444
case missingTarget(String)
4545
case conflictingPort(UInt32, String)
46+
case invalidPriority(String)
4647

4748
public var description: String {
4849
switch self {
@@ -54,6 +55,8 @@ public struct RelayConfigurationLoader {
5455
return "Relay in service '\(service)' requires 'target' for inter-container routing"
5556
case .conflictingPort(let port, let service):
5657
return "Port \(port) already in use by service '\(service)'"
58+
case .invalidPriority(let priority):
59+
return "Invalid priority: '\(priority)'. Must be 'high', 'medium', or 'low' (case-insensitive)"
5760
}
5861
}
5962
}
@@ -109,6 +112,14 @@ public struct RelayConfigurationLoader {
109112
throw ConfigurationError.missingTarget(serviceName)
110113
}
111114

115+
// Validate priority (if provided)
116+
if let priority = relayConfig.priority {
117+
let validPriorities = ["high", "medium", "low"]
118+
guard validPriorities.contains(priority.lowercased()) else {
119+
throw ConfigurationError.invalidPriority(priority)
120+
}
121+
}
122+
112123
let loaded = LoadedRelay(
113124
serviceName: serviceName,
114125
type: supportedType,

Tests/Container-Compose-Tests/Integration/ComposeSchemaMappingTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import XCTest
2-
@testable import Container_Compose
2+
@testable import ContainerComposeCore
33

44
// MARK: - Compose YAML Schema Mapping Tests (Plan 77 Phase 6)
55

Tests/Container-Compose-Tests/Networking/NetworkTraceTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import XCTest
2-
@testable import Container_Compose
2+
@testable import ContainerComposeCore
33

44
// MARK: - Network Trace Tests (Plan 77 Phase 3)
55

Tests/Container-Compose-Tests/Networking/RelayConfigurationLoaderTests.swift

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import XCTest
2-
@testable import Container_Compose
2+
@testable import ContainerComposeCore
33

44
// MARK: - Relay Configuration Loader Tests (Plan 77 Phase 6)
55

@@ -155,6 +155,45 @@ final class RelayConfigurationLoaderTests: XCTestCase {
155155
}
156156
}
157157

158+
// MARK: - Priority Validation Tests
159+
160+
func testAcceptsValidPriorityValues() throws {
161+
let validPriorities = ["high", "medium", "low", "HIGH", "Medium", "LOW"]
162+
163+
for priority in validPriorities {
164+
let relay = AppleRelayConfig(type: "vsock-db", port: 5432, priority: priority)
165+
let service = Service(image: "test:latest", x_apple_relays: [relay])
166+
167+
// Should not throw
168+
let loaded = try loader.loadRelays(from: [("test", service)])
169+
XCTAssertEqual(loaded.count, 1, "Should load relay with priority: \(priority)")
170+
}
171+
}
172+
173+
func testRejectsInvalidPriorityValues() {
174+
let relay = AppleRelayConfig(type: "vsock-db", port: 5432, priority: "urgent")
175+
let service = Service(image: "test:latest", x_apple_relays: [relay])
176+
177+
XCTAssertThrowsError(try loader.loadRelays(from: [("test", service)])) { error in
178+
guard let configError = error as? RelayConfigurationLoader.ConfigurationError,
179+
case .invalidPriority(let priority) = configError else {
180+
XCTFail("Wrong error type")
181+
return
182+
}
183+
XCTAssertEqual(priority, "urgent")
184+
}
185+
}
186+
187+
func testAcceptsNilPriority() throws {
188+
// Priority is optional
189+
let relay = AppleRelayConfig(type: "vsock-db", port: 5432, priority: nil)
190+
let service = Service(image: "test:latest", x_apple_relays: [relay])
191+
192+
// Should not throw
193+
let loaded = try loader.loadRelays(from: [("test", service)])
194+
XCTAssertNil(loaded.first?.priority, "Priority should be nil")
195+
}
196+
158197
// MARK: - Integration Tests
159198

160199
func testLoadsHermesHonchoConfiguration() throws {

examples/README-vsock-relays.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,38 @@ Container-Compose validates:
9191
- Port number is valid (1-65535)
9292
- No port conflicts across services
9393
- Required `target` field for MCP/log-stream types
94+
- Priority values (if provided) are `high`, `medium`, or `low`
9495
- Schema correctness before starting containers
9596
97+
### Error Types
98+
99+
Container-Compose throws specific errors that can be caught:
100+
101+
```swift
102+
enum ConfigurationError: Error {
103+
case unsupportedRelayType(String) // Unknown relay type
104+
case invalidPort(UInt32) // Port 0 or > 65535
105+
case missingTarget(String) // MCP/log-stream missing target
106+
case conflictingPort(UInt32, String) // Port already used
107+
case invalidPriority(String) // Priority not high/medium/low
108+
}
109+
```
110+
111+
**Example error handling:**
112+
```swift
113+
do {
114+
let relays = try loader.loadRelays(from: services)
115+
} catch let error as RelayConfigurationLoader.ConfigurationError {
116+
switch error {
117+
case .invalidPriority(let value):
118+
print("Priority '\(value)' not supported. Use: high, medium, low")
119+
case .missingTarget(let service):
120+
print("Service '\(service)' requires target field")
121+
// ... handle other errors
122+
}
123+
}
124+
```
125+
96126
## Test Coverage
97127

98128
- `RelayConfigurationLoaderTests.swift` - 23 tests

0 commit comments

Comments
 (0)