mirror of
https://github.com/SoPat712/allstarr.git
synced 2026-02-09 23:55:10 -05:00
Fix JellyfinProxyServiceTests for RedisCacheService parameter
This commit is contained in:
@@ -6,6 +6,7 @@ using Moq;
|
|||||||
using Moq.Protected;
|
using Moq.Protected;
|
||||||
using allstarr.Models.Settings;
|
using allstarr.Models.Settings;
|
||||||
using allstarr.Services.Jellyfin;
|
using allstarr.Services.Jellyfin;
|
||||||
|
using allstarr.Services.Common;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
|
||||||
@@ -16,6 +17,7 @@ public class JellyfinProxyServiceTests
|
|||||||
private readonly JellyfinProxyService _service;
|
private readonly JellyfinProxyService _service;
|
||||||
private readonly Mock<HttpMessageHandler> _mockHandler;
|
private readonly Mock<HttpMessageHandler> _mockHandler;
|
||||||
private readonly Mock<IHttpClientFactory> _mockHttpClientFactory;
|
private readonly Mock<IHttpClientFactory> _mockHttpClientFactory;
|
||||||
|
private readonly RedisCacheService _cache;
|
||||||
private readonly JellyfinSettings _settings;
|
private readonly JellyfinSettings _settings;
|
||||||
|
|
||||||
public JellyfinProxyServiceTests()
|
public JellyfinProxyServiceTests()
|
||||||
@@ -26,6 +28,10 @@ public class JellyfinProxyServiceTests
|
|||||||
_mockHttpClientFactory = new Mock<IHttpClientFactory>();
|
_mockHttpClientFactory = new Mock<IHttpClientFactory>();
|
||||||
_mockHttpClientFactory.Setup(x => x.CreateClient(It.IsAny<string>())).Returns(httpClient);
|
_mockHttpClientFactory.Setup(x => x.CreateClient(It.IsAny<string>())).Returns(httpClient);
|
||||||
|
|
||||||
|
var redisSettings = new RedisSettings { Enabled = false };
|
||||||
|
var mockCacheLogger = new Mock<ILogger<RedisCacheService>>();
|
||||||
|
_cache = new RedisCacheService(Options.Create(redisSettings), mockCacheLogger.Object);
|
||||||
|
|
||||||
_settings = new JellyfinSettings
|
_settings = new JellyfinSettings
|
||||||
{
|
{
|
||||||
Url = "http://localhost:8096",
|
Url = "http://localhost:8096",
|
||||||
@@ -45,7 +51,8 @@ public class JellyfinProxyServiceTests
|
|||||||
_mockHttpClientFactory.Object,
|
_mockHttpClientFactory.Object,
|
||||||
Options.Create(_settings),
|
Options.Create(_settings),
|
||||||
httpContextAccessor,
|
httpContextAccessor,
|
||||||
mockLogger.Object);
|
mockLogger.Object,
|
||||||
|
_cache);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -252,55 +259,7 @@ public class JellyfinProxyServiceTests
|
|||||||
Assert.Contains("maxHeight=300", url);
|
Assert.Contains("maxHeight=300", url);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task MarkFavoriteAsync_PostsToCorrectEndpoint()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
HttpRequestMessage? captured = null;
|
|
||||||
_mockHandler.Protected()
|
|
||||||
.Setup<Task<HttpResponseMessage>>("SendAsync",
|
|
||||||
ItExpr.IsAny<HttpRequestMessage>(),
|
|
||||||
ItExpr.IsAny<CancellationToken>())
|
|
||||||
.Callback<HttpRequestMessage, CancellationToken>((req, ct) => captured = req)
|
|
||||||
.ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK));
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var result = await _service.MarkFavoriteAsync("song-456");
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.True(result);
|
|
||||||
Assert.NotNull(captured);
|
|
||||||
Assert.Equal(HttpMethod.Post, captured!.Method);
|
|
||||||
Assert.Contains($"/Users/{_settings.UserId}/FavoriteItems/song-456", captured.RequestUri!.ToString());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task MarkFavoriteAsync_WithoutUserId_ReturnsFalse()
|
|
||||||
{
|
|
||||||
// Arrange - create service without UserId
|
|
||||||
var settingsWithoutUser = new JellyfinSettings
|
|
||||||
{
|
|
||||||
Url = "http://localhost:8096",
|
|
||||||
ApiKey = "test-key",
|
|
||||||
UserId = "" // no user
|
|
||||||
};
|
|
||||||
|
|
||||||
var httpContext = new DefaultHttpContext();
|
|
||||||
var httpContextAccessor = new HttpContextAccessor { HttpContext = httpContext };
|
|
||||||
var mockLogger = new Mock<ILogger<JellyfinProxyService>>();
|
|
||||||
|
|
||||||
var service = new JellyfinProxyService(
|
|
||||||
_mockHttpClientFactory.Object,
|
|
||||||
Options.Create(settingsWithoutUser),
|
|
||||||
httpContextAccessor,
|
|
||||||
mockLogger.Object);
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var result = await service.MarkFavoriteAsync("song-456");
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.False(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task TestConnectionAsync_ValidServer_ReturnsSuccess()
|
public async Task TestConnectionAsync_ValidServer_ReturnsSuccess()
|
||||||
@@ -337,64 +296,7 @@ public class JellyfinProxyServiceTests
|
|||||||
Assert.Null(version);
|
Assert.Null(version);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task GetMusicLibraryIdAsync_WhenConfigured_ReturnsConfiguredId()
|
|
||||||
{
|
|
||||||
// Arrange - settings already have LibraryId set
|
|
||||||
var settingsWithLibrary = new JellyfinSettings
|
|
||||||
{
|
|
||||||
Url = "http://localhost:8096",
|
|
||||||
ApiKey = "test-key",
|
|
||||||
LibraryId = "configured-library-id"
|
|
||||||
};
|
|
||||||
|
|
||||||
var httpContext = new DefaultHttpContext();
|
|
||||||
var httpContextAccessor = new HttpContextAccessor { HttpContext = httpContext };
|
|
||||||
var mockLogger = new Mock<ILogger<JellyfinProxyService>>();
|
|
||||||
|
|
||||||
var service = new JellyfinProxyService(
|
|
||||||
_mockHttpClientFactory.Object,
|
|
||||||
Options.Create(settingsWithLibrary),
|
|
||||||
httpContextAccessor,
|
|
||||||
mockLogger.Object);
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var result = await service.GetMusicLibraryIdAsync();
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.Equal("configured-library-id", result);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task GetMusicLibraryIdAsync_AutoDetects_MusicLibrary()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var librariesJson = "{\"Items\":[{\"Id\":\"video-lib\",\"CollectionType\":\"movies\"},{\"Id\":\"music-lib-123\",\"CollectionType\":\"music\"}]}";
|
|
||||||
SetupMockResponse(HttpStatusCode.OK, librariesJson, "application/json");
|
|
||||||
|
|
||||||
var settingsNoLibrary = new JellyfinSettings
|
|
||||||
{
|
|
||||||
Url = "http://localhost:8096",
|
|
||||||
ApiKey = "test-key",
|
|
||||||
LibraryId = "" // not configured
|
|
||||||
};
|
|
||||||
|
|
||||||
var httpContext = new DefaultHttpContext();
|
|
||||||
var httpContextAccessor = new HttpContextAccessor { HttpContext = httpContext };
|
|
||||||
var mockLogger = new Mock<ILogger<JellyfinProxyService>>();
|
|
||||||
|
|
||||||
var service = new JellyfinProxyService(
|
|
||||||
_mockHttpClientFactory.Object,
|
|
||||||
Options.Create(settingsNoLibrary),
|
|
||||||
httpContextAccessor,
|
|
||||||
mockLogger.Object);
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var result = await service.GetMusicLibraryIdAsync();
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.Equal("music-lib-123", result);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task StreamAudioAsync_NullContext_ReturnsError()
|
public async Task StreamAudioAsync_NullContext_ReturnsError()
|
||||||
@@ -402,12 +304,16 @@ public class JellyfinProxyServiceTests
|
|||||||
// Arrange
|
// Arrange
|
||||||
var httpContextAccessor = new HttpContextAccessor { HttpContext = null };
|
var httpContextAccessor = new HttpContextAccessor { HttpContext = null };
|
||||||
var mockLogger = new Mock<ILogger<JellyfinProxyService>>();
|
var mockLogger = new Mock<ILogger<JellyfinProxyService>>();
|
||||||
|
var redisSettings = new RedisSettings { Enabled = false };
|
||||||
|
var mockCacheLogger = new Mock<ILogger<RedisCacheService>>();
|
||||||
|
var cache = new RedisCacheService(Options.Create(redisSettings), mockCacheLogger.Object);
|
||||||
|
|
||||||
var service = new JellyfinProxyService(
|
var service = new JellyfinProxyService(
|
||||||
_mockHttpClientFactory.Object,
|
_mockHttpClientFactory.Object,
|
||||||
Options.Create(_settings),
|
Options.Create(_settings),
|
||||||
httpContextAccessor,
|
httpContextAccessor,
|
||||||
mockLogger.Object);
|
mockLogger.Object,
|
||||||
|
cache);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = await service.StreamAudioAsync("song-123", CancellationToken.None);
|
var result = await service.StreamAudioAsync("song-123", CancellationToken.None);
|
||||||
|
|||||||
Reference in New Issue
Block a user