import Foundation import ParcelConfig import Testing @Suite struct HighlightConfigurationTests { @Test func defaultsAndStarterFileUseActiveTwoPointBorder() throws { let defaults = MotionConfiguration() #expect(defaults.highlight == .active) #expect(defaults.highlightWidth == 2) #expect(MotionConfiguration.highlightWidths == 2...6) #expect(try configuration("").motion == defaults) #expect(try configuration(ConfigStore.defaultFileContents).motion == defaults) #expect(ConfigStore.defaultFileContents.contains("highlight-width = 2.0")) } @Test(arguments: ConfigHighlight.allCases, [2.0, 2.5, 4.0, 5.75, 6.0]) func acceptsAllStylesAndWidths(style: ConfigHighlight, width: Double) throws { let value = try configuration("[motion]\nhighlight = '\(style.rawValue)'\nhighlight-width = \(width)\n") #expect(value.motion.highlight == style) #expect(value.motion.highlightWidth == width) #expect(value.motion.focusBorder == (style != .none)) } @Test(arguments: ["2", "6"]) func integralWidthsAreAccepted(width: String) throws { #expect(try configuration("[motion]\nhighlight-width = \(width)\n").motion.highlightWidth == Double(width)) } @Test(arguments: [ "highlight = 'unknown'", "highlight = 'Active'", "highlight = true", "highlight = 2", "highlight = []", "highlight-width = 1.99", "highlight-width = 6.01", "highlight-width = -2", "highlight-width = nan", "highlight-width = inf", "highlight-width = -inf", "highlight-width = '2'", "highlight-width = true", "highlight-width = []", ]) func rejectsInvalidValuesWithSourceLocations(assignment: String) throws { let result = ConfigValidator.validate(try TOMLDocument(parsing: "[motion]\n\(assignment)\n")) #expect(result.configuration == nil) let error = try #require(result.diagnostics.first { $0.severity == .error }) #expect(error.location.line == 2) #expect(error.location.column > 0) #expect(error.message.contains("motion.highlight")) } @Test(arguments: [true, false]) func legacyBorderSwitchStillWorks(enabled: Bool) throws { let value = try configuration("[motion]\nfocus-border = \(enabled)\n") #expect(value.motion.highlight == (enabled ? .active : .none)) #expect(MotionConfiguration(focusBorder: enabled).highlight == value.motion.highlight) } @Test(arguments: ConfigHighlight.allCases, [true, false]) func explicitHighlightWinsOverLegacySwitch(style: ConfigHighlight, enabled: Bool) throws { for assignments in [ "focus-border = \(enabled)\nhighlight = '\(style.rawValue)'", "highlight = '\(style.rawValue)'\nfocus-border = \(enabled)", ] { #expect(try configuration("[motion]\n\(assignments)\n").motion.highlight == style) } } @Test func noneIsAStyleNotAnOptionalDefault() { #expect(MotionConfiguration(highlight: .none).highlight == .none) } @Test func explicitHighlightDoesNotHideInvalidLegacyValues() throws { let result = ConfigValidator.validate( try TOMLDocument(parsing: "[motion]\nfocus-border = 'no'\nhighlight = 'all'\n")) #expect(result.configuration == nil) } @Test func legacyMutationKeepsEnabledStylesAndCanDisableOrReenable() { var configuration = MotionConfiguration(highlight: .glow, highlightWidth: 4) configuration.focusBorder = true #expect(configuration.highlight == .glow) configuration.focusBorder = false #expect(configuration.highlight == .none) configuration.focusBorder = true #expect(configuration.highlight == .active) #expect(configuration.highlightWidth == 4) } @Test func multiwordMotionKeysUseDashedSpellings() throws { let assignments = """ focus-border = false drop-hints = false experimental-dimming = true menu-bar-icon = 'logo' highlight-width = 5.5 """ #expect( try configuration("[motion]\n" + assignments).motion == MotionConfiguration( dropHints: false, experimentalDimming: true, menuBarIcon: .logo, highlight: .none, highlightWidth: 5.5)) } @Test(arguments: ["focus-border", "drop-hints", "experimental-dimming"], ["'false'", "1", "[]", "{}"]) func invalidBooleanValuesAreRejected(key: String, invalid: String) throws { try expectInvalidValue(key: key, invalid: invalid) } @Test(arguments: ["'unknown'", "'Logo'", "true", "1", "[]", "{}"]) func invalidMenuIconValuesAreRejected(invalid: String) throws { try expectInvalidValue(key: "menu-bar-icon", invalid: invalid) } @Test(arguments: ["1.99", "6.01", "nan", "inf", "-inf", "'2'", "true", "[]", "{}"]) func invalidWidthValuesAreRejected(invalid: String) throws { try expectInvalidValue(key: "highlight-width", invalid: invalid) } private func expectInvalidValue(key: String, invalid: String) throws { let document = try TOMLDocument(parsing: "[motion]\n\(key) = \(invalid)") let result = ConfigValidator.validate(document) #expect(result.configuration == nil) #expect(result.diagnostics.count == 1) let error = try #require(result.diagnostics.first) #expect(error.severity == .error) #expect(error.location == document.root["motion"]?[key]?.range.start) #expect(error.message.contains("motion.\(key)")) } private func configuration(_ source: String) throws -> ParcelConfiguration { let result = ConfigValidator.validate(try TOMLDocument(parsing: source)) #expect(result.diagnostics.isEmpty) return try #require(result.configuration) } }