Tests/OrgAppTests/CommandRankingTests.swift

a79f5362fbc7c9473d040271cb3e718f88e5daf6
orgstar/Tests/OrgAppTests/CommandRankingTests.swift

71 lines · 4019 bytes

9 symbols in this file
 1import Foundation
 2import OrgCore
 3import Testing
 4@testable import OrgApp
 5
 6struct CommandRankingTests {
 7    private func rank(_ titles: [String], _ query: String, _ usage: CommandUsage) -> (recent: [String], rest: [String]) {
 8        CommandRanking.rank(titles, query: query, usage: usage, id: { $0 }, titles: { [$0] })
 9    }
10
11    @Test func recentsAreTheLastTenMostRecentFirst() {
12        let usage = CommandUsage(defaults: TestDefaults("orgstar-command-usage"))
13        for n in 1...12 { usage.record("c\(n)") }
14        usage.record("c5")
15        #expect(usage.recent == ["c5", "c12", "c11", "c10", "c9", "c8", "c7", "c6", "c4", "c3"])
16        #expect(usage.uses(of: "c5") == 2 && usage.uses(of: "c1") == 1 && usage.uses(of: "other") == 0)
17    }
18
19    @Test func emptyQueryListsRecentsThenTheRestByTitle() {
20        let usage = CommandUsage(defaults: TestDefaults("orgstar-command-usage"))
21        usage.record("Tags")
22        usage.record("Gone")
23        usage.record("Schedule")
24        let ranked = rank(["Tags", "Archive", "Schedule", "Deadline"], " ", usage)
25        // A recent command no longer offered is left out.
26        #expect(ranked.recent == ["Schedule", "Tags"])
27        #expect(ranked.rest == ["Archive", "Deadline"])
28    }
29
30    @Test func frequentUseBreaksTies() {
31        let usage = CommandUsage(defaults: TestDefaults("orgstar-command-usage"))
32        #expect(FuzzyMatch.score("ac", "Alpha Copy") == FuzzyMatch.score("ac", "Alpha Cuts"))
33        #expect(rank(["Alpha Cuts", "Alpha Copy"], "ac", usage).rest == ["Alpha Copy", "Alpha Cuts"])
34        usage.record("Alpha Cuts")
35        let ranked = rank(["Alpha Cuts", "Alpha Copy"], "ac", usage)
36        #expect(ranked.recent.isEmpty && ranked.rest == ["Alpha Cuts", "Alpha Copy"])
37    }
38
39    @Test func frequentUseNeverBeatsAClearlyBetterMatch() throws {
40        let usage = CommandUsage(defaults: TestDefaults("orgstar-command-usage"))
41        for _ in 0..<100 { usage.record("Make Fun") }
42        #expect(usage.bonus(for: "Make Fun") == FuzzyMatch.wordStartBonus * 100)
43        // One word start apart, less one character of length.
44        let better = try #require(FuzzyMatch.score("mu", "Move Up")), worse = try #require(FuzzyMatch.score("mu", "Make Fun"))
45        #expect(better - worse == FuzzyMatch.wordStartBonus * 100 + 1)
46        #expect(rank(["Make Fun", "Move Up"], "mu", usage).rest == ["Move Up", "Make Fun"])
47    }
48
49    @Test func groupsByFirstMatchingPrefix() {
50        #expect(CommandGroups.group(of: "org.table.align").map { CommandGroups.orgMenu[$0].prefixes.first } == "org.table")
51        #expect(CommandGroups.group(of: "org.ctrl-c-ctrl-c") == nil)
52        #expect(CommandGroups.group(of: "app.clock.in") == nil)
53        #expect(CommandGroups.group(of: "app.clock.in", in: CommandGroups.commandList) == CommandGroups.orgMenu.count)
54    }
55
56    private func context(_ text: String, caret: Int) -> EditContext {
57        EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [caret..<caret])
58    }
59
60    @Test func reasonNamesWhatTheCaretIsNotIn() {
61        let text = "Before\n* One\nBody\n| a | b |\n- item\n"
62        #expect(CommandReason.reason(for: "org.table.align", in: context(text, caret: 0)) == "No table at point")
63        #expect(CommandReason.reason(for: "org.item.indent", in: context(text, caret: 0)) == "Not in a list")
64        #expect(CommandReason.reason(for: "org.subtree.move-up", in: context(text, caret: 0)) == "Not in a subtree")
65        #expect(CommandReason.reason(for: "org.link.open", in: context(text, caret: 0)) == "Not on a link")
66        #expect(CommandReason.reason(for: "org.babel.execute", in: context(text, caret: 0)) == "Not in a code block")
67        // In the entry, a subtree command that still doesn't apply has nothing to do.
68        #expect(CommandReason.reason(for: "org.subtree.move-up", in: context(text, caret: 15)) == "Nothing to do here")
69        #expect(CommandReason.reason(for: "org.table.align", in: context(text, caret: 20)) == "Nothing to do here")
70    }
71}
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