Files
h-school/tests/HSchool.Content.Tests/CatalogLoaderTests.cs
T
Leonid Pershin b135a9caad Enhance protocol and simulation features with activity tracking and behavior definitions
- Updated protocol documentation to include new `activity` and `activityLabel` fields in the person card response, reflecting real-time activity status.
- Introduced `BehaviorDef` to define behavior rules, including need thresholds and lesson skill gains, enhancing AI decision-making.
- Revised the `DefCatalog` to incorporate behavior definitions and updated validation logic to ensure proper behavior handling.
- Enhanced the simulation to manage presence and activity states, allowing for more dynamic interactions within the school environment.
- Updated tests to validate the new activity tracking and behavior functionalities, ensuring robust performance and reliability.
- Improved localization strings to support new activity and behavior features, enhancing user experience.
2026-08-19 19:49:44 +03:00

154 lines
5.7 KiB
C#

namespace HSchool.Content.Tests;
public class CatalogLoaderTests
{
private readonly CatalogLoader _loader = new();
[Fact]
public void Jsonc_AllowsCommentsAndTrailingCommas()
{
var catalog = _loader.Load(
[CatalogLoader.CorePackId],
[
PackDocuments.Def(
CatalogLoader.CorePackId,
"actions",
"sit",
"""
// a verb the Sit system already knows
{ "defName": "Sit", "abstract": true, }
"""),
]);
Assert.True(catalog.Actions.ContainsKey("Sit"));
}
[Fact]
public void DuplicateDefName_LastPackWinsAndWarns()
{
var log = new RecordingLog();
var catalog = _loader.Load(
[CatalogLoader.CorePackId, "addon"],
[
PackDocuments.Def(CatalogLoader.CorePackId, "things", "chair", """{ "defName": "Chair", "actions": [] }"""),
PackDocuments.Def("addon", "things", "chair", """{ "defName": "Chair", "actions": ["Sit"] }"""),
PackDocuments.Def(CatalogLoader.CorePackId, "actions", "sit", """{ "defName": "Sit", "abstract": true }"""),
],
log);
Assert.Equal(["Sit"], catalog.Things["Chair"].Actions);
Assert.Contains(log.Warnings, warning => warning.Contains("Chair") && warning.Contains("addon"));
}
[Fact]
public void DuplicateLocaleKey_LastPackWinsAndWarns()
{
var log = new RecordingLog();
var catalog = _loader.Load(
[CatalogLoader.CorePackId, "addon"],
[
PackDocuments.Def(CatalogLoader.CorePackId, "actions", "sit", """{ "defName": "Sit", "abstract": true }"""),
PackDocuments.Locale(CatalogLoader.CorePackId, "ru", """{ "Sit": "Сесть" }"""),
PackDocuments.Locale("addon", "ru", """{ "Sit": "Присесть" }"""),
],
log);
Assert.Equal("Присесть", catalog.Label("ru", catalog.Actions["Sit"]));
Assert.Contains(log.Warnings, warning => warning.Contains("Sit") && warning.Contains("addon"));
}
[Fact]
public void Text_FallsBackToTheKeyWhenMissing()
{
var catalog = _loader.Load(
[CatalogLoader.CorePackId],
[
PackDocuments.Def(CatalogLoader.CorePackId, "actions", "sit", """{ "defName": "Sit", "abstract": true }"""),
PackDocuments.Locale(CatalogLoader.CorePackId, "ru", """{ "Sit": "Сесть" }"""),
]);
Assert.Equal("Сесть", catalog.Text("ru", "Sit"));
Assert.Equal("Skinny", catalog.Text("en", "Skinny"));
}
[Fact]
public void CoreIsAlwaysFirst_EvenIfOmittedFromThePackList()
{
var catalog = _loader.Load(
["addon"],
[
PackDocuments.Def(CatalogLoader.CorePackId, "actions", "sit", """{ "defName": "Sit", "abstract": true }"""),
PackDocuments.Def("addon", "actions", "wave", """{ "defName": "Wave", "abstract": true }"""),
]);
Assert.Equal([CatalogLoader.CorePackId, "addon"], catalog.PackIds);
Assert.True(catalog.Actions.ContainsKey("Sit"));
Assert.True(catalog.Actions.ContainsKey("Wave"));
}
[Fact]
public void ThingActions_MustExist()
{
var ex = Assert.Throws<ContentLoadException>(() => _loader.Load(
[CatalogLoader.CorePackId],
[PackDocuments.Def(CatalogLoader.CorePackId, "things", "chair", """{ "defName": "Chair", "actions": ["Sit"] }""")]));
Assert.Contains("Sit", ex.Message);
}
[Fact]
public void RoomSlotsAndPositions_MustExist()
{
var ex = Assert.Throws<ContentLoadException>(() => _loader.Load(
[CatalogLoader.CorePackId],
[
PackDocuments.Def(
CatalogLoader.CorePackId,
"rooms",
"office",
"""{ "defName": "Office", "slots": [{ "key": "chair", "thing": "Chair" }], "positions": ["Principal"], "travelMinutes": 1 }"""),
]));
Assert.Contains("Chair", ex.Message);
}
[Fact]
public void Label_FallsBackToParentThenDefName()
{
var catalog = _loader.Load(
[CatalogLoader.CorePackId],
[
PackDocuments.Def(CatalogLoader.CorePackId, "things", "base", """{ "defName": "FurnitureBase", "abstract": true }"""),
PackDocuments.Def(CatalogLoader.CorePackId, "things", "chair", """{ "defName": "Chair", "parent": "FurnitureBase" }"""),
PackDocuments.Locale(CatalogLoader.CorePackId, "ru", """{ "FurnitureBase": "Мебель" }"""),
]);
Assert.Equal("Мебель", catalog.Label("ru", catalog.Things["Chair"]));
Assert.Equal("Chair", catalog.Label("en", catalog.Things["Chair"]));
}
[Fact]
public void ConcreteRoomWithoutTravelMinutes_FailsTheCatalog()
{
var ex = Assert.Throws<ContentLoadException>(() => _loader.Load(
[CatalogLoader.CorePackId],
[
PackDocuments.Def(CatalogLoader.CorePackId, "rooms", "office", """{ "defName": "Office" }"""),
]));
Assert.Contains("travelMinutes", ex.Message, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public void ConcreteTerritoryWithoutTravelMinutes_FailsTheCatalog()
{
var ex = Assert.Throws<ContentLoadException>(() => _loader.Load(
[CatalogLoader.CorePackId],
[
PackDocuments.Def(CatalogLoader.CorePackId, "territories", "yard", """{ "defName": "Yard" }"""),
]));
Assert.Contains("travelMinutes", ex.Message, StringComparison.OrdinalIgnoreCase);
}
}