-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAgentsTests.swift
122 lines (105 loc) · 4.1 KB
/
AgentsTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
@testable import Coder_Desktop
import SwiftUI
import Testing
import ViewInspector
@MainActor
@Suite(.timeLimit(.minutes(1)))
struct AgentsTests {
let vpn: MockVPNService
let state: AppState
let sut: Agents<MockVPNService>
let view: any View
init() {
vpn = MockVPNService()
state = AppState(persistent: false)
state.login(baseAccessURL: URL(string: "https://coder.example.com")!, sessionToken: "fake-token")
sut = Agents<MockVPNService>()
view = sut.environmentObject(vpn).environmentObject(state)
}
private func createMockAgents(count: Int, status: AgentStatus = .okay) -> [UUID: Agent] {
Dictionary(uniqueKeysWithValues: (1 ... count).map {
let agent = Agent(
id: UUID(),
name: "dev",
status: status,
hosts: ["a\($0).coder"],
wsName: "ws\($0)",
wsID: UUID(),
primaryHost: "a\($0).coder"
)
return (agent.id, agent)
})
}
@Test
func agentsWhenVPNOff() throws {
vpn.state = .disabled
#expect(throws: (any Error).self) {
_ = try view.inspect().find(ViewType.ForEach.self)
}
}
@Test func noAgents() async throws {
vpn.state = .connected
vpn.menuState = .init(agents: [:])
try await ViewHosting.host(view) {
try await sut.inspection.inspect { view in
#expect(throws: Never.self) { try view.find(text: "No workspaces!") }
}
}
}
@Test
func agentsWhenVPNOn() throws {
vpn.state = .connected
vpn.menuState = .init(agents: createMockAgents(count: Theme.defaultVisibleAgents + 2))
let forEach = try view.inspect().find(ViewType.ForEach.self)
#expect(forEach.count == Theme.defaultVisibleAgents)
// Agents are sorted by status, and then by name in alphabetical order
#expect(throws: Never.self) { try view.inspect().find(link: "a1.coder") }
}
@Test
func showAllToggle() async throws {
vpn.state = .connected
vpn.menuState = .init(agents: createMockAgents(count: 7))
try await ViewHosting.host(view) {
try await sut.inspection.inspect { view in
var toggle = try view.find(ViewType.Toggle.self)
var forEach = try view.find(ViewType.ForEach.self)
#expect(forEach.count == Theme.defaultVisibleAgents)
#expect(try toggle.labelView().text().string() == "Show all")
#expect(try !toggle.isOn())
try toggle.tap()
toggle = try view.find(ViewType.Toggle.self)
forEach = try view.find(ViewType.ForEach.self)
#expect(forEach.count == Theme.defaultVisibleAgents + 2)
#expect(try toggle.labelView().text().string() == "Show less")
try toggle.tap()
toggle = try view.find(ViewType.Toggle.self)
forEach = try view.find(ViewType.ForEach.self)
#expect(try toggle.labelView().text().string() == "Show all")
#expect(forEach.count == Theme.defaultVisibleAgents)
}
}
}
@Test
func noToggleFewAgents() throws {
vpn.state = .connected
vpn.menuState = .init(agents: createMockAgents(count: 3))
#expect(throws: (any Error).self) {
_ = try view.inspect().find(ViewType.Toggle.self)
}
}
@Test
func showOfflineWorkspace() async throws {
vpn.state = .connected
vpn.menuState = .init(
agents: createMockAgents(count: Theme.defaultVisibleAgents - 1),
workspaces: [UUID(): Workspace(id: UUID(), name: "offline", agents: .init())]
)
try await ViewHosting.host(view) {
try await sut.inspection.inspect { view in
let forEach = try view.find(ViewType.ForEach.self)
#expect(forEach.count == Theme.defaultVisibleAgents)
#expect(throws: Never.self) { try view.find(link: "offline.coder") }
}
}
}
}