using Avalonia.Headless.XUnit; using PostIt.Services; using PostIt.ViewModels; using Yavsc.Api.Client; namespace PostIt.Tests; public sealed class PodcastPreferencesTests { [AvaloniaFact] public async Task Detection_selection_and_title_survive_settings_reload_without_detecting_again() { var path = Path.Combine(Path.GetTempPath(), $"postit-preferences-{Guid.NewGuid():N}.json"); var previous = Platform.GetPodcastCaptureDevices; try { Platform.GetPodcastCaptureDevices = () => new( [new("Front", "front"), new("Rear", "rear")], [new("None", ""), new("USB", "usb")]); var settings = new Settings(path); await using var api = new YavscApiClient(settings, new TokenStore(path + ".tokens")); var session = new SessionStatusViewModel(); var vm = new PodcastsPageViewModel(new PodcastApiClient(api, () => settings.ApiUrl), api, session); vm.DetectDevicesCommand.Execute(null); vm.SelectedCamera = vm.Cameras[1]; vm.SelectedMicrophone = vm.Microphones[0]; vm.Title = "Mon emission habituelle"; vm.DetectDevicesCommand.Execute(null); Assert.Equal("rear", vm.SelectedCamera?.Value); Assert.Equal("", vm.SelectedMicrophone?.Value); var restored = new Settings(path); restored.Authentication.Authority = ""; restored.Load(); Assert.False(restored.IsDirty); Assert.Equal("Mon emission habituelle", restored.PodcastDefaultTitle); Assert.Equal("rear", restored.PodcastCameraId); Assert.Equal("", restored.PodcastMicrophoneId); Platform.GetPodcastCaptureDevices = () => throw new InvalidOperationException("Must use saved devices."); await using var otherApi = new YavscApiClient(restored, new TokenStore(path + ".tokens")); var other = new PodcastsPageViewModel(new PodcastApiClient(otherApi, () => restored.ApiUrl), otherApi, session); Assert.Equal(2, other.Cameras.Count); Assert.Equal(2, other.Microphones.Count); Assert.Equal("rear", other.SelectedCamera?.Value); Assert.Equal("", other.SelectedMicrophone?.Value); Assert.Equal(vm.Title, other.Title); Platform.GetPodcastCaptureDevices = () => new([], []); other.DetectDevicesCommand.Execute(null); Assert.Null(other.SelectedCamera); Assert.Null(other.SelectedMicrophone); Assert.Contains("Aucune camera", other.Status); } finally { Platform.GetPodcastCaptureDevices = previous; File.Delete(path); File.Delete(path + ".tokens"); } } [AvaloniaFact] public async Task Failed_save_is_reported_without_losing_the_title() { var path = Path.Combine(Path.GetTempPath(), $"postit-preferences-{Guid.NewGuid():N}"); Directory.CreateDirectory(path); try { var settings = new Settings(path); await using var api = new YavscApiClient(settings, new TokenStore(path + ".tokens")); var vm = new PodcastsPageViewModel(new PodcastApiClient(api, () => settings.ApiUrl), api, new()); vm.Title = "Titre conserve"; Assert.Equal("Titre conserve", vm.Title); Assert.Contains("sauvegarde impossible", vm.Status); Assert.True(settings.IsDirty); } finally { Directory.Delete(path); File.Delete(path + ".tokens"); } } }