diff --git a/tests/Aetheria.Shared.Tests/LoadoutTests.cs b/tests/Aetheria.Shared.Tests/LoadoutTests.cs index f5fb51a04..69258453b 100644 --- a/tests/Aetheria.Shared.Tests/LoadoutTests.cs +++ b/tests/Aetheria.Shared.Tests/LoadoutTests.cs @@ -316,26 +316,71 @@ public sealed class LoadoutTests : IDisposable Assert.Contains("slot 0,0: no available product of Lamp", Assert.Single(failures)); } - // Materialize a hand-built ship, pack it into a zone, commit through Open() and reopen: every crafted instance - // still resolves in RunSave.Lots, its Data still equals the lot's Design, the origin is Attributed with the - // product's maker, and Quality is unchanged from the lot minted before the save. + // F2: this test must not enumerate reloaded instances through EntitySerializer.Items, the GC root walk under + // test. It hand-mints one real lot per EntityPack root (Hull, Equipment, CargoBays, DockingBays, CargoContents, + // Children, DockingBayContents) so the expected lot ids are known independently of any enumerator, and reads + // the reloaded pack's own fields directly. Skipping any one root in Items/Reachable drops that root's lot from + // the stored ledger, which RunSave.Lots' indexer throws loudly on. [Fact] public void MaterializedLotsSurviveSaveAndReload() { - Dictionary preSaveQuality; CultRecordKey maker; - using (var cache = Open()) + int[] expectedLots; + using (var cache = AetheriaStores.Open(Catalog, Run, Player, catalogWritable: true)) { maker = cache.RefOf(cache.GetByName("Maker")).Key; + var dockingBay = cache.Upsert(new DockingBayData { Name = "Bay", Shape = new Shape(), InteriorShape = new Shape(), Price = 20 }); + cache.Upsert(new FactionProductData { Name = "Bay by Maker", Design = new CultRecordRef(dockingBay.Key), Manufacturer = cache.RefOf(cache.GetByName("Maker")) }); + cache.FlushAsync().Wait(); + + var skiff = cache.GetAll().Single(p => p.Name == "Skiff by Maker"); + var lamp = cache.GetAll().Single(p => p.Name == "Lamp by Maker"); + var crate = cache.GetAll().Single(p => p.Name == "Crate by Maker"); + var gun = cache.GetAll().Single(p => p.Name == "Gun by Maker"); + var bay = cache.GetAll().Single(p => p.Name == "Bay by Maker"); + var items = new ItemManager(cache, new ProvenanceLedger(), RunSaveTests.TestSettings(), _ => { }); - var hand = HandBuilt(cache); - var failures = new List(); - var ship = Build(items, hand, failures); - Assert.Empty(failures); - preSaveQuality = items.Lots.Lots.ToDictionary(kv => kv.Key, kv => kv.Value.Quality); + EquippableItem Mint(FactionProductData product) => (EquippableItem) items.CreateInstance(product); + + var hull = Mint(skiff); + var equipment = Mint(lamp); + var cargoBay = Mint(crate); + var dockingBayUnit = Mint(bay); + var cargoContent = Mint(gun); + var dockingBayContent = Mint(lamp); + var childHull = Mint(skiff); + + var childPack = new ShipPack + { + Name = "child", Hull = childHull, + Equipment = Array.Empty<(int2, EquippableItem)>(), CargoBays = Array.Empty<(int2, EquippableItem)>(), + DockingBays = Array.Empty<(int2, EquippableItem)>(), CargoContents = Array.Empty<(int2, ItemInstance)[]>(), + DockingBayContents = Array.Empty<(int2, ItemInstance)[]>(), Children = Array.Empty(), + PersistedBehaviors = Array.Empty<(int2, PersistentBehaviorData[])>(), Temperature = new float[0, 0], + Armor = new float[0, 0], Conductivity = new bool2[0, 0], DockingBayAssignments = Array.Empty(), + Settings = new EntitySettings(), WeaponGroups = Array.Empty() + }; + var rootPack = new ShipPack + { + Name = "root", Hull = hull, + Equipment = new[] { (default(int2), equipment) }, + CargoBays = new[] { (default(int2), cargoBay) }, + DockingBays = new[] { (default(int2), dockingBayUnit) }, + CargoContents = new[] { new[] { (default(int2), (ItemInstance) cargoContent) } }, + DockingBayContents = new[] { new[] { (default(int2), (ItemInstance) dockingBayContent) } }, + Children = new EntityPack[] { childPack }, + DockingBayAssignments = new[] { 0 }, + PersistedBehaviors = Array.Empty<(int2, PersistentBehaviorData[])>(), Temperature = new float[0, 0], + Armor = new float[0, 0], Conductivity = new bool2[0, 0], + Settings = new EntitySettings(), WeaponGroups = Array.Empty() + }; + + expectedLots = new[] + { + hull.Lot, equipment.Lot, cargoBay.Lot, dockingBayUnit.Lot, cargoContent.Lot, dockingBayContent.Lot, childHull.Lot + }; - var pack = EntitySerializer.Pack(ship); var game = new SavedGame { Factions = Array.Empty>(), @@ -351,7 +396,7 @@ public sealed class LoadoutTests : IDisposable new SavedZone { Name = "Zone 0", AdjacentZones = Array.Empty(), Factions = Array.Empty(), Owner = -1, - Contents = new ZonePack { Entities = new List { pack } } + Contents = new ZonePack { Entities = new List { rootPack } } } }; RunSave.Commit(cache, game, zones, items.Lots); @@ -359,18 +404,24 @@ public sealed class LoadoutTests : IDisposable using (var cache = Open()) { - var lots = RunSave.Lots(cache); var run = cache.GetGlobal(); var zone = cache.Get(run.Zones[0]); - var crafted = EntitySerializer.Items(zone.Contents.Entities[0]).OfType().ToArray(); - Assert.NotEmpty(crafted); - foreach (var instance in crafted) + var reloaded = (ShipPack) zone.Contents.Entities[0]; + var reloadedChild = (ShipPack) reloaded.Children[0]; + var lots = RunSave.Lots(cache); + + Assert.Equal(expectedLots.OrderBy(l => l), new[] { - var lot = lots[instance.Lot]; - Assert.Equal(instance.Data.Key, lot.Design.Key); + reloaded.Hull.Lot, reloaded.Equipment[0].item.Lot, reloaded.CargoBays[0].item.Lot, + reloaded.DockingBays[0].item.Lot, ((CraftedItemInstance) reloaded.CargoContents[0][0].item).Lot, + ((CraftedItemInstance) reloaded.DockingBayContents[0][0].item).Lot, reloadedChild.Hull.Lot + }.OrderBy(l => l)); + + foreach (var lotId in expectedLots) + { + var lot = lots[lotId]; var attributed = Assert.IsType(lot.Origin); Assert.Equal(maker, attributed.Faction.Key); - Assert.Equal(preSaveQuality[instance.Lot], lot.Quality); } } }