Tests/OrgAppTests/KeyBarLayoutTests.swift
124 lines · 6341 bytes
1import Foundation
2import Testing
3@testable import OrgApp
4
5@MainActor
6struct KeyBarLayoutTests {
7 func defaults() -> UserDefaults { TestDefaults("orgstar-keybar") }
8
9 @Test func defaultsWithoutTheTable() {
10 let d = defaults()
11 #expect(UserConfig.apply("fill-column = 70\n", to: d).isEmpty)
12 #expect(KeyBarLayout.stored(.iphone, in: d) == nil)
13 #expect(KeyBarLayout.stored(.ipad, in: d) == nil)
14 #expect(KeyBarLayout.Width.iphone.defaultIDs.contains("plan"))
15 #expect(!KeyBarLayout.Width.ipad.defaultIDs.contains("plan"))
16 #expect(KeyBarLayout.Width.ipad.defaultIDs.suffix(4) == ["schedule", "deadline", "tags", "hide-keyboard"])
17 }
18
19 @Test func readsIPhoneAndIPadLists() {
20 let d = defaults()
21 let problems = UserConfig.apply("""
22 [key-bar]
23 iphone = ["commands", "org.snippet:td", "macro.named.tidy", "commands"]
24 ipad = [
25 "fold", # TAB
26 "hide-keyboard",
27 ]
28 """, to: d)
29 #expect(problems.isEmpty)
30 #expect(KeyBarLayout.stored(.iphone, in: d) == ["commands", "org.snippet:td", "macro.named.tidy"])
31 #expect(KeyBarLayout.stored(.ipad, in: d) == ["fold", "hide-keyboard"])
32 // A list taken out of the file goes back to the default.
33 UserConfig.apply("[key-bar]\nipad = []\n", to: d)
34 #expect(KeyBarLayout.stored(.iphone, in: d) == nil)
35 #expect(KeyBarLayout.stored(.ipad, in: d) == [])
36 }
37
38 @Test func reportsBadValuesAndUnknownIDs() {
39 let d = defaults()
40 let problems = UserConfig.apply("""
41 [key-bar]
42 iphone = "fold"
43 ipad = ["fold", 3]
44 watch = []
45 """, to: d)
46 #expect(problems.count == 3)
47 #expect(problems.contains { $0.contains("key-bar.iphone must be a list") })
48 #expect(problems.contains { $0.contains("unknown setting key-bar.watch") })
49 #expect(KeyBarLayout.stored(.iphone, in: d) == nil)
50 let known: Set = ["fold", "commands"]
51 #expect(KeyBarLayout.problems(["fold", "nonsense", "commands"], .ipad, known: known.contains)
52 == ["config.toml: key-bar.ipad has no command nonsense"])
53 }
54
55 @Test func writesAndRemovesLists() throws {
56 let start = """
57 fill-column = 80
58
59 [key-bar]
60 iphone = [
61 "fold",
62 "todo",
63 ] # mine
64 ipad = ["fold"]
65 """
66 let written = UserConfig.setting(start, section: KeyBarLayout.table, key: "iphone", to: .array([.string("act"), .string("org.snippet:td")]))
67 #expect(written.contains("iphone = [\"act\", \"org.snippet:td\"]\nipad = [\"fold\"]"))
68 let d = defaults()
69 #expect(UserConfig.apply(written, to: d).isEmpty)
70 #expect(KeyBarLayout.stored(.iphone, in: d) == ["act", "org.snippet:td"])
71 let removed = UserConfig.removingSetting(start, section: KeyBarLayout.table, key: "iphone")
72 #expect(removed == "fill-column = 80\n\n[key-bar]\nipad = [\"fold\"]")
73 let added = UserConfig.setting("fill-column = 80\n", section: KeyBarLayout.table, key: "ipad", to: .array([.string("fold")]))
74 #expect(added.hasSuffix("[key-bar]\nipad = [\"fold\"]\n"))
75 }
76
77 @Test func fitsOrOverflows() {
78 let ids = ["a", "b", "wide", "c", "d"]
79 let slots: (String) -> Int = { $0 == "wide" ? 2 : 1 }
80 #expect(KeyBarLayout.fit(ids, available: 6, slots: slots) == (ids, []))
81 #expect(KeyBarLayout.fit(ids, available: 20, slots: slots) == (ids, []))
82 // One slot goes to More.
83 #expect(KeyBarLayout.fit(ids, available: 5, slots: slots) == (["a", "b", "wide"], ["c", "d"]))
84 // A wide button that doesn't fit ends the row, though a narrow one after it would fit.
85 #expect(KeyBarLayout.fit(ids, available: 4, slots: slots) == (["a", "b"], ["wide", "c", "d"]))
86 #expect(KeyBarLayout.fit(ids, available: 2, slots: slots) == (["a"], ["b", "wide", "c", "d"]))
87 #expect(KeyBarLayout.fit(ids, available: 0, slots: slots) == ([], ids))
88 #expect(KeyBarLayout.fit([], available: 0, slots: slots) == ([], []))
89 }
90
91 @Test func slotsWidenWithTheSymbols() {
92 // iPhone 17: 402 points less the bar's margins.
93 let width: CGFloat = 390
94 let ids = KeyBarLayout.Width.iphone.defaultIDs
95 #expect(KeyBarLayout.availableSlots(width: width, scale: 1) == 12)
96 #expect(KeyBarLayout.availableSlots(width: width, scale: 0.8) == 12)
97 #expect(KeyBarLayout.fit(ids, available: KeyBarLayout.availableSlots(width: width, scale: 1)) { _ in 1 } == (ids, []))
98 // At the largest symbol size (23 against 17 points of body text), slots are a third wider.
99 let large = KeyBarLayout.availableSlots(width: width, scale: 23.0 / 17)
100 #expect(large == 9)
101 let (shown, more) = KeyBarLayout.fit(ids, available: large) { _ in 1 }
102 #expect(shown == Array(ids.prefix(8)) && more == Array(ids.dropFirst(8)))
103 #expect(CGFloat(large) * KeyBarLayout.slotWidth * 23 / 17 <= width)
104 #expect(KeyBarLayout.availableSlots(width: 0, scale: 2) == 0)
105 }
106
107 @Test func snippetFieldsTakePromoteAndDemote() {
108 let next = KeyBarLayout.nextField, previous = KeyBarLayout.previousField
109 #expect(KeyBarLayout.withSnippetFields(KeyBarLayout.Width.iphone.defaultIDs).prefix(4)
110 == ["commands", "fold", previous, next])
111 #expect(KeyBarLayout.withSnippetFields(KeyBarLayout.Width.iphone.defaultIDs).count == KeyBarLayout.Width.iphone.defaultIDs.count)
112 #expect(KeyBarLayout.withSnippetFields(["fold", "todo"]) == [previous, next, "fold", "todo"])
113 #expect(KeyBarLayout.withSnippetFields(["demote", "fold"]) == [previous, next, "fold"])
114 #expect(KeyBarLayout.withSnippetFields([next, "promote", "demote"]) == [next, previous, "demote"])
115 #expect(KeyBarLayout.withSnippetFields([previous, next]) == [previous, next])
116 }
117
118 @Test func faces() {
119 #expect(KeyBarLayout.face(title: "Insert Link…") == "Insert Link")
120 #expect(KeyBarLayout.slots(symbol: true, face: "Insert Link") == 1)
121 #expect(KeyBarLayout.slots(symbol: false, face: "TODO") == 1)
122 #expect(KeyBarLayout.slots(symbol: false, face: "Insert Link") == 2)
123 }
124}