import Foundation import ParcelConfig import Testing struct ClassificationRulesAcceptanceTests { @Test func allDefaultExclusionsStartEnabled() throws { #expect(ParcelConfiguration.defaults.classification.disabledDefaultFloatApps.isEmpty) #expect(try configuration("version = 1").classification == ClassificationConfiguration()) #expect( try configuration("[classification]\ndisabled-default-float-apps = []").classification == ClassificationConfiguration()) } @Test func disabledBundlesParseAsAnExactSet() throws { let value = try configuration( "[classification]\ndisabled-default-float-apps = [\"com.apple.calculator\", \"com.apple.clock\"]") #expect(value.classification.disabledDefaultFloatApps == ["com.apple.calculator", "com.apple.clock"]) } @Test func anExistingSystemSettingsDefaultOptOutStillLoads() throws { let value = try configuration( "[classification]\ndisabled-default-float-apps = [\"com.apple.systempreferences\"]") #expect(value.classification.disabledDefaultFloatApps == ["com.apple.systempreferences"]) } @Test(arguments: [ "false", "\"com.apple.calculator\"", "[3]", "[\"\"]", "[\" \"]", "[\"com.apple.clock\", \"com.apple.clock\"]", ]) func invalidDisabledBundleListsAreRejected(value: String) throws { let result = try validate("[classification]\ndisabled-default-float-apps = \(value)") #expect(result.configuration == nil) #expect(result.diagnostics.contains { $0.severity == .error && $0.location.line == 2 }) } @Test(arguments: ConfigLayout.allCases) func everyLayoutIsAValidStandaloneRuleOutcome(layout: ConfigLayout) throws { let value = try configuration("[[rules]]\nbundle-id = \"test.fixture\"\nlayout = \"\(layout.rawValue)\"") #expect(value.rules == [RuleConfiguration(bundleID: "test.fixture", layout: layout)]) } @Test(arguments: [1, 16]) func nativeSpaceEndpointsAreValidStandaloneRuleOutcomes(space: Int) throws { let value = try configuration("[[rules]]\nbundle-id = \"test.fixture\"\nspace = \(space)") #expect(value.rules == [RuleConfiguration(bundleID: "test.fixture", space: space)]) } @Test(arguments: ["\"diagonal\"", "\"ROWS\"", "\"\"", "3", "false"]) func invalidRuleLayoutsAreRejected(value: String) throws { let result = try validate("[[rules]]\nbundle-id = \"test.fixture\"\nlayout = \(value)") #expect(result.configuration == nil) #expect( result.diagnostics.contains { $0.severity == .error && $0.location.line == 3 && $0.message.contains("rules[0].layout") }) } @Test(arguments: ["0", "-1", "17", "1.5", "\"1\"", "false"]) func invalidNativeSpaceRuleOutcomesAreRejected(value: String) throws { let result = try validate("[[rules]]\nbundle-id = \"test.fixture\"\nspace = \(value)") #expect(result.configuration == nil) #expect( result.diagnostics.contains { $0.severity == .error && $0.location.line == 3 && $0.message.contains("rules[0].space") }) } @Test(arguments: [false, true]) func spaceMoveConsentIsExplicitAndRoundTrips(consent: Bool) throws { let value = try configuration("[[rules]]\nbundle-id = \"test.fixture\"\nspace = 2\nmove-to-space = \(consent)") #expect(value.rules == [RuleConfiguration(bundleID: "test.fixture", space: 2, moveToSpace: consent)]) #expect( try JSONDecoder().decode([RuleConfiguration].self, from: JSONEncoder().encode(value.rules)) == value.rules) } @Test func olderRulePayloadsDoNotImplicitlyConsentToMoves() throws { let rule = try JSONDecoder().decode( RuleConfiguration.self, from: Data(#"{"bundle-id":"test.fixture","space":2}"#.utf8)) #expect(rule.space == 2) #expect(rule.moveToSpace == false) } @Test(arguments: ["1", "\"true\"", "[]"]) func invalidMoveConsentIsRejectedAtItsLocation(value: String) throws { let result = try validate("[[rules]]\nbundle-id = \"test.fixture\"\nspace = 2\nmove-to-space = \(value)") #expect(result.configuration == nil) #expect( result.diagnostics.contains { $0.severity == .error && $0.location.line == 4 && $0.message.contains("move-to-space") }) } @Test func consentAloneDoesNotCreateAPlacementRule() throws { let result = try validate("[[rules]]\nbundle-id = \"test.fixture\"\nmove-to-space = true") #expect(result.configuration == nil) } @Test func assignmentOutcomesStillRequireAMatcher() throws { for source in ["[[rules]]\nlayout = \"rows\"", "[[rules]]\nspace = 1"] { let result = try validate(source) #expect(result.configuration == nil) #expect(result.diagnostics.contains { $0.severity == .error && $0.message.contains("matcher") }) } } @Test func combinedRuleAssignmentsPreserveOrderingAndCodableFields() throws { let source = """ [[rules]] bundle-id = "test.fixture" title-contains = "Draft" action = "tile" layout = "rows" space = 2 position = 0 [[rules]] bundle-id = "test.fixture" layout = "columns" space = 16 """ let value = try configuration(source) let expected = [ RuleConfiguration( bundleID: "test.fixture", titleContains: "Draft", action: .tile, position: 0, layout: .rows, space: 2), RuleConfiguration(bundleID: "test.fixture", layout: .columns, space: 16), ] #expect(value.rules == expected) #expect(try JSONDecoder().decode([RuleConfiguration].self, from: JSONEncoder().encode(value.rules)) == expected) } @Test func schemaPublishesDisabledDefaultsAndNativeAssignments() throws { let root = try JSONDecoder().decode(Schema.self, from: Data(ConfigJSONSchema.string.utf8)) let classification = try #require(root.properties?["classification"]) let disabled = try #require(classification.properties?["disabled-default-float-apps"]) #expect(disabled.type == "array") #expect(disabled.uniqueItems == true) let rules = try #require(root.properties?["rules"]?.items?.properties) #expect(rules["layout"]?.choices == ConfigLayout.allCases.map(\.rawValue)) #expect(rules["space"]?.minimum == 1) #expect(rules["space"]?.maximum == 16) } private func validate(_ source: String) throws -> ConfigValidationResult { ConfigValidator.validate(try TOMLParser.parse(source)) } private func configuration(_ source: String) throws -> ParcelConfiguration { let result = try validate(source) #expect(result.diagnostics.isEmpty) return try #require(result.configuration) } private final class Schema: Decodable { let type: String? let properties: [String: Schema]? let items: Schema? let minimum: Double? let maximum: Double? let uniqueItems: Bool? let choices: [String]? enum CodingKeys: String, CodingKey { case type, properties, items, minimum, maximum, uniqueItems case choices = "enum" } } }