Branchwell

Voxley / app · voxley.si / Tests / VoxleyTests / LibraryTests.swift

Raw 4.8 KB on 65820bdbf93f3277c3f8b2c09a8acbcf3783d60a
1import AppKit
2import Foundation
3import Testing
4@testable import Voxley
5
6@Test func wordListMergesDelimitedPhrasesWithManualVocabulary() throws {
7 var preferences = Preferences()
8 preferences.vocabulary = "Voxley\n"
9 let added = try preferences.importVocabulary("\u{FEFF}voxley, Protomota Lab\r\nInkspring; \"Acme, Inc.\"\tinkSpring")
10
11 #expect(added == 3)
12 #expect(preferences.contextWords == ["Voxley", "Protomota Lab", "Inkspring", "Acme, Inc."])
13}
14
15@Test func wordListDoesNotChangeVocabularyWhenImportFails() throws {
16 var preferences = Preferences()
17 preferences.vocabulary = (1..<Preferences.vocabularyLimit).map { "Word \($0)" }.joined(separator: "\n")
18 let original = preferences.vocabulary
19 do {
20 _ = try preferences.importVocabulary("Protomota, Voxley")
21 Issue.record("Expected an import beyond the hint limit to fail")
22 } catch WordList.ImportError.overLimit(let newEntries, let available) {
23 #expect(newEntries == 2)
24 #expect(available == 1)
25 }
26 #expect(preferences.vocabulary == original)
27 #expect(throws: WordList.ImportError.self) { try preferences.importVocabulary("\"Unclosed") }
28 #expect(preferences.vocabulary == original)
29}
30
31@Test func dictionaryPreservesWordBoundariesAndLiteralReplacement() {
32 var preferences = Preferences()
33 preferences.replacements = [WordReplacement(spoken: "ink", written: "$Ink")]
34 #expect(preferences.applyingReplacements(to: "INK, Inkspring, ink. Think.") == "$Ink, Inkspring, $Ink. Think.")
35}
36
37@Test func backupRestoresPreferencesWithoutDuplicatingHistory() throws {
38 let entry = Transcript(original: "hi", text: "Hi.", source: "Notes", mode: .clean)
39 var backup = Library()
40 backup.history = [entry]
41 backup.preferences.appModes["com.apple.mail"] = .email
42 let encoded = try JSONEncoder().encode(backup)
43 let decoded = try JSONDecoder().decode(Library.self, from: encoded)
44 var local = Library()
45 let localEntry = Transcript(original: "test", text: "test", source: "Mail", mode: .verbatim)
46 local.history = [localEntry]
47 local.restore(decoded)
48 local.restore(decoded)
49 #expect(Set(local.history.map(\.id)) == [entry.id, localEntry.id])
50 #expect(local.history.count == 2)
51 #expect(local.preferences.appModes["com.apple.mail"] == .email)
52}
53
54@Test func transcriptJoinsPunctuationAndSeparatesWords() {
55 #expect(AppleModels.join("Hello", ", world") == "Hello, world")
56 #expect(AppleModels.join("Hello", "world") == "Hello world")
57 #expect(AppleModels.join("Hello", " world") == "Hello world")
58}
59
60@Test func quickTapsStartAndStopWithoutWaitingForSpeechSetup() {
61 var gesture = DictationShortcutGesture()
62 #expect(gesture.update(pressed: true, time: 0, canStart: true, isRecording: false) == .start)
63 #expect(gesture.update(pressed: false, time: 0.12, canStart: false, isRecording: true) == .none)
64 #expect(gesture.update(pressed: true, time: 0.50, canStart: false, isRecording: true) == .none)
65 #expect(gesture.update(pressed: false, time: 0.59, canStart: false, isRecording: true) == .stop)
66}
67
68@Test func heldShortcutStopsOnReleaseAndIgnoresDuplicatePresses() {
69 var gesture = DictationShortcutGesture()
70 #expect(gesture.update(pressed: true, time: 0, canStart: true, isRecording: false) == .start)
71 #expect(gesture.update(pressed: true, time: 0.4, canStart: false, isRecording: true) == .none)
72 #expect(gesture.update(pressed: false, time: 0.6, canStart: false, isRecording: true) == .stop)
73 #expect(gesture.update(pressed: false, time: 0.7, canStart: false, isRecording: false) == .none)
74}
75
76@Test func shortcutDecodesExistingPreferencesAndRoundTripsCustomKeys() throws {
77 let decoder = JSONDecoder()
78 let legacy = try decoder.decode(Shortcut.self, from: Data("\"⌃⇧Space\"".utf8))
79 #expect(legacy == .controlShiftSpace)
80 let custom = Shortcut(code: 40, modifiers: 0x900, keyLabel: "K")
81 let encoded = try JSONEncoder().encode(custom)
82 #expect(try decoder.decode(Shortcut.self, from: encoded) == custom)
83 #expect(custom.label == "⌥⌘K")
84}
85
86@Test func modifierChordKeepsBothKeysUntilTheLastRelease() throws {
87 let control = Shortcut.chordModifiers(for: .control)
88 let both = Shortcut.chordModifiers(for: [.control, .option])
89 let option = Shortcut.chordModifiers(for: .option)
90 var capture = ModifierChordCapture()
91 #expect(capture.modifiersChanged(control) == nil)
92 #expect(capture.modifiersChanged(both) == nil)
93 #expect(capture.modifiersChanged(option) == nil)
94 let result = capture.modifiersChanged(0)
95 let saved = try #require(result)
96 #expect(saved.isModifierOnly)
97 #expect(saved.label == "⌃⌥")
98 #expect(try JSONDecoder().decode(Shortcut.self, from: JSONEncoder().encode(saved)) == saved)
99
100 _ = capture.modifiersChanged(both)
101 capture.ordinaryKeyDown()
102 #expect(capture.modifiersChanged(0) == nil)
103}