using System; using System.IO; using NUnit.Framework; namespace Ironspore.Tests { public sealed class ProfileStoreTests { string folder; string path; ProfileStore store; [SetUp] public void Setup() { folder = Path.Combine(Path.GetTempPath(), "IronsporeLedgerTests", Guid.NewGuid().ToString("N")); path = Path.Combine(folder, "profile.json"); store = new ProfileStore(path); Assert.IsTrue(store.Begin(1, new[] { 0, 0, 0, 0 }, 180, 7)); } [TearDown] public void Cleanup() { string allowed = Path.GetFullPath(Path.Combine(Path.GetTempPath(), "IronsporeLedgerTests")) + Path.DirectorySeparatorChar; if (Path.GetFullPath(folder).StartsWith(allowed, StringComparison.OrdinalIgnoreCase) && Directory.Exists(folder)) Directory.Delete(folder, true); } [Test] public void PodBanksOnceAndSurvivesDeathAndReload() { Assert.IsTrue(store.AddCargo("blueprint_vector", 1, "crateA")); Assert.IsTrue(store.AddCargo("alloy", 4, "crateB")); string uid = store.Data.run.cargo[0].uid; Assert.IsTrue(store.SendCargo(new[] { uid })); Assert.IsFalse(store.SendCargo(new[] { uid })); Assert.IsTrue(store.Finish(true)); var loaded = new ProfileStore(path); Assert.AreEqual(1, loaded.Data.blueprints.Count); Assert.AreEqual(0, loaded.Data.alloy); Assert.IsFalse(loaded.Data.run.active); Assert.IsTrue(loaded.Data.run.podUsed); } [Test] public void PodRejectsOvercapacityAndDuplicateSelectionsWithoutConsuming() { for (int i = 0; i < 3; i++) Assert.IsTrue(store.AddCargo("blueprint_" + i, 1, "crate" + i)); var bag = store.Data.run.cargo; Assert.IsFalse(store.SendCargo(new[] { bag[0].uid, bag[1].uid, bag[2].uid })); Assert.IsFalse(store.SendCargo(new[] { bag[0].uid, bag[0].uid })); Assert.IsFalse(store.Data.run.podUsed); Assert.AreEqual(3, store.Data.run.cargo.Count); Assert.AreEqual(0, store.Data.blueprints.Count); } [Test] public void SearchedSourceCannotDuplicateLootAfterReload() { Assert.IsTrue(store.AddCargo("alloy", 5, "crateA")); store = new ProfileStore(path); Assert.IsFalse(store.AddCargo("alloy", 5, "crateA")); Assert.AreEqual(5, store.Data.run.cargo[0].count); Assert.IsTrue(store.Finish(false)); Assert.IsFalse(store.Finish(false)); Assert.AreEqual(5, store.Data.alloy); Assert.AreEqual(1, store.Data.extractions); } [Test] public void CargoThenWholeMechReturnAreIndependentReceipts() { store.AddCargo("alloy", 4); store.AddCargo("cells", 3); Assert.IsTrue(store.SendCargo(new[] { store.Data.run.cargo[0].uid })); Assert.IsTrue(store.CompleteRoute()); Assert.IsFalse(store.CompleteRoute()); Assert.IsTrue(store.Finish(false)); Assert.AreEqual(4, store.Data.alloy); Assert.AreEqual(3, store.Data.cells); Assert.AreEqual(1, store.Data.completed); Assert.AreEqual(1, store.Data.extractions); } [Test] public void WriteFailureLeavesOriginalMemoryState() { string blocker = Path.Combine(folder, "blocked"); File.WriteAllText(blocker, "not a directory"); var unavailable = new ProfileStore(Path.Combine(blocker, "save.json")); Assert.IsFalse(unavailable.Begin(0, new[] { 0, 0, 0, 0 }, 100, 1)); Assert.IsFalse(unavailable.Data.run.active); Assert.IsNotEmpty(unavailable.Error); } [Test] public void CorruptPrimaryRecoversPreviousCompleteSnapshot() { store.AddCargo("cells", 2); store.Commit(s => true); File.WriteAllText(path, "{broken json"); var recovered = new ProfileStore(path); Assert.IsTrue(recovered.Data.run.active); Assert.AreEqual(2, recovered.Data.run.cargo[0].count); } } }