mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 18:21:10 +00:00
Implements tests for commit 45f9aa3bd5
This commit is contained in:
1 parent
d61a114d80
commit
aeefa9487e
44 files changed
+6929
-18
No files matched your search
@@ -0,0 +1,14 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Fixtures.Common.Api;
|
||||
|
||||
/// <summary>
|
||||
/// A streamed request that was sent to the <see cref="StubStreamingApiHttpClient"/> stub.
|
||||
/// </summary>
|
||||
/// <param name="Endpoint">The endpoint the stream was requested from.</param>
|
||||
/// <param name="Headers">The request headers that were forwarded with the request.</param>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public record StubStreamRequest(string Endpoint, IReadOnlyDictionary<string, string> Headers);
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Presentation.Web.Common.Api;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Fixtures.Common.Api;
|
||||
|
||||
/// <summary>
|
||||
/// Test double for the <see cref="IStreamingApiHttpClient"/> interface, allowing integration and security tests to stub the
|
||||
/// streamed responses of the remote API without hosting a real backend.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class StubStreamingApiHttpClient : IStreamingApiHttpClient
|
||||
{
|
||||
private readonly Dictionary<string, Func<ApiStreamResult>> _streamResponseFactories = [];
|
||||
private readonly Dictionary<string, Func<Exception>> _streamExceptionFactories = [];
|
||||
|
||||
/// <summary>
|
||||
/// Gets the streamed requests that were sent, with the headers that were forwarded with them.
|
||||
/// </summary>
|
||||
public List<StubStreamRequest> StreamRequests { get; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// Gets the list of endpoints the streams were requested from.
|
||||
/// </summary>
|
||||
public List<string> GetEndpointsCalled { get; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// Registers the streamed response returned when the specified endpoint is called.
|
||||
/// </summary>
|
||||
/// <param name="endpoint">The API endpoint for which to register the response.</param>
|
||||
/// <param name="response">The streamed response to return.</param>
|
||||
public void RegisterStreamResponse(string endpoint, ApiStreamResult response)
|
||||
{
|
||||
_streamResponseFactories[endpoint] = () => response;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers the response factory invoked when the specified endpoint is called.
|
||||
/// </summary>
|
||||
/// <param name="endpoint">The API endpoint for which to register the response factory.</param>
|
||||
/// <param name="responseFactory">The factory that produces the streamed response.</param>
|
||||
public void RegisterStreamResponseFactory(string endpoint, Func<ApiStreamResult> responseFactory)
|
||||
{
|
||||
_streamResponseFactories[endpoint] = responseFactory;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers the exception thrown when the specified endpoint is called.
|
||||
/// </summary>
|
||||
/// <param name="endpoint">The API endpoint for which to register the exception.</param>
|
||||
/// <param name="exception">The exception to throw.</param>
|
||||
public void RegisterStreamException(string endpoint, Exception exception)
|
||||
{
|
||||
_streamExceptionFactories[endpoint] = () => exception;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets all registered responses and captured requests.
|
||||
/// </summary>
|
||||
public void Reset()
|
||||
{
|
||||
_streamResponseFactories.Clear();
|
||||
_streamExceptionFactories.Clear();
|
||||
StreamRequests.Clear();
|
||||
GetEndpointsCalled.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends a GET request to the specified <paramref name="endpoint"/> as an asynchronous operation and returns the response as a stream.
|
||||
/// </summary>
|
||||
/// <param name="endpoint">The API endpoint where the request is being sent.</param>
|
||||
/// <param name="requestHeaders">The range and conditional headers of the incoming request to forward.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>The streamed response of the API request.</returns>
|
||||
public Task<ApiStreamResult> GetStreamAsync(string endpoint, IReadOnlyDictionary<string, string> requestHeaders, CancellationToken cancellationToken = default)
|
||||
{
|
||||
GetEndpointsCalled.Add(endpoint);
|
||||
StreamRequests.Add(new StubStreamRequest(endpoint, new Dictionary<string, string>(requestHeaders)));
|
||||
if (_streamExceptionFactories.TryGetValue(endpoint, out Func<Exception>? exceptionFactory))
|
||||
throw exceptionFactory();
|
||||
if (_streamResponseFactories.TryGetValue(endpoint, out Func<ApiStreamResult>? responseFactory))
|
||||
return Task.FromResult(responseFactory());
|
||||
throw new InvalidOperationException($"No stream response is registered for the endpoint '{endpoint}'.");
|
||||
}
|
||||
}
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Bogus;
|
||||
using Lumina.Presentation.Web.Common.DTO.AudioLibrary.MusicLibrary;
|
||||
using Lumina.Presentation.Web.Common.Enums.AudioLibrary;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Fixtures.Common.DTO.AudioLibrary.MusicLibrary;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="PlaybackManifestDto"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class PlaybackManifestDtoFixture
|
||||
{
|
||||
private readonly Faker _faker = new();
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="PlaybackManifestDto"/> instance with randomized test data.
|
||||
/// </summary>
|
||||
/// <param name="trackId">Optional. The Id of the track the manifest describes.</param>
|
||||
/// <param name="durationInSeconds">Optional. The duration of the audio of the track, in seconds.</param>
|
||||
/// <param name="sourceContainer">Optional. The container of the stored audio file of the track.</param>
|
||||
/// <param name="sourceCodec">Optional. The codec of the stored audio of the track.</param>
|
||||
/// <param name="sampleRate">Optional. The sample rate of the stored audio, in Hz.</param>
|
||||
/// <param name="channels">Optional. The number of audio channels of the track.</param>
|
||||
/// <param name="bitDepth">Optional. The bit depth of the stored audio of the track.</param>
|
||||
/// <param name="replayGainTrackGain">Optional. The ReplayGain track gain of the track, in decibels.</param>
|
||||
/// <param name="replayGainTrackPeak">Optional. The ReplayGain track peak of the track.</param>
|
||||
/// <param name="replayGainAlbumGain">Optional. The ReplayGain album gain of the track, in decibels.</param>
|
||||
/// <param name="replayGainAlbumPeak">Optional. The ReplayGain album peak of the track.</param>
|
||||
/// <param name="isDirectPlay">Whether the stored audio is delivered as it is.</param>
|
||||
/// <param name="deliveryContainer">Optional. The container of the delivered audio.</param>
|
||||
/// <param name="deliveryCodec">Optional. The codec of the delivered audio.</param>
|
||||
/// <param name="deliveryShape">Optional. The shape the delivered audio is addressed by.</param>
|
||||
/// <param name="segmentDurationSeconds">Optional. The duration of each segment of the delivered audio, in seconds.</param>
|
||||
/// <param name="isSeekable">Whether the delivered audio can be seeked by the player.</param>
|
||||
/// <param name="streamPath">Optional. The relative path of the track stream.</param>
|
||||
/// <param name="deliveryId">Optional. The opaque handle of the normalized delivery.</param>
|
||||
/// <param name="segmentStreamPathTemplate">Optional. The relative path template of the segment route.</param>
|
||||
/// <param name="includeBitDepth">Whether the bit depth should be included, or forced to <see langword="null"/>.</param>
|
||||
/// <param name="includeReplayGain">Whether the ReplayGain values should be included, or forced to <see langword="null"/>.</param>
|
||||
/// <param name="includeDeliveryContainer">Whether the delivery container should be included, or forced to <see langword="null"/>.</param>
|
||||
/// <param name="includeDeliveryCodec">Whether the delivery codec should be included, or forced to <see langword="null"/>.</param>
|
||||
/// <param name="includeDeliveryShape">Whether the delivery shape should be included, or forced to <see langword="null"/>.</param>
|
||||
/// <param name="includeSegmentDurationSeconds">Whether the segment duration should be included, or forced to <see langword="null"/>.</param>
|
||||
/// <param name="includeDeliveryId">Whether the delivery handle should be included, or forced to <see langword="null"/>.</param>
|
||||
/// <param name="includeSegmentStreamPathTemplate">Whether the segment path template should be included, or forced to <see langword="null"/>.</param>
|
||||
/// <returns>A configured <see cref="PlaybackManifestDto"/> instance.</returns>
|
||||
public PlaybackManifestDto Create(
|
||||
Guid? trackId = null,
|
||||
int? durationInSeconds = null,
|
||||
string? sourceContainer = null,
|
||||
string? sourceCodec = null,
|
||||
int? sampleRate = null,
|
||||
int? channels = null,
|
||||
int? bitDepth = null,
|
||||
decimal? replayGainTrackGain = null,
|
||||
decimal? replayGainTrackPeak = null,
|
||||
decimal? replayGainAlbumGain = null,
|
||||
decimal? replayGainAlbumPeak = null,
|
||||
bool isDirectPlay = false,
|
||||
string? deliveryContainer = null,
|
||||
string? deliveryCodec = null,
|
||||
PlaybackDeliveryShape? deliveryShape = null,
|
||||
int? segmentDurationSeconds = null,
|
||||
bool isSeekable = true,
|
||||
string? streamPath = null,
|
||||
string? deliveryId = null,
|
||||
string? segmentStreamPathTemplate = null,
|
||||
bool includeBitDepth = true,
|
||||
bool includeReplayGain = true,
|
||||
bool includeDeliveryContainer = true,
|
||||
bool includeDeliveryCodec = true,
|
||||
bool includeDeliveryShape = true,
|
||||
bool includeSegmentDurationSeconds = true,
|
||||
bool includeDeliveryId = true,
|
||||
bool includeSegmentStreamPathTemplate = true)
|
||||
{
|
||||
Guid resolvedTrackId = trackId ?? _faker.Random.Guid();
|
||||
return new PlaybackManifestDto
|
||||
{
|
||||
TrackId = resolvedTrackId,
|
||||
DurationInSeconds = durationInSeconds ?? _faker.Random.Int(1, 3600),
|
||||
SourceContainer = sourceContainer ?? _faker.PickRandom("FLAC", "MP3", "OGG", "MP4", "AAC", "WAV"),
|
||||
SourceCodec = sourceCodec ?? _faker.PickRandom("FLAC", "MP3", "AAC", "ALAC", "OPUS", "PCM"),
|
||||
SampleRate = sampleRate ?? _faker.PickRandom(44100, 48000, 96000),
|
||||
Channels = channels ?? _faker.PickRandom(1, 2, 6),
|
||||
BitDepth = includeBitDepth ? (bitDepth ?? _faker.Random.Int(8, 32)) : null,
|
||||
ReplayGainTrackGain = includeReplayGain ? (replayGainTrackGain ?? _faker.Random.Decimal(-20m, 0m)) : null,
|
||||
ReplayGainTrackPeak = includeReplayGain ? (replayGainTrackPeak ?? _faker.Random.Decimal(0m, 1.5m)) : null,
|
||||
ReplayGainAlbumGain = includeReplayGain ? (replayGainAlbumGain ?? _faker.Random.Decimal(-20m, 0m)) : null,
|
||||
ReplayGainAlbumPeak = includeReplayGain ? (replayGainAlbumPeak ?? _faker.Random.Decimal(0m, 1.5m)) : null,
|
||||
IsDirectPlay = isDirectPlay,
|
||||
DeliveryContainer = includeDeliveryContainer ? (deliveryContainer ?? "MP3") : null,
|
||||
DeliveryCodec = includeDeliveryCodec ? (deliveryCodec ?? "MP3") : null,
|
||||
DeliveryShape = includeDeliveryShape ? (deliveryShape ?? PlaybackDeliveryShape.TimeAddressedSegments) : null,
|
||||
SegmentDurationSeconds = includeSegmentDurationSeconds ? (segmentDurationSeconds ?? 6) : null,
|
||||
IsSeekable = isSeekable,
|
||||
StreamPath = streamPath ?? $"/libraries/{Guid.NewGuid():N}/artists/{Guid.NewGuid():N}/albums/{Guid.NewGuid():N}/tracks/{resolvedTrackId:N}/stream",
|
||||
DeliveryId = includeDeliveryId ? (deliveryId ?? Guid.NewGuid().ToString("N")) : null,
|
||||
SegmentStreamPathTemplate = includeSegmentStreamPathTemplate
|
||||
? (segmentStreamPathTemplate ?? $"/libraries/{Guid.NewGuid():N}/artists/{Guid.NewGuid():N}/albums/{Guid.NewGuid():N}/tracks/{resolvedTrackId:N}/stream/deliveries/{{deliveryId}}/segments/{{segmentIndex}}")
|
||||
: null
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates multiple <see cref="PlaybackManifestDto"/> instances with randomized test data.
|
||||
/// </summary>
|
||||
/// <param name="count">Number of instances to create.</param>
|
||||
/// <returns>List of configured <see cref="PlaybackManifestDto"/> instances.</returns>
|
||||
public List<PlaybackManifestDto> CreateMany(int count = 3)
|
||||
{
|
||||
return [.. Enumerable.Range(0, count).Select(_ => Create())];
|
||||
}
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Presentation.Web.Common.Requests.Library.AudioLibrary.MusicLibrary.Tracks;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Fixtures.Common.Requests.Library.AudioLibrary.MusicLibrary.Tracks;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="GetPlaybackManifestRequest"/> record.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class GetPlaybackManifestRequestFixture
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="GetPlaybackManifestRequest"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="artistId">Optional unique identifier of the artist the album belongs to.</param>
|
||||
/// <param name="albumId">Optional unique identifier of the album the track belongs to.</param>
|
||||
/// <param name="trackId">Optional unique identifier of the track whose manifest is requested.</param>
|
||||
/// <param name="libraryId">Optional unique identifier of the media library the track belongs to.</param>
|
||||
/// <param name="capabilities">Optional capabilities the player advertises it can play directly.</param>
|
||||
/// <param name="includeCapabilities">Whether the capabilities should be included, or forced to <see langword="null"/>.</param>
|
||||
/// <returns>A configured <see cref="GetPlaybackManifestRequest"/> instance.</returns>
|
||||
public GetPlaybackManifestRequest Create(
|
||||
string? artistId = null,
|
||||
string? albumId = null,
|
||||
string? trackId = null,
|
||||
Guid? libraryId = null,
|
||||
string? capabilities = null,
|
||||
bool includeCapabilities = true)
|
||||
{
|
||||
return new GetPlaybackManifestRequest(
|
||||
artistId ?? Guid.NewGuid().ToString(),
|
||||
albumId ?? Guid.NewGuid().ToString(),
|
||||
trackId ?? Guid.NewGuid().ToString(),
|
||||
libraryId ?? Guid.NewGuid(),
|
||||
includeCapabilities ? (capabilities ?? "mp3/mp3") : null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a list of <see cref="GetPlaybackManifestRequest"/> instances.
|
||||
/// </summary>
|
||||
/// <param name="count">Number of instances to create.</param>
|
||||
/// <returns>List of configured <see cref="GetPlaybackManifestRequest"/> instances.</returns>
|
||||
public List<GetPlaybackManifestRequest> CreateMany(int count = 3)
|
||||
{
|
||||
return [.. Enumerable.Range(0, count).Select(_ => Create())];
|
||||
}
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Presentation.Web.Common.Requests.Library.AudioLibrary.MusicLibrary.Tracks;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Fixtures.Common.Requests.Library.AudioLibrary.MusicLibrary.Tracks;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="StreamMusicTrackRequest"/> record.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class StreamMusicTrackRequestFixture
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="StreamMusicTrackRequest"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="artistId">Optional unique identifier of the artist the album belongs to.</param>
|
||||
/// <param name="albumId">Optional unique identifier of the album the track belongs to.</param>
|
||||
/// <param name="trackId">Optional unique identifier of the track to stream.</param>
|
||||
/// <param name="libraryId">Optional unique identifier of the media library the track belongs to.</param>
|
||||
/// <param name="includeArtistId">Whether the artist Id should be included, or forced to <see langword="null"/>.</param>
|
||||
/// <param name="includeAlbumId">Whether the album Id should be included, or forced to <see langword="null"/>.</param>
|
||||
/// <param name="includeTrackId">Whether the track Id should be included, or forced to <see langword="null"/>.</param>
|
||||
/// <returns>A configured <see cref="StreamMusicTrackRequest"/> instance.</returns>
|
||||
public StreamMusicTrackRequest Create(
|
||||
string? artistId = null,
|
||||
string? albumId = null,
|
||||
string? trackId = null,
|
||||
Guid? libraryId = null,
|
||||
bool includeArtistId = true,
|
||||
bool includeAlbumId = true,
|
||||
bool includeTrackId = true)
|
||||
{
|
||||
return new StreamMusicTrackRequest(
|
||||
includeArtistId ? (artistId ?? Guid.NewGuid().ToString()) : null!,
|
||||
includeAlbumId ? (albumId ?? Guid.NewGuid().ToString()) : null!,
|
||||
includeTrackId ? (trackId ?? Guid.NewGuid().ToString()) : null!,
|
||||
libraryId ?? Guid.NewGuid());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a list of <see cref="StreamMusicTrackRequest"/> instances.
|
||||
/// </summary>
|
||||
/// <param name="count">Number of instances to create.</param>
|
||||
/// <returns>List of configured <see cref="StreamMusicTrackRequest"/> instances.</returns>
|
||||
public List<StreamMusicTrackRequest> CreateMany(int count = 3)
|
||||
{
|
||||
return [.. Enumerable.Range(0, count).Select(_ => Create())];
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Presentation.Web.Common.Requests.Library.AudioLibrary.MusicLibrary.Tracks;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Fixtures.Common.Requests.Library.AudioLibrary.MusicLibrary.Tracks;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="StreamMusicTrackSegmentRequest"/> record.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class StreamMusicTrackSegmentRequestFixture
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="StreamMusicTrackSegmentRequest"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="artistId">Optional unique identifier of the artist the album belongs to.</param>
|
||||
/// <param name="albumId">Optional unique identifier of the album the track belongs to.</param>
|
||||
/// <param name="trackId">Optional unique identifier of the track to stream.</param>
|
||||
/// <param name="deliveryId">Optional opaque handle of the normalized delivery.</param>
|
||||
/// <param name="segmentIndex">Optional zero based index of the segment.</param>
|
||||
/// <param name="libraryId">Optional unique identifier of the media library the track belongs to.</param>
|
||||
/// <param name="includeDeliveryId">Whether the delivery handle should be included, or forced to <see langword="null"/>.</param>
|
||||
/// <returns>A configured <see cref="StreamMusicTrackSegmentRequest"/> instance.</returns>
|
||||
public StreamMusicTrackSegmentRequest Create(
|
||||
string? artistId = null,
|
||||
string? albumId = null,
|
||||
string? trackId = null,
|
||||
string? deliveryId = null,
|
||||
int? segmentIndex = null,
|
||||
Guid? libraryId = null,
|
||||
bool includeDeliveryId = true)
|
||||
{
|
||||
return new StreamMusicTrackSegmentRequest(
|
||||
artistId ?? Guid.NewGuid().ToString(),
|
||||
albumId ?? Guid.NewGuid().ToString(),
|
||||
trackId ?? Guid.NewGuid().ToString(),
|
||||
includeDeliveryId ? (deliveryId ?? Guid.NewGuid().ToString("N")) : null!,
|
||||
segmentIndex ?? Random.Shared.Next(0, 100),
|
||||
libraryId ?? Guid.NewGuid());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a list of <see cref="StreamMusicTrackSegmentRequest"/> instances.
|
||||
/// </summary>
|
||||
/// <param name="count">Number of instances to create.</param>
|
||||
/// <returns>List of configured <see cref="StreamMusicTrackSegmentRequest"/> instances.</returns>
|
||||
public List<StreamMusicTrackSegmentRequest> CreateMany(int count = 3)
|
||||
{
|
||||
return [.. Enumerable.Range(0, count).Select(_ => Create())];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user