-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVPNMenuStateTests.swift
211 lines (174 loc) · 6.68 KB
/
VPNMenuStateTests.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
@testable import Coder_Desktop
import Testing
@testable import VPNLib
@MainActor
@Suite
struct VPNMenuStateTests {
var state = VPNMenuState()
@Test
mutating func testUpsertAgent_addsAgent() async throws {
let agentID = UUID()
let workspaceID = UUID()
state.upsertWorkspace(Vpn_Workspace.with { $0.id = workspaceID.uuidData; $0.name = "foo" })
let agent = Vpn_Agent.with {
$0.id = agentID.uuidData
$0.workspaceID = workspaceID.uuidData
$0.name = "dev"
$0.lastHandshake = .init(date: Date.now)
$0.fqdn = ["foo.coder"]
}
state.upsertAgent(agent)
let storedAgent = try #require(state.agents[agentID])
#expect(storedAgent.name == "dev")
#expect(storedAgent.wsID == workspaceID)
#expect(storedAgent.wsName == "foo")
#expect(storedAgent.primaryHost == "foo.coder")
#expect(storedAgent.status == .okay)
}
@Test
mutating func testDeleteAgent_removesAgent() async throws {
let agentID = UUID()
let workspaceID = UUID()
state.upsertWorkspace(Vpn_Workspace.with { $0.id = workspaceID.uuidData; $0.name = "foo" })
let agent = Vpn_Agent.with {
$0.id = agentID.uuidData
$0.workspaceID = workspaceID.uuidData
$0.name = "agent1"
$0.lastHandshake = .init(date: Date.now)
$0.fqdn = ["foo.coder"]
}
state.upsertAgent(agent)
state.deleteAgent(withId: agent.id)
#expect(state.agents[agentID] == nil)
}
@Test
mutating func testDeleteWorkspace_removesWorkspaceAndAgents() async throws {
let agentID = UUID()
let workspaceID = UUID()
state.upsertWorkspace(Vpn_Workspace.with { $0.id = workspaceID.uuidData; $0.name = "foo" })
let agent = Vpn_Agent.with {
$0.id = agentID.uuidData
$0.workspaceID = workspaceID.uuidData
$0.name = "agent1"
$0.lastHandshake = .init(date: Date.now)
$0.fqdn = ["foo.coder"]
}
state.upsertAgent(agent)
state.deleteWorkspace(withId: workspaceID.uuidData)
#expect(state.agents[agentID] == nil)
#expect(state.workspaces[workspaceID] == nil)
}
@Test
mutating func testUpsertAgent_unhealthyAgent() async throws {
let agentID = UUID()
let workspaceID = UUID()
state.upsertWorkspace(Vpn_Workspace.with { $0.id = workspaceID.uuidData; $0.name = "foo" })
let agent = Vpn_Agent.with {
$0.id = agentID.uuidData
$0.workspaceID = workspaceID.uuidData
$0.name = "agent1"
$0.lastHandshake = .init(date: Date.now.addingTimeInterval(-600))
$0.fqdn = ["foo.coder"]
}
state.upsertAgent(agent)
let storedAgent = try #require(state.agents[agentID])
#expect(storedAgent.status == .warn)
}
@Test
mutating func testUpsertAgent_replacesOldAgent() async throws {
let workspaceID = UUID()
let oldAgentID = UUID()
let newAgentID = UUID()
state.upsertWorkspace(Vpn_Workspace.with { $0.id = workspaceID.uuidData; $0.name = "foo" })
let oldAgent = Vpn_Agent.with {
$0.id = oldAgentID.uuidData
$0.workspaceID = workspaceID.uuidData
$0.name = "agent1"
$0.lastHandshake = .init(date: Date.now.addingTimeInterval(-600))
$0.fqdn = ["foo.coder"]
}
state.upsertAgent(oldAgent)
let newAgent = Vpn_Agent.with {
$0.id = newAgentID.uuidData
$0.workspaceID = workspaceID.uuidData
$0.name = "agent1" // Same name as old agent
$0.lastHandshake = .init(date: Date.now)
$0.fqdn = ["foo.coder"]
}
state.upsertAgent(newAgent)
#expect(state.agents[oldAgentID] == nil)
let storedAgent = try #require(state.agents[newAgentID])
#expect(storedAgent.name == "agent1")
#expect(storedAgent.wsID == workspaceID)
#expect(storedAgent.primaryHost == "foo.coder")
#expect(storedAgent.status == .okay)
}
@Test
mutating func testUpsertWorkspace_addsOfflineWorkspace() async throws {
let workspaceID = UUID()
state.upsertWorkspace(Vpn_Workspace.with { $0.id = workspaceID.uuidData; $0.name = "foo" })
let storedWorkspace = try #require(state.workspaces[workspaceID])
#expect(storedWorkspace.name == "foo")
var output = state.sorted
#expect(output.count == 1)
#expect(output[0].id == workspaceID)
#expect(output[0].wsName == "foo")
let agentID = UUID()
let agent = Vpn_Agent.with {
$0.id = agentID.uuidData
$0.workspaceID = workspaceID.uuidData
$0.name = "agent1"
$0.lastHandshake = .init(date: Date.now.addingTimeInterval(-200))
$0.fqdn = ["foo.coder"]
}
state.upsertAgent(agent)
output = state.sorted
#expect(output.count == 1)
#expect(output[0].id == agentID)
#expect(output[0].wsName == "foo")
#expect(output[0].status == .okay)
}
@Test
mutating func testUpsertAgent_invalidAgent_noUUID() async throws {
let agent = Vpn_Agent.with {
$0.name = "invalidAgent"
$0.fqdn = ["invalid.coder"]
}
state.upsertAgent(agent)
#expect(state.agents.isEmpty)
#expect(state.invalidAgents.count == 1)
}
@Test
mutating func testUpsertAgent_outOfOrder() async throws {
let agentID = UUID()
let workspaceID = UUID()
let agent = Vpn_Agent.with {
$0.id = agentID.uuidData
$0.workspaceID = workspaceID.uuidData
$0.name = "orphanAgent"
$0.lastHandshake = .init(date: Date.now)
$0.fqdn = ["orphan.coder"]
}
state.upsertAgent(agent)
#expect(state.agents.isEmpty)
state.upsertWorkspace(Vpn_Workspace.with { $0.id = workspaceID.uuidData; $0.name = "validWorkspace" })
#expect(state.agents.count == 1)
}
@Test
mutating func testDeleteInvalidAgent_removesInvalid() async throws {
let agentID = UUID()
let workspaceID = UUID()
let agent = Vpn_Agent.with {
$0.id = agentID.uuidData
$0.workspaceID = workspaceID.uuidData
$0.name = "invalidAgent"
$0.lastHandshake = .init(date: Date.now)
$0.fqdn = ["invalid.coder"]
}
state.upsertAgent(agent)
#expect(state.agents.isEmpty)
state.deleteAgent(withId: agentID.uuidData)
#expect(state.agents.isEmpty)
#expect(state.invalidAgents.isEmpty)
}
}