-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathCustomListsDataSourceTests.swift
104 lines (84 loc) · 3.8 KB
/
CustomListsDataSourceTests.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
//
// CustomListsDataSourceTests.swift
// MullvadVPNTests
//
// Created by Jon Petersson on 2024-02-29.
// Copyright © 2024 Mullvad VPN AB. All rights reserved.
//
@testable import MullvadSettings
@testable import MullvadTypes
import XCTest
class CustomListsDataSourceTests: XCTestCase {
var allLocationNodes = [LocationNode]()
var dataSource: CustomListsDataSource!
override func setUp() async throws {
createAllLocationNodes()
setUpDataSource()
}
func testNodeTree() throws {
let nodes = dataSource.nodes
let netflixNode = try XCTUnwrap(nodes.first(where: { $0.name == "Netflix" }))
XCTAssertNotNil(netflixNode.descendantNodeFor(codes: ["netflix", "es1-wireguard"]))
XCTAssertNotNil(netflixNode.descendantNodeFor(codes: ["netflix", "se"]))
XCTAssertNotNil(netflixNode.descendantNodeFor(codes: ["netflix", "us", "dal"]))
let youtubeNode = try XCTUnwrap(nodes.first(where: { $0.name == "Youtube" }))
XCTAssertNotNil(youtubeNode.descendantNodeFor(codes: ["youtube", "se2-wireguard"]))
XCTAssertNotNil(youtubeNode.descendantNodeFor(codes: ["youtube", "us", "dal"]))
}
func testParents() throws {
let listNode = try XCTUnwrap(dataSource.nodes.first(where: { $0.name == "Netflix" }))
let countryNode = try XCTUnwrap(listNode.descendantNodeFor(codes: ["netflix-se"]))
let cityNode = try XCTUnwrap(listNode.descendantNodeFor(codes: ["netflix-se-got"]))
let hostNode = try XCTUnwrap(listNode.descendantNodeFor(codes: ["netflix-se10-wireguard"]))
XCTAssertNil(listNode.parent)
XCTAssertEqual(countryNode.parent, listNode)
XCTAssertEqual(cityNode.parent, countryNode)
XCTAssertEqual(hostNode.parent, cityNode)
}
func testSearch() throws {
let nodes = dataSource.search(by: "got")
let rootNode = RootLocationNode(children: nodes)
XCTAssertTrue(rootNode.descendantNodeFor(codes: ["netflix", "se", "got"])?.isHiddenFromSearch == false)
XCTAssertTrue(rootNode.descendantNodeFor(codes: ["netflix", "se", "sto"])?.isHiddenFromSearch == true)
}
func testSearchWithEmptyText() throws {
let nodes = dataSource.search(by: "")
XCTAssertEqual(nodes, dataSource.nodes)
}
func testSearchYieldsNoListNodes() throws {
let nodes = dataSource.search(by: "net")
XCTAssertFalse(nodes.contains(where: { $0.name == "Netflix" }))
}
func testNodeByLocations() throws {
let relays = UserSelectedRelays(locations: [.hostname("es", "mad", "es1-wireguard")], customListSelection: nil)
let nodeByLocations = dataSource.node(by: relays, for: customLists.first!)
let nodeByCode = dataSource.nodes.first?.descendantNodeFor(codes: ["netflix", "es1-wireguard"])
XCTAssertEqual(nodeByLocations, nodeByCode)
}
}
extension CustomListsDataSourceTests {
private func setUpDataSource() {
dataSource = CustomListsDataSource(repository: CustomListsRepositoryStub(customLists: customLists))
dataSource.reload(allLocationNodes: allLocationNodes, isFiltered: false)
}
private func createAllLocationNodes() {
let response = ServerRelaysResponseStubs.sampleRelays
let relays = response.wireguard.relays
let dataSource = AllLocationDataSource()
dataSource.reload(response, relays: relays)
allLocationNodes = dataSource.nodes
}
var customLists: [CustomList] {
[
CustomList(name: "Netflix", locations: [
.hostname("es", "mad", "es1-wireguard"),
.country("se"),
.city("us", "dal"),
]),
CustomList(name: "Youtube", locations: [
.hostname("se", "sto", "se2-wireguard"),
.city("us", "dal"),
]),
]
}
}