Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279import Foundationimport RealityDriverSupportimport Testing
struct RealityDriverTests { @Test(arguments: [ DriverCommand.preflight, .register(pid: 123, windowID: 456), .standardWindows(pid: 123, candidates: [456, 789]), .key(virtualKey: 12, flags: 0x0010_0000), .heldKeys(first: 48, second: 124, flags: 0x0008_0000, holdMilliseconds: 2000), .minimize(pid: 123, windowID: 456), .drag(pid: 123, windowID: 456, fromX: -100, fromY: 20, toX: 300, toY: 40, flags: 0), .move(pid: 123, windowID: 456, x: -100, y: 200, width: 640, height: 400), .focus(pid: 123, windowID: 456), .close(pid: 123, windowID: 456), .increments(pid: 123, windowID: 456), .pressTourTidy(pid: 123, windowID: 456, launchDate: 123456.5), .pressTourTidy(pid: 123, windowID: 456, launchDate: 123456.5, control: .continue), .clickTourTidy(pid: 123, windowID: 456, launchDate: 123456.5), .captureTour(pid: 123, windowID: 456, launchDate: 123456.5, directory: "/tmp/private-capture", optedIn: true), .capture(directory: "/tmp/private-capture", optedIn: true), .missionControlChords, .missionControlSnapshot, .quit, ]) func commandsRoundTrip(command: DriverCommand) throws { let request = DriverRequest(id: UInt64.max, command: command) let data = try JSONEncoder().encode(request) #expect(try DriverWire.decodeRequest(data) == request) }
@Test func wireUsesSynthesizedAssociatedValueLabels() throws { let data = Data(#"{"id":7,"command":{"register":{"pid":123,"windowID":456}}}"#.utf8) #expect(try DriverWire.decodeRequest(data) == DriverRequest(id: 7, command: .register(pid: 123, windowID: 456))) }
@Test func responsePayloadsRoundTripWithOneNewline() throws { let response = DriverResponse( id: 9, preflight: DriverPreflight(axTrusted: true, postEventAccess: false, screenRecording: false), increments: DriverIncrements(width: 8, height: 16, source: "AXSizeIncrement"), chords: [DriverChord(symbolicID: 32, virtualKey: 126, flags: 0x0004_0000, enabled: true)], captures: [ DriverCapture( path: "/tmp/capture/screen.png", displayID: 1, frame: DriverFrame(x: -100, y: 0, width: 1000, height: 600), pixelWidth: 2000, pixelHeight: 1200) ], frame: DriverFrame(x: 100, y: 100, width: 640, height: 400), nodes: [DriverAXNode(path: [0, 1], role: "AXButton", identifier: "test-control", selected: false)], dragFrames: [DriverFrame(x: 100, y: 100, width: 640, height: 400)], windowIDs: [456, 789], tourPress: DriverTourPressReceipt( pid: 123, windowID: 456, launchDate: 123456.5, identifier: "tour.tidy", role: "AXButton", action: "AXPress", enabled: true, visitedNodes: 20)) let data = try DriverWire.encodeResponse(response) #expect(data.last == 10) #expect(data.filter { $0 == 10 }.count == 1) #expect(try JSONDecoder().decode(DriverResponse.self, from: data) == response) let failure = DriverResponse(id: 9, error: "Failure\nwith a newline") let encoded = try DriverWire.encodeResponse(failure) #expect(encoded.filter { $0 == 10 }.count == 1) #expect(try JSONDecoder().decode(DriverResponse.self, from: encoded) == failure) let error = DriverError("Explicit failure") #expect(try JSONDecoder().decode(DriverError.self, from: JSONEncoder().encode(error)) == error) }
@Test(arguments: [ DriverCommand.register(pid: 0, windowID: 4), .focus(pid: -1, windowID: 4), .close(pid: 1, windowID: 0), .increments(pid: 0, windowID: 0), .pressTourTidy(pid: 0, windowID: 1, launchDate: 123), .pressTourTidy(pid: 1, windowID: 0, launchDate: 123), .pressTourTidy(pid: 1, windowID: 1, launchDate: .nan), .pressTourTidy(pid: 1, windowID: 1, launchDate: .infinity), .pressTourTidy(pid: 1, windowID: 1, launchDate: 0), .clickTourTidy(pid: 0, windowID: 1, launchDate: 123), .clickTourTidy(pid: 1, windowID: 0, launchDate: 123), .clickTourTidy(pid: 1, windowID: 1, launchDate: .nan), .move(pid: 1, windowID: 1, x: .nan, y: 0, width: 10, height: 10), .move(pid: 1, windowID: 1, x: 0, y: .infinity, width: 10, height: 10), .move(pid: 1, windowID: 1, x: 0, y: 0, width: 0, height: 10), .move(pid: 1, windowID: 1, x: 0, y: 0, width: -10, height: 10), .move(pid: 1, windowID: 1, x: 999_999, y: 0, width: 10, height: 10), .drag(pid: 123, windowID: 456, fromX: 0, fromY: 0, toX: .infinity, toY: 0, flags: 0), .drag(pid: 123, windowID: 456, fromX: -1_000_001, fromY: 0, toX: 0, toY: 0, flags: 0), .key(virtualKey: 128, flags: 0), .key(virtualKey: 0, flags: UInt64.max), .minimize(pid: 0, windowID: 1), .minimize(pid: 1, windowID: 0), .heldKeys(first: 128, second: 124, flags: 0, holdMilliseconds: 500), .heldKeys(first: 48, second: 128, flags: 0, holdMilliseconds: 500), .heldKeys(first: 48, second: 124, flags: 0, holdMilliseconds: 499), .heldKeys(first: 48, second: 124, flags: 0, holdMilliseconds: 3001), .heldKeys(first: 48, second: 124, flags: UInt64.max, holdMilliseconds: 500), .captureTour(pid: 0, windowID: 456, launchDate: 123, directory: "/tmp/capture", optedIn: true), .captureTour(pid: 123, windowID: 0, launchDate: 123, directory: "/tmp/capture", optedIn: true), .captureTour(pid: 123, windowID: 456, launchDate: .nan, directory: "/tmp/capture", optedIn: true), .captureTour(pid: 123, windowID: 456, launchDate: 0, directory: "/tmp/capture", optedIn: true), .captureTour(pid: 123, windowID: 456, launchDate: 123, directory: "relative", optedIn: true), .captureTour(pid: 123, windowID: 456, launchDate: 123, directory: "/tmp/capture", optedIn: false), .capture(directory: "relative", optedIn: true), .capture(directory: "/tmp/\0capture", optedIn: true), .capture(directory: "/tmp/private-capture"), ]) func unsafeCommandsAreRejected(command: DriverCommand) { #expect(throws: DriverError.self) { try command.validate() } }
@Test(arguments: [ "pid", "window", "launch", "identifier", "role", "action", "disabled", "empty-tree", "too-many-nodes", ]) func tidyAXPressRequiresExactExternalButtonReceipt(_ failure: String) throws { let receipt = DriverTourPressReceipt( pid: failure == "pid" ? 124 : 123, windowID: failure == "window" ? 457 : 456, launchDate: failure == "launch" ? 123457.5 : 123456.5, identifier: failure == "identifier" ? "tour.continue" : "tour.tidy", role: failure == "role" ? "AXGroup" : "AXButton", action: failure == "action" ? "AXRaise" : "AXPress", enabled: failure != "disabled", visitedNodes: failure == "empty-tree" ? 0 : (failure == "too-many-nodes" ? 513 : 20)) #expect(throws: DriverError.self) { try receipt.requireMatch(pid: 123, windowID: 456, launchDate: 123456.5) } }
@Test func tidyAXPressReceiptMatchesTheExactLaunch() throws { let receipt = DriverTourPressReceipt( pid: 123, windowID: 456, launchDate: 123456.5, identifier: "tour.tidy", role: "AXButton", action: "AXPress", enabled: true, visitedNodes: 20) try receipt.requireMatch(pid: 123, windowID: 456, launchDate: 123456.5) }
@Test func mouseTidyReceiptMustIdentifyMouseDeliveryAndItsButtonFrame() throws { let receipt = DriverTourPressReceipt( pid: 123, windowID: 456, launchDate: 123456.5, identifier: "tour.tidy", role: "AXButton", action: "leftMouseDown/leftMouseUp", enabled: true, visitedNodes: 20, method: .mouse, clickFrame: DriverFrame(x: 100, y: 200, width: 160, height: 28)) try receipt.requireMatch(pid: 123, windowID: 456, launchDate: 123456.5, method: .mouse) #expect(throws: DriverError.self) { try receipt.requireMatch(pid: 123, windowID: 456, launchDate: 123456.5) } #expect(try JSONDecoder().decode(DriverTourPressReceipt.self, from: JSONEncoder().encode(receipt)) == receipt) for frame in [ DriverFrame?.none, .some(.init(x: 100, y: 200, width: 0, height: 28)), .some(.init(x: .nan, y: 200, width: 160, height: 28)), ] { let invalid = DriverTourPressReceipt( pid: 123, windowID: 456, launchDate: 123456.5, identifier: "tour.tidy", role: "AXButton", action: "leftMouseDown/leftMouseUp", enabled: true, visitedNodes: 20, method: .mouse, clickFrame: frame) #expect(throws: DriverError.self) { try invalid.requireMatch(pid: 123, windowID: 456, launchDate: 123456.5, method: .mouse) } } }
@Test(arguments: DriverTourControl.allCases) func renderedTourControlsRequireTheirOwnReceipt(_ control: DriverTourControl) throws { let receipt = DriverTourPressReceipt( pid: 123, windowID: 456, launchDate: 123456.5, identifier: control.rawValue, role: control.role, action: "AXPress", enabled: true, visitedNodes: 20) try receipt.requireMatch(pid: 123, windowID: 456, launchDate: 123456.5, control: control) let request = DriverRequest( id: 7, command: .pressTourTidy( pid: 123, windowID: 456, launchDate: 123456.5, control: control)) try request.validate() #expect(try DriverWire.decodeRequest(JSONEncoder().encode(request)) == request) if control != .tidy { #expect(throws: DriverError.self) { try receipt.requireMatch(pid: 123, windowID: 456, launchDate: 123456.5) } } }
@Test func unknownTourControlsCannotDecode() { let data = Data( #"{"id":7,"command":{"pressTourTidy":{"pid":123,"windowID":456,"launchDate":123456.5,"control":"unowned.button"}}}"# .utf8) #expect(throws: (any Error).self) { try DriverWire.decodeRequest(data) } }
@Test func zeroRequestIDIsReserved() throws { #expect(throws: DriverError.self) { try DriverRequest(id: 0, command: .preflight).validate() } let data = Data(#"{"id":0,"command":{"preflight":{}}}"#.utf8) #expect(throws: DriverError.self) { try DriverWire.decodeRequest(data) } }
@Test(arguments: [ #"{"id":51,"command":{"unknown":{}}}"#, #"{"id":51,"command":{"move":{"pid":1}}}"#, #"{"id":51}"#, #"{"id":51,"command":{"preflight":{},"quit":{}}}"#, ]) func malformedCommandPreservesCorrelation(json: String) { let data = Data(json.utf8) do { _ = try DriverWire.decodeRequest(data) Issue.record("Malformed request was accepted") } catch { let response = DriverWire.failure(for: data, error: error) #expect(response.id == 51) #expect(response.error?.isEmpty == false) } }
@Test(arguments: ["", "not JSON", #"{"command":{"quit":{}}}"#, #"{"id":-1}"#, #"{"id":"4"}"#]) func malformedEnvelopeUsesReservedID(json: String) { #expect(DriverWire.failure(for: Data(json.utf8), error: DriverError("Malformed")).id == 0) }
@Test func framingHandlesSplitAndCoalescedRequests() { var framer = DriverLineFramer() #expect(framer.append(Data("first".utf8)).isEmpty) #expect( framer.append(Data("\nsecond\nthird".utf8)) == [ .request(Data("first".utf8)), .request(Data("second".utf8)), ]) #expect(framer.finish() == .rejected(prefix: Data("third".utf8), reason: "Request missing final newline")) #expect(framer.finish() == nil) }
@Test func oversizedLineCannotExecuteItsSuffix() { var framer = DriverLineFramer() let prefix = Data(repeating: 32, count: DriverLineFramer.maximumBytes) #expect(framer.append(prefix).isEmpty) #expect(framer.append(Data("malicious suffix".utf8)).isEmpty) #expect( framer.append(Data("\nvalid\n".utf8)) == [ .rejected(prefix: prefix, reason: "Request exceeds 64 KiB"), .request(Data("valid".utf8)), ]) #expect(framer.finish() == nil) }
@Test func maximumSizedLineAndBlankLineAreFramed() { var framer = DriverLineFramer() var data = Data(repeating: 32, count: DriverLineFramer.maximumBytes) data.append(10) data.append(10) #expect( framer.append(data) == [ .request(Data(repeating: 32, count: DriverLineFramer.maximumBytes)), .request(Data()), ]) }
@Test func symbolicChordsPreserveEnabledStateAndNeverInventDefaults() throws { let document = ChordDocument(entries: [ "32": ChordEntry( enabled: true, value: ChordValue(type: "standard", parameters: [65535, 126, 0x0004_0000])), "79": ChordEntry( enabled: false, value: ChordValue(type: "standard", parameters: [65535, 123, 0x0004_0000])), "81": ChordEntry(enabled: true, value: ChordValue(type: "standard", parameters: [65535, 65535, 0])), "999": ChordEntry(enabled: true, value: ChordValue(type: "standard", parameters: [0, 12, 0])), ]) #expect( try DriverSymbolicChords.parse(PropertyListEncoder().encode(document)) == [ DriverChord(symbolicID: 32, virtualKey: 126, flags: 0x0004_0000, enabled: true), DriverChord(symbolicID: 79, virtualKey: 123, flags: 0x0004_0000, enabled: false), ]) #expect(try DriverSymbolicChords.parse(PropertyListEncoder().encode(ChordDocument(entries: [:]))).isEmpty) }
@Test func malformedSymbolicChordSurfacesAnError() throws { let document = ChordDocument(entries: [ "32": ChordEntry(enabled: true, value: ChordValue(type: "standard", parameters: [0, 126])) ]) let data = try PropertyListEncoder().encode(document) #expect(throws: DriverError.self) { try DriverSymbolicChords.parse(data) } }}
private struct ChordDocument: Encodable { let entries: [String: ChordEntry] enum CodingKeys: String, CodingKey { case entries = "AppleSymbolicHotKeys" }}private struct ChordEntry: Encodable { let enabled: Bool let value: ChordValue?}private struct ChordValue: Encodable { let type: String let parameters: [Int64]}