Files
Termind/TFMTests/QuickOpenTests.swift
T

214 lines
8.4 KiB
Swift

import AgentKit
import TermindApplication
import TermindDomain
import TermindInfrastructure
import TermindPersistence
import TermindPresentation
import TermindRemoteInfrastructure
import Testing
import struct TermindDomain.Host
@testable import Termind
@Suite
@MainActor
struct QuickOpenTests {
@Test
func registryCombinesManagementWorkspacesHostsAndActions() {
let host = Host(name: "Production", host: "prod.example", tags: ["critical"])
let store = HostLibraryModel(hosts: [host])
let app = AppModel(hosts: store)
let workspace = Workspace.sshSession(host: host)
let window = WindowModel(workspaces: [workspace])
let commands = app.commands.commands(app: app, window: window)
#expect(commands.contains { $0.target == .vaultPage(.hosts) })
#expect(commands.contains { $0.target == .vault })
#expect(commands.contains { $0.target == .assistant })
#expect(commands.contains { $0.target == .settings })
#expect(commands.contains { $0.target == .workspace(workspace.id) })
#expect(commands.contains { $0.target == .connectHost(host.id, .sshSession) })
#expect(commands.contains { $0.target == .newChat })
#expect(commands.contains { $0.target == .showAssistant })
#expect(commands.filter { $0.matches("critical") }.count == 2)
#expect(
commands.filter { $0.section == .navigation }.allSatisfy {
switch $0.target {
case .vaultPage, .vault, .assistant, .settings: true
default: false
}
})
#expect(
commands.filter { $0.section == .workspaces }.allSatisfy {
if case .workspace = $0.target { true } else { false }
})
#expect(commands.filter { $0.section == .conversations }.isEmpty)
#expect(
commands.filter { $0.section == .hosts }.allSatisfy {
if case .connectHost = $0.target { true } else { false }
})
#expect(
commands.filter { $0.section == .actions }.allSatisfy {
switch $0.target {
case .newChat, .newHost, .showAssistant: true
default: false
}
})
}
@Test
func registryIncludesActiveConversationsInLibraryOrderAndSearchesVisibleContent() throws {
let pinned = ConversationModel(
messages: [
ChatMessage(role: .assistant, text: "The nginx upstream is healthy.")
], sortIndex: 20)
pinned.customTitle = "Pinned deployment"
pinned.isPinned = true
let firstChat = ConversationModel(
messages: [
ChatMessage(role: .user, text: "Inspect the database vacuum schedule")
], sortIndex: 1)
firstChat.customTitle = "Database maintenance"
let secondChat = ConversationModel(
messages: [
ChatMessage(
role: .assistant, text: "The proxy is ready.",
reasoning: [AgentReasoningBlock(text: "hidden-token")]
)
], sortIndex: 2)
secondChat.customTitle = "NGINX rollout"
let archived = ConversationModel(
messages: [
ChatMessage(role: .user, text: "Archived nginx notes")
], sortIndex: 0)
archived.customTitle = "Archived nginx chat"
archived.archivedAt = .now
let library = AssistantLibraryModel(
store: AssistantConversationStore(
conversations: [secondChat, archived, pinned, firstChat]
))
let app = AppModel(hosts: HostLibraryModel())
let window = WindowModel(assistantLibrary: library)
let conversationCommands = app.commands.commands(app: app, window: window).filter {
$0.section == .conversations
}
#expect(
conversationCommands.map(\.title) == [
"Pinned deployment", "Database maintenance", "NGINX rollout",
])
#expect(Set(conversationCommands.map(\.id)).count == conversationCommands.count)
#expect(
conversationCommands.map(\.target) == [
.assistantConversation(pinned.id),
.assistantConversation(firstChat.id),
.assistantConversation(secondChat.id),
])
#expect(
!conversationCommands.contains { command in
command.target == .assistantConversation(archived.id)
})
let nginx = QuickOpenResults(commands: conversationCommands, query: "nginx")
#expect(
nginx.commands.map(\.target) == [
.assistantConversation(pinned.id),
.assistantConversation(secondChat.id),
])
#expect(QuickOpenResults(commands: conversationCommands, query: "NGINX").commands == nginx.commands)
#expect(QuickOpenResults(commands: conversationCommands, query: "hidden-token").commands.isEmpty)
}
@Test
func resultsGroupMatchesAndKeyboardSelectionSkipsHeaders() throws {
let host = Host(name: "Production", host: "prod.example", tags: ["critical"])
let store = HostLibraryModel(hosts: [host])
let app = AppModel(hosts: store)
let workspace = Workspace.sshSession(host: host)
let window = WindowModel(workspaces: [workspace])
let commands = app.commands.commands(app: app, window: window)
let results = QuickOpenResults(commands: commands, query: "")
#expect(results.sections == [.navigation, .workspaces, .hosts, .actions])
#expect(sectionItems(in: results.listItems(selectedID: nil)) == results.sections)
let first = try #require(results.commands.first)
let second = try #require(results.commands.dropFirst().first)
let last = try #require(results.commands.last)
#expect(results.selection(movingFrom: nil, by: 1) == first.id)
#expect(results.selection(movingFrom: first.id, by: -1) == first.id)
#expect(results.selection(movingFrom: first.id, by: 1) == second.id)
#expect(results.selection(movingFrom: last.id, by: 1) == last.id)
let filtered = QuickOpenResults(commands: commands, query: "critical")
#expect(filtered.sections == [.hosts])
#expect(filtered.commands.count == 2)
#expect(
sectionItems(in: filtered.listItems(selectedID: filtered.commands.first?.id)) == [
.hosts
])
#expect(filtered.listItems(selectedID: filtered.commands.first?.id).filter(\.isSelected).count == 1)
}
@Test
func registryRoutesWithoutDuplicatingNavigationLogic() {
let store = HostLibraryModel()
let app = AppModel(hosts: store)
let window = WindowModel()
#expect(
!app.commands.commands(app: app, window: window).contains {
$0.target == .showAssistant
}
)
app.commands.perform(.settings, app: app, window: window)
#expect(window.selectedDestination == .settings)
#expect(window.settingsRoute == .index)
#expect(window.workspaces.isEmpty)
app.commands.perform(.newChat, app: app, window: window)
#expect(window.selectedDestination == .assistant)
#expect(window.workspaces.isEmpty)
#expect(window.assistantLibrary.conversations.isEmpty)
#expect(window.assistantLibrary.selectedConversationID == nil)
}
@Test
func registryOpensConversationInMainAssistantAndLeavesArchive() {
let conversation = ConversationModel(messages: [
ChatMessage(role: .user, text: "Review deployment")
])
let library = AssistantLibraryModel(
store: AssistantConversationStore(
conversations: [conversation]
))
let app = AppModel(hosts: HostLibraryModel())
let window = WindowModel(assistantLibrary: library)
window.showAssistantArchive()
app.commands.perform(
.assistantConversation(conversation.id), app: app, window: window
)
#expect(window.selectedDestination == .assistant)
#expect(window.assistantLibrary.selectedConversationID == conversation.id)
#expect(!window.isAssistantArchivePresented)
}
private func sectionItems(in items: [QuickOpenListItem]) -> [AppCommandSection] {
items.compactMap { item in
guard case .section(let section) = item else { return nil }
return section
}
}
}