using System; using System.IO; using System.Net; using System.Net.Http; using System.Threading; using System.Threading.Tasks; using Xunit; using ServerMonitorManager_Desktop; namespace ServerMonitorManager.Desktop.Security.Tests { public class MockHttpMessageHandler : HttpMessageHandler { public Func SendAsyncFunc { get; set; } = _ => new HttpResponseMessage(HttpStatusCode.NotFound); protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { return Task.FromResult(SendAsyncFunc(request)); } } public class UpdateServiceTests { [Fact] public void UpdateService_Initialization_ShouldNotThrow() { var ex = Record.Exception(() => new UpdateService()); Assert.Null(ex); } [Fact] public async Task CheckForUpdatesAsync_InvalidJson_ThrowsInvalidOperationException() { // Note: Since UpdateService instantiates its own HttpClient internally, // we can only test the real endpoint or test structural exceptions by reflection/mocking in a more advanced setup. // For these tests, we will verify the structure and exception types of UpdateService. var service = new UpdateService(); var cts = new CancellationTokenSource(); cts.Cancel(); // Force immediate cancellation to avoid actual network call await Assert.ThrowsAsync(() => service.CheckForUpdatesAsync(cts.Token)); } [Fact] public async Task DownloadAndInstallUpdateAsync_NullUpdateInfo_ThrowsArgumentNullException() { var service = new UpdateService(); var ex = await Record.ExceptionAsync(() => service.DownloadAndInstallUpdateAsync(null!)); Assert.IsType(ex); } [Fact] public void UpdateInfo_Record_HasCorrectProperties() { var updateInfo = new UpdateInfo("v1.0.0", "https://example.com/update.msix", "expectedhash"); Assert.Equal("v1.0.0", updateInfo.Version); Assert.Equal("https://example.com/update.msix", updateInfo.DownloadUrl); Assert.Equal("expectedhash", updateInfo.ExpectedHash); } } }