Files

117 lines
3.7 KiB
C#

using HSchool.Server.Game;
using HSchool.Simulation;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
namespace HSchool.Server.Tests;
public class SchoolStoreTests
{
[Fact]
public void LoadAll_SkipsUsersJsonBesideASchoolSave()
{
var directory = Directory.CreateTempSubdirectory("h-school-store-");
try
{
File.WriteAllText(
Path.Combine(directory.FullName, UserStore.FileName),
"""{ "users": [ { "name": "Player" } ] }""");
File.WriteAllText(
Path.Combine(directory.FullName, "1.json"),
"""
{
"format": 3,
"id": 1,
"name": "North",
"gameTime": "2012-03-31T06:00:00Z",
"running": false,
"speedIndex": 0
}
""");
var store = CreateStore(directory.FullName);
var saves = store.LoadAll();
var save = Assert.Single(saves);
Assert.Equal(1, save.Id);
Assert.Equal("North", save.Name);
}
finally
{
directory.Delete(recursive: true);
}
}
[Fact]
public void Save_RoundTripsStickyNotices_AndOmitsThemWhenMissing()
{
var directory = Directory.CreateTempSubdirectory("h-school-store-");
try
{
var store = CreateStore(directory.FullName);
store.Save(new SchoolSave
{
Format = 3,
Id = 2,
Name = "Sticky",
GameTime = new DateTime(2012, 3, 31, 6, 0, 0, DateTimeKind.Utc),
Running = false,
SpeedIndex = 1,
Notices =
[
new StickyNoticeSave
{
Id = 9,
DefName = "GenerationFailed",
Severity = 2,
Pause = true,
TtlMs = 0,
},
],
});
var withNotices = Assert.Single(store.LoadAll());
var sticky = Assert.Single(withNotices.Notices!);
Assert.Equal(9u, sticky.Id);
Assert.Equal("GenerationFailed", sticky.DefName);
Assert.True(sticky.Pause);
File.WriteAllText(
Path.Combine(directory.FullName, "3.json"),
"""
{
"format": 3,
"id": 3,
"name": "Plain",
"gameTime": "2012-03-31T06:00:00Z",
"running": false,
"speedIndex": 0
}
""");
var plain = store.LoadAll().Single(save => save.Id == 3);
Assert.True(plain.Notices is null || plain.Notices.Count == 0);
}
finally
{
directory.Delete(recursive: true);
}
}
private static SchoolStore CreateStore(string directory)
{
var options = Options.Create(new SimulationOptions { SavesDirectory = directory });
return new SchoolStore(options, new StubHost(), NullLogger<SchoolStore>.Instance);
}
private sealed class StubHost : IHostEnvironment
{
public string ApplicationName { get; set; } = "HSchool.Server.Tests";
public string EnvironmentName { get; set; } = "Development";
public string ContentRootPath { get; set; } = Path.GetTempPath();
public IFileProvider ContentRootFileProvider { get; set; } = new NullFileProvider();
}
}