Skip to content

Commit

Permalink
Add live ranking service tests
Browse files Browse the repository at this point in the history
  • Loading branch information
araszka committed Aug 9, 2024
1 parent 5d00798 commit e163431
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,19 @@ public interface ILiveRankingService
public Task RequestScoresAsync();

/// <summary>
/// Maps the given scores and displays the widget.
/// Maps the scores and displays the widget.
/// </summary>
/// <param name="scores"></param>
/// <returns></returns>
public Task MapScoresAndSendWidgetAsync(ScoresEventArgs scores);

/// <summary>
/// Maps the given ScoresEventArgs to LiveRankingPositions.
/// </summary>
/// <param name="scores"></param>
/// <returns></returns>
public Task<IEnumerable<LiveRankingPosition>> MapScores(ScoresEventArgs scores);

/// <summary>
/// Hides the live ranking widget for everyone.
/// </summary>
Expand Down
19 changes: 12 additions & 7 deletions src/Modules/LiveRankingModule/Services/LiveRankingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,19 @@ public Task RequestScoresAsync()

public async Task MapScoresAndSendWidgetAsync(ScoresEventArgs scores)
{
var liveRankingPositions = scores.Players.Take(settings.MaxWidgetRows)
.Where(score => score != null)
.OfType<PlayerScore>()
.Where(score => ScoreShouldBeDisplayedAsync(score).Result)
.Select(score => PlayerScoreToLiveRankingPositionAsync(score).Result);

await manialinkManager.SendPersistentManialinkAsync(WidgetTemplate,
new { settings, isPointsBased = _isPointsBased, scores = liveRankingPositions });
new { settings, isPointsBased = _isPointsBased, scores = await MapScores(scores) });
}

public Task<IEnumerable<LiveRankingPosition>> MapScores(ScoresEventArgs scores)
{
return Task.FromResult(
scores.Players.Take(settings.MaxWidgetRows)
.Where(score => score != null)
.OfType<PlayerScore>()
.Where(score => ScoreShouldBeDisplayedAsync(score).Result)
.Select(score => PlayerScoreToLiveRankingPositionAsync(score).Result)
);
}

public Task HideWidgetAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

namespace EvoSC.Modules.Official.LiveRankingModule.Tests.Controllers;

public class LiveRankingControllerTests : ControllerMock<LiveRankingEventController, IEventControllerContext>
public class LiveRankingEventControllerTests : ControllerMock<LiveRankingEventController, IEventControllerContext>
{
private Mock<ILiveRankingService> _liveRankingService = new();

public LiveRankingControllerTests()
public LiveRankingEventControllerTests()
{
InitMock(_liveRankingService);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
using EvoSC.Common.Interfaces;
using EvoSC.Common.Interfaces.Models;
using EvoSC.Common.Interfaces.Services;
using EvoSC.Common.Models;
using EvoSC.Common.Models.Callbacks;
using EvoSC.Common.Models.Players;
using EvoSC.Common.Remote.EventArgsModels;
using EvoSC.Common.Util.MatchSettings;
using EvoSC.Manialinks.Interfaces;
using EvoSC.Modules.Official.LiveRankingModule.Config;
using EvoSC.Modules.Official.LiveRankingModule.Interfaces;
using EvoSC.Modules.Official.LiveRankingModule.Models;
using EvoSC.Modules.Official.LiveRankingModule.Services;
using EvoSC.Testing;
using GbxRemoteNet.Interfaces;
using Moq;
using Xunit;

namespace EvoSC.Modules.Official.LiveRankingModule.Tests.Services;

public class LiveRankingServiceTests
{
private readonly Mock<IManialinkManager> _manialinkManager = new();
private readonly Mock<ILiveRankingSettings> _settings = new();
private readonly Mock<IPlayerManagerService> _playerManagerService = new();
private readonly Mock<IMatchSettingsService> _matchSettingsService = new();

private readonly (Mock<IServerClient> Client, Mock<IGbxRemoteClient> Remote)
_server = Mocking.NewServerClientMock();

private ILiveRankingService LiveRankingServiceMock()
{
return new LiveRankingService(
_manialinkManager.Object,
_server.Client.Object,
_settings.Object,
_playerManagerService.Object,
_matchSettingsService.Object
);
}

[Fact]
public async Task Detects_Mode_And_Requests_Scores()
{
await LiveRankingServiceMock().DetectModeAndRequestScoreAsync();

_server.Remote.Verify(
remote => remote.TriggerModeScriptEventArrayAsync("Trackmania.GetScores"),
Times.Once
);
}

[Fact]
public async Task Requests_Scores_When_Asked()
{
await LiveRankingServiceMock().RequestScoresAsync();

_server.Remote.Verify(
remote => remote.TriggerModeScriptEventArrayAsync("Trackmania.GetScores"),
Times.Once
);
}

[Theory]
[InlineData(0, 0, DefaultModeScriptName.TimeAttack, 0)]
[InlineData(0, 0, DefaultModeScriptName.Rounds, 0)]
[InlineData(0, 10, DefaultModeScriptName.Rounds, 1)]
[InlineData(0, 20, DefaultModeScriptName.TimeAttack, 0)]
[InlineData(30, 0, DefaultModeScriptName.TimeAttack, 1)]
[InlineData(40, 0, DefaultModeScriptName.Rounds, 0)]
public async Task Maps_Scores_To_LiveRankingPositions(int bestRaceTime, int matchPoints,
DefaultModeScriptName modeScriptName,
int expectedLiveRankingPositionCount)
{
var liveRankingService = LiveRankingServiceMock();
var player = new Player { AccountId = "UnitTest", NickName = "unit_test", UbisoftName = "unittest"};
var scoresEventArgs = new ScoresEventArgs
{
Section = ModeScriptSection.Undefined,
UseTeams = false,
WinnerTeam = 0,
WinnerPlayer = null,
Teams = new List<TeamScore?>(),
Players = new List<PlayerScore?>
{
new()
{
AccountId = player.AccountId,
Name = player.UbisoftName,
BestRaceTime = bestRaceTime,
MatchPoints = matchPoints,
Rank = 1
}
}
};

_matchSettingsService.Setup(m => m.GetCurrentModeAsync())
.Returns(Task.FromResult(modeScriptName));

_playerManagerService.Setup(p => p.GetPlayerAsync("UnitTest"))
.Returns(Task.FromResult((IPlayer?)player));

await liveRankingService.DetectModeAndRequestScoreAsync();

if (modeScriptName == DefaultModeScriptName.TimeAttack)
{
Assert.False(await liveRankingService.CurrentModeIsPointsBasedAsync());
}
else
{
Assert.True(await liveRankingService.CurrentModeIsPointsBasedAsync());
}

if (expectedLiveRankingPositionCount > 0)
{
Assert.True(await liveRankingService.ScoreShouldBeDisplayedAsync(scoresEventArgs.Players.First()));
}
else
{
Assert.False(await liveRankingService.ScoreShouldBeDisplayedAsync(scoresEventArgs.Players.First()));
}

var mappedScores = await liveRankingService.MapScores(scoresEventArgs);

Assert.IsAssignableFrom<IEnumerable<LiveRankingPosition>>(mappedScores);
}

[Fact]
public async Task Maps_Scores_And_Displays_Widget()
{
var liveRankingService = LiveRankingServiceMock();
var scoresEventArgs = new ScoresEventArgs
{
Section = ModeScriptSection.Undefined,
UseTeams = false,
WinnerTeam = 0,
WinnerPlayer = null,
Teams = new List<TeamScore?>(),
Players = new List<PlayerScore?>()
};

await liveRankingService.MapScoresAndSendWidgetAsync(scoresEventArgs);

_manialinkManager.Verify(
m => m.SendPersistentManialinkAsync("LiveRankingModule.LiveRanking", It.IsAny<object>()),
Times.Once
);
}
}

0 comments on commit e163431

Please sign in to comment.