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, FakeSupervisionDuties? supervisionDuties = 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), new PeriodScheduleService(tempPath), supervisionDuties ?? new FakeSupervisionDuties(), new LetterTemplateService(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, new PeriodScheduleService(tempPath), new FakeSupervisionDuties(), new LetterTemplateService(tempPath)); vm.SelectedStateName = "Bayern"; Assert.Equal(GermanState.BY, calendarSettings.State); } [Fact] public void SavePeriodTimes_GueltigeEingabe_WirdPersistiert() { var tempPath = System.IO.Path.Combine( System.IO.Path.GetTempPath(), $"lehrerapp-settingsvm-periods-tests-{Guid.NewGuid():N}"); Directory.CreateDirectory(tempPath); var periodSchedule = new PeriodScheduleService(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(), new SchoolCalendarSettingsService(tempPath), periodSchedule, new FakeSupervisionDuties(), new LetterTemplateService(tempPath)); vm.PeriodTimes[0].StartText = "08:00"; vm.PeriodTimes[0].EndText = "08:45"; vm.SavePeriodTimesCommand.Execute(null); Assert.Equal("", vm.PeriodTimesError); Assert.Equal(45, periodSchedule.GetDurationMinutes(1)); } [Fact] public void SavePeriodTimes_EndeVorBeginn_SetztFehlerUndSpeichertNicht() { var tempPath = System.IO.Path.Combine( System.IO.Path.GetTempPath(), $"lehrerapp-settingsvm-periods-invalid-tests-{Guid.NewGuid():N}"); Directory.CreateDirectory(tempPath); var periodSchedule = new PeriodScheduleService(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(), new SchoolCalendarSettingsService(tempPath), periodSchedule, new FakeSupervisionDuties(), new LetterTemplateService(tempPath)); vm.PeriodTimes[0].StartText = "08:45"; vm.PeriodTimes[0].EndText = "08:00"; vm.SavePeriodTimesCommand.Execute(null); Assert.NotEqual("", vm.PeriodTimesError); Assert.Equal(0, periodSchedule.GetDurationMinutes(1)); } [Fact] public void AddSupervisionDuty_GueltigeEingabe_WirdGespeichertUndInListeAngezeigt() { var duties = new FakeSupervisionDuties(); var vm = BuildViewModel(supervisionDuties: duties); vm.NewDutyWeekdayName = "Montag"; vm.NewDutyAfterPeriod = 2; vm.NewDutyLocation = "Pausenhof"; vm.AddSupervisionDutyCommand.Execute(null); Assert.Single(duties.GetAll()); Assert.Single(vm.SupervisionDuties); Assert.Equal(DayOfWeek.Monday, duties.GetAll()[0].Weekday); Assert.Equal(2, duties.GetAll()[0].AfterPeriod); } [Fact] public void AddSupervisionDuty_FehlenderOrt_SetztFehlerUndSpeichertNicht() { var duties = new FakeSupervisionDuties(); var vm = BuildViewModel(supervisionDuties: duties); vm.NewDutyWeekdayName = "Montag"; vm.NewDutyAfterPeriod = 2; vm.AddSupervisionDutyCommand.Execute(null); Assert.Empty(duties.GetAll()); Assert.NotEqual("", vm.NewDutyError); } [Fact] public void AddSupervisionDuty_PauseBereitsBelegt_ZeigtFreundlicheFehlermeldung() { var duties = new FakeSupervisionDuties(); duties.Add(new SupervisionDuty { Weekday = DayOfWeek.Monday, AfterPeriod = 2, Location = "Pausenhof" }); var vm = BuildViewModel(supervisionDuties: duties); vm.NewDutyWeekdayName = "Montag"; vm.NewDutyAfterPeriod = 2; vm.NewDutyLocation = "Bibliothek"; vm.AddSupervisionDutyCommand.Execute(null); Assert.Single(duties.GetAll()); Assert.Equal("Für diese Pause ist bereits eine Aufsicht eingetragen.", vm.NewDutyError); } [Fact] public void RemoveSupervisionDuty_EntferntEintragAusRepositoryUndListe() { var duties = new FakeSupervisionDuties(); duties.Add(new SupervisionDuty { Weekday = DayOfWeek.Monday, AfterPeriod = 2, Location = "Pausenhof" }); var vm = BuildViewModel(supervisionDuties: duties); vm.RemoveSupervisionDutyCommand.Execute(vm.SupervisionDuties[0]); Assert.Empty(duties.GetAll()); Assert.Empty(vm.SupervisionDuties); } }