Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132import Foundationimport Testing
@testable import ParcelIPC
@Suite("JSON protocol")struct ProtocolTests { @Test(arguments: [ JSONValue.null, .bool(true), .bool(false), .integer(.min), .integer(.max), .number(1.25), .string("café\n\"☀️"), .array([.null, .integer(3)]), .object(["nested": .array([.bool(true)])]), ]) func jsonRoundTrip(_ value: JSONValue) throws { #expect(try IPCCodec.decode(JSONValue.self, from: IPCCodec.encode(value)) == value) }
@Test(arguments: [IPCRequestID.string("request-☀️"), .integer(.min), .integer(.max)]) func requestRoundTrip(_ id: IPCRequestID) throws { let request = IPCRequest(id: id, command: "run", args: ["focus", "left", "two words"]) #expect(try IPCCodec.decode(IPCRequest.self, from: IPCCodec.encode(request)) == request) }
@Test func defaultVersion() { #expect(IPCProtocol.version == 1) }
@Test func defaultIDsAreUnique() { #expect(IPCRequest(command: "ping").id != IPCRequest(command: "ping").id) }
@Test(arguments: [JSONValue.null, .string("pong"), .array([]), .object(["id": .integer(1)])]) func successfulResponse(_ result: JSONValue) throws { let response = IPCResponse(id: .integer(1), result: result) #expect(try IPCCodec.decode(IPCResponse.self, from: IPCCodec.encode(response)) == response) }
@Test func errorResponse() throws { let response = IPCResponse(id: nil, error: IPCError(code: "bad_command", message: "Try another command.")) #expect(try IPCCodec.decode(IPCResponse.self, from: IPCCodec.encode(response)) == response) #expect(response.error?.localizedDescription == "Try another command.") }
@Test(arguments: IPCTopic.allCases) func eventRoundTrip(_ topic: IPCTopic) throws { let event = IPCEvent(topic: topic, data: .object(["window": .integer(42)])) #expect(try IPCCodec.decode(IPCEvent.self, from: IPCCodec.encode(event)) == event) }
@Test(arguments: [ "{\"v\":1,\"id\":1,\"ok\":true}", "{\"v\":1,\"id\":1,\"ok\":true,\"result\":null,\"error\":null}", "{\"v\":1,\"id\":1,\"ok\":false}", "{\"v\":1,\"id\":1,\"ok\":false,\"result\":null,\"error\":{\"code\":\"bad\",\"message\":\"bad\"}}", "{\"v\":1,\"id\":1,\"ok\":false,\"error\":null}", ]) func invalidResponseShape(_ json: String) { #expect(throws: (any Error).self) { try IPCCodec.decode(IPCResponse.self, from: Data(json.utf8)) } }
@Test(arguments: [ "{}", "[]", "null", "{\"v\":1,\"id\":true,\"command\":\"ping\",\"args\":[]}", "{\"v\":1,\"id\":1,\"command\":\"run\",\"args\":[42]}", ]) func invalidRequests(_ json: String) { #expect(throws: (any Error).self) { try IPCCodec.decode(IPCRequest.self, from: Data(json.utf8)) } }
@Test func encoderProducesOneSortedLine() throws { let data = try IPCCodec.encode(JSONValue.object(["z": .string("one\ntwo"), "a": .integer(2)])) #expect(String(decoding: data, as: UTF8.self) == "{\"a\":2,\"z\":\"one\\ntwo\"}\n") #expect(data.filter { $0 == 10 }.count == 1) }
@Test func oversizedEncodeFails() { #expect(throws: IPCTransportError.messageTooLarge) { try IPCCodec.encode(JSONValue.string(String(repeating: "x", count: IPCProtocol.maximumMessageBytes))) } }
@Test func oversizedDecodeFails() { #expect(throws: IPCTransportError.messageTooLarge) { try IPCCodec.decode(JSONValue.self, from: Data(repeating: 32, count: IPCProtocol.maximumMessageBytes + 2)) } }
@Test func nonFiniteNumberFails() { #expect(throws: (any Error).self) { try IPCCodec.encode(JSONValue.number(.infinity)) } }}
@Suite("Socket paths")struct PathTests { @Test func temporaryDirectoryAndUID() throws { #expect( try IPCSocketPath.resolve(environment: ["TMPDIR": "/tmp/private/"], uid: 501) == "/tmp/private/parcel-501.sock") }
@Test func platformTemporaryDirectory() throws { #expect( try IPCSocketPath.resolve(environment: [:], uid: 42, temporaryDirectory: "/tmp") == "/tmp/parcel-42.sock") }
@Test func overrideWins() throws { #expect( try IPCSocketPath.resolve(environment: ["PARCEL_SOCKET": "/tmp/custom.sock", "TMPDIR": "invalid"], uid: 1) == "/tmp/custom.sock") }
@Test(arguments: [ "", "relative.sock", "/tmp/\u{0}bad", "/" + String(repeating: "x", count: 103), "/" + String(repeating: "é", count: 52), ]) func invalidPaths(_ path: String) { #expect(throws: IPCTransportError.invalidPath(path)) { try IPCSocketPath.resolve(environment: ["PARCEL_SOCKET": path]) } #expect(throws: IPCTransportError.invalidPath(path)) { try IPCClient(socketPath: path) } }
@Test func longestPathFits() throws { let path = "/" + String(repeating: "x", count: 102) #expect(try IPCSocketPath.resolve(environment: ["PARCEL_SOCKET": path]) == path) }
@Test(arguments: [ IPCTransportError.closed, .truncatedMessage, .messageTooLarge, .operationInProgress, .notRunning("/tmp/test.sock"), .alreadyRunning("/tmp/test.sock"), .invalidPath(""), .unsafeExistingPath("/tmp/test"), .system(operation: "connect", code: 2), .protocolViolation("wrong id"), ]) func transportErrorsHaveMessages(_ error: IPCTransportError) { #expect(!error.localizedDescription.isEmpty) }}