Repository navigation
Expand file tree
/
Copy pathFilePickerTests.swift
More file actions
172 lines (148 loc) · 6.22 KB
/
Copy pathFilePickerTests.swift
File metadata and controls
172 lines (148 loc) · 6.22 KB
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
@testable import Coder_Desktop
@testable import CoderSDK
import Mocker
import SwiftUI
import Testing
import ViewInspector
@MainActor
@Suite(.timeLimit(.minutes(1)))
struct FilePickerTests {
let mockResponse: LSResponse
init() {
mockResponse = LSResponse(
absolute_path: ["/"],
absolute_path_string: "/",
contents: [
LSFile(name: "home", absolute_path_string: "/home", is_dir: true),
LSFile(name: "tmp", absolute_path_string: "/tmp", is_dir: true),
LSFile(name: "etc", absolute_path_string: "/etc", is_dir: true),
LSFile(name: "README.md", absolute_path_string: "/README.md", is_dir: false),
]
)
}
@Test
func testLoadError() async throws {
let host = "test-error.coder"
let sut = FilePicker(host: host, outputAbsPath: .constant(""))
let view = sut
let url = URL(string: "http://\(host):4")!
let errorMessage = "Connection failed"
Mock(
url: url.appendingPathComponent("/api/v0/list-directory"),
contentType: .json,
statusCode: 500,
data: [.post: errorMessage.data(using: .utf8)!]
).register()
try await ViewHosting.host(view) {
try await sut.inspection.inspect { view in
try #expect(await eventually { @MainActor in
let text = try view.find(ViewType.Text.self)
return try text.string().contains("Connection failed")
})
}
}
}
@Test
func testSuccessfulFileLoad() async throws {
let host = "test-success.coder"
let sut = FilePicker(host: host, outputAbsPath: .constant(""))
let view = sut
let url = URL(string: "http://\(host):4")!
try Mock(
url: url.appendingPathComponent("/api/v0/list-directory"),
statusCode: 200,
data: [.post: CoderSDK.encoder.encode(mockResponse)]
).register()
try await ViewHosting.host(view) {
try await sut.inspection.inspect { view in
try #expect(await eventually { @MainActor in
_ = try view.find(ViewType.List.self)
return true
})
_ = try view.find(text: "README.md")
_ = try view.find(text: "home")
let selectButton = try view.find(button: "Select")
#expect(selectButton.isDisabled())
}
}
}
@Test
func testHiddenFilesToggle() async throws {
let key = "ShowHiddenFilesInRemoteFilePicker"
UserDefaults.standard.removeObject(forKey: key)
defer { UserDefaults.standard.removeObject(forKey: key) }
let hiddenMockResponse = LSResponse(
absolute_path: ["/"],
absolute_path_string: "/",
contents: [
LSFile(name: ".mux", absolute_path_string: "/.mux", is_dir: true),
LSFile(name: "home", absolute_path_string: "/home", is_dir: true),
LSFile(name: "README.md", absolute_path_string: "/README.md", is_dir: false),
]
)
let host = "test-hidden-toggle.coder"
let sut = FilePicker(host: host, outputAbsPath: .constant(""))
let view = sut
let url = URL(string: "http://\(host):4")!
try Mock(
url: url.appendingPathComponent("/api/v0/list-directory"),
statusCode: 200,
data: [.post: CoderSDK.encoder.encode(hiddenMockResponse)]
).register()
try await ViewHosting.host(view) {
try await sut.inspection.inspect { view in
try #expect(await eventually { @MainActor in
_ = try view.find(ViewType.List.self)
return true
})
_ = try view.find(text: "README.md")
var toggle = try view.find(ViewType.Toggle.self)
#expect(try toggle.labelView().text().string() == "Show hidden files")
#expect(try !toggle.isOn())
#expect(throws: (any Error).self) { _ = try view.find(text: ".mux") }
#expect(view.findAll(ViewType.DisclosureGroup.self).count == 1)
try toggle.tap()
toggle = try view.find(ViewType.Toggle.self)
#expect(try toggle.isOn())
#expect(UserDefaults.standard.bool(forKey: key))
_ = try view.find(text: ".mux")
#expect(view.findAll(ViewType.DisclosureGroup.self).count == 2)
try toggle.tap()
toggle = try view.find(ViewType.Toggle.self)
#expect(try !toggle.isOn())
#expect(!UserDefaults.standard.bool(forKey: key))
#expect(throws: (any Error).self) { _ = try view.find(text: ".mux") }
#expect(view.findAll(ViewType.DisclosureGroup.self).count == 1)
}
}
}
@Test
func testDirectoryExpansion() async throws {
let host = "test-expansion.coder"
let sut = FilePicker(host: host, outputAbsPath: .constant(""))
let view = sut
let url = URL(string: "http://\(host):4")!
try Mock(
url: url.appendingPathComponent("/api/v0/list-directory"),
statusCode: 200,
data: [.post: CoderSDK.encoder.encode(mockResponse)]
).register()
try await ViewHosting.host(view) {
try await sut.inspection.inspect { view in
try #expect(await eventually { @MainActor in
_ = try view.find(ViewType.List.self)
return true
})
let disclosureGroup = try view.find(ViewType.DisclosureGroup.self)
#expect(view.findAll(ViewType.DisclosureGroup.self).count == 3)
try disclosureGroup.expand()
// Disclosure group should expand out to 3 more directories
#expect(await eventually { @MainActor in
return view.findAll(ViewType.DisclosureGroup.self).count == 6
})
}
}
}
// TODO: The writing of more extensive tests is blocked by ViewInspector,
// as it can't select an item in a list...
}