Files
helder/Tests/HelderUITests/Shell/KeyRouterTests.swift
T

255 lines
14 KiB
Swift

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: [
("p", Command.push), ("s", .save), ("m", .cycleMode), ("g", .toggleGit),
("l", .toggleAutoFit), (".", .toggleHidden), ("w", .closeFileOrWindow),
("1", .togglePane(.explorer)), ("2", .togglePane(.editor)), ("3", .togglePane(.terminal)),
])
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 commandEnterCommitsOnlyOverTheGitView() {
#expect(route(KeyPress(.enter, command: true)) == .pass)
#expect(route(KeyPress(.enter, command: true), KeyContext(owner: .text(in: .editor))) == .pass)
#expect(route(KeyPress(.enter, command: true), KeyContext(gitViewOpen: true)) == .perform(.commit))
}
@Test func commandEnterInTheGitConsoleStaysWithTheConsole() {
let console = KeyContext(owner: .text(in: nil), gitViewOpen: true, inGitConsole: true)
#expect(route(KeyPress(.enter, command: true), console) == .pass)
#expect(route(cmd("p"), console) == .perform(.push))
}
@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: .explorer), activePanel: .explorer)) == .pass)
#expect(route(KeyPress(.enter), KeyContext(owner: .terminal, activePanel: .terminal)) == .pass)
}
@Test func commandRightOpensTheRowMenuPassesASelectionOrMovesFocus() {
#expect(route(KeyPress(.right, command: true), KeyContext(activePanel: .explorer)) == .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: .editor))) == .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: .other, owner: .text(in: nil))) == .perform(.dismissOverlay))
#expect(route(KeyPress(.escape), KeyContext(owner: .terminal)) == .pass)
}
@Test func commandGTogglesTheGitViewFromAnywhere() {
#expect(route(cmd("g")) == .perform(.toggleGit))
#expect(route(cmd("g"), KeyContext(owner: .terminal)) == .perform(.toggleGit))
#expect(route(cmd("g"), KeyContext(owner: .text(in: nil), gitViewOpen: true)) == .perform(.toggleGit))
}
@Test func escapeClosesTheGitViewOnlyWhenNoOverlayIsOpen() {
let git = KeyContext(gitViewOpen: true)
#expect(route(KeyPress(.escape), git) == .perform(.closeGitView))
#expect(route(KeyPress(.escape), KeyContext(owner: .text(in: nil), gitViewOpen: true)) == .perform(.closeGitView))
#expect(route(KeyPress(.escape), KeyContext(overlay: .other, gitViewOpen: true)) == .perform(.dismissOverlay))
#expect(route(KeyPress(.escape), KeyContext(overlay: .diff(busy: false), gitViewOpen: true)) == .perform(.dismissOverlay))
#expect(route(KeyPress(.escape), KeyContext(overlay: .diff(busy: true), gitViewOpen: true)) == .pass)
#expect(route(KeyPress(.escape)) == .pass)
}
@Test func overTheGitViewCommitPushAndSearchWorkFromItsFields() {
let field = KeyContext(owner: .text(in: nil), gitViewOpen: true)
#expect(route(KeyPress(.enter, command: true), field) == .perform(.commit))
#expect(route(cmd("p"), field) == .perform(.push))
#expect(route(cmd("f"), field) == .perform(.showSearch(seed: "")))
#expect(route(cmd("g"), field) == .perform(.toggleGit))
#expect(route(cmd("2"), field) == .perform(.togglePane(.editor)))
}
@Test func overTheGitViewTheColumnKeysRestAndTypingReachesTheField() {
let git = KeyContext(activePanel: .explorer, focusZone: .editor, recentFileCount: 3, gitViewOpen: true)
#expect(route(KeyPress(.down), git) == .pass)
#expect(route(KeyPress(.enter), git) == .pass)
#expect(route(KeyPress(.right, command: true), git) == .pass)
#expect(route(KeyPress(.left, command: true), git) == .pass)
#expect(route(KeyPress(.down, command: true), git) == .pass)
#expect(route(cmd("a"), git) == .pass)
let field = KeyContext(owner: .text(in: nil), gitViewOpen: true)
#expect(route(KeyPress(.character("x")), field) == .pass)
#expect(route(KeyPress(.character("?"), shift: true), field) == .pass)
#expect(route(cmd("c"), KeyContext(owner: .text(in: nil), hasTextSelection: true, gitViewOpen: true)) == .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 commandNIsLeftUnbound() {
#expect(route(cmd("n")) == .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 theSearchSwitchesToTheGitAndSchemaViewsAndOwnsEveryOtherKey() {
let search = KeyContext(overlay: .search, owner: .text(in: nil), schemaAvailable: true)
#expect(OverlayKind(.search(seed: "x")) == .search)
#expect(route(cmd("g"), search) == .perform(.toggleGit))
#expect(route(cmd("d"), search) == .perform(.toggleSchemaView))
#expect(route(cmd("d"), KeyContext(overlay: .search, owner: .text(in: nil))) == .pass)
#expect(route(cmd("f"), search) == .pass)
#expect(route(cmd("s"), search) == .pass)
#expect(route(cmd("g", shift: true), search) == .pass)
#expect(route(KeyPress(.character("g")), search) == .pass)
#expect(route(KeyPress(.down), search) == .pass)
#expect(route(KeyPress(.enter), search) == .pass)
#expect(route(KeyPress(.escape), search) == .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) == .pass)
#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)
}
@Test func commandDTogglesTheSchemaViewOnlyInALaravelApp() {
#expect(route(cmd("d"), KeyContext(schemaAvailable: true)) == .perform(.toggleSchemaView))
#expect(route(cmd("d"), KeyContext(owner: .text(in: .editor), schemaAvailable: true)) == .perform(.toggleSchemaView))
#expect(route(cmd("d"), KeyContext(gitViewOpen: true, schemaAvailable: true)) == .perform(.toggleSchemaView))
#expect(route(cmd("d"), KeyContext(schemaAvailable: true, schemaViewOpen: true)) == .perform(.toggleSchemaView))
#expect(route(cmd("d")) == .pass)
#expect(route(cmd("d"), KeyContext(owner: .terminal)) == .pass)
}
@Test func escapeOverTheSchemaViewClearsTheSelectionThenTheSearchThenCloses() {
let open = KeyContext(schemaAvailable: true, schemaViewOpen: true)
#expect(route(KeyPress(.escape), open) == .perform(.closeSchemaView))
var selected = open
selected.schemaHasSelection = true
selected.schemaSearchHasText = true
#expect(route(KeyPress(.escape), selected) == .perform(.clearSchemaSelection))
var searching = open
searching.owner = .text(in: nil)
searching.schemaSearchHasText = true
#expect(route(KeyPress(.escape), searching) == .perform(.clearSchemaSearch))
var overlaid = selected
overlaid.overlay = .other
#expect(route(KeyPress(.escape), overlaid) == .perform(.dismissOverlay))
#expect(route(KeyPress(.escape, command: true), open) == .pass)
}
@Test func overTheSchemaViewTheColumnKeysRestAndNothingCommits() {
let schema = KeyContext(activePanel: .explorer, focusZone: .editor, recentFileCount: 3,
schemaAvailable: true, schemaViewOpen: true)
#expect(route(KeyPress(.down), schema) == .pass)
#expect(route(KeyPress(.enter), schema) == .pass)
#expect(route(KeyPress(.right, command: true), schema) == .pass)
#expect(route(KeyPress(.down, command: true), schema) == .pass)
#expect(route(KeyPress(.enter, command: true), schema) == .pass)
#expect(route(cmd("c"), schema) == .pass)
#expect(route(cmd("a"), schema) == .pass)
#expect(route(cmd("g"), schema) == .perform(.toggleGit))
#expect(route(cmd("f"), schema) == .perform(.showSearch(seed: "")))
#expect(route(KeyPress(.character("?")), schema) == .perform(.showShortcuts))
#expect(route(KeyPress(.character("?")), KeyContext(owner: .text(in: nil), schemaViewOpen: true)) == .pass)
}
}