Tests/OrgEditorAppKitTests/KeyTests.swift
155 lines · 6143 bytes
1import AppKit
2import OrgCore
3import Testing
4@testable import OrgEditorAppKit
5
6@MainActor
7struct KeyTests {
8 static let codes: [Character: UInt16] = ["a": 0, "s": 1, "f": 3, "g": 5, "z": 6, "x": 7, "c": 8, "w": 13, "q": 12, "t": 17]
9
10 /// Presses a key. `control` sends the control character, as macOS does.
11 func press(_ h: Harness, _ key: Character, control: Bool = false, option: UInt = 0, typed: String? = nil) {
12 var flags: NSEvent.ModifierFlags = []
13 if control { flags.insert(.control) }
14 if option != 0 { flags = NSEvent.ModifierFlags(rawValue: flags.rawValue | NSEvent.ModifierFlags.option.rawValue | option) }
15 let ignoring = String(key)
16 let characters = typed ?? (control ? String(UnicodeScalar(UInt8(key.asciiValue! & 0x1f))) : ignoring)
17 send(h, characters: characters, ignoring: ignoring, code: Self.codes[key] ?? 0, flags: flags)
18 }
19
20 func send(_ h: Harness, characters: String, ignoring: String, code: UInt16, flags: NSEvent.ModifierFlags = []) {
21 let event = NSEvent.keyEvent(
22 with: .keyDown, location: .zero, modifierFlags: flags, timestamp: 0, windowNumber: h.window.windowNumber,
23 context: nil, characters: characters, charactersIgnoringModifiers: ignoring, isARepeat: false, keyCode: code
24 )!
25 h.textView.keyDown(with: event)
26 }
27
28 @Test func prefixSequenceRunsAnOrgCommand() {
29 let h = Harness("* a\n")
30 var pending: [String?] = []
31 var next: [(key: String, title: String)] = []
32 h.editor.onKeysPending = { keys, continuations in
33 pending.append(keys)
34 if keys != nil { next = continuations }
35 }
36 h.caret(at: 1)
37 press(h, "c", control: true)
38 #expect(pending == ["C-c"])
39 #expect(next.contains { $0.key == "C-t" && $0.title == "Cycle TODO State" })
40 press(h, "t", control: true)
41 #expect(h.string == "* TODO a\n")
42 #expect(pending == ["C-c", nil])
43 h.checkInSync()
44 }
45
46 @Test func undefinedSequenceReportsAMessage() {
47 let h = Harness("text\n")
48 var messages: [String] = []
49 h.editor.onMessage = { messages.append($0) }
50 press(h, "c", control: true)
51 press(h, "z", control: true)
52 #expect(messages == ["C-c C-z is undefined"])
53 press(h, "x", control: true)
54 press(h, "g", control: true)
55 #expect(messages.last == "Quit")
56 #expect(h.string == "text\n")
57 }
58
59 @Test func tabFoldsOnHeadingsAndIndentsElsewhere() {
60 let h = Harness(outline)
61 h.caret(at: 1)
62 send(h, characters: "\t", ignoring: "\t", code: 48)
63 #expect(h.visibleLines == ["* a\n", "* c\n", "text\n"])
64 h.caret(at: h.offset(of: "text"))
65 send(h, characters: "\t", ignoring: "\t", code: 48)
66 #expect(h.string.hasSuffix("\ttext\n"))
67 }
68
69 @Test func markExtendsMovementAndKillTakesTheRegion() {
70 let h = Harness("abc\n")
71 h.caret(at: 0)
72 send(h, characters: "\0", ignoring: " ", code: 49, flags: .control)
73 #expect(h.editor.markActive)
74 press(h, "f", control: true)
75 press(h, "f", control: true)
76 #expect(h.textView.selectedRange() == NSRange(location: 0, length: 2))
77 press(h, "w", control: true)
78 #expect(h.string == "c\n")
79 #expect(!h.editor.markActive)
80 h.checkInSync()
81 }
82
83 @Test func leftOptionIsMetaRightOptionTypes() {
84 let h = Harness("one two\n")
85 h.caret(at: 0)
86 press(h, "f", option: KeyEvents.leftOption, typed: "ƒ")
87 #expect(h.caret == 3)
88 press(h, "f", option: KeyEvents.rightOption, typed: "ƒ")
89 #expect(h.string == "oneƒ two\n")
90 }
91
92 @Test func appCommandsGoToTheApp() {
93 let h = Harness("text\n")
94 var commands: [String] = []
95 h.editor.onAppCommand = { commands.append($0) }
96 press(h, "x", control: true)
97 press(h, "s", control: true)
98 #expect(commands == ["app.save"])
99 }
100
101 @Test func userKeymapOverridesThePreset() throws {
102 let h = Harness("* a\n")
103 var problems: [String] = []
104 let user = try Keymap(toml: "[[bind]]\nkeys = \"C-c C-t\"\ncommand = \"org.priority.up\"\n", problems: &problems)
105 h.editor.keymap = .layered([KeymapPreset.emacs.keymap, user])
106 h.caret(at: 1)
107 press(h, "c", control: true)
108 press(h, "t", control: true)
109 #expect(h.string == "* [#B] a\n")
110 }
111
112 @Test func promptsAskAndRunAgainWithTheAnswer() {
113 let h = Harness("* a\n")
114 var asked: [Prompt] = []
115 var reply: ((String?) -> Void)?
116 h.editor.onPrompt = { prompt, answer in
117 asked.append(prompt)
118 reply = answer
119 }
120 h.caret(at: 1)
121 press(h, "c", control: true)
122 press(h, "q", control: true)
123 #expect(asked.map(\.key) == ["tags"])
124 reply?("x")
125 #expect(h.string.hasPrefix("* a ") && h.string.hasSuffix(":x:\n"))
126 h.checkInSync()
127 }
128
129 @Test func everyPresetCommandExists() {
130 for preset in KeymapPreset.allCases {
131 for binding in preset.keymap.bindings {
132 #expect(EditorCommands.command(binding.command) != nil, "\(preset): \(binding.command)")
133 }
134 }
135 }
136}
137
138@MainActor
139struct ListKeyTests {
140 @Test func metaReturnInsertsAnItemInAListAndAHeadingElsewhere() {
141 let h = Harness("* a\n- x\n")
142 let event = { (flags: NSEvent.ModifierFlags) in
143 NSEvent.keyEvent(with: .keyDown, location: .zero, modifierFlags: flags, timestamp: 0, windowNumber: h.window.windowNumber,
144 context: nil, characters: "\r", charactersIgnoringModifiers: "\r", isARepeat: false, keyCode: 36)!
145 }
146 let meta = NSEvent.ModifierFlags(rawValue: NSEvent.ModifierFlags.option.rawValue | KeyEvents.leftOption)
147 h.caret(at: 7)
148 h.textView.keyDown(with: event(meta))
149 #expect(h.string == "* a\n- x\n- \n")
150 h.caret(at: 3)
151 h.textView.keyDown(with: event(meta))
152 #expect(h.string.hasPrefix("* a\n* \n"))
153 h.checkInSync()
154 }
155}