summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPNTests/MullvadVPN/View controllers/SelectLocation/RecentConnectionsRepositoryTests.swift
blob: 2d63e645636cb25dbaf7fbc909e0a8226a03dc80 (plain)
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
//
//  RecentConnectionsRepositoryTests.swift
//  MullvadVPN
//
//  Created by Mojgan on 2025-09-25.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//
import Testing

@testable import MullvadSettings
@testable import MullvadTypes

@Suite("RecentConnectionsRepositoryTests")
final class RecentConnectionsRepositoryTests {
    let se = UserSelectedRelays(locations: [.country("se")])
    let fr = UserSelectedRelays(locations: [.country("fr")])
    let nl = UserSelectedRelays(locations: [.country("nl")])
    let de = UserSelectedRelays(locations: [.country("de")])

    @Test("Adds locations up to the limit 1 for either entry or exit")
    func addLocations() throws {
        let maxLimit: UInt = 1
        let repository = makeRepository(max: maxLimit)
        try repository.setRecentsEnabled(true)
        try addLocations(repository, locations: [se, de], as: .entry)
        try addLocations(repository, locations: [de], as: .exit)

        let recentsSettings = try repository.all()
        #expect(recentsSettings.isEnabled)
        #expect(recentsSettings.exitLocations.count == maxLimit)
        #expect(recentsSettings.entryLocations.count == maxLimit)
    }

    @Test("Adds locations up to the default limit (50) for either entry or exit")
    func addDuplicate() throws {
        let repository = makeRepository()
        try repository.setRecentsEnabled(true)
        try addLocations(repository, locations: [se, de], as: .entry)
        try addLocations(repository, locations: [de, se, nl, se], as: .exit)

        let recentsSettings = try repository.all()
        #expect(recentsSettings.isEnabled)
        #expect(recentsSettings.entryLocations.count == 2)
        #expect(recentsSettings.exitLocations.count == 3)
    }

    @Test("Removes all recents connections with disabling recents")
    func disable() throws {
        let repository = makeRepository()
        try repository.setRecentsEnabled(true)
        try addLocations(repository, locations: [se, de], as: .entry)
        try addLocations(repository, locations: [de, se, nl], as: .exit)

        var recentConnections = try repository.all()
        #expect(recentConnections.isEnabled)
        #expect(recentConnections.entryLocations.count == 2)
        #expect(recentConnections.exitLocations.count == 3)

        try repository.setRecentsEnabled(false)

        recentConnections = try repository.all()
        #expect(!recentConnections.isEnabled)
        #expect(recentConnections.entryLocations.count == 0)
        #expect(recentConnections.exitLocations.count == 0)

    }

    @Test("Fails with an error if a location is added while recents are disabled.")
    func addRecentsBeforeEnablingRecents() throws {
        let repository = makeRepository()

        try repository.setRecentsEnabled(false)
        let action: () throws -> Void = { [self] in
            try addLocations(
                repository,
                locations: [self.se],
                as: RecentLocationType.entry
            )
        }

        #expect(throws: RecentConnectionsRepositoryError.recentsDisabled, performing: action)
    }

    private func makeRepository(max: UInt = 50) -> RecentConnectionsRepository {
        return RecentConnectionsRepository(store: InMemorySettingsStore<SettingNotFound>(), maxLimit: max)
    }

    private func addLocations(
        _ repository: RecentConnectionsRepository, locations: [UserSelectedRelays], as type: RecentLocationType
    ) throws {
        for location in locations {
            try repository.add(location, as: type)
        }
    }
}