|
| 1 | +using System.Net; |
| 2 | +using System.Net.Http; |
| 3 | +using ImmichFrame.WebApi.Tests.Mocks; |
| 4 | +using Microsoft.AspNetCore.Mvc.Testing; |
| 5 | +using Microsoft.AspNetCore.TestHost; |
| 6 | +using Microsoft.Extensions.DependencyInjection; |
| 7 | +using Microsoft.Extensions.Http; // Added this |
| 8 | +using Moq; |
| 9 | +using Moq.Protected; |
| 10 | +using ImmichFrame.Core.Api; |
| 11 | +using ImmichFrame.WebApi.Models; |
| 12 | +using ImmichFrame.Core.Interfaces; // Added this back |
| 13 | +using NUnit.Framework; |
| 14 | + |
| 15 | +namespace ImmichFrame.WebApi.Tests.Controllers |
| 16 | +{ |
| 17 | + [TestFixture] |
| 18 | + public class AssetControllerTests |
| 19 | + { |
| 20 | + private WebApplicationFactory<Program> _factory; |
| 21 | + private Mock<HttpMessageHandler> _mockHttpMessageHandler; |
| 22 | + |
| 23 | + [SetUp] |
| 24 | + public void Setup() |
| 25 | + { |
| 26 | + _mockHttpMessageHandler = new Mock<HttpMessageHandler>(); |
| 27 | + _factory = new WebApplicationFactory<Program>() |
| 28 | + .WithWebHostBuilder(builder => |
| 29 | + { |
| 30 | + builder.ConfigureTestServices(services => |
| 31 | + { |
| 32 | + // 1. Mock HttpMessageHandler and IHttpClientFactory |
| 33 | + services.AddSingleton<HttpMessageHandler>(_mockHttpMessageHandler.Object); |
| 34 | + services.AddHttpClient("ImmichApiAccountClient") |
| 35 | + .ConfigurePrimaryHttpMessageHandler(sp => sp.GetRequiredService<HttpMessageHandler>()); |
| 36 | + services.ConfigureAll<HttpClientFactoryOptions>(options => |
| 37 | + { |
| 38 | + options.HttpMessageHandlerBuilderActions.Add(b => |
| 39 | + { |
| 40 | + b.PrimaryHandler = b.Services.GetRequiredService<HttpMessageHandler>(); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + // 2. Directly instantiate and register settings objects |
| 45 | + var generalSettings = new GeneralSettings |
| 46 | + { |
| 47 | + ShowWeatherDescription = false, // Assuming this corresponds to ShowWeather |
| 48 | + ShowClock = true, |
| 49 | + ClockFormat = "HH:mm", |
| 50 | + Language = "en", |
| 51 | + PhotoDateFormat = "MM/dd/yyyy", // Crucial for the NRE |
| 52 | + ImageLocationFormat = "City,State,Country", |
| 53 | + DownloadImages = false, |
| 54 | + RenewImagesDuration = 30, |
| 55 | + // Ensure all non-nullable string properties that might be used have defaults if not set here |
| 56 | + PrimaryColor = "#FFFFFF", // Example default |
| 57 | + SecondaryColor = "#000000", // Example default |
| 58 | + Style = "none", |
| 59 | + BaseFontSize = "16px", |
| 60 | + WeatherApiKey = "", |
| 61 | + UnitSystem = "imperial", |
| 62 | + WeatherLatLong = "0,0" |
| 63 | + }; |
| 64 | + |
| 65 | + var accountSettings = new ServerAccountSettings |
| 66 | + { |
| 67 | + ImmichServerUrl = "http://mock-immich-server.com", |
| 68 | + ApiKey = "test-api-key", |
| 69 | + ShowMemories = false, |
| 70 | + ShowFavorites = true, |
| 71 | + ShowArchived = false, |
| 72 | + Albums = new List<Guid>(), |
| 73 | + ExcludedAlbums = new List<Guid>(), |
| 74 | + People = new List<Guid>() |
| 75 | + }; |
| 76 | + |
| 77 | + var serverSettings = new ServerSettings |
| 78 | + { |
| 79 | + GeneralSettingsImpl = generalSettings, |
| 80 | + AccountsImpl = new List<ServerAccountSettings> { accountSettings } |
| 81 | + }; |
| 82 | + |
| 83 | + services.AddSingleton<IServerSettings>(serverSettings); |
| 84 | + services.AddSingleton<IGeneralSettings>(generalSettings); |
| 85 | + // Ensure IAccountSettings can be resolved if needed by MultiImmichFrameLogicDelegate directly |
| 86 | + // However, PooledImmichFrameLogic receives IAccountSettings via the factory Func |
| 87 | + }); |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + // Removed OneTimeSetup that created Settings.json |
| 92 | + |
| 93 | + [TearDown] |
| 94 | + public void TearDown() |
| 95 | + { |
| 96 | + _factory.Dispose(); |
| 97 | + } |
| 98 | + |
| 99 | + [Test] |
| 100 | + public async Task GetRandomImage_ReturnsImageFromMockServer() |
| 101 | + { |
| 102 | + // Arrange |
| 103 | + var expectedAssetId = Guid.NewGuid(); |
| 104 | + var assetDtoJson = $@" |
| 105 | + {{ |
| 106 | + ""id"": ""{expectedAssetId}"", |
| 107 | + ""originalPath"": ""/path/to/image.jpg"", |
| 108 | + ""type"": ""IMAGE"", |
| 109 | + ""fileCreatedAt"": ""2023-10-26T10:00:00Z"", |
| 110 | + ""fileModifiedAt"": ""2023-10-26T10:00:00Z"", |
| 111 | + ""isFavorite"": true, |
| 112 | + ""duration"": ""0:00:00"", |
| 113 | + ""checksum"": ""testchecksum"", |
| 114 | + ""deviceAssetId"": ""testDeviceAssetId"", |
| 115 | + ""deviceId"": ""testDeviceId"", |
| 116 | + ""ownerId"": ""testOwnerId"", |
| 117 | + ""originalFileName"": ""image.jpg"", |
| 118 | + ""localDateTime"": ""2023-10-26T10:00:00Z"", |
| 119 | + ""visibility"": ""timeline"", |
| 120 | + ""hasMetadata"": true, |
| 121 | + ""isArchived"": false, |
| 122 | + ""isOffline"": false, |
| 123 | + ""isTrashed"": false, |
| 124 | + ""thumbhash"": ""I0cMCQS94XmImZeXmYd3d3g="", |
| 125 | + ""updatedAt"": ""2023-10-26T10:00:00Z"" |
| 126 | + }}"; |
| 127 | + |
| 128 | + // JSON structure for SearchResponseDto |
| 129 | + var jsonResponse = $@" |
| 130 | + {{ |
| 131 | + ""albums"": {{ |
| 132 | + ""count"": 0, |
| 133 | + ""items"": [], |
| 134 | + ""total"": 0, |
| 135 | + ""facets"": [] |
| 136 | + }}, |
| 137 | + ""assets"": {{ |
| 138 | + ""count"": 1, |
| 139 | + ""items"": [ |
| 140 | + {assetDtoJson} |
| 141 | + ], |
| 142 | + ""total"": 1, |
| 143 | + ""facets"": [], |
| 144 | + ""nextPage"": null |
| 145 | + }} |
| 146 | + }}"; |
| 147 | + |
| 148 | + // Setup for SearchAssetsAsync |
| 149 | + _mockHttpMessageHandler.Protected() |
| 150 | + .Setup<Task<HttpResponseMessage>>( |
| 151 | + "SendAsync", |
| 152 | + ItExpr.Is<HttpRequestMessage>(req => req.RequestUri!.ToString().Contains("/search/metadata")), |
| 153 | + ItExpr.IsAny<CancellationToken>() |
| 154 | + ) |
| 155 | + .ReturnsAsync(() => new HttpResponseMessage // Use a Func to return new instance each time |
| 156 | + { |
| 157 | + StatusCode = HttpStatusCode.OK, |
| 158 | + Content = new StringContent(jsonResponse) |
| 159 | + }); |
| 160 | + |
| 161 | + // Setup for ViewAssetAsync (thumbnail) |
| 162 | + var mockImageData = new byte[] { 0xFF, 0xD8, 0xFF, 0xE0 }; // Minimal JPEG |
| 163 | + _mockHttpMessageHandler.Protected() |
| 164 | + .Setup<Task<HttpResponseMessage>>( |
| 165 | + "SendAsync", |
| 166 | + ItExpr.Is<HttpRequestMessage>(req => req.RequestUri!.ToString().Contains("/thumbnail")), |
| 167 | + ItExpr.IsAny<CancellationToken>() |
| 168 | + ) |
| 169 | + .ReturnsAsync(() => new HttpResponseMessage |
| 170 | + { |
| 171 | + StatusCode = HttpStatusCode.OK, |
| 172 | + Content = new ByteArrayContent(mockImageData) |
| 173 | + }); |
| 174 | + |
| 175 | + var client = _factory.CreateClient(); |
| 176 | + |
| 177 | + // Act |
| 178 | + var response = await client.GetAsync("/api/Asset/RandomImageAndInfo"); |
| 179 | + |
| 180 | + // Assert |
| 181 | + response.EnsureSuccessStatusCode(); |
| 182 | + var content = await response.Content.ReadAsStringAsync(); |
| 183 | + // For now, we'll just check if the response is not empty. |
| 184 | + // A more robust check would be to deserialize the response and check the asset ID. |
| 185 | + Assert.That(content, Is.Not.Empty); |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments