Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227import Foundationimport Testing
@testable import ParcelConfig
@Suite("Configuration rules")struct RulesTests { @Test func defaultsAndLegacyFloatRulesRemainCompatible() throws { let result = try validate("[[float-rules]]\nbundle-id = \"org.example.notes\"") let config = try #require(result.configuration) #expect(config.rules.isEmpty) #expect(config.floatRules == [FloatRule(bundleID: "org.example.notes")]) #expect(result.diagnostics.isEmpty) }
@Test func arraysPreserveOrderAndAllFields() throws { let config = try configuration( """ [[rules]] bundle-id = "org.example.notes" app-name = "Notes" title-contains = "Draft" role = "AXWindow" subrole = "AXStandardWindow" action = "float" display = "main" position = 0 [[rules]] title-regex = '^Quick .*$' action = "tile" [[rules]] role = "AXDialog" action = "ignore" [[rules]] app-name = "Browser" position = 7 """ ) #expect( config.rules == [ RuleConfiguration( bundleID: "org.example.notes", appName: "Notes", titleContains: "Draft", role: "AXWindow", subrole: "AXStandardWindow", action: .float, display: "main", position: 0), RuleConfiguration(titleRegex: "^Quick .*$", action: .tile), RuleConfiguration(role: "AXDialog", action: .ignore), RuleConfiguration(appName: "Browser", position: 7), ]) }
@Test func inlineAndEmptyArraysAreAccepted() throws { #expect(try configuration("rules = []").rules.isEmpty) #expect( try configuration("rules = [{ role = \"AXWindow\", display = \"left\" }]").rules == [ RuleConfiguration(role: "AXWindow", display: "left") ]) }
@Test(arguments: ["[rules]\naction = \"float\"", "rules = 2", "rules = [1]"]) func nonArraysAndNonTableEntriesAreRejected(_ source: String) throws { #expect(try validate(source).configuration == nil) }
@Test func matcherAndOutcomeAreRequired() throws { for (source, message) in [ ("[[rules]]\naction = \"tile\"", "at least one matcher"), ("[[rules]]\nrole = \"AXWindow\"", "at least one action or placement"), ("[[rules]]", "at least one matcher"), ] { let result = try validate(source) #expect(result.configuration == nil) #expect(result.diagnostics.contains { $0.message.contains(message) }) } let typo = try validate("[[rules]]\nbundle-i = \"x\"\naction = \"float\"") #expect(typo.configuration == nil) #expect(typo.diagnostics.contains { $0.severity == .warning && $0.message.contains("rules[0].bundle-id") }) #expect(typo.diagnostics.contains { $0.message.contains("at least one matcher") }) }
@Test func unknownKeysWarnAtTheKeyAndSuggestCorrectSpelling() throws { let source = "[[rules]]\nrole = \"AXWindow\"\naction = \"tile\"\n positon = 1" let document = try TOMLDocument(parsing: source) let result = ConfigValidator.validate(document) #expect(result.configuration != nil) let warning = try #require(result.diagnostics.first) #expect(warning.severity == .warning) #expect(warning.location.line == 4) #expect(warning.location.column == 3) #expect(warning.message.contains("Did you mean “rules[0].position”")) }
@Test(arguments: ["bundle-id", "app-name", "title-contains", "title-regex", "role", "subrole"]) func matchersMustBeNonblankStrings(_ key: String) throws { for value in ["\"\"", "\" \"", "42"] { #expect(try validate("[[rules]]\n\(key) = \(value)\naction = \"float\"").configuration == nil) } }
@Test func regexUsesNativeValidationAtTheValueLocation() throws { let source = "[[rules]]\ntitle-regex = \"[\"\naction = \"tile\"" let result = try validate(source) #expect(result.configuration == nil) let error = try #require(result.diagnostics.first) #expect(error.message.contains("regular expression")) #expect(error.location.line == 2) #expect(error.location.column == 15) #expect(error.description.contains("line 2, column 15")) #expect(try configuration("[[rules]]\ntitle-regex = ' a '\naction = \"tile\"").rules[0].titleRegex == " a ") }
@Test(arguments: [false, true]) func titleMatchersAreMutuallyExclusiveRegardlessOfOrder(_ reversed: Bool) throws { let contains = "title-contains = \"Draft\"" let regex = "title-regex = \".*\"" let source = "[[rules]]\n" + (reversed ? "\(regex)\n\(contains)" : "\(contains)\n\(regex)") + "\naction = \"tile\"" let result = try validate(source) #expect(result.configuration == nil) let diagnostic = try #require(result.diagnostics.first) #expect(diagnostic.message.contains("cannot use both")) #expect(diagnostic.location.line == 3) #expect(diagnostic.relatedLocation?.line == 2) }
@Test(arguments: ["\"unknown\"", "\"FLOAT\"", "42", "false"]) func invalidActionsAreRejected(_ value: String) throws { let result = try validate("[[rules]]\nrole = \"AXWindow\"\naction = \(value)") #expect(result.configuration == nil) #expect(result.diagnostics.contains { $0.location.line == 3 && $0.message.contains("rules[0].action") }) }
@Test(arguments: ["-1", "1.5", "\"1\"", "false"]) func invalidPositionsAreRejected(_ value: String) throws { let result = try validate("[[rules]]\nrole = \"AXWindow\"\nposition = \(value)") #expect(result.configuration == nil) #expect(result.diagnostics.contains { $0.location.line == 3 && $0.message.contains("rules[0].position") }) }
@Test func displayMustBeAString() throws { #expect(try validate("[[rules]]\nrole = \"AXWindow\"\ndisplay = 1").configuration == nil) }
@Test func codableUsesConfigKeysAndRoundTrips() throws { let value = RuleConfiguration( bundleID: "org.example.notes", appName: "Notes", titleContains: "Draft", role: "AXWindow", subrole: "AXStandardWindow", action: .float, display: "main", position: 2) let data = try JSONEncoder().encode(value) let json = String(decoding: data, as: UTF8.self) for key in ["bundle-id", "app-name", "title-contains", "role", "subrole", "action", "display", "position"] { #expect(json.contains("\"\(key)\":")) } #expect(try JSONDecoder().decode(RuleConfiguration.self, from: data) == value) let placement = RuleConfiguration(titleRegex: "abc", position: 0) #expect(try JSONDecoder().decode(RuleConfiguration.self, from: JSONEncoder().encode(placement)) == placement) }
@Test func schemaDescribesTheRulesContract() throws { let root = try JSONDecoder().decode(Schema.self, from: Data(ConfigJSONSchema.string.utf8)) let rules = try #require(root.properties?["rules"]) #expect(rules.type == "array") let item = try #require(rules.items) let properties = try #require(item.properties) #expect( Set(properties.keys) == [ "bundle-id", "app-name", "title-contains", "title-regex", "role", "subrole", "action", "display", "position", "layout", "space", "move-to-space", ]) for key in ["bundle-id", "app-name", "title-contains", "title-regex", "role", "subrole"] { #expect(properties[key]?.type == "string") #expect(properties[key]?.pattern == "\\S") } #expect(properties["title-regex"]?.format == "parcel-title-regex") #expect(properties["action"]?.choices == ["float", "tile", "ignore"]) #expect(properties["display"]?.type == "string") #expect(properties["move-to-space"]?.type == "boolean") #expect(properties["position"]?.type == "integer") #expect(properties["position"]?.minimum == 0) let constraints = try #require(item.allOf) #expect(constraints.count == 3) #expect( Set(constraints[0].anyOf?.compactMap { $0.required?.first } ?? []) == [ "bundle-id", "app-name", "title-contains", "title-regex", "role", "subrole", ]) #expect( Set(constraints[1].anyOf?.compactMap { $0.required?.first } ?? []) == [ "action", "display", "position", "layout", "space", ]) #expect(constraints[2].not?.required == ["title-contains", "title-regex"]) }
private func validate(_ source: String) throws -> ConfigValidationResult { ConfigValidator.validate(try TOMLDocument(parsing: source)) }
private func configuration(_ source: String) throws -> ParcelConfiguration { let result = try validate(source) #expect(result.diagnostics.isEmpty) return try #require(result.configuration) }
private struct Schema: Decodable { var type: String? var properties: [String: Schema]? var items: Box? var allOf: [Schema]? var anyOf: [Schema]? var not: Box? var required: [String]? var pattern: String? var format: String? var choices: [String]? var minimum: Double?
enum CodingKeys: String, CodingKey { case type, properties, items, allOf, anyOf, not, required, pattern, format, minimum case choices = "enum" } }
private final class Box: Decodable { let schema: Schema init(from decoder: any Decoder) throws { schema = try Schema(from: decoder) } var properties: [String: Schema]? { schema.properties } var allOf: [Schema]? { schema.allOf } var required: [String]? { schema.required } }}