Files
h-school/tests/HSchool.Ai.Tests/DecisionPlannerTests.cs
T
Leonid Pershin 5eabc90d53
ci / server (push) Failing after 3m43s
ci / client (push) Successful in 15s
Enhance school day structure and decision-making for lunch breaks
- Updated `ai.md` to clarify the mechanics of hunger restoration and the importance of lunch breaks in the school schedule.
- Revised `schedule.md` to detail the new lunch break structure, allowing for separate sittings for different grade levels.
- Enhanced `Decision.cs` and `DecisionPlanner.cs` to incorporate logic for lunch breaks, ensuring that students only leave lessons during their designated lunch windows.
- Updated `DayFrameDef` and related classes to support multiple lunch breaks and validate their configurations.
- Adjusted tests to validate the new decision-making logic regarding lunch breaks and hunger management, ensuring robust functionality.
- Improved localization strings to reflect changes in the school day structure and lunch functionalities.
2026-08-19 23:17:16 +03:00

304 lines
9.8 KiB
C#

using HSchool.Content;
namespace HSchool.Ai.Tests;
public class DecisionPlannerTests
{
[Fact]
public void ZeroToilet_BeatsALesson()
{
var (catalog, map, walks) = World();
var duty = map.Rooms.First(room => room.Def == "Classroom").Id;
var decision = DecisionPlanner.Decide(
catalog,
map,
walks,
Actor(duty, boundToLesson: true, duty, new Dictionary<string, float>(StringComparer.Ordinal)
{
["Toilet"] = 0f,
["Hunger"] = 1f,
["Social"] = 1f,
["Sleep"] = 1f,
}),
(_, _) => 0);
Assert.Equal(GoalKind.Need, decision.Intent.Kind);
Assert.Equal("Toilet", decision.Intent.Id);
Assert.Equal("UseToilet", decision.Intent.ActionId);
Assert.NotNull(decision.WalkTo);
Assert.Equal("Restroom", map.Rooms.First(room => room.Id == decision.WalkTo).Def);
}
[Fact]
public void NeedJustBelowThreshold_DoesNotLeaveClass()
{
var (catalog, map, walks) = World();
var duty = map.Rooms.First(room => room.Def == "Classroom").Id;
var decision = DecisionPlanner.Decide(
catalog,
map,
walks,
Actor(duty, boundToLesson: true, duty, new Dictionary<string, float>(StringComparer.Ordinal)
{
["Toilet"] = 0.34f,
["Hunger"] = 1f,
["Social"] = 1f,
["Sleep"] = 1f,
}),
(_, _) => 0);
Assert.Equal(GoalKind.Duty, decision.Intent.Kind);
Assert.Null(decision.StartAction);
Assert.True(decision.WalkTo is null || decision.WalkTo == duty);
}
[Fact]
public void EqualNeeds_FinishTheCurrentAction()
{
var (catalog, map, walks) = World();
var restroom = map.Rooms.First(room => room.Def == "Restroom").Id;
var intent = new Intent(GoalKind.Need, "Toilet", DecisionPlanner.NeedWeightAtZero, "UseToilet");
var decision = DecisionPlanner.Decide(
catalog,
map,
walks,
Actor(
restroom,
boundToLesson: true,
dutyRoom: map.Rooms.First(room => room.Def == "Classroom").Id,
needs: new Dictionary<string, float>(StringComparer.Ordinal)
{
["Toilet"] = 0f,
["Hunger"] = 0f,
["Social"] = 1f,
["Sleep"] = 1f,
},
intent: intent,
activityActive: true),
(_, _) => 0);
Assert.Equal("Toilet", decision.Intent.Id);
Assert.Null(decision.WalkTo);
Assert.Null(decision.StartAction);
}
[Fact]
public void BreakAtDutyRoom_PicksLeisureEvenWithAStaleLessonIntent()
{
var (catalog, map, walks) = World();
var duty = map.Rooms.First(room => room.Def == "Classroom").Id;
var decision = DecisionPlanner.Decide(
catalog,
map,
walks,
Actor(
duty,
boundToLesson: false,
duty,
FullNeeds(),
intent: new Intent(GoalKind.Duty, duty, DecisionPlanner.DutyLessonWeight, null)),
(_, _) => 0);
Assert.Equal(GoalKind.Leisure, decision.Intent.Kind);
}
[Fact]
public void LeisureOnABreak_IsNotYankedBackToTheNextHomeroom()
{
var (catalog, map, walks) = World();
var classroom = "classroom-101";
var corridor = "corridor-1";
var decision = DecisionPlanner.Decide(
catalog,
map,
walks,
Actor(
corridor,
boundToLesson: false,
classroom,
FullNeeds(),
intent: new Intent(GoalKind.Leisure, "Chat", 3f, "Chat")),
(_, _) => 0);
Assert.Equal(GoalKind.Leisure, decision.Intent.Kind);
Assert.NotEqual(classroom, decision.WalkTo);
}
[Fact]
public void BreakAtDutyRoom_PicksLeisure_LessonDoesNot()
{
var (catalog, map, walks) = World();
var duty = map.Rooms.First(room => room.Def == "Classroom").Id;
var onBreak = DecisionPlanner.Decide(
catalog,
map,
walks,
Actor(duty, boundToLesson: false, duty, FullNeeds()),
(_, _) => 0);
var inLesson = DecisionPlanner.Decide(
catalog,
map,
walks,
Actor(duty, boundToLesson: true, duty, FullNeeds()),
(_, _) => 0);
Assert.Equal(GoalKind.Leisure, onBreak.Intent.Kind);
Assert.NotNull(onBreak.Intent.ActionId);
Assert.True(catalog.Actions[onBreak.Intent.ActionId!].Weight > 0);
Assert.Equal(GoalKind.Duty, inLesson.Intent.Kind);
Assert.Null(inLesson.StartAction);
}
[Fact]
public void BreakAwayFromNextRoom_WalksThereInsteadOfChatting()
{
var (catalog, map, walks) = World();
var classroom = map.Rooms.First(room => room.Id == "classroom-101").Id;
var gym = "gym-hall";
var fromIdle = DecisionPlanner.Decide(
catalog,
map,
walks,
Actor(classroom, boundToLesson: false, gym, FullNeeds()),
(_, _) => 0);
var fromStaleLesson = DecisionPlanner.Decide(
catalog,
map,
walks,
Actor(
classroom,
boundToLesson: false,
gym,
FullNeeds(),
intent: new Intent(GoalKind.Duty, classroom, DecisionPlanner.DutyLessonWeight, null)),
(_, _) => 0);
Assert.Equal(GoalKind.Duty, fromIdle.Intent.Kind);
Assert.Equal(gym, fromIdle.WalkTo);
Assert.Equal(gym, fromStaleLesson.WalkTo);
Assert.Equal(gym, fromStaleLesson.Intent.Id);
}
[Fact]
public void ArrivedAtNextRoomThisBreak_StaysInsteadOfChatting()
{
var (catalog, map, walks) = World();
var gym = "gym-hall";
var decision = DecisionPlanner.Decide(
catalog,
map,
walks,
Actor(
gym,
boundToLesson: false,
gym,
FullNeeds(),
intent: new Intent(GoalKind.Duty, gym, DecisionPlanner.DutyTravelWeight, null)),
(_, _) => 0);
Assert.Equal(GoalKind.Duty, decision.Intent.Kind);
Assert.Equal(gym, decision.Intent.Id);
Assert.Null(decision.WalkTo);
Assert.Null(decision.StartAction);
}
/// <summary>
/// Lunch is a timetable, not an urge. Inside the sitting a pupil who is merely peckish still
/// goes; outside it a hungry one does not walk out of the corridor to the canteen, and a
/// hungry one in class does not walk out of the lesson either.
/// </summary>
[Fact]
public void OutsideTheSitting_HungerFindsNoAction()
{
var (catalog, map, walks) = World();
var corridor = map.Rooms.First(room => room.Def == "Corridor").Id;
var needs = FullNeeds();
needs["Hunger"] = 0.05f;
var closed = DecisionPlanner.Decide(
catalog, map, walks,
Actor(corridor, boundToLesson: false, corridor, needs, lunchWindowOpen: false),
(_, _) => 0);
Assert.NotEqual("EatLunch", closed.Intent.ActionId);
var open = DecisionPlanner.Decide(
catalog, map, walks,
Actor(corridor, boundToLesson: false, corridor, needs, lunchWindowOpen: true),
(_, _) => 0);
Assert.Equal(GoalKind.Need, open.Intent.Kind);
Assert.Equal("EatLunch", open.Intent.ActionId);
Assert.Equal("Cafeteria", map.Rooms.First(room => room.Id == open.WalkTo).Def);
}
[Fact]
public void InsideTheSitting_EvenAPeckishPupilGoesToEat()
{
var (catalog, map, walks) = World();
var corridor = map.Rooms.First(room => room.Def == "Corridor").Id;
var needs = FullNeeds();
needs["Hunger"] = 0.6f;
var decision = DecisionPlanner.Decide(
catalog, map, walks,
Actor(corridor, boundToLesson: false, corridor, needs, lunchWindowOpen: true),
(_, _) => 0);
Assert.Equal("EatLunch", decision.Intent.ActionId);
Assert.Equal(DecisionPlanner.LunchWeight, decision.Intent.Weight);
}
[Fact]
public void ALessonOutweighsTheSitting()
{
var (catalog, map, walks) = World();
var classroom = map.Rooms.First(room => room.Def == "Classroom").Id;
var needs = FullNeeds();
needs["Hunger"] = 0.6f;
var decision = DecisionPlanner.Decide(
catalog, map, walks,
Actor(classroom, boundToLesson: true, classroom, needs, lunchWindowOpen: true),
(_, _) => 0);
Assert.Equal(GoalKind.Duty, decision.Intent.Kind);
}
private static ActorState Actor(
string node,
bool boundToLesson,
string dutyRoom,
IReadOnlyDictionary<string, float> needs,
Intent? intent = null,
bool activityActive = false,
bool lunchWindowOpen = false) =>
new(
node,
node,
IsWalking: false,
activityActive,
IsStudent: true,
IsStaff: false,
IsParent: false,
boundToLesson,
dutyRoom,
needs,
intent ?? Intent.None,
lunchWindowOpen);
private static Dictionary<string, float> FullNeeds() => new(StringComparer.Ordinal)
{
["Toilet"] = 1f,
["Hunger"] = 1f,
["Social"] = 1f,
["Sleep"] = 1f,
};
private static (DefCatalog Catalog, MapLayout Map, WalkGraph Walks) World()
{
var (catalog, map) = Fixtures.Vanilla();
return (catalog, map, WalkGraph.Build(catalog, map));
}
}