using LehrerApp.Core.Models; using LehrerApp.Core.Services; using LehrerApp.Data; using LehrerApp.Desktop.ViewModels.Settings; using Xunit; namespace LehrerApp.Desktop.Tests; public sealed class SettingsViewModelTests { private static SettingsViewModel BuildViewModel(FakeSchoolHolidays? holidays = null) { // Bewusst kein "using": SchoolCalendarSettingsService liest den Pfad erst bei SetState, // das Verzeichnis muss über die Lebensdauer des ViewModels bestehen bleiben. var tempPath = System.IO.Path.Combine( System.IO.Path.GetTempPath(), $"lehrerapp-settingsvm-tests-{Guid.NewGuid():N}"); Directory.CreateDirectory(tempPath); return new SettingsViewModel( new FakeSubjects([]), new FakeCompetencyDomains(), new FakeGradingKeyTemplates(), new FakeSchemes(), new GradingService(), new BackupService(tempPath), new DatabaseEncryptionService(), new AppLockService(tempPath), new LiteDbContext(new MemoryStream()), new PrivacySettingsService(tempPath), new FakeDocumentation(), new FakeStudents([]), new FakeShorthandCodes([]), holidays ?? new FakeSchoolHolidays(), new SchoolCalendarSettingsService(tempPath)); } [Fact] public void AddSchoolHoliday_GueltigeEingabe_WirdGespeichertUndInListeAngezeigt() { var holidays = new FakeSchoolHolidays(); var vm = BuildViewModel(holidays); vm.NewHolidayName = "Sommerferien"; vm.NewHolidayStartText = "01.07.2026"; vm.NewHolidayEndText = "10.08.2026"; vm.AddSchoolHolidayCommand.Execute(null); Assert.Single(holidays.GetAll()); Assert.Single(vm.SchoolHolidayEntries); } [Fact] public void AddSchoolHoliday_EndeVorBeginn_SetztFehlerUndSpeichertNicht() { var holidays = new FakeSchoolHolidays(); var vm = BuildViewModel(holidays); vm.NewHolidayName = "Ungültig"; vm.NewHolidayStartText = "10.08.2026"; vm.NewHolidayEndText = "01.07.2026"; vm.AddSchoolHolidayCommand.Execute(null); Assert.Empty(holidays.GetAll()); Assert.NotEqual("", vm.HolidayDateError); } [Fact] public void AddSchoolHoliday_FehlenderName_SetztFehlerUndSpeichertNicht() { var holidays = new FakeSchoolHolidays(); var vm = BuildViewModel(holidays); vm.NewHolidayStartText = "01.07.2026"; vm.NewHolidayEndText = "10.08.2026"; vm.AddSchoolHolidayCommand.Execute(null); Assert.Empty(holidays.GetAll()); Assert.NotEqual("", vm.HolidayNameError); } [Fact] public void RemoveSchoolHoliday_EntferntEintragAusRepositoryUndListe() { var holidays = new FakeSchoolHolidays(); var holiday = new SchoolHoliday { Name = "Herbstferien", StartDate = new DateOnly(2026, 10, 12), EndDate = new DateOnly(2026, 10, 24) }; holidays.Add(holiday); var vm = BuildViewModel(holidays); vm.RemoveSchoolHolidayCommand.Execute(vm.SchoolHolidayEntries[0]); Assert.Empty(holidays.GetAll()); Assert.Empty(vm.SchoolHolidayEntries); } [Fact] public void SelectedStateName_Aendern_PersistiertUeberSchoolCalendarSettings() { var tempPath = System.IO.Path.Combine( System.IO.Path.GetTempPath(), $"lehrerapp-settingsvm-state-tests-{Guid.NewGuid():N}"); Directory.CreateDirectory(tempPath); var calendarSettings = new SchoolCalendarSettingsService(tempPath); var vm = new SettingsViewModel( new FakeSubjects([]), new FakeCompetencyDomains(), new FakeGradingKeyTemplates(), new FakeSchemes(), new GradingService(), new BackupService(tempPath), new DatabaseEncryptionService(), new AppLockService(tempPath), new LiteDbContext(new MemoryStream()), new PrivacySettingsService(tempPath), new FakeDocumentation(), new FakeStudents([]), new FakeShorthandCodes([]), new FakeSchoolHolidays(), calendarSettings); vm.SelectedStateName = "Bayern"; Assert.Equal(GermanState.BY, calendarSettings.State); } }