/*
* Copyright (c) 2026 Proton AG
*
* This file is part of ProtonVPN.
*
* ProtonVPN is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ProtonVPN is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ProtonVPN. If not, see .
*/
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NSubstitute;
using ProtonVPN.Client.Localization.Contracts;
using ProtonVPN.Client.Logic.Servers.Contracts;
using ProtonVPN.Client.Logic.Servers.Contracts.Enums;
using ProtonVPN.Client.Logic.Servers.Contracts.Models;
using ProtonVPN.Client.Settings.Contracts;
namespace ProtonVPN.Client.Logic.Searches.Tests;
[TestClass]
public partial class GlobalSearchTest
{
private IServersLoader? _serversLoader;
private ILocalizationProvider? _localizer;
private GlobalSearch? _globalSearch;
[TestInitialize]
public void Initialize()
{
_serversLoader = Substitute.For();
List standardServers = new()
{
CreateMockOfServer("DZ#123", "Argel", "", "DZ", default(ServerFeatures)),
CreateMockOfServer("CA#234", "Ottawa", "", "CA", default(ServerFeatures)),
CreateMockOfServer("US-AK#345", "Anchorage", "Alaska", "US", default(ServerFeatures)),
CreateMockOfServer("US-FL#456", "Jacksonville", "Florida", "US", default(ServerFeatures)),
CreateMockOfServer("US-MI#567", "Detroit", "Michigan", "US", default(ServerFeatures)),
CreateMockOfServer("CH#678", "Zurich", "", "CH", default(ServerFeatures)),
CreateMockOfServer("CL#789", "Santiago", "", "CL", default(ServerFeatures)),
};
List secureCoreServers = new()
{
CreateMockOfServer("US-WI#123", "Milwaukee", "Wisconsin", "US", ServerFeatures.SecureCore),
CreateMockOfServer("US-IL#234", "Chicago", "Illinois", "US", ServerFeatures.SecureCore),
CreateMockOfServer("IS#345", "Reykjavík", "", "IS", ServerFeatures.SecureCore),
CreateMockOfServer("PK#456", "Islamabad", "", "PK", ServerFeatures.SecureCore),
};
_serversLoader.GetServers().Returns(standardServers.Concat(secureCoreServers));
_serversLoader.GetServersByFeatures(Arg.Is(ServerFeatures.SecureCore)).Returns(secureCoreServers);
List standardCities = new()
{
CreateMockOfCity("Argel", "", "DZ", default(ServerFeatures)),
CreateMockOfCity("Ottawa", "", "CA", default(ServerFeatures)),
CreateMockOfCity("Anchorage", "Alaska", "US", default(ServerFeatures)),
CreateMockOfCity("Jacksonville", "Florida", "US", default(ServerFeatures)),
CreateMockOfCity("Detroit", "Michigan", "US", default(ServerFeatures)),
CreateMockOfCity("Zurich", "", "CH", default(ServerFeatures)),
CreateMockOfCity("Santiago", "", "CL", default(ServerFeatures)),
};
List secureCoreCities = new()
{
CreateMockOfCity("Milwaukee", "Wisconsin", "US", ServerFeatures.SecureCore),
CreateMockOfCity("Chicago", "Illinois", "US", ServerFeatures.SecureCore),
CreateMockOfCity("Reykjavík", "", "IS", ServerFeatures.SecureCore),
CreateMockOfCity("Islamabad", "", "PK", ServerFeatures.SecureCore),
};
_serversLoader.GetCities().Returns(standardCities.Concat(secureCoreCities));
_serversLoader.GetCitiesByFeatures(Arg.Is(ServerFeatures.SecureCore)).Returns(secureCoreCities);
List standardStates = new()
{
CreateMockOfState("Alaska", "US", default(ServerFeatures)),
CreateMockOfState("Florida", "US", default(ServerFeatures)),
CreateMockOfState("Michigan", "US", default(ServerFeatures)),
};
List secureCoreStates = new()
{
CreateMockOfState("Wisconsin", "US", ServerFeatures.SecureCore),
CreateMockOfState("Illinois", "US", ServerFeatures.SecureCore),
};
_serversLoader.GetStates().Returns(standardStates.Concat(secureCoreStates));
_serversLoader.GetStatesByFeatures(Arg.Is(ServerFeatures.SecureCore)).Returns(secureCoreStates);
List standardCountries = new()
{
CreateMockOfCountry("AE", default(ServerFeatures)),
CreateMockOfCountry("DZ", default(ServerFeatures)),
CreateMockOfCountry("CA", default(ServerFeatures)),
CreateMockOfCountry("US", default(ServerFeatures)),
CreateMockOfCountry("CH", default(ServerFeatures)),
CreateMockOfCountry("CL", default(ServerFeatures)),
};
List secureCoreCountries = new()
{
CreateMockOfCountry("IS", ServerFeatures.SecureCore),
CreateMockOfCountry("PK", default(ServerFeatures)),
};
_serversLoader.GetCountries().Returns(standardCountries.Concat(secureCoreCountries));
_serversLoader.GetCountriesByFeatures(Arg.Is(ServerFeatures.SecureCore)).Returns(secureCoreCountries);
_localizer = Substitute.For();
_localizer.Get("Country_val_AE").Returns("United Arab Emirates");
_localizer.Get("Country_val_DZ").Returns("Algeria");
_localizer.Get("Country_val_CA").Returns("Canada");
_localizer.Get("Country_val_US").Returns("United States");
_localizer.Get("Country_val_CH").Returns("Switzerland");
_localizer.Get("Country_val_CL").Returns("Chile");
_localizer.Get("Country_val_IS").Returns("Iceland");
_localizer.Get("Country_val_PK").Returns("Pakistan");
_localizer.GetCityName(Arg.Any(), Arg.Any())
.Returns(callInfo => callInfo.ArgAt(0) ?? string.Empty);
_localizer.GetStateName(Arg.Any(), Arg.Any())
.Returns(callInfo => callInfo.ArgAt(0) ?? string.Empty);
_globalSearch = new GlobalSearch(_serversLoader, _localizer);
}
private Server CreateMockOfServer(string name, string city, string state, string countryCode, ServerFeatures features)
{
return new Server()
{
Id = Guid.NewGuid().ToString(),
Name = name,
City = city,
State = state,
EntryCountry = countryCode,
ExitCountry = countryCode,
HostCountry = "",
Domain = "test",
GatewayName = "",
Servers = [],
Features = features,
StatusReference = new()
{
Index = 0,
Cost = 0,
Penalty = 0,
},
EntryLocation = new()
{
Latitude = 0,
Longitude = 0,
},
ExitLocation = new()
{
Latitude = 0,
Longitude = 0,
},
};
}
private City CreateMockOfCity(string name, string stateName, string countryCode, ServerFeatures features)
{
return new City()
{
Name = name,
StateName = stateName,
CountryCode = countryCode,
IsStandardUnderMaintenance = false,
IsP2PUnderMaintenance = false,
IsSecureCoreUnderMaintenance = false,
IsTorUnderMaintenance = false,
Features = features,
};
}
private State CreateMockOfState(string name, string countryCode, ServerFeatures features)
{
return new State()
{
Name = name,
CountryCode = countryCode,
IsStandardUnderMaintenance = false,
IsP2PUnderMaintenance = false,
IsSecureCoreUnderMaintenance = false,
IsTorUnderMaintenance = false,
Features = features,
};
}
private Country CreateMockOfCountry(string countryCode, ServerFeatures features)
{
return new Country()
{
Code = countryCode,
IsStandardUnderMaintenance = false,
IsP2PUnderMaintenance = false,
IsSecureCoreUnderMaintenance = false,
IsTorUnderMaintenance = false,
Features = features,
};
}
[TestCleanup]
public void Cleanup()
{
_serversLoader = null;
_localizer = null;
_globalSearch = null;
}
}