From 1f1d0bfa3549f7141d3ada0f5e5e6f81952484a0 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Mon, 12 Oct 2026 19:31:53 +0200 Subject: [PATCH] Tests: the gate holds each of the five parts, catalog load and Upsert run it, consumables are refused, and the ship-sum separation is read on a ship with drives and a sensor Builds (unverified until the next ygg run). Co-Authored-By: Claude Opus 5.5 --- .../MunitionPartsTests.cs | 142 +++++++++++++++++- 1 file changed, 135 insertions(+), 7 deletions(-) diff --git a/tests/Aetheria.Shared.Tests/MunitionPartsTests.cs b/tests/Aetheria.Shared.Tests/MunitionPartsTests.cs index a7da2c8c2..25f42f1c9 100644 --- a/tests/Aetheria.Shared.Tests/MunitionPartsTests.cs +++ b/tests/Aetheria.Shared.Tests/MunitionPartsTests.cs @@ -3,6 +3,8 @@ using System.Collections.Generic; using System.IO; using System.Linq; using GameCult.Caching; +using GameCult.Caching.MessagePack; +using CultMath; using Newtonsoft.Json; using Xunit; @@ -150,11 +152,18 @@ public sealed class MunitionPartsTests : IDisposable { CultRecordRefs.Validate(Munition("Complete", new LauncherData())); - var missing = Munition("No Tank", new LauncherData()); - missing.Behaviors.RemoveAll(b => b is MunitionFuelData); - var refusal = Assert.Throws(() => CultRecordRefs.Validate(missing)); - Assert.Contains("No Tank", refusal.Message); - Assert.Contains(nameof(MunitionFuelData), refusal.Message); + // Each of the five parts is required, on either launcher behaviour: a design missing it is refused naming the design + // and the part. + InvalidOperationException refusal; + foreach (var part in AllParts) + foreach (var launcher in new Func[] { () => new LauncherData(), () => new GuidedWeaponData() }) + { + var missing = Munition("No " + part.Name, launcher()); + missing.Behaviors.RemoveAll(b => b.GetType() == part); + refusal = Assert.Throws(() => CultRecordRefs.Validate(missing)); + Assert.Contains(missing.Name, refusal.Message); + Assert.Contains(part.Name, refusal.Message); + } var doubled = Munition("Two Seekers", new GuidedWeaponData()); doubled.Behaviors.Add(new MunitionSeekerData()); @@ -167,9 +176,78 @@ public sealed class MunitionPartsTests : IDisposable refusal = Assert.Throws(() => CultRecordRefs.Validate(stray)); Assert.Contains("Stray Part", refusal.Message); Assert.Contains(nameof(MunitionHullData), refusal.Message); + + // A consumable is a design too, and carries no munition part. + var potion = new ConsumableItemData { Name = "Stray Potion" }; + potion.Behaviors.Add(new MunitionFuelData()); + refusal = Assert.Throws(() => CultRecordRefs.Validate(potion)); + Assert.Contains("Stray Potion", refusal.Message); + Assert.Contains(nameof(MunitionFuelData), refusal.Message); } - // A GT 3K fitted to a Longinus launcher hardpoint, its lot's thruster role at the given quality. + private static readonly Type[] AllParts = + { typeof(MunitionHullData), typeof(MunitionThrusterData), typeof(MunitionSeekerData), typeof(MunitionControlData), typeof(MunitionFuelData) }; + + // A catalog file holding the shipped catalog with one design edited around validation (the way a hand-edited file or a + // bare-cache tool writes), for the production load path to meet. + private string Forged(Func pick, Action edit) where T : ItemData + { + var path = Path.Combine(_root, "forged-" + Guid.NewGuid().ToString("N") + ".cc"); + File.Copy(TestCatalog.Repo, path); + var cache = new CultCache(TestCatalog.Registry()); + cache.AddBackingStore(new SingleFileMessagePackBackingStore(path, false), AetheriaStores.CatalogTypes); + var design = cache.GetAll().First(pick); + edit(design); + var key = cache.RefOf(design).Key; + cache.Commit(batch => batch.Upsert(design.GetType(), design, key)); + cache.FlushAsync().Wait(); + cache.Dispose(); + return path; + } + + [Fact] + public void CatalogLoadRefusesAnIncompleteOrStrayMunitionPart() + { + using (AetheriaStores.Open(Forged(d => d.Name == "GT 3K", _ => { }), registry: TestCatalog.Registry())) { } + + foreach (var part in AllParts) + { + var path = Forged(d => d.Name == "GT 3K", d => d.Behaviors.RemoveAll(b => b.GetType() == part)); + var refusal = Assert.ThrowsAny(() => AetheriaStores.Open(path, registry: TestCatalog.Registry())); + Assert.Contains("GT 3K", refusal.ToString()); + Assert.Contains(part.Name, refusal.ToString()); + } + + var gear = Forged(d => d.Behaviors.Any(b => b is ThrusterData), d => d.Behaviors.Add(new MunitionHullData())); + Assert.Contains(nameof(MunitionHullData), Assert.ThrowsAny(() => AetheriaStores.Open(gear, registry: TestCatalog.Registry())).ToString()); + + var consumable = Forged(_ => true, d => d.Behaviors.Add(new MunitionFuelData())); + Assert.Contains(nameof(MunitionFuelData), Assert.ThrowsAny(() => AetheriaStores.Open(consumable, registry: TestCatalog.Registry())).ToString()); + } + + [Fact] + public void UpsertRefusesAnIncompleteOrStrayMunitionPart() + { + using var cache = AetheriaStores.Open(Catalog, catalogWritable: true); + cache.Upsert(new VerseGrammar { Revision = 1 }); + cache.Upsert(Munition("Whole", new LauncherData())); + + var incomplete = Munition("Incomplete", new GuidedWeaponData()); + incomplete.Behaviors.RemoveAll(b => b is MunitionThrusterData); + Assert.Contains(nameof(MunitionThrusterData), Assert.Throws(() => cache.Upsert(incomplete)).Message); + Assert.DoesNotContain(cache.GetAll(), d => d.Name == "Incomplete"); + + var potion = new ConsumableItemData { Name = "Stray Potion" }; + potion.Behaviors.Add(new MunitionControlData()); + Assert.Contains(nameof(MunitionControlData), Assert.Throws(() => cache.Upsert(potion)).Message); + } + + // The Longinus's own gear: two drives, a core, a cockpit and a sensor, so the ship has thrusters, a sensor and a plume + // for any munition contribution to be seen against. + private static readonly (string Name, int X, int Y)[] ShipGear = + { ("Large Drive", 1, 0), ("Large Drive", 3, 0), ("Core Power", 2, 4), ("Cockpit 2x2", 2, 6), ("not if i see you first", 3, 10) }; + + // A GT 3K fitted to a Longinus launcher hardpoint beside that gear, its lot's thruster role at the given quality. private static EquippedItem Fit(CultCache cache, float thrusterQuality, out Ship ship) { var hull = cache.GetByName("Longinus"); @@ -182,6 +260,11 @@ public sealed class MunitionPartsTests : IDisposable var items = new ItemManager(cache, ledger, settings, _ => { }); var zone = new Zone(items, new PlanetSettings(), new ZonePack(), new GalaxyZone { Name = "Test", Owner = null }, null); ship = new Ship(items, zone, new EquippableItem { Data = cache.RefOf(hull), Durability = hull.Durability, Lot = 1 }, settings.DefaultEntitySettings); + foreach (var (name, x, y) in ShipGear) + { + var gear = cache.GetByName(name); + Assert.True(ship.TryEquip(new EquippableItem { Data = cache.RefOf(gear), Durability = gear.Durability, Lot = 1 }, new int2(x, y)), name); + } Assert.True(ship.TryEquip(new EquippableItem { Data = cache.RefOf(gt), Durability = gt.Durability, Lot = 2 }, hardpoint.Position)); zone.Entities.Add(ship); ship.Activate(); @@ -211,7 +294,52 @@ public sealed class MunitionPartsTests : IDisposable Assert.InRange(low, part.Acceleration.Min, part.Acceleration.Max); Assert.InRange(high, part.Acceleration.Min, part.Acceleration.Max); Assert.True(poor.Evaluate(part.MaxSpeed) < good.Evaluate(part.MaxSpeed)); - Assert.Empty(poorShip.GetBehaviors()); Assert.DoesNotContain(poor.Behaviors, b => b is Thruster); + Assert.Equal(ShipGear.Count(g => cache.GetByName(g.Name).Behaviors.Any(b => b is ThrusterData)), poorShip.GetBehaviors().Count()); + } + + private static string ShipSums(Ship s) + { + var e = s.Envelope; + var channels = string.Join(",", Enum.GetValues(typeof(SignatureChannel)).Cast().Select(c => $"{c}={s.SignatureOn(c):0.####}")); + var sensors = string.Join(",", s.GetBehaviors().Select(b => b.Data.GetType().Name)); + return $"env F{e.Forward:0.###} R{e.Reverse:0.###} L{e.Left:0.###} Ri{e.Right:0.###} CW{e.Clockwise:0.###} CCW{e.CounterClockwise:0.###} " + + $"vis={s.Visibility:0.####} sources={s.VisibilitySources.Count} {channels} thrusters={s.GetBehaviors().Count()} sensors=[{sensors}]"; + } + + private static void ScaleParts(EquippableItemData design, float factor) + { + foreach (var part in design.Behaviors.OfType()) + foreach (var field in part.GetType().GetFields().Where(f => f.FieldType == typeof(PerformanceStat))) + { + var stat = (PerformanceStat) field.GetValue(part); + stat.Min *= factor; + stat.Max *= factor; + } + } + + // A munition's thruster and seeker are not the ship's: with real drives and a sensor aboard, the ship's envelope, plume, + // signatures, thrusters and sensors do not move when the fitted launcher's part stats are a thousand times larger, while + // the value the game reads from the part does. + [Fact] + public void ShipSumsIgnoreTheFittedMunitionParts() + { + using var cache = RestoredHullsTests.OpenCatalog(); + var design = cache.GetByName("GT 3K"); + var part = Part(design); + var item = Fit(cache, .5f, out var ship); + var shipBefore = ShipSums(ship); + var partBefore = item.Evaluate(part.Acceleration); + Assert.NotEmpty(ship.GetBehaviors()); + Assert.NotEmpty(ship.GetBehaviors()); + + ScaleParts(design, 1000f); + try + { + var scaledItem = Fit(cache, .5f, out var scaledShip); + Assert.True(scaledItem.Evaluate(part.Acceleration) > partBefore * 100, "the scaled record must move the value the game reads"); + Assert.Equal(shipBefore, ShipSums(scaledShip)); + } + finally { ScaleParts(design, .001f); } } }