Merge branch 'phase/74-attendance'
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
using HSchool.Content;
|
||||
|
||||
namespace HSchool.Content.Tests;
|
||||
|
||||
public class AttendanceBehaviorTests
|
||||
{
|
||||
private readonly CatalogLoader _loader = new();
|
||||
|
||||
[Fact]
|
||||
public void MissingAttendanceFields_UseVanillaDefaults()
|
||||
{
|
||||
var catalog = _loader.Load(
|
||||
[CatalogLoader.CorePackId],
|
||||
[
|
||||
PackDocuments.Def(
|
||||
CatalogLoader.CorePackId,
|
||||
"behavior",
|
||||
"rules",
|
||||
"""{ "defName": "Behavior", "needThreshold": 0.35, "lessonSkillPerHour": 0.05, "commuteSlackMin": 0, "commuteSlackMax": 6, "switchMargin": 0.15 }"""),
|
||||
]);
|
||||
|
||||
Assert.NotNull(catalog.BehaviorRules);
|
||||
Assert.Equal(40, catalog.BehaviorRules.AttendanceMax);
|
||||
Assert.Equal(5f, catalog.BehaviorRules.LessonLateMinutes);
|
||||
Assert.True(catalog.BehaviorRules.LessonMarkWhenLate);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NegativeLateMinutes_FailTheCatalog()
|
||||
{
|
||||
var error = Assert.Throws<ContentLoadException>(() => _loader.Load(
|
||||
[CatalogLoader.CorePackId],
|
||||
[
|
||||
PackDocuments.Def(
|
||||
CatalogLoader.CorePackId,
|
||||
"behavior",
|
||||
"rules",
|
||||
"""{ "defName": "Behavior", "lessonLateMinutes": -1 }"""),
|
||||
]));
|
||||
|
||||
Assert.Contains("lessonLateMinutes", error.Message, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
using HSchool.Content;
|
||||
|
||||
namespace HSchool.People.Tests;
|
||||
|
||||
public class AttendanceMemoryTests
|
||||
{
|
||||
[Fact]
|
||||
public void OverCeiling_DropsTheOldest()
|
||||
{
|
||||
var person = Blank("a");
|
||||
var rules = Rules(max: 2);
|
||||
var t0 = new DateTime(2012, 4, 3, 10, 0, 0, DateTimeKind.Utc);
|
||||
Assert.True(AttendanceMemory.Record(person, "Mathematics", AttendanceStatuses.Present, t0, 1, rules));
|
||||
Assert.True(AttendanceMemory.Record(person, "Literature", AttendanceStatuses.Late, t0.AddMinutes(1), 2, rules));
|
||||
Assert.True(AttendanceMemory.Record(person, "History", AttendanceStatuses.Absent, t0.AddMinutes(2), 3, rules, AbsenceReasons.Truancy));
|
||||
|
||||
Assert.Equal(2, person.Attendance!.Count);
|
||||
Assert.Equal("Literature", person.Attendance[0].Subject);
|
||||
Assert.Equal("History", person.Attendance[1].Subject);
|
||||
Assert.Equal(AbsenceReasons.Truancy, person.Attendance[1].AbsenceReason);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AbsentUpgradesToPresent_SameSlot()
|
||||
{
|
||||
var person = Blank("a");
|
||||
var rules = Rules(max: 40);
|
||||
var t0 = new DateTime(2012, 4, 3, 8, 30, 0, DateTimeKind.Utc);
|
||||
Assert.True(AttendanceMemory.Record(person, "Mathematics", AttendanceStatuses.Absent, t0, 1, rules));
|
||||
Assert.True(AttendanceMemory.Record(person, "Mathematics", AttendanceStatuses.Present, t0.AddMinutes(2), 1, rules));
|
||||
Assert.Single(person.Attendance!);
|
||||
Assert.Equal(AttendanceStatuses.Present, person.Attendance![0].Status);
|
||||
Assert.Null(person.Attendance[0].AbsenceReason);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RosterJson_RoundTripsAttendance_AndOmitsEmpty()
|
||||
{
|
||||
var roster = Fixtures.Generate(Fixtures.Classrooms(4));
|
||||
var pupil = roster.People.First(person => person.IsStudent && !person.IsParent);
|
||||
var time = new DateTime(2012, 4, 3, 11, 20, 0, DateTimeKind.Utc);
|
||||
AttendanceMemory.Record(pupil, "Mathematics", AttendanceStatuses.Late, time, period: 1, Rules(max: 40));
|
||||
|
||||
var json = RosterJson.Serialize(RosterDocument.From(1, roster));
|
||||
Assert.Contains("\"attendance\"", json, StringComparison.Ordinal);
|
||||
Assert.Contains("\"status\": \"late\"", json, StringComparison.Ordinal);
|
||||
Assert.Contains("\"subject\": \"Mathematics\"", json, StringComparison.Ordinal);
|
||||
|
||||
var without = roster.People.First(person => person.Attendance is null || person.Attendance.Count == 0);
|
||||
var withoutSlice = PersonJsonSlice(json, without.Id);
|
||||
Assert.DoesNotContain("\"attendance\"", withoutSlice, StringComparison.Ordinal);
|
||||
|
||||
var loaded = RosterJson.Parse(json).ToRoster();
|
||||
var loadedPupil = loaded.People.First(person => person.Id.Equals(pupil.Id, StringComparison.Ordinal));
|
||||
Assert.NotNull(loadedPupil.Attendance);
|
||||
Assert.Single(loadedPupil.Attendance!);
|
||||
Assert.Equal(AttendanceStatuses.Late, loadedPupil.Attendance[0].Status);
|
||||
Assert.Equal("Mathematics", loadedPupil.Attendance[0].Subject);
|
||||
Assert.Equal(1, loadedPupil.Attendance[0].Period);
|
||||
Assert.Equal(time, loadedPupil.Attendance[0].Time);
|
||||
Assert.Null(loadedPupil.Attendance[0].AbsenceReason);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RosterJson_RoundTripsAbsentReason()
|
||||
{
|
||||
var roster = Fixtures.Generate(Fixtures.Classrooms(4));
|
||||
var pupil = roster.People.First(person => person.IsStudent && !person.IsParent);
|
||||
var time = new DateTime(2012, 4, 3, 11, 20, 0, DateTimeKind.Utc);
|
||||
AttendanceMemory.Record(
|
||||
pupil,
|
||||
"Mathematics",
|
||||
AttendanceStatuses.Absent,
|
||||
time,
|
||||
period: 1,
|
||||
Rules(max: 40),
|
||||
AbsenceReasons.Truancy);
|
||||
|
||||
var loaded = RosterJson.Parse(RosterJson.Serialize(RosterDocument.From(1, roster))).ToRoster();
|
||||
var loadedPupil = loaded.People.First(person => person.Id.Equals(pupil.Id, StringComparison.Ordinal));
|
||||
Assert.Equal(AttendanceStatuses.Absent, loadedPupil.Attendance![0].Status);
|
||||
Assert.Equal(AbsenceReasons.Truancy, loadedPupil.Attendance[0].AbsenceReason);
|
||||
}
|
||||
|
||||
private static string PersonJsonSlice(string json, string personId)
|
||||
{
|
||||
var marker = $"\"id\": \"{personId}\"";
|
||||
var start = json.IndexOf(marker, StringComparison.Ordinal);
|
||||
Assert.True(start >= 0);
|
||||
var end = json.IndexOf("},", start, StringComparison.Ordinal);
|
||||
if (end < 0)
|
||||
{
|
||||
end = json.Length;
|
||||
}
|
||||
|
||||
return json[start..end];
|
||||
}
|
||||
|
||||
private static BehaviorDef Rules(int max) => new()
|
||||
{
|
||||
DefName = "Behavior",
|
||||
AttendanceMax = max,
|
||||
LessonLateMinutes = 5f,
|
||||
LessonMarkWhenLate = true,
|
||||
};
|
||||
|
||||
private static Person Blank(string id)
|
||||
{
|
||||
var cases = new CaseTable
|
||||
{
|
||||
Nom = id,
|
||||
Gen = id,
|
||||
Dat = id,
|
||||
Acc = id,
|
||||
Ins = id,
|
||||
Pre = id,
|
||||
};
|
||||
return new Person
|
||||
{
|
||||
Id = id,
|
||||
FamilyId = "f",
|
||||
Female = false,
|
||||
BirthDate = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc),
|
||||
Name = new PersonName(id, id, id, cases, cases, cases),
|
||||
IsStudent = true,
|
||||
IsStaff = false,
|
||||
IsParent = false,
|
||||
Numbers = new Dictionary<string, int>(StringComparer.Ordinal),
|
||||
Choices = new Dictionary<string, string>(StringComparer.Ordinal),
|
||||
Skills = new Dictionary<string, int>(StringComparer.Ordinal),
|
||||
Traits = [],
|
||||
Needs = new Dictionary<string, float>(StringComparer.Ordinal),
|
||||
Opinions = new Dictionary<string, int>(StringComparer.Ordinal),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,256 @@
|
||||
using Arch.Core;
|
||||
using HSchool.Ai;
|
||||
using HSchool.Content;
|
||||
using HSchool.People;
|
||||
using HSchool.Schedule;
|
||||
|
||||
namespace HSchool.Simulation.Tests;
|
||||
|
||||
public class AttendanceSimulationTests
|
||||
{
|
||||
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 InRoomAtStart_IsPresent()
|
||||
{
|
||||
var (school, room, pupilId, _) = StaffedMath();
|
||||
using (school)
|
||||
{
|
||||
AdvanceTo(school, LessonStart.AddMinutes(-1));
|
||||
SetPlace(school, pupilId, room);
|
||||
school.Clock.JumpTo(LessonStart);
|
||||
AttendanceSystem.Apply(school);
|
||||
|
||||
var row = RowOf(school, pupilId);
|
||||
Assert.NotNull(row);
|
||||
Assert.Equal(AttendanceStatuses.Present, row!.Status);
|
||||
Assert.Equal("Mathematics", row.Subject);
|
||||
Assert.Equal(1, row.Period);
|
||||
Assert.Null(row.AbsenceReason);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ArrivesAfterThreshold_IsLate()
|
||||
{
|
||||
var (school, room, pupilId, _) = StaffedMath();
|
||||
using (school)
|
||||
{
|
||||
Assert.Equal(5f, school.Catalog!.BehaviorRules!.LessonLateMinutes);
|
||||
AdvanceTo(school, LessonStart.AddMinutes(-1));
|
||||
SetPlace(school, pupilId, null);
|
||||
school.Clock.JumpTo(LessonStart);
|
||||
AttendanceSystem.Apply(school);
|
||||
Assert.Null(RowOf(school, pupilId));
|
||||
|
||||
school.Clock.JumpTo(LessonStart.AddMinutes(6));
|
||||
SetPlace(school, pupilId, room);
|
||||
AttendanceSystem.Apply(school);
|
||||
|
||||
var row = RowOf(school, pupilId);
|
||||
Assert.NotNull(row);
|
||||
Assert.Equal(AttendanceStatuses.Late, row!.Status);
|
||||
Assert.Null(row.AbsenceReason);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OffCampusWholeSlot_IsAbsentTruancy()
|
||||
{
|
||||
var (school, _, pupilId, _) = StaffedMath();
|
||||
using (school)
|
||||
{
|
||||
AdvanceTo(school, LessonStart.AddMinutes(-1));
|
||||
SetPlace(school, pupilId, null);
|
||||
school.Clock.JumpTo(LessonStart);
|
||||
AttendanceSystem.Apply(school);
|
||||
Assert.Null(RowOf(school, pupilId));
|
||||
|
||||
school.Clock.JumpTo(LessonStart.AddMinutes(45));
|
||||
AttendanceSystem.Apply(school);
|
||||
|
||||
var row = RowOf(school, pupilId);
|
||||
Assert.NotNull(row);
|
||||
Assert.Equal(AttendanceStatuses.Absent, row!.Status);
|
||||
Assert.Equal(AbsenceReasons.Truancy, row.AbsenceReason);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OtherNodeWholeSlot_IsAbsent()
|
||||
{
|
||||
var (school, room, pupilId, _) = StaffedMath();
|
||||
using (school)
|
||||
{
|
||||
AdvanceTo(school, LessonStart.AddMinutes(-1));
|
||||
var other = room.Equals("classroom-101", StringComparison.Ordinal) ? "classroom-102" : "classroom-101";
|
||||
SetPlace(school, pupilId, other);
|
||||
school.Clock.JumpTo(LessonStart);
|
||||
AttendanceSystem.Apply(school);
|
||||
school.Clock.JumpTo(LessonStart.AddMinutes(45));
|
||||
AttendanceSystem.Apply(school);
|
||||
|
||||
var row = RowOf(school, pupilId);
|
||||
Assert.NotNull(row);
|
||||
Assert.Equal(AttendanceStatuses.Absent, row!.Status);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SameDayAndSeed_SameAttendance()
|
||||
{
|
||||
var first = CapturePresent();
|
||||
var second = CapturePresent();
|
||||
Assert.Equal(first.Status, second.Status);
|
||||
Assert.Equal(first.Subject, second.Subject);
|
||||
Assert.Equal(first.Period, second.Period);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Late_DoesNotCancelMarkWhenConfigured()
|
||||
{
|
||||
var (school, room, pupilId, teacherId) = StaffedMath();
|
||||
using (school)
|
||||
{
|
||||
Assert.True(school.Catalog!.BehaviorRules!.LessonMarkWhenLate);
|
||||
AdvanceTo(school, LessonStart.AddMinutes(-1));
|
||||
SetIdle(school, pupilId);
|
||||
SetIdle(school, teacherId);
|
||||
school.Clock.JumpTo(LessonStart.AddMinutes(6));
|
||||
SetPlace(school, pupilId, room);
|
||||
SetPlace(school, teacherId, room);
|
||||
PlaceTextbook(school, pupilId, "Mathematics");
|
||||
LessonLearningSystem.Apply(school, 5);
|
||||
AttendanceSystem.Apply(school);
|
||||
|
||||
Assert.Equal(AttendanceStatuses.Late, RowOf(school, pupilId)!.Status);
|
||||
var marks = school.Roster!.People.First(row => row.Id == pupilId).LessonMarks;
|
||||
Assert.NotNull(marks);
|
||||
Assert.Single(marks!);
|
||||
}
|
||||
}
|
||||
|
||||
private static AttendanceRecord CapturePresent()
|
||||
{
|
||||
var (school, room, pupilId, _) = StaffedMath();
|
||||
using (school)
|
||||
{
|
||||
AdvanceTo(school, LessonStart.AddMinutes(-1));
|
||||
SetPlace(school, pupilId, room);
|
||||
school.Clock.JumpTo(LessonStart);
|
||||
AttendanceSystem.Apply(school);
|
||||
var row = RowOf(school, pupilId);
|
||||
Assert.NotNull(row);
|
||||
return row!;
|
||||
}
|
||||
}
|
||||
|
||||
private static AttendanceRecord? RowOf(School school, string pupilId) =>
|
||||
school.Roster!.People.First(row => row.Id == pupilId).Attendance?.LastOrDefault();
|
||||
|
||||
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 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? node, string[]? path = null, float remaining = 0f)
|
||||
{
|
||||
var query = new QueryDescription().WithAll<PersonIdentity, Presence>();
|
||||
school.World.Query(
|
||||
in query,
|
||||
(ref PersonIdentity identity, ref Presence presence) =>
|
||||
{
|
||||
if (!identity.Id.Equals(personId, StringComparison.Ordinal))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
presence = node is null
|
||||
? Presence.OffCampus
|
||||
: new Presence(node, remaining, node, HeadingHome: false, path ?? []);
|
||||
});
|
||||
}
|
||||
|
||||
private static void SetIdle(School school, string personId)
|
||||
{
|
||||
var query = new QueryDescription().WithAll<PersonIdentity, PersonActivity>();
|
||||
school.World.Query(
|
||||
in query,
|
||||
(ref PersonIdentity identity, ref PersonActivity activity) =>
|
||||
{
|
||||
if (identity.Id.Equals(personId, StringComparison.Ordinal))
|
||||
{
|
||||
activity = PersonActivity.Idle;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void PlaceTextbook(School school, string personId, string subject)
|
||||
{
|
||||
var person = school.Roster!.People.First(row => row.Id.Equals(personId, StringComparison.Ordinal));
|
||||
if (person.Items is not IList<InventoryItem> items || items.IsReadOnly)
|
||||
{
|
||||
throw new InvalidOperationException($"Cannot mutate items for {personId}.");
|
||||
}
|
||||
|
||||
for (var i = items.Count - 1; i >= 0; i--)
|
||||
{
|
||||
if (items[i].Subject is not null)
|
||||
{
|
||||
items.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
|
||||
items.Add(new InventoryItem("Textbook", Color: null, Condition: 1f, ItemLocations.Bag, subject));
|
||||
}
|
||||
|
||||
private static (DefCatalog Catalog, MapLayout Map) Vanilla()
|
||||
{
|
||||
var root = Path.Combine(AppContext.BaseDirectory, "vanilla");
|
||||
var documents = new List<ContentDocument>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user