Add health condition list, tick, and save for phase 77.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,157 @@
|
||||
namespace HSchool.People.Tests;
|
||||
|
||||
public class HealthConditionTests
|
||||
{
|
||||
[Fact]
|
||||
public void SameSeed_YieldsSameSeverityCurve()
|
||||
{
|
||||
var started = new DateTime(2012, 4, 3, 8, 0, 0, DateTimeKind.Utc);
|
||||
var first = Blank("p1");
|
||||
var second = Blank("p1");
|
||||
HealthConditions.Add(first, Artificial(started, severity: 0.2f));
|
||||
HealthConditions.Add(second, Artificial(started, severity: 0.2f));
|
||||
|
||||
Assert.True(HealthConditions.Tick(first, peopleSeed: 7, dayNumber: 100, gameMinutes: 24 * 60));
|
||||
Assert.True(HealthConditions.Tick(second, peopleSeed: 7, dayNumber: 100, gameMinutes: 24 * 60));
|
||||
|
||||
Assert.Equal(first.Conditions![0].Severity, second.Conditions![0].Severity);
|
||||
Assert.True(first.Conditions[0].Severity > 0.2f);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DifferentDaySeed_ChangesTheCurve()
|
||||
{
|
||||
var started = new DateTime(2012, 4, 3, 8, 0, 0, DateTimeKind.Utc);
|
||||
var a = Blank("p1");
|
||||
var b = Blank("p1");
|
||||
HealthConditions.Add(a, Artificial(started, severity: 0.2f));
|
||||
HealthConditions.Add(b, Artificial(started, severity: 0.2f));
|
||||
|
||||
HealthConditions.Tick(a, peopleSeed: 7, dayNumber: 100, gameMinutes: 24 * 60);
|
||||
HealthConditions.Tick(b, peopleSeed: 7, dayNumber: 101, gameMinutes: 24 * 60);
|
||||
|
||||
Assert.NotEqual(a.Conditions![0].Severity, b.Conditions![0].Severity);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RosterJson_RoundTripsConditions_AndOmitsHealthy()
|
||||
{
|
||||
var roster = Fixtures.Generate(Fixtures.Classrooms(4));
|
||||
var pupil = roster.People.First(person => person.IsStudent && !person.IsParent);
|
||||
var started = new DateTime(2012, 4, 3, 9, 0, 0, DateTimeKind.Utc);
|
||||
HealthConditions.Add(pupil, Artificial(started, severity: 0.35f, defName: "TestCold"));
|
||||
HealthConditions.Add(
|
||||
pupil,
|
||||
new HealthCondition
|
||||
{
|
||||
DefName = "TestStomach",
|
||||
Severity = 0.15f,
|
||||
Progress = 0.05f,
|
||||
Source = "idiopathic",
|
||||
StartedAt = started.AddHours(1),
|
||||
SeverityPerDay = 0.05f,
|
||||
ProgressPerDay = 0.02f,
|
||||
});
|
||||
|
||||
var json = RosterJson.Serialize(RosterDocument.From(1, roster));
|
||||
Assert.Contains("\"conditions\"", json, StringComparison.Ordinal);
|
||||
Assert.Contains("\"defName\": \"TestCold\"", json, StringComparison.Ordinal);
|
||||
Assert.Contains("\"defName\": \"TestStomach\"", json, StringComparison.Ordinal);
|
||||
|
||||
var healthy = roster.People.First(person => person.Conditions is null || person.Conditions.Count == 0);
|
||||
var healthySlice = PersonJsonSlice(json, healthy.Id);
|
||||
Assert.DoesNotContain("\"conditions\"", healthySlice, StringComparison.Ordinal);
|
||||
|
||||
var loaded = RosterJson.Parse(json).ToRoster();
|
||||
var loadedPupil = loaded.People.First(person => person.Id.Equals(pupil.Id, StringComparison.Ordinal));
|
||||
Assert.NotNull(loadedPupil.Conditions);
|
||||
Assert.Equal(2, loadedPupil.Conditions!.Count);
|
||||
Assert.Equal("TestCold", loadedPupil.Conditions[0].DefName);
|
||||
Assert.Equal(0.35f, loadedPupil.Conditions[0].Severity);
|
||||
Assert.Equal("cold", loadedPupil.Conditions[0].Source);
|
||||
Assert.Equal(started, loadedPupil.Conditions[0].StartedAt);
|
||||
Assert.Equal("TestStomach", loadedPupil.Conditions[1].DefName);
|
||||
Assert.Equal(0.15f, loadedPupil.Conditions[1].Severity);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TwoConditions_DoNotOverwriteEachOther()
|
||||
{
|
||||
var person = Blank("p1");
|
||||
var started = new DateTime(2012, 4, 3, 8, 0, 0, DateTimeKind.Utc);
|
||||
HealthConditions.Add(person, Artificial(started, severity: 0.4f, defName: "A"));
|
||||
HealthConditions.Add(person, Artificial(started, severity: 0.1f, defName: "B"));
|
||||
|
||||
Assert.Equal(2, person.Conditions!.Count);
|
||||
Assert.Equal("A", person.Conditions[0].DefName);
|
||||
Assert.Equal(0.4f, person.Conditions[0].Severity);
|
||||
Assert.Equal("B", person.Conditions[1].DefName);
|
||||
Assert.Equal(0.1f, person.Conditions[1].Severity);
|
||||
|
||||
HealthConditions.Tick(person, peopleSeed: 3, dayNumber: 50, gameMinutes: 60);
|
||||
Assert.Equal(2, person.Conditions.Count);
|
||||
Assert.Equal("A", person.Conditions[0].DefName);
|
||||
Assert.Equal("B", person.Conditions[1].DefName);
|
||||
Assert.True(person.Conditions[0].Severity > 0.4f);
|
||||
Assert.True(person.Conditions[1].Severity > 0.1f);
|
||||
}
|
||||
|
||||
private static HealthCondition Artificial(
|
||||
DateTime started,
|
||||
float severity,
|
||||
string defName = "TestCold") =>
|
||||
new()
|
||||
{
|
||||
DefName = defName,
|
||||
Severity = severity,
|
||||
Progress = 0f,
|
||||
Source = "cold",
|
||||
StartedAt = DateTime.SpecifyKind(started, DateTimeKind.Utc),
|
||||
SeverityPerDay = 0.2f,
|
||||
ProgressPerDay = 0.1f,
|
||||
};
|
||||
|
||||
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 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),
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user