Establish watch relay contracts and platform package wiring

This commit is contained in:
2026-09-18 09:34:26 +04:00
parent a92698e1e6
commit 9b87299bf5
22 changed files with 778 additions and 159 deletions
@@ -0,0 +1,26 @@
import Foundation
import Testing
import RemoteCore
import WatchBridgeCommon
@Test func payloadUsesUnixMillisecondsInBothDirections() throws {
let issued = Date(timeIntervalSince1970: 1_700_000_000.125)
let envelope = CommandEnvelope(
sessionID: UUID(), televisionID: UUID(), sequence: 1,
issuedAt: issued, expiresAt: issued.addingTimeInterval(0.5), command: .right
)
let container = try RelayContainer(kind: .command, value: envelope)
let data = container.payload
#expect(container.schemaVersion == RelayWire.schemaVersion)
#expect(container.kind == .command)
let object = try #require(JSONSerialization.jsonObject(with: data) as? [String: Any])
#expect((object["issuedAt"] as? Double) == 1_700_000_000_125)
#expect((object["expiresAt"] as? Double) == 1_700_000_000_625)
#expect(try RelayJSON.decode(CommandEnvelope.self, from: data) == envelope)
}
@Test func outgoingContainerRejectsOversizedPayload() {
#expect(throws: RelayContractError.payloadTooLarge) {
try RelayContainer(kind: .error, value: String(repeating: "x", count: RelayWire.maximumPayloadBytes))
}
}
@@ -0,0 +1,19 @@
import Foundation
import Testing
import RemoteCore
import WatchBridgeCommon
import WatchBridgeWatch
@Test func watchPayloadPreservesCommandIdentityAndExpiry() throws {
let issued = Date(timeIntervalSince1970: 1_700_000_000.125)
let envelope = CommandEnvelope(
sessionID: UUID(), televisionID: UUID(), sequence: 42,
issuedAt: issued, expiresAt: issued.addingTimeInterval(0.5), command: .left
)
let container = try WatchCommandPayload.container(for: envelope)
#expect(container.kind == .command)
let data = container.payload
#expect(try RelayJSON.decode(CommandEnvelope.self, from: data) == envelope)
let object = try #require(JSONSerialization.jsonObject(with: data) as? [String: Any])
#expect((object["expiresAt"] as? Double) == 1_700_000_000_625)
}
@@ -0,0 +1,41 @@
import Foundation
import Testing
import RemoteCore
import WatchBridgePhone
private actor FakePhoneCoordinator: PhoneCommandCoordinating {
private(set) var receivedCommands: [CommandEnvelope] = []
private(set) var receivedPeers: [WatchPeerGeneration] = []
func watchBridgeDidActivate(peer: WatchPeerGeneration) {}
func watchBridgeDidDeactivate(peer: WatchPeerGeneration) {}
func beginWatchSession(_ request: BeginRemoteSessionRequest, peer: WatchPeerGeneration)
-> Result<RemoteSessionGrant, RelayFailure> {
.failure(RelayFailure(code: .unavailable))
}
func endWatchSession(_ request: EndRemoteSessionRequest, peer: WatchPeerGeneration)
-> Result<RemoteSessionEnded, RelayFailure> {
.failure(RelayFailure(code: .sessionInvalidated))
}
func submitWatchCommand(_ envelope: CommandEnvelope, peer: WatchPeerGeneration) -> CommandReceipt {
receivedCommands.append(envelope)
receivedPeers.append(peer)
return CommandReceipt(id: envelope.id, status: .rejected)
}
}
@Test func phoneDependencyUsesInjectedActorAndPreservesItsDecision() async {
let actor = FakePhoneCoordinator()
let dependency = PhoneRelayDependency(coordinator: actor)
#expect(dependency.coordinator === actor)
let peer = WatchPeerGeneration()
let now = Date(timeIntervalSince1970: 1_700_000_000)
let command = CommandEnvelope(
sessionID: UUID(), televisionID: UUID(), sequence: 1,
issuedAt: now, expiresAt: now.addingTimeInterval(0.5), command: .right
)
let receipt = await dependency.coordinator.submitWatchCommand(command, peer: peer)
#expect(receipt == CommandReceipt(id: command.id, status: .rejected))
#expect(await actor.receivedCommands == [command])
#expect(await actor.receivedPeers == [peer])
}