import Foundation import ParcelConfig import Testing @Suite struct FocusModeConfigurationTests { @Test func defaultsMatchEmptyConfigurationAndStarterFile() throws { let expected = FocusModeConfiguration(noPeekLevel: 0.25, noPeekBlur: true, cleanSpaceColor: .graphite) #expect(ParcelConfiguration.defaults.focusModes == expected) #expect(try configuration("").focusModes == expected) #expect(try configuration(ConfigStore.defaultFileContents).focusModes == expected) #expect(ConfigStore.defaultFileContents.contains("[focus-modes]")) } @Test(arguments: [0.1, 0.35, 0.5, 0.9]) func acceptsEveryLevelInsideBounds(level: Double) throws { let value = try configuration("[focus-modes]\nno-peek-level = \(level)\n") #expect(value.focusModes.noPeekLevel == level) #expect(value.focusModes.noPeekBlur) #expect(value.focusModes.isValid) } @Test(arguments: ConfigCleanSpaceColor.allCases) func acceptsEveryBackdropColor(color: ConfigCleanSpaceColor) throws { let value = try configuration("[focus-modes]\nclean-space-color = '\(color.rawValue)'\nno-peek-blur = false\n") #expect(value.focusModes.cleanSpaceColor == color) #expect(!value.focusModes.noPeekBlur) #expect(value.focusModes.noPeekLevel == 0.25) } @Test(arguments: [ "no-peek-level = 0.09", "no-peek-level = 0.91", "no-peek-level = nan", "no-peek-level = inf", "no-peek-level = -inf", "no-peek-level = '0.5'", "no-peek-level = true", "no-peek-level = 1", "no-peek-blur = 'true'", "no-peek-blur = 1", "clean-space-color = 'dark'", "clean-space-color = true", ]) func invalidValuesHaveSourceLocationsAndRejectConfiguration(assignment: String) throws { let result = ConfigValidator.validate(try TOMLDocument(parsing: "[focus-modes]\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("focus-modes.")) } @Test(arguments: ["true", "[]", "'enabled'"]) func focusModesRequiresATable(value: String) throws { let result = ConfigValidator.validate(try TOMLDocument(parsing: "focus-modes = \(value)\n")) #expect(result.configuration == nil) #expect(result.diagnostics.contains { $0.severity == .error }) } @Test func unknownFocusModeKeysWarnWithoutChangingPreferences() throws { let result = ConfigValidator.validate(try TOMLDocument(parsing: "[focus-modes]\nno-peek-blr = false\n")) #expect(result.configuration?.focusModes == FocusModeConfiguration()) let warning = try #require(result.diagnostics.first) #expect(warning.severity == .warning) #expect(warning.message.contains("focus-modes.no-peek-blur")) } @Test(arguments: [Double.nan, .infinity, -.infinity, 0.09, 0.91]) func directlyConstructedInvalidPreferencesAreInspectable(level: Double) { #expect(!FocusModeConfiguration(noPeekLevel: level).isValid) } @Test func dimmingKeyRemainsAcceptedAlongsideFocusPreferences() throws { let value = try configuration("[focus-modes]\nno-peek-level = 0.7\n[motion]\nexperimental-dimming = true\n") #expect(value.motion.experimentalDimming) #expect(value.focusModes.noPeekLevel == 0.7) } @Test func defaultModeBindingsUseDistinctPhysicalKeys() throws { let modeKeys: [(ConfigCommand, ANSIKey)] = [ (.toggleFocus, .z), (.toggleNoPeek, .d), (.toggleCleanSpace, .p), ] let prefixes: [KeyModifiers] = [ [.ctrl, .alt], [.ctrl, .shift], [.alt, .cmd], [.ctrl, .alt, .cmd, .shift, .fn], ] for prefix in prefixes { let bindings = DefaultKeybindings.bindings(prefix: prefix) #expect(Set(bindings.values).count == bindings.count) for (command, key) in modeKeys { #expect(bindings[command] == KeyChord(modifiers: prefix, key: key)) } #expect(bindings[.toggleAutomaticTiling] == KeyChord(modifiers: prefix, key: .t)) let treeKey = prefix.contains(.shift) ? ANSIKey.f18 : .t #expect(bindings[.layoutTree] == KeyChord(modifiers: prefix.union(.shift), key: treeKey)) let moveRight = prefix.contains(.shift) ? ANSIKey.f13 : .d #expect(bindings[.moveRight] == KeyChord(modifiers: prefix.union(.shift), key: moveRight)) } } @Test func modeBindingsCanBeDisabledAndReassigned() throws { let source = "[keybindings]\nfocus-toggle = false\nno-peek-toggle = 'cmd-f20'\nclean-space-toggle = 'cmd-f19'\n" let value = try configuration(source) #expect(value.keybindings[.toggleFocus] == nil) #expect(value.keybindings[.toggleNoPeek] == KeyChord(modifiers: .cmd, key: .f20)) #expect(value.keybindings[.toggleCleanSpace] == KeyChord(modifiers: .cmd, key: .f19)) } private func configuration(_ source: String) throws -> ParcelConfiguration { let result = ConfigValidator.validate(try TOMLDocument(parsing: source)) #expect(result.diagnostics.isEmpty) return try #require(result.configuration) } }