using Arch.Core; using HSchool.Ai; using HSchool.Content; using HSchool.People; using HSchool.Schedule; namespace HSchool.Simulation.Tests; public class SickStaffLessonTests { private static readonly DateTime TuesdayMorning = new(2012, 4, 3, 6, 0, 0, DateTimeKind.Utc); private static readonly DateTime LessonStart = new(2012, 4, 3, 8, 30, 0, DateTimeKind.Utc); [Fact] public void SickTeacherOffCampus_PupilsDoNotGainLikeWithTeacher() { var (sickSchool, room, pupilId, teacherId) = StaffedMath(); using (sickSchool) { InfectHeavyFlu(sickSchool, teacherId); var stayDay = TuesdayWithStayHome(sickSchool, teacherId); RebuildDayAt(sickSchool, stayDay); AdvanceTo(sickSchool, stayDay.Date.Add(LessonStart.TimeOfDay)); Assert.False(IsOnCampus(sickSchool, teacherId)); SetPlace(sickSchool, pupilId, room); var before = SkillOf(sickSchool, pupilId, "Mathematics"); LessonLearningSystem.Apply(sickSchool, 45); Assert.Equal(before, SkillOf(sickSchool, pupilId, "Mathematics")); Assert.Equal(2, MarkOf(sickSchool, pupilId)!.Value); } var (healthySchool, healthyRoom, healthyPupil, healthyTeacher) = StaffedMath(); using (healthySchool) { AdvanceTo(healthySchool, LessonStart); SetPlace(healthySchool, healthyPupil, healthyRoom); SetPlace(healthySchool, healthyTeacher, healthyRoom); var before = SkillOf(healthySchool, healthyPupil, "Mathematics"); LessonLearningSystem.Apply(healthySchool, 45); Assert.True(SkillOf(healthySchool, healthyPupil, "Mathematics") > before); } } [Fact] public void SickTeacher_NoticeAndLog_AtMostOncePerSlot() { var (school, room, pupilId, teacherId) = StaffedMath(); using (school) { InfectHeavyFlu(school, teacherId); var stayDay = TuesdayWithStayHome(school, teacherId); RebuildDayAt(school, stayDay); // Jump — do not AdvanceTo through the lesson, or Tick raises the event and claims it. school.Clock.JumpTo(DateTime.SpecifyKind(stayDay.Date.Add(LessonStart.TimeOfDay), DateTimeKind.Utc)); SetPlace(school, pupilId, room); SetPlace(school, teacherId, null); school.DrainWorldEvents(); Assert.True(school.Plans.TryGetValue(teacherId, out var plan) && !plan.Comes); LessonLearningSystem.Apply(school, 5); LessonLearningSystem.Apply(school, 5); LessonLearningSystem.Apply(school, 5); var logs = school.DayLog .Where(row => row.PersonId == pupilId && row.Type == PersonLogTypes.LessonNoTeacher) .ToArray(); Assert.Single(logs); var facts = school.DrainWorldEvents() .Where(row => row.Trigger.Equals(EventTriggers.LessonNoTeacher, StringComparison.Ordinal)) .ToArray(); Assert.Single(facts); Assert.Equal(teacherId, facts[0].PersonKey); } } [Fact] public void PupilsPresent_WhileTeacherSick_AreNotMarkedTruancy() { var (school, room, pupilId, teacherId) = StaffedMath(); using (school) { InfectHeavyFlu(school, teacherId); var stayDay = TuesdayWithStayHome(school, teacherId); RebuildDayAt(school, stayDay); var lessonAt = stayDay.Date.Add(LessonStart.TimeOfDay); AdvanceTo(school, lessonAt.AddMinutes(-1)); SetPlace(school, pupilId, room); SetPlace(school, teacherId, null); school.Clock.JumpTo(lessonAt); AttendanceSystem.Apply(school); school.Clock.JumpTo(lessonAt.AddMinutes(45)); AttendanceSystem.Apply(school); var row = school.Roster!.People.First(p => p.Id == pupilId).Attendance?.LastOrDefault(); Assert.NotNull(row); Assert.Equal(AttendanceStatuses.Present, row!.Status); Assert.Null(row.AbsenceReason); Assert.NotEqual(AbsenceReasons.Truancy, row.AbsenceReason); } } [Fact] public void SameDiseaseSeed_SameStayHomeDecision() { var (school, _, _, teacherId) = StaffedMath(); using (school) { InfectHeavyFlu(school, teacherId); var teacher = school.Roster!.People.First(row => row.Id == teacherId); var when = LessonStart.AddDays(2); var first = DiseaseEffects.ShouldStayHome(teacher, school.Catalog!, school.PeopleSeed, when); var second = DiseaseEffects.ShouldStayHome(teacher, school.Catalog!, school.PeopleSeed, when); Assert.Equal(first, second); } } private static void InfectHeavyFlu(School school, string teacherId) { var teacher = school.Roster!.People.First(row => row.Id == teacherId); HealthConditions.Add( teacher, new HealthCondition { DefName = "Influenza", Severity = 0.85f, Progress = 0.1f, Source = "cold", StartedAt = TuesdayMorning.AddDays(-3), }); Assert.True(DiseaseEffects.StayHomeChance(teacher, school.Catalog!, LessonStart) >= 0.9f); } private static DateTime TuesdayWithStayHome(School school, string teacherId) { var teacher = school.Roster!.People.First(row => row.Id == teacherId); for (var i = 0; i < 60; i++) { var candidate = LessonStart.AddDays(i * 7); if (DiseaseEffects.ShouldStayHome(teacher, school.Catalog!, school.PeopleSeed, candidate)) { return candidate; } } throw new InvalidOperationException("Heavy flu should stay home on some Tuesday."); } private static void RebuildDayAt(School school, DateTime lessonInstant) { school.PlanDay = null; school.Clock.JumpTo(DateTime.SpecifyKind(lessonInstant.Date.AddHours(6), DateTimeKind.Utc)); school.Tick(0.2d, 5d); } private static bool IsOnCampus(School school, string personId) { var on = false; var query = new QueryDescription().WithAll(); school.World.Query( in query, (ref PersonIdentity identity, ref Presence presence) => { if (identity.Id.Equals(personId, StringComparison.Ordinal)) { on = presence.IsOnCampus; } }); return on; } private static LessonMarkRecord? MarkOf(School school, string pupilId) => school.Roster!.People.First(row => row.Id == pupilId).LessonMarks?.LastOrDefault(); private static void AdvanceTo(School school, DateTime until) { while (school.Clock.Time < until) { school.Tick(0.2d, 5d); } } private static void SetPlace(School school, string personId, string? nodeId) { var query = new QueryDescription().WithAll(); school.World.Query( in query, (ref PersonIdentity identity, ref Presence presence) => { if (!identity.Id.Equals(personId, StringComparison.Ordinal)) { return; } presence = nodeId is null ? Presence.OffCampus : new Presence(nodeId, 0f, nodeId, false, []); }); } private static float SkillOf(School school, string personId, string skill) { var value = float.NaN; var query = new QueryDescription().WithAll(); school.World.Query( in query, (ref PersonIdentity identity, ref PersonSkills skills) => { if (identity.Id.Equals(personId, StringComparison.Ordinal)) { value = skills.Values.GetValueOrDefault(skill); } }); return value; } private static (School School, string Homeroom, string PupilId, string TeacherId) StaffedMath() { var (catalog, map) = Vanilla(); var roster = RosterGenerator.Generate(catalog, map, schoolSeed: 1, "Russia", TuesdayMorning); var pool = ApplicantPool.Create(catalog, roster, schoolSeed: 1, "Russia", TuesdayMorning); var hired = Staffing.Hire(catalog, map, roster, pool, pool.Applicants[0].Person.Id, Staffing.TeacherPosition, 1_000_000f); Assert.Equal(StaffingError.None, hired.Error); roster = hired.Roster; pool = hired.Pool; var hiredId = roster.People.First(person => person.IsStaff).Id; var schoolClass = roster.Classes.First(row => row.RoomId is "classroom-101" or "classroom-102" or "classroom-103" or "classroom-104"); var school = School.Create(1, "Больной учитель", TuesdayMorning, catalog, map); school.InstallPeople(roster, seed: 1, "Russia", pool); school.SetTimetable(new Timetable( [new LessonPlacement(schoolClass.Id, "Mathematics", hiredId, schoolClass.RoomId, Day: 1, Period: 1)], [])); school.ConfigurePresence(weekDays: 5, maxDecisionsPerTick: 10_000); var pupil = schoolClass.PupilIds .Select(id => school.Roster!.People.First(person => person.Id == id)) .First(person => !person.Traits.Contains("Lazy")); return (school, schoolClass.RoomId, pupil.Id, hiredId); } private static (DefCatalog Catalog, MapLayout Map) Vanilla() { var root = Path.Combine(AppContext.BaseDirectory, "vanilla"); var documents = new List(); foreach (var path in Directory.EnumerateFiles(root, "*.*", SearchOption.AllDirectories)) { if (!path.EndsWith(".jsonc", StringComparison.OrdinalIgnoreCase) && !path.EndsWith(".json", StringComparison.OrdinalIgnoreCase)) { continue; } var relative = Path.GetRelativePath(root, path).Replace('\\', '/'); documents.Add(new ContentDocument(CatalogLoader.CorePackId, relative, File.ReadAllText(path))); } var catalog = new CatalogLoader().Load([CatalogLoader.CorePackId], documents); var map = CatalogLoader.LastDefaultMap([CatalogLoader.CorePackId], documents); Assert.NotNull(map); return (catalog, map); } }