Tests/OrgEditorAppKitTests/UserCommandEditorTests.swift
75 lines · 3718 bytes
3 symbols in this file
1import AppKit
2import Foundation
3import OrgCore
4import OrgDocument
5@testable import OrgApp
6import Testing
7@testable import OrgEditorAppKit
8
9/// A user command run through the command host on the session's editor.
10@MainActor
11struct UserCommandEditorTests {
12 @Test func editsApplyAsOneUndoStep() async throws {
13 let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-\(UUID().uuidString)")
14 defer { try? FileManager.default.removeItem(at: folder) }
15 let modules = folder.appendingPathComponent("config/commands")
16 try FileManager.default.createDirectory(at: modules, withIntermediateDirectories: true)
17 try Data("""
18 export async function swap(org) {
19 const [{ from }] = await org.selection();
20 const h = await org.heading();
21 return {
22 edits: [
23 { from: h.titleStart, to: h.titleStart, text: "[" },
24 { from: h.titleEnd, to: h.titleEnd, text: "]" },
25 { from: h.end, to: h.end, text: "added\\n" },
26 ],
27 selection: from + 1,
28 message: "Swapped",
29 };
30 }
31 """.utf8).write(to: modules.appendingPathComponent("swap.js"))
32 let file = folder.appendingPathComponent("a.org")
33 let text = "* One\nbody\n* Two\n"
34 try Data(text.utf8).write(to: file)
35 let session = DocumentSession(recovery: FileRecoveryStore(directory: folder.appendingPathComponent("r")))
36 session.saveMode = .explicit
37 session.userCommands = UserCommands()
38 let command = UserCommand(name: "swap", title: "Swap", file: "swap.js", function: "swap")
39 session.userCommands.replace([command], folder: modules)
40 session.open(file.path)
41 let h = Harness(try #require(session.document))
42 h.editor.onChange = { [weak session] in session?.bufferChanged() }
43 session.attach(h.editor)
44 var messages: [String] = []
45 h.editor.onMessage = { messages.append($0) }
46 h.caret(at: 3)
47 var requested: UserCommandRequest?
48 h.editor.onExternal = { if case .userCommand(let request) = $0 { requested = request } }
49 h.editor.perform(command)
50 let request = try #require(requested)
51 #expect(await session.runUserCommand(request, context: h.editor.context(), file: "a.org", ask: { _, _ in }, report: { _ in }) == nil)
52 #expect(h.string == "* [One]\nbody\nadded\n* Two\n")
53 #expect(h.textView.selectedRange() == NSRange(location: 4, length: 0))
54 #expect(messages == ["Swapped"])
55 #expect(session.isDirty)
56 h.checkInSync()
57 h.textView.undoManager?.undo()
58 #expect(h.string == text)
59 #expect(h.textView.undoManager?.canUndo != true)
60 h.checkInSync()
61 }
62
63 @Test func registeredWhereCommandIDsAre() throws {
64 let store = UserCommands()
65 store.replace([UserCommand(name: "review", title: "Weekly Review", file: "r.js", function: "r", when: "heading")], folder: nil)
66 let commands = EditorCommands.userCommands(store)
67 #expect(commands.map(\.id) == ["user.review"])
68 #expect(commands[0].localizedTitle == "Weekly Review" && commands[0].inPalette)
69 var problems: [String] = []
70 let ids = Set(EditorCommands.builtIn.map(\.id) + commands.map(\.id))
71 let keymap = try Keymap(toml: "[[bind]]\nkeys = \"C-c w\"\ncommand = \"user.review\"\n[[bind]]\nkeys = \"C-c x\"\ncommand = \"user.other\"\n", problems: &problems, commands: ids)
72 #expect(keymap.bindings.map(\.command) == ["user.review"])
73 #expect(problems == ["line 4: unknown command user.other"])
74 }
75}