Adds music stack test coverage and fixes playlist cache and stream error handling

This commit is contained in:
BinaryAlley committed 2026-10-29 15:46:24 +02:00
1 parent 394f6b510c
commit aba803b106
197 files changed
+29081 -34

No files matched your search

@@ -0,0 +1,91 @@
#region ========================================================================= USING =====================================================================================
using FastEndpoints;
using Lumina.Presentation.Web.Common.Api;
using Lumina.Presentation.Web.Common.DTO.UsersManagement;
using Lumina.Presentation.Web.Common.Routes;
using Lumina.Presentation.Web.Core.Endpoints.Tools.Settings.GetPlaybackState;
using Lumina.Presentation.Web.Fixtures.Common.DTO.UsersManagement;
using Lumina.Presentation.Web.Fixtures.Common.TestHelpers;
using Microsoft.AspNetCore.Http;
using NSubstitute;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Presentation.Web.UnitTests.Core.Endpoints.Tools.Settings.GetPlaybackState;
/// <summary>
/// Contains unit tests for the <see cref="GetPlaybackStateEndpoint"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class GetPlaybackStateEndpointTests
{
private readonly IApiHttpClient _mockApiHttpClient;
private readonly GetPlaybackStateEndpoint _sut;
private readonly PlaybackStateDtoFixture _playbackStateDtoFixture = new();
/// <summary>
/// Initializes a new instance of the <see cref="GetPlaybackStateEndpointTests"/> class.
/// </summary>
public GetPlaybackStateEndpointTests()
{
_mockApiHttpClient = Substitute.For<IApiHttpClient>();
_sut = Factory.Create<GetPlaybackStateEndpoint>(_mockApiHttpClient);
}
[Fact]
public async Task ExecuteAsync_WhenApiReturnsState_ShouldReturnSuccessJsonWithState()
{
// Arrange
PlaybackStateDto expectedState = _playbackStateDtoFixture.Create(volume: 0.35, isMuted: true);
_mockApiHttpClient.GetAsync<PlaybackStateDto>(ApiRoutes.Users.GET_PLAYBACK_STATE, Arg.Any<CancellationToken>())
.Returns(expectedState);
// Act
IResult result = await _sut.ExecuteAsync(EmptyRequest.Instance, CancellationToken.None);
string body = await JsonResultTestHelper.GetResponseBodyAsync(result);
// Assert
using JsonDocument jsonDocument = JsonDocument.Parse(body);
Assert.True(jsonDocument.RootElement.GetProperty("success").GetBoolean());
JsonElement data = jsonDocument.RootElement.GetProperty("data");
Assert.Equal(0.35, data.GetProperty("volume").GetDouble());
Assert.True(data.GetProperty("isMuted").GetBoolean());
}
[Fact]
public async Task ExecuteAsync_WhenCalled_ShouldRequestPlaybackStateFromApi()
{
// Arrange
_mockApiHttpClient.GetAsync<PlaybackStateDto>(ApiRoutes.Users.GET_PLAYBACK_STATE, Arg.Any<CancellationToken>())
.Returns(_playbackStateDtoFixture.Create());
// Act
await _sut.ExecuteAsync(EmptyRequest.Instance, CancellationToken.None);
// Assert
await _mockApiHttpClient.Received(1).GetAsync<PlaybackStateDto>(ApiRoutes.Users.GET_PLAYBACK_STATE, Arg.Any<CancellationToken>());
}
[Fact]
public async Task ExecuteAsync_WhenStateHasNullValues_ShouldStillReturnSuccessJson()
{
// Arrange
PlaybackStateDto expectedState = _playbackStateDtoFixture.Create(includeVolume: false, includeIsMuted: false);
_mockApiHttpClient.GetAsync<PlaybackStateDto>(ApiRoutes.Users.GET_PLAYBACK_STATE, Arg.Any<CancellationToken>())
.Returns(expectedState);
// Act
IResult result = await _sut.ExecuteAsync(EmptyRequest.Instance, CancellationToken.None);
string body = await JsonResultTestHelper.GetResponseBodyAsync(result);
// Assert
using JsonDocument jsonDocument = JsonDocument.Parse(body);
Assert.True(jsonDocument.RootElement.GetProperty("success").GetBoolean());
JsonElement data = jsonDocument.RootElement.GetProperty("data");
Assert.Equal(JsonValueKind.Null, data.GetProperty("volume").ValueKind);
Assert.Equal(JsonValueKind.Null, data.GetProperty("isMuted").ValueKind);
}
}
@@ -0,0 +1,97 @@
#region ========================================================================= USING =====================================================================================
using FastEndpoints;
using Lumina.Presentation.Web.Common.Api;
using Lumina.Presentation.Web.Common.DTO.UsersManagement;
using Lumina.Presentation.Web.Common.Requests.Common;
using Lumina.Presentation.Web.Common.Routes;
using Lumina.Presentation.Web.Core.Endpoints.Tools.Settings.UpdatePlaybackState;
using Lumina.Presentation.Web.Fixtures.Common.DTO.UsersManagement;
using Lumina.Presentation.Web.Fixtures.Common.TestHelpers;
using Microsoft.AspNetCore.Http;
using NSubstitute;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using WebEmptyRequest = Lumina.Presentation.Web.Common.Requests.Common.EmptyRequest;
#endregion
namespace Lumina.Presentation.Web.UnitTests.Core.Endpoints.Tools.Settings.UpdatePlaybackState;
/// <summary>
/// Contains unit tests for the <see cref="UpdatePlaybackStateEndpoint"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class UpdatePlaybackStateEndpointTests
{
private readonly IApiHttpClient _mockApiHttpClient;
private readonly UpdatePlaybackStateEndpoint _sut;
private readonly PlaybackStateDtoFixture _playbackStateDtoFixture = new();
/// <summary>
/// Initializes a new instance of the <see cref="UpdatePlaybackStateEndpointTests"/> class.
/// </summary>
public UpdatePlaybackStateEndpointTests()
{
_mockApiHttpClient = Substitute.For<IApiHttpClient>();
_sut = Factory.Create<UpdatePlaybackStateEndpoint>(_mockApiHttpClient);
}
[Fact]
public async Task ExecuteAsync_WhenCalled_ShouldUpdatePlaybackStateViaApiAndReturnSuccess()
{
// Arrange
PlaybackStateDto request = _playbackStateDtoFixture.Create(volume: 0.35, isMuted: true);
_mockApiHttpClient.PutAsync<WebEmptyRequest, PlaybackStateDto>(Arg.Any<string>(), Arg.Any<PlaybackStateDto>(), Arg.Any<CancellationToken>())
.Returns(new WebEmptyRequest());
// Act
IResult result = await _sut.ExecuteAsync(request, CancellationToken.None);
string body = await JsonResultTestHelper.GetResponseBodyAsync(result);
// Assert
await _mockApiHttpClient.Received(1).PutAsync<WebEmptyRequest, PlaybackStateDto>(
ApiRoutes.Users.UPDATE_PLAYBACK_STATE,
Arg.Is<PlaybackStateDto>(state => state.Volume == 0.35 && state.IsMuted == true),
Arg.Any<CancellationToken>());
using JsonDocument jsonDocument = JsonDocument.Parse(body);
Assert.True(jsonDocument.RootElement.GetProperty("success").GetBoolean());
Assert.True(jsonDocument.RootElement.GetProperty("data").GetProperty("isUpdated").GetBoolean());
}
[Fact]
public async Task ExecuteAsync_WhenCalled_ShouldForwardTheVolumeAndMuteStateToApi()
{
// Arrange
PlaybackStateDto request = _playbackStateDtoFixture.Create(volume: 0.0, isMuted: false);
_mockApiHttpClient.PutAsync<WebEmptyRequest, PlaybackStateDto>(Arg.Any<string>(), Arg.Any<PlaybackStateDto>(), Arg.Any<CancellationToken>())
.Returns(new WebEmptyRequest());
// Act
await _sut.ExecuteAsync(request, CancellationToken.None);
// Assert
await _mockApiHttpClient.Received(1).PutAsync<WebEmptyRequest, PlaybackStateDto>(
ApiRoutes.Users.UPDATE_PLAYBACK_STATE,
Arg.Is<PlaybackStateDto>(state => state.Volume == 0.0 && state.IsMuted == false),
Arg.Any<CancellationToken>());
}
[Fact]
public async Task ExecuteAsync_WhenRequestHasNullValues_ShouldForwardTheNullsToApi()
{
// Arrange
PlaybackStateDto request = _playbackStateDtoFixture.Create(includeVolume: false, includeIsMuted: false);
_mockApiHttpClient.PutAsync<WebEmptyRequest, PlaybackStateDto>(Arg.Any<string>(), Arg.Any<PlaybackStateDto>(), Arg.Any<CancellationToken>())
.Returns(new WebEmptyRequest());
// Act
await _sut.ExecuteAsync(request, CancellationToken.None);
// Assert
await _mockApiHttpClient.Received(1).PutAsync<WebEmptyRequest, PlaybackStateDto>(
ApiRoutes.Users.UPDATE_PLAYBACK_STATE,
Arg.Is<PlaybackStateDto>(state => state.Volume == null && state.IsMuted == null),
Arg.Any<CancellationToken>());
}
}