import HelderCore @testable import HelderUI import Testing @Suite struct KeyRouterTests { private func cmd(_ c: String, shift: Bool = false) -> KeyPress { KeyPress(.character(c), command: true, shift: shift) } private func route(_ press: KeyPress, _ context: KeyContext = KeyContext()) -> KeyRoute { KeyRouter.route(press, context) } @Test(arguments: [ ("n", Command.showNote), ("p", .push), ("s", .save), ("m", .cycleMode), ("g", .toggleGit), ("l", .toggleAutoFit), (".", .toggleHidden), ("w", .closeFileOrWindow), ]) func commandLettersMapToTheirCommand(key: String, command: Command) { #expect(route(cmd(key)) == .perform(command)) #expect(route(cmd(key), KeyContext(owner: .text(in: .editor))) == .perform(command)) } @Test func searchIsSeededWithTheSelection() { #expect(route(cmd("f"), KeyContext(searchSeed: "needle")) == .perform(.showSearch(seed: "needle"))) } @Test func shiftCommandOOpensTheRecentProjectsAndPlainOIsLeftToTheMenu() { #expect(route(cmd("o", shift: true)) == .perform(.showRecentProjects)) #expect(route(cmd("o")) == .pass) #expect(route(cmd("r")) == .pass) #expect(route(cmd("n", shift: true)) == .pass) } @Test func commandASelectsTheOpenFileOutsideAFieldAndCommandLTogglesFluid() { #expect(route(cmd("a")) == .perform(.selectAllInFile)) #expect(route(cmd("l")) == .perform(.toggleAutoFit)) #expect(route(cmd("a"), KeyContext(owner: .text(in: .editor))) == .pass) #expect(route(cmd("a"), KeyContext(owner: .terminal)) == .pass) } @Test func commandCFocusesTheCommitBoxOutsideTheTerminalUnlessThereIsSomethingToCopy() { #expect(route(cmd("c")) == .perform(.focusCommitMessage)) #expect(route(cmd("c"), KeyContext(owner: .text(in: .editor), hasTextSelection: true)) == .pass) #expect(route(cmd("c"), KeyContext(hasEditorLineSelection: true)) == .pass) #expect(route(cmd("c"), KeyContext(owner: .terminal)) == .pass) #expect(route(cmd("c"), KeyContext(owner: .terminal, hasTextSelection: true)) == .pass) } @Test func commandEnterCommitsExceptInTheCommitBoxItself() { #expect(route(KeyPress(.enter, command: true)) == .perform(.commit)) #expect(route(KeyPress(.enter, command: true), KeyContext(owner: .text(in: .sourceControl))) == .pass) } @Test func arrowsMoveTheRowCursorOnlyInAListWithoutAField() { let list = KeyContext(activePanel: .explorer) #expect(route(KeyPress(.down), list) == .perform(.moveRowCursor(1))) #expect(route(KeyPress(.up), list) == .perform(.moveRowCursor(-1))) #expect(route(KeyPress(.enter), list) == .perform(.openSelectedRow)) #expect(route(KeyPress(.down), KeyContext(activePanel: .editor)) == .pass) #expect(route(KeyPress(.down), KeyContext(owner: .text(in: .sourceControl), activePanel: .sourceControl)) == .pass) #expect(route(KeyPress(.enter), KeyContext(owner: .terminal, activePanel: .terminal)) == .pass) } @Test func commandRightOpensTheRowMenuPassesASelectionOrMovesFocus() { #expect(route(KeyPress(.right, command: true), KeyContext(activePanel: .sourceControl)) == .perform(.openRowMenu)) let selection = TextSelectionInfo(text: "x", startLine: 3, endLine: 4) #expect(route(KeyPress(.right, command: true), KeyContext(owner: .text(in: .editor), editorTextSelection: selection)) == .perform(.passSelection(selection))) #expect(route(KeyPress(.right, command: true), KeyContext(hasEditorLineSelection: true, activePanel: .editor)) == .perform(.passSelection(nil))) #expect(route(KeyPress(.right, command: true), KeyContext(activePanel: .editor)) == .perform(.focusNeighbour(step: 1))) #expect(route(KeyPress(.right, command: true), KeyContext(owner: .text(in: .editor))) == .pass) } @Test func commandLeftMovesFocusButNotOutOfATextField() { #expect(route(KeyPress(.left, command: true)) == .perform(.focusNeighbour(step: -1))) #expect(route(KeyPress(.left, command: true), KeyContext(owner: .text(in: .editor))) == .pass) } @Test func commandUpAndDownOpenRecentFilesFromTheEditor() { let editor = KeyContext(focusZone: .editor, recentFileCount: 3) #expect(route(KeyPress(.down, command: true), editor) == .perform(.showRecentFiles(initialSelection: 1))) #expect(route(KeyPress(.up, command: true), editor) == .perform(.showRecentFiles(initialSelection: 0))) #expect(route(KeyPress(.down, command: true), KeyContext(focusZone: .editor, recentFileCount: 1)) == .perform(.showRecentFiles(initialSelection: 0))) #expect(route(KeyPress(.down, command: true), KeyContext(focusZone: .terminal, recentFileCount: 3)) == .pass) #expect(route(KeyPress(.down, command: true), KeyContext(focusZone: .editor)) == .pass) } @Test func questionMarkOpensHelpButTypesInAField() { #expect(route(KeyPress(.character("?"), shift: true)) == .perform(.showShortcuts)) #expect(route(KeyPress(.character("?"), shift: true), KeyContext(owner: .text(in: .sourceControl))) == .pass) #expect(route(KeyPress(.character("?"), shift: true), KeyContext(owner: .terminal)) == .pass) } @Test func escapeClosesAnOverlayAndOtherwiseReachesTheTerminal() { #expect(route(KeyPress(.escape), KeyContext(overlay: .other)) == .perform(.dismissOverlay)) #expect(route(KeyPress(.escape), KeyContext(overlay: .note, owner: .text(in: nil))) == .perform(.dismissOverlay)) #expect(route(KeyPress(.escape), KeyContext(owner: .terminal)) == .pass) } @Test func anOpenOverlayOwnsEveryOtherKey() { let search = KeyContext(overlay: .other, activePanel: .explorer) #expect(route(cmd("g"), search) == .pass) #expect(route(KeyPress(.down), search) == .pass) #expect(route(KeyPress(.right, command: true), search) == .pass) } @Test func commandPPassesTheNoteWhileItIsOpen() { #expect(route(cmd("p"), KeyContext(overlay: .note)) == .perform(.passNote)) #expect(route(cmd("g"), KeyContext(overlay: .note)) == .pass) } @Test func theBareLauncherTakesArrowsAndEnter() { let context = KeyContext(hasProject: false) #expect(route(KeyPress(.down), context) == .perform(.launcherMove(1))) #expect(route(KeyPress(.up, command: true), context) == .perform(.launcherMove(-1))) #expect(route(KeyPress(.enter), context) == .perform(.launcherActivate)) #expect(route(cmd("o"), context) == .pass) } @Test func theRecentProjectListOwnsItsKeysButEsc() { let context = KeyContext(overlay: OverlayKind(.launcher)) #expect(route(KeyPress(.down), context) == .pass) #expect(route(KeyPress(.enter), context) == .pass) #expect(route(KeyPress(.escape), context) == .perform(.dismissOverlay)) } @Test func theWorkspaceShortcutsKeepWorkingOverTheDiff() { let diff = KeyContext(overlay: .diff(busy: false), activePanel: .editor) #expect(route(cmd("m"), diff) == .perform(.cycleMode)) #expect(route(cmd("p"), diff) == .perform(.push)) #expect(route(cmd("s"), diff) == .perform(.save)) #expect(route(cmd("c"), diff) == .perform(.focusCommitMessage)) #expect(route(KeyPress(.enter, command: true), diff) == .perform(.commit)) #expect(route(KeyPress(.down, command: true), KeyContext(overlay: .diff(busy: false), focusZone: .editor, recentFileCount: 2)) == .perform(.showRecentFiles(initialSelection: 1))) #expect(route(KeyPress(.escape), diff) == .perform(.dismissOverlay)) } @Test func overTheDiffCopyAndSelectAllStayWithItsPanes() { #expect(route(cmd("c"), KeyContext(overlay: .diff(busy: false), hasTextSelection: true)) == .pass) #expect(route(cmd("a"), KeyContext(overlay: .diff(busy: false))) == .pass) } @Test func theDiffsOwnMenuOrPromptTakesEveryKey() { let busy = KeyContext(overlay: .diff(busy: true)) #expect(route(KeyPress(.down), busy) == .pass) #expect(route(KeyPress(.enter), busy) == .pass) #expect(route(KeyPress(.escape), busy) == .pass) #expect(route(cmd("m"), busy) == .pass) } @Test func controlAndOptionChordsAlwaysReachTheFocusedView() { #expect(route(KeyPress(.character("c"), control: true)) == .pass) #expect(route(KeyPress(.character("g"), command: true, option: true)) == .pass) } }