Tests/OrgAppTests/CompletionSourcesTests.swift

3aa264c947476955d58c5cccc70e97072bb47877
orgstar/Tests/OrgAppTests/CompletionSourcesTests.swift

55 lines · 3588 bytes

 1import Foundation
 2import OrgCore
 3import OrgDocument
 4import OrgIndex
 5import Testing
 6@testable import OrgApp
 7
 8@MainActor
 9struct CompletionSourcesTests {
10    @Test func presetDecidesUntilSet() {
11        let defaults = TestDefaults("orgstar-completion")
12        #expect(OrgPreferences.completion(defaults) == CompletionOptions(auto: false, delay: 0.4, prefix: 2, fuzzy: false, words: false))
13        defaults.set("doom", forKey: "keymap")
14        #expect(OrgPreferences.completion(defaults) == CompletionOptions(auto: true, delay: 0.4, prefix: 2, fuzzy: true, words: false))
15        defaults.set("off", forKey: "completionAuto")
16        defaults.set("prefix", forKey: "completionStyle")
17        #expect(OrgPreferences.completion(defaults) == CompletionOptions(auto: false, fuzzy: false))
18        defaults.set("mac", forKey: "keymap")
19        defaults.set("on", forKey: "completionAuto")
20        defaults.set(250, forKey: "completionAutoDelay")
21        defaults.set(3, forKey: "completionAutoPrefix")
22        defaults.set(true, forKey: "completionWords")
23        #expect(OrgPreferences.completion(defaults) == CompletionOptions(auto: true, delay: 0.25, prefix: 3, fuzzy: false, words: true))
24        #expect(UserConfig.apply("[orgstar]\ncompletion-auto = \"sometimes\"\n", to: defaults) == ["config.toml: completion-auto must be one of preset, on, off"])
25    }
26
27    @Test func answersFromTheIndexLeavingOutThisFile() async throws {
28        let index = try IndexStore()
29        try index.write(FileRecord(path: "/n/a.org", root: "/n", kind: .org, bytes: Array("* Here\n".utf8), mtime: 1, settings: IndexSettings()))
30        try index.write(FileRecord(path: "/n/b.org", root: "/n", kind: .org, bytes: Array("* There\nThermal.\n".utf8), mtime: 1, settings: IndexSettings()))
31        let targets = await CompletionSources.answer(.linkTargets, index: index, excluding: "/n/a.org")
32        #expect(targets.items.map(\.title) == ["There"])
33        #expect(await CompletionSources.answer(.words("the"), index: index, excluding: nil).strings == ["There", "Thermal"])
34    }
35
36    /// Completing `[[id:` to a heading in another file without an ID gives it one there.
37    @Test func linkingAHeadingElsewhereGivesItAnID() throws {
38        let notes = try Folder()
39        let target = try notes.write("target.org", "* Intro\n* Plan\nBody\n")
40        let here = try notes.write("here.org", "See ")
41        let session = DocumentSession(recovery: FileRecoveryStore(directory: notes.url.appendingPathComponent(".r")))
42        session.open(here.path)
43        let item = CompletionItem(title: "Plan", id: nil, path: target.path, start: 8, heading: true)
44        #expect(CompletionSources.createID(item, session: session, newID: "plan-id") == "plan-id")
45        #expect(try String(contentsOf: target, encoding: .utf8) == "* Intro\n* Plan\n:PROPERTIES:\n:ID:       plan-id\n:END:\nBody\n")
46        // A heading that moved is left alone.
47        let moved = CompletionItem(title: "Plan", id: nil, path: target.path, start: 0, heading: true)
48        #expect(CompletionSources.createID(moved, session: session, newID: "other") == nil)
49        // The link the completion inserts.
50        var completion = OrgCompletion.at(EditContext(revision: 0, text: "See [[id:Pl", tree: OrgParser.parse("See [[id:Pl"), selection: [11..<11]))!
51        completion = OrgCompletion.filled(completion, with: CompletionResult(items: [item]), fuzzy: false)
52        let edit = CompletionFlow.linkEdit(completion, completion.item("Plan")!, id: "plan-id")
53        #expect(edit.apply(to: "See [[id:Pl") == "See [[id:plan-id][Plan]]")
54    }
55}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form