import AppKit import ConjureKit import Foundation import Testing @testable import Conjure @Test @MainActor func commandsShareUsageFavoritesAndTextRankingWithApplications() { let suite = "ConjureCommandRanking.\(UUID().uuidString)" let defaults = UserDefaults(suiteName: suite)! defer { defaults.removePersistentDomain(forName: suite) } var now = Date(timeIntervalSinceReferenceDate: 12_000) let history = LaunchHistory(defaults: defaults, now: { now }) let command = Application(command: .translate) let app = Application(url: URL(fileURLWithPath: "/Applications/Conjure Test.app"), name: "Conjure Test") let sameNameApp = Application(url: URL(fileURLWithPath: "/Applications/Conjure Translate.app"), name: command.name) let entries = [app, sameNameApp, command] history.recordLaunch(of: app) now += 1 history.recordLaunch(of: command) #expect(history.suggestions(in: entries).first?.id == command.id) #expect(history.results(for: "conjure", in: entries).first?.id == command.id) #expect(history.results(for: "ct", in: entries).first?.id == command.id) history.recordLaunch(of: app) #expect(history.suggestions(in: entries).first?.id == app.id) #expect(history.results(for: "Conjure Translate", in: entries).first?.id == command.id) history.toggleFavorite(command) #expect(history.suggestions(in: entries).first?.id == command.id) #expect(!history.isFavorite(sameNameApp)) let reloaded = LaunchHistory(defaults: defaults, now: { now }) #expect(reloaded.isFavorite(command)) #expect(reloaded.suggestions(in: entries).first?.id == command.id) reloaded.toggleFavorite(command) #expect(reloaded.results(for: "conjure", in: entries).first?.id == app.id) #expect(reloaded.results(for: "Conjure Translate", in: entries).first?.id == command.id) reloaded.recordLaunch(of: sameNameApp) reloaded.resetRanking(of: command) let afterReset = LaunchHistory(defaults: defaults, now: { now }) #expect(afterReset.suggestions(in: entries).first?.id == app.id) #expect(afterReset.results(for: "Conjure Translate", in: entries).first?.id == sameNameApp.id) } @Test @MainActor func openingCommandByReturnAndNumberShortcutRecordsUsage() throws { _ = NSApplication.shared let suite = "ConjureCommandOpening.\(UUID().uuidString)" let defaults = UserDefaults(suiteName: suite)! defer { defaults.removePersistentDomain(forName: suite) } let now = Date(timeIntervalSinceReferenceDate: 13_000) let history = LaunchHistory(defaults: defaults, now: { now }) let command = Application(command: .translate) let competitor = Application(url: URL(fileURLWithPath: "/Applications/Alpha.app"), name: "Alpha") history.recordLaunch(of: competitor) let launcher = LauncherWindowController(history: history) let content = try #require(launcher.window?.contentView) func descendants(_ view: NSView) -> [NSView] { [view] + view.subviews.flatMap(descendants) } let search = try #require(descendants(content).compactMap { $0 as? NSSearchField }.first) search.stringValue = "ct" launcher.controlTextDidChange(Notification(name: NSControl.textDidChangeNotification, object: search)) let table = try #require(descendants(content).compactMap { $0 as? NSTableView }.first) #expect(table.numberOfRows == 1) _ = launcher.control(search, textView: NSTextView(), doCommandBy: #selector(NSResponder.insertNewline(_:))) #expect(search.accessibilityLabel() == "English text to translate into Japanese") _ = launcher.control(search, textView: NSTextView(), doCommandBy: #selector(NSResponder.cancelOperation(_:))) #expect(history.suggestions(in: [command, competitor]).first?.id == competitor.id) let panel = try #require(launcher.window as? LauncherPanel) panel.launchNumber?(0) #expect(search.accessibilityLabel() == "English text to translate into Japanese") _ = launcher.control(search, textView: NSTextView(), doCommandBy: #selector(NSResponder.cancelOperation(_:))) let reloaded = LaunchHistory(defaults: defaults, now: { now }) #expect(reloaded.suggestions(in: [command, competitor]).first?.id == command.id) }