42 lines
2.2 KiB
Swift
42 lines
2.2 KiB
Swift
import Foundation
|
|
import Testing
|
|
import RemoteCore
|
|
@testable import SamsungProtocol
|
|
|
|
@Test func commandFramesContainExpectedClickProtocol() throws {
|
|
let expected: [RemoteCommand: String] = [
|
|
.up: "KEY_UP", .down: "KEY_DOWN", .left: "KEY_LEFT", .right: "KEY_RIGHT",
|
|
.select: "KEY_ENTER", .back: "KEY_RETURN", .home: "KEY_HOME",
|
|
.playPause: "KEY_PLAYPAUSE", .volumeUp: "KEY_VOLUP", .volumeDown: "KEY_VOLDOWN", .mute: "KEY_MUTE"
|
|
]
|
|
for command in RemoteCommand.allCases {
|
|
let frame = try #require(JSONSerialization.jsonObject(with: SamsungRemoteEncoder.clickFrame(for: command)) as? [String: Any])
|
|
#expect(frame["method"] as? String == "ms.remote.control")
|
|
let parameters = try #require(frame["params"] as? [String: String])
|
|
#expect(parameters == ["Cmd": "Click", "DataOfCmd": expected[command]!, "Option": "false", "TypeOfRemote": "SendRemoteKey"])
|
|
}
|
|
}
|
|
|
|
@Test func endpointEscapesTokenAndIdentifiesClient() throws {
|
|
let token = "prototype+token /?&=#%"
|
|
let url = try SamsungRemoteEncoder.endpoint(host: "192.0.2.10", token: token)
|
|
let parts = try #require(URLComponents(url: url, resolvingAgainstBaseURL: false))
|
|
#expect(parts.scheme == "wss")
|
|
#expect(parts.port == 8002)
|
|
#expect(parts.host == "192.0.2.10")
|
|
#expect(parts.path == "/api/v2/channels/samsung.remote.control")
|
|
#expect(parts.queryItems?.first(where: { $0.name == "token" })?.value == token)
|
|
#expect(!url.absoluteString.contains("+"))
|
|
let name = try #require(parts.queryItems?.first(where: { $0.name == "name" })?.value)
|
|
#expect(Data(base64Encoded: name).flatMap { String(data: $0, encoding: .utf8) } == "Wristivo")
|
|
}
|
|
|
|
@Test func endpointOmitsAbsentTokenAndRejectsURLsAsHosts() throws {
|
|
let url = try SamsungRemoteEncoder.endpoint(host: "tv.local")
|
|
#expect(URLComponents(url: url, resolvingAgainstBaseURL: false)?.queryItems?.count == 1)
|
|
for host in ["", "wss://tv.local", "tv.local:8002", "tv.local/path", "tv.local?token=oops", "[email protected]", "tv local", "tv.local#x"] {
|
|
#expect(throws: SamsungRemoteEncoder.EndpointError.self) { try SamsungRemoteEncoder.endpoint(host: host) }
|
|
}
|
|
#expect(try SamsungRemoteEncoder.endpoint(host: "[2001:db8::1]").port == 8002)
|
|
}
|