Files
Project-M/Assets/_Project/Tests/EditMode/SavePersistenceTests.cs
T
kronic 66edbdec69 Tests: EB-1 structure damage/death, fortress targeting, persistence v3
10 new EditMode tests (312 total, all green): HealthApplyDamage destroys a Destructible at 0 + a wounded structure survives clamped; PickWeightedNearest x5 (player-only, structure-preferred-by-weight, player-in-the-way wins, raze undefended base, no targets); persistence (StructureSave.HP round-trip + writes v3, v2 in the load floor, SaveApply.ToPending maps the wounded HP - the staging-copy bug the pre-code review caught); + the StructureAggroWeight default pin. See DR-032.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-11 23:53:50 -07:00

147 lines
6.4 KiB
C#

using NUnit.Framework;
using ProjectM.Simulation;
using Unity.Entities;
using UnityEngine;
namespace ProjectM.Tests
{
/// <summary>
/// Pure tests for the save FOUNDATION: the JSON schema round-trips (JsonUtility), version handling is safe,
/// and the born-correct ledger apply (<see cref="SaveApply.WriteLedger"/>) the server spawn system uses to
/// overwrite a director's StorageEntry buffer from a staged PendingSave.
/// </summary>
public class SavePersistenceTests
{
[Test]
public void SaveData_Json_RoundTrip_PreservesFields()
{
var data = new SaveData
{
GoalCharge = 42,
GoalTarget = 10,
Ledger = new[]
{
new LedgerRow { ItemId = 1, Count = 5 },
new LedgerRow { ItemId = 2, Count = 9 },
},
SavedAtMs = 1234567890123L,
};
var json = JsonUtility.ToJson(data);
var back = JsonUtility.FromJson<SaveData>(json);
Assert.AreEqual(SaveData.CurrentVersion, back.Version);
Assert.AreEqual(42, back.GoalCharge);
Assert.AreEqual(10, back.GoalTarget);
Assert.AreEqual(2, back.Ledger.Length);
Assert.AreEqual(1, back.Ledger[0].ItemId);
Assert.AreEqual(5, back.Ledger[0].Count);
Assert.AreEqual(2, back.Ledger[1].ItemId);
Assert.AreEqual(9, back.Ledger[1].Count);
Assert.AreEqual(1234567890123L, back.SavedAtMs);
}
[Test]
public void SaveData_EmptyJson_DoesNotThrow_And_EmptyLedgerRoundTrips()
{
Assert.DoesNotThrow(() => JsonUtility.FromJson<SaveData>("{}"));
var empty = new SaveData { GoalCharge = 0, GoalTarget = 10 };
var back = JsonUtility.FromJson<SaveData>(JsonUtility.ToJson(empty));
Assert.IsNotNull(back.Ledger);
Assert.AreEqual(0, back.Ledger.Length);
}
[Test]
public void SaveData_OldVersion_IsDetectable()
{
// A stale-version blob round-trips with its Version intact, so SaveService.Load rejects it (-> New Game).
var old = new SaveData { Version = 0, GoalCharge = 7 };
var back = JsonUtility.FromJson<SaveData>(JsonUtility.ToJson(old));
Assert.AreEqual(0, back.Version);
Assert.AreNotEqual(SaveData.CurrentVersion, back.Version);
}
[Test]
public void WriteLedger_Overwrites_Destination_From_Staged_Rows()
{
using var world = new World("SaveApplyTest");
var em = world.EntityManager;
var e = em.CreateEntity();
em.AddBuffer<PendingSaveLedgerRow>(e);
em.AddBuffer<StorageEntry>(e);
var src = em.GetBuffer<PendingSaveLedgerRow>(e);
src.Add(new PendingSaveLedgerRow { ItemId = 3, Count = 7 });
src.Add(new PendingSaveLedgerRow { ItemId = 5, Count = 12 });
var dest = em.GetBuffer<StorageEntry>(e);
dest.Add(new StorageEntry { ItemId = 99, Count = 1 }); // pre-existing junk that must be cleared
SaveApply.WriteLedger(em.GetBuffer<PendingSaveLedgerRow>(e), em.GetBuffer<StorageEntry>(e));
var result = em.GetBuffer<StorageEntry>(e);
Assert.AreEqual(2, result.Length);
Assert.AreEqual(3, result[0].ItemId);
Assert.AreEqual(7, result[0].Count);
Assert.AreEqual(5, result[1].ItemId);
Assert.AreEqual(12, result[1].Count);
}
[Test]
public void WriteLedger_EmptySource_ClearsDestination()
{
using var world = new World("SaveApplyEmptyTest");
var em = world.EntityManager;
var e = em.CreateEntity();
em.AddBuffer<PendingSaveLedgerRow>(e);
em.AddBuffer<StorageEntry>(e);
var dest = em.GetBuffer<StorageEntry>(e);
dest.Add(new StorageEntry { ItemId = 1, Count = 1 });
SaveApply.WriteLedger(em.GetBuffer<PendingSaveLedgerRow>(e), em.GetBuffer<StorageEntry>(e));
Assert.AreEqual(0, em.GetBuffer<StorageEntry>(e).Length);
}
[Test]
public void StructureSave_HP_RoundTrips_And_Writes_V3()
{
var data = new SaveData { Structures = new[] { new StructureSave { Type = 1, CellX = 1, CellZ = 2, HP = 37f } } };
var back = JsonUtility.FromJson<SaveData>(JsonUtility.ToJson(data));
Assert.AreEqual(3, back.Version, "EB-1: new saves write v3.");
Assert.AreEqual(1, back.Structures.Length);
Assert.AreEqual(37f, back.Structures[0].HP, 1e-4f, "the wounded HP round-trips through JSON.");
}
[Test]
public void V2_Save_IsWithinLoadableRange_And_ZeroHp_Restores_Full()
{
// A pre-EB-1 v2 save sits inside the additive load floor [Min,Current], so SaveService.Load accepts it;
// an unset HP (0) is mapped by BaseRestoreSystem to the baked Max (structures come back at full HP).
var v2 = new SaveData { Version = 2, GoalCharge = 3, GoalTarget = 10, Structures = new[] { new StructureSave { Type = 1, CellX = 2, CellZ = 4 } } };
var back = JsonUtility.FromJson<SaveData>(JsonUtility.ToJson(v2));
Assert.AreEqual(2, back.Version);
Assert.GreaterOrEqual(back.Version, SaveData.MinLoadableVersion, "v2 is at/above the load floor.");
Assert.LessOrEqual(back.Version, SaveData.CurrentVersion);
Assert.AreEqual(0f, back.Structures[0].HP, 1e-4f, "unset HP (0) -> restore maps to baked Max.");
}
[Test]
public void ToPending_Maps_All_Fields_Including_The_Wounded_HP()
{
// The WorldLauncher save->stage copy: omitting any field here silently restores at full HP (review-caught).
var s = new StructureSave { Type = 1, CellX = 3, CellZ = -2, Direction = 2, RemainingTicks = 50, ConveyorResId = 1, ConveyorCount = 4, HP = 37f };
var p = SaveApply.ToPending(s);
Assert.AreEqual(1, p.Type);
Assert.AreEqual(3, p.CellX);
Assert.AreEqual(-2, p.CellZ);
Assert.AreEqual(2, p.Direction);
Assert.AreEqual(50u, p.RemainingTicks);
Assert.AreEqual(1, p.ConveyorResId);
Assert.AreEqual(4, p.ConveyorCount);
Assert.AreEqual(37f, p.HP, 1e-4f, "the wounded HP survives the save->staging copy.");
}
}
}