From f54dada78694b764d05a6ff3f186f367213063f6 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 17 Oct 2026 15:59:01 +0200 Subject: [PATCH] Tests: minor powers are ordinary factions, command idempotence and conflict, Pirates top-up, shipped minor-powers rows These fail until the catalog commit lands. Co-Authored-By: Claude Opus 5.5 --- .../Aetheria.Shared.Tests/MinorPowersTests.cs | 138 ++++++++++++++++++ .../PiratesFactionCommandTests.cs | 39 ++++- .../ProductLinesCatalogTests.cs | 10 +- 3 files changed, 181 insertions(+), 6 deletions(-) create mode 100644 tests/Aetheria.Shared.Tests/MinorPowersTests.cs diff --git a/tests/Aetheria.Shared.Tests/MinorPowersTests.cs b/tests/Aetheria.Shared.Tests/MinorPowersTests.cs new file mode 100644 index 000000000..8990ae9fa --- /dev/null +++ b/tests/Aetheria.Shared.Tests/MinorPowersTests.cs @@ -0,0 +1,138 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +using System; +using System.IO; +using System.Linq; +using System.Reflection; +using CultMath; +using GameCult.Caching; +using GameCult.Caching.MessagePack; +using Xunit; + +// The Aya Collective, Megiddo and Orbital Forge (aetheria-release, cut minor-power-factions; ruling no-territory-ban): three +// ordinary factions in the shipped catalog, authored by AetherDb's minor-power-factions command, which a scratch copy of the +// catalog without them proves idempotent and conflict-refusing. +public sealed class MinorPowersTests : IDisposable +{ + private static readonly string[] Minor = { "Aya", "Megiddo", "Forge" }; + + private readonly string _root = Path.Combine(Path.GetTempPath(), "minor-powers-" + Guid.NewGuid().ToString("N")); + private string CatalogPath => Path.Combine(_root, "GameData", "Aetheria.cc"); + + public void Dispose() + { + if (Directory.Exists(_root)) Directory.Delete(_root, true); + } + + // A scratch copy of the shipped catalog with the three, their products and every allegiance toward them removed, through a + // registry scoped to the shipped assembly (as PiratesFactionCommandTests does), so the command has all of it to author. + private void StripTheMinorPowers() + { + Directory.CreateDirectory(Path.Combine(_root, "GameData")); + File.Copy(Path.Combine(AetherDb.FindRoot(), "GameData", "Aetheria.cc"), CatalogPath); + using var cache = new CultCache(TestCatalog.Registry()); + cache.AddBackingStore(new SingleFileMessagePackBackingStore(CatalogPath), AetheriaStores.CatalogTypes); + var gone = cache.GetAll().Where(f => Minor.Contains(f.ShortName)).Select(f => cache.RefOf(f)).ToArray(); + Assert.Equal(3, gone.Length); + foreach (var product in cache.GetAll().Where(p => gone.Any(g => g.Key.Equals(p.Manufacturer.Key))).ToArray()) + Assert.True(cache.Remove(cache.RefOf(product).Key)); + foreach (var reference in gone) Assert.True(cache.Remove(reference.Key)); + foreach (var faction in cache.GetAll()) + foreach (var key in faction.Allegiance.Keys.Where(k => gone.Any(g => g.Key.Equals(k.Key))).ToArray()) + faction.Allegiance.Remove(key); + cache.Commit(batch => + { + foreach (var faction in cache.GetAll()) batch.Upsert(typeof(Faction), faction, cache.RefOf(faction).Key); + }); + cache.FlushAsync().Wait(); + } + + [Fact] + public void TheMinorPowersAreOrdinaryFactions() + { + using var cache = RestoredHullsTests.OpenCatalog(); + var factions = cache.GetAll().ToArray(); + foreach (var shortName in Minor) + { + var faction = Assert.Single(factions, f => f.ShortName == shortName); + Assert.NotNull(cache.Get(faction.GeonameFile)); + Assert.True(faction.InfluenceDistance > 0, shortName); + var named = faction.Allegiance.Keys.Select(k => k.Key).ToArray(); + var expected = factions.Where(f => f != faction && f.ShortName != "Pirates").Select(f => cache.RefOf(f).Key).ToArray(); + Assert.Equal(expected.Length, named.Length); + Assert.All(expected, key => Assert.Contains(named, n => n.Equals(key))); + } + // Territory is nothing a faction can say of itself: no member of Faction marks a maker-only or landless power. + Assert.DoesNotContain(typeof(Faction).GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic), + m => m.Name.Contains("ManufacturerOnly") || m.Name.Contains("Territory")); + } + + [Fact] + public void TheCommandIsIdempotentAndRefusesAConflict() + { + StripTheMinorPowers(); + var before = File.ReadAllBytes(CatalogPath); + Assert.Equal(0, MinorPowerFactionsCommand.Run(new string[0], _root)); + Assert.Equal(before, File.ReadAllBytes(CatalogPath)); + + Assert.Equal(0, MinorPowerFactionsCommand.Run(new[] { "apply" }, _root)); + var landed = File.ReadAllBytes(CatalogPath); + Assert.NotEqual(before, landed); + var db = AetherDb.Open(root: _root); + try + { + var factions = db.Cache.GetAll().ToArray(); + foreach (var shortName in Minor) Assert.Single(factions, f => f.ShortName == shortName); + var forge = db.Cache.RefOf(factions.Single(f => f.ShortName == "Forge")); + Assert.Equal(.5f, factions.Single(f => f.ShortName == "AU").Allegiance[forge]); + // The Pirates' allegiance is pirates-faction's; Miss Terri's is her own. + Assert.All(factions.Where(f => f.ShortName is "Pirates" or "Miss Terri's"), f => Assert.DoesNotContain(f.Allegiance.Keys, k => k.Equals(forge))); + } + finally { db.Cache.Dispose(); } + + Assert.Equal(0, MinorPowerFactionsCommand.Run(new[] { "apply" }, _root)); + Assert.Equal(landed, File.ReadAllBytes(CatalogPath)); + } + + [Fact] + public void ANameHeldUnderAnotherKeyIsRefusedWithNothingWritten() + { + StripTheMinorPowers(); + var squatter = AetherDb.Open(catalogWritable: true, root: _root); + squatter.Cache.Commit(batch => batch.Upsert(typeof(Faction), new Faction { Name = "Megiddo", ShortName = "Megiddo" })); + squatter.Cache.Dispose(); + var before = File.ReadAllBytes(CatalogPath); + Assert.Throws(() => MinorPowerFactionsCommand.Run(new[] { "apply" }, _root)); + Assert.Equal(before, File.ReadAllBytes(CatalogPath)); + } +} + +public sealed partial class RunStartTests +{ + // Forge holds no zone in the prelude, so its gear reaches AU only through allegiance (ruling gear-without-presence); a + // design-only slot draws among the makers AU can reach (ruling design-only-weighted-draw). Miss Terri's names none of them. + [Fact] + public void TheirGearSellsWhereTheyAreAbsent() + { + var factions = _cache.GetAll().ToArray(); + var forge = factions.Single(f => f.ShortName == "Forge"); + Assert.DoesNotContain(_galaxy.Factions, f => f == forge); + var autocannon = _cache.GetAll().Single(d => d.Name == "Autocannon"); + var pattern30 = _cache.GetAll().Single(p => p.Name == "Pattern 30"); + + LoadoutGenerator For(string shortName, uint seed) + { + _items.Random = new CultMath.Random(seed); + return new LoadoutGenerator(ref _items.Random, _items, _galaxy, _galaxy.Entrance, factions.Single(f => f.ShortName == shortName), .5f); + } + Assert.True(For("AU", 1).IsAvailable(pattern30)); + Assert.Contains(Enumerable.Range(1, 64), seed => For("AU", (uint) seed).ChooseProduct(autocannon)?.Name == pattern30.Name); + + var terri = For("Miss Terri's", 1); + var theirs = _cache.GetAll().Where(p => new[] { "Aya", "Megiddo", "Forge" }.Contains(_cache.Get(p.Manufacturer).ShortName)).ToArray(); + Assert.Equal(10, theirs.Length); + Assert.All(theirs, product => Assert.False(terri.IsAvailable(product), product.Name)); + } +} diff --git a/tests/Aetheria.Shared.Tests/PiratesFactionCommandTests.cs b/tests/Aetheria.Shared.Tests/PiratesFactionCommandTests.cs index 7ccce7cc8..67e91bde1 100644 --- a/tests/Aetheria.Shared.Tests/PiratesFactionCommandTests.cs +++ b/tests/Aetheria.Shared.Tests/PiratesFactionCommandTests.cs @@ -105,17 +105,52 @@ public sealed class PiratesFactionCommandTests : IDisposable Assert.Equal(landed, File.ReadAllBytes(CatalogPath)); } + // An existing hand-authored Pirates record is topped up, never rewritten (follow-up pirates-allegiance-and-identity): it keeps + // its description, short name and record key and gains no products; after a faction is added, apply gives allegiance 1.0 to + // that faction alone; a second apply leaves the file bytes unchanged and a dry run writes nothing. [Fact] - public void An_existing_Pirates_faction_is_left_as_it_is() + public void An_existing_Pirates_faction_is_topped_up_with_allegiance_and_nothing_else() { var db = AetherDb.Open(catalogWritable: true, root: _root); db.Cache.Commit(batch => batch.Upsert(typeof(Faction), new Faction { Name = "Pirates", ShortName = "Mine", Description = "authored by hand" })); + var key = db.Cache.RefOf(db.Cache.GetAll().Single(f => f.Name == "Pirates")).Key; db.Cache.Dispose(); + var beforeDry = File.ReadAllBytes(CatalogPath); + Assert.Equal(0, Program.PiratesFaction(apply: false, root: _root)); + Assert.Equal(beforeDry, File.ReadAllBytes(CatalogPath)); + Assert.Equal(0, Program.PiratesFaction(apply: true, root: _root)); var (pirates, products) = Read(); - Assert.Equal("authored by hand", Assert.Single(pirates).Description); + var record = Assert.Single(pirates); + Assert.Equal("authored by hand", record.Description); + Assert.Equal("Mine", record.ShortName); Assert.Empty(products); + var topped = File.ReadAllBytes(CatalogPath); + + var late = AetherDb.Open(catalogWritable: true, root: _root); + late.Cache.Commit(batch => batch.Upsert(typeof(Faction), new Faction { Name = "Latecomer", ShortName = "Late" })); + var lateRef = late.Cache.RefOf(late.Cache.GetAll().Single(f => f.Name == "Latecomer")); + var others = late.Cache.GetAll().Count(f => f.Name != "Pirates"); + late.Cache.Dispose(); + + Assert.Equal(0, Program.PiratesFaction(apply: true, root: _root)); + var check = AetherDb.Open(root: _root); + try + { + var after = check.Cache.GetAll().Single(f => f.Name == "Pirates"); + Assert.Equal(key, check.Cache.RefOf(after).Key); + Assert.Equal("authored by hand", after.Description); + Assert.Equal(others, after.Allegiance.Count); + Assert.Equal(1f, after.Allegiance[lateRef]); + Assert.All(after.Allegiance.Values, value => Assert.Equal(1f, value)); + } + finally { check.Cache.Dispose(); } + + var settled = File.ReadAllBytes(CatalogPath); + Assert.NotEqual(topped, settled); + Assert.Equal(0, Program.PiratesFaction(apply: true, root: _root)); + Assert.Equal(settled, File.ReadAllBytes(CatalogPath)); } [Fact] diff --git a/tests/Aetheria.Shared.Tests/ProductLinesCatalogTests.cs b/tests/Aetheria.Shared.Tests/ProductLinesCatalogTests.cs index 756e22355..0d5095da7 100644 --- a/tests/Aetheria.Shared.Tests/ProductLinesCatalogTests.cs +++ b/tests/Aetheria.Shared.Tests/ProductLinesCatalogTests.cs @@ -45,12 +45,14 @@ public sealed class ProductLinesCatalogTests Assert.Empty(shared); } - [Fact] - public void WeaponLineRowsAreShipped() + [Theory] + [InlineData("weapons", 20)] + [InlineData("minor-powers", 10)] + public void LineRowsAreShipped(string line, int rows) { using var cache = RestoredHullsTests.OpenCatalog(); - var table = ProductLinesCommand.Table("weapons"); - Assert.Equal(20, table.Count); + var table = ProductLinesCommand.Table(line); + Assert.Equal(rows, table.Count); foreach (var row in table) { var product = Assert.Single(cache.GetAll(), p => p.Name == row.Name);