Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .oxfmtrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"node_modules/**",
"**/*.md",
"scripts/maestro-conformance/corpus/**",
"fallow-baselines/**"
"fallow-baselines/**",
// One entry per line, so each pinned runner request changes as one reviewable diff line.
"contracts/fixtures/runner-requests.json"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import AgentDeviceSnapshotPresentation

// MARK: - Wire Models

enum CommandType: String, Codable {
enum CommandType: String, Codable, CaseIterable {
case tap
case mouseClick
case longPress
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,115 @@ extension RunnerTests {
func runnerCommandFixture(_ json: String) throws -> Command {
try JSONDecoder().decode(Command.self, from: Data(json.utf8))
}

func testProductionRunnerRequestsDecodeWithoutDroppingAKey() throws {
for (name, request) in try productionRunnerRequests() {
let command = try decodeProductionRunnerRequest(request, name)
XCTAssertEqual(command.command.rawValue, request["command"] as? String, name)
let reencoded = try JSONSerialization.jsonObject(with: JSONEncoder().encode(command))
XCTAssertEqual(runnerRequestKeyPaths(reencoded), runnerRequestKeyPaths(request), name)
}
}

func testEveryRunnerCommandTypeHasAProductionRequest() throws {
let produced = Set(try productionRunnerRequests().compactMap { $0.request["command"] as? String })
let orphaned = CommandType.allCases.map(\.rawValue).filter { !produced.contains($0) }
XCTAssertEqual(orphaned, [], "CommandType cases with no production request")
}

func testEveryRunnerRequestFieldHasAProductionRequest() throws {
let entries = try productionRunnerRequests()
let requests = entries.map(\.request)
let steps = requests.flatMap { $0["steps"] as? [[String: Any]] ?? [] }
let plans = requests.compactMap { $0["gesturePlan"] as? [String: Any] }
let pointers = plans.flatMap { $0["pointers"] as? [[String: Any]] ?? [] }
let samples = pointers.flatMap { $0["samples"] as? [[String: Any]] ?? [] }
let commands = try entries.map { try decodeProductionRunnerRequest($0.request, $0.name) }
let command = try XCTUnwrap(commands.first, "no production request")
let plan = try XCTUnwrap(
commands.compactMap(\.gesturePlan).first,
"no production request carries a gesturePlan"
)
let step = try JSONDecoder().decode(SequenceStep.self, from: Data(#"{"kind":"tap"}"#.utf8))
let sample = try XCTUnwrap(plan.pointers.first?.samples.first)
assertEveryStoredField(of: command, appearsIn: requests, "Command")
assertEveryStoredField(of: step, appearsIn: steps, "SequenceStep")
assertEveryStoredField(of: plan, appearsIn: plans, "RunnerGesturePlan")
assertEveryStoredField(
of: plan.viewport,
appearsIn: plans.compactMap { $0["viewport"] as? [String: Any] },
"RunnerGestureViewport"
)
assertEveryStoredField(of: plan.pointers[0], appearsIn: pointers, "RunnerGesturePointer")
assertEveryStoredField(of: sample, appearsIn: samples, "RunnerGestureSample")
assertEveryStoredField(
of: sample.point,
appearsIn: samples.compactMap { $0["point"] as? [String: Any] },
"RunnerGesturePoint"
)
}

private func productionRunnerRequests() throws -> [(name: String, request: [String: Any])] {
let fixtureURL = URL(fileURLWithPath: #filePath)
.deletingLastPathComponent()
.deletingLastPathComponent()
.deletingLastPathComponent()
.deletingLastPathComponent()
.deletingLastPathComponent()
.deletingLastPathComponent()
.appendingPathComponent("contracts/fixtures/runner-requests.json")
let entries = try XCTUnwrap(
JSONSerialization.jsonObject(with: Data(contentsOf: fixtureURL)) as? [[String: Any]]
)
return try entries.map { entry in
(
name: try XCTUnwrap(entry["name"] as? String),
request: try XCTUnwrap(entry["request"] as? [String: Any], "\(entry["name"] ?? "?")")
)
}
}

private func decodeProductionRunnerRequest(_ request: [String: Any], _ name: String) throws
-> Command
{
do {
return try JSONDecoder().decode(
Command.self,
from: JSONSerialization.data(withJSONObject: request)
)
} catch {
XCTFail("\(name) does not decode as Command: \(error)")
throw error
}
}

private func runnerRequestKeyPaths(_ value: Any, _ prefix: String = "") -> Set<String> {
if let object = value as? [String: Any] {
return object.reduce(into: Set<String>()) { paths, field in
paths.insert(prefix + field.key)
paths.formUnion(runnerRequestKeyPaths(field.value, "\(prefix)\(field.key)."))
}
}
if let array = value as? [Any] {
return array.reduce(into: Set<String>()) { paths, element in
paths.formUnion(runnerRequestKeyPaths(element, "\(prefix)[]."))
}
}
return []
}

private func assertEveryStoredField(
of value: Any,
appearsIn objects: [[String: Any]],
_ level: String
) {
let fields = Set(Mirror(reflecting: value).children.compactMap(\.label))
let produced = Set(objects.flatMap(\.keys))
XCTAssertEqual(
fields.subtracting(produced).sorted(),
[],
"\(level) fields with no production request"
)
}
}
#endif
1 change: 1 addition & 0 deletions apple/runner/RUNNER_PROTOCOL.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The daemon probes `http://127.0.0.1:<port>/command` for simulator and desktop fl
## Request Shape

Every request includes a `command` field. Additional fields depend on the command family.
The request vocabulary is `contracts/fixtures/runner-requests.json`: the requests production builds.

Examples:

Expand Down
Loading
Loading