diff --git a/tests/Aetheria.Shared.Tests/ProductLinesCatalogTests.cs b/tests/Aetheria.Shared.Tests/ProductLinesCatalogTests.cs new file mode 100644 index 000000000..a4cfa7ac0 --- /dev/null +++ b/tests/Aetheria.Shared.Tests/ProductLinesCatalogTests.cs @@ -0,0 +1,67 @@ +using System; +using System.Linq; +using GameCult.Caching; +using Xunit; + +// The weapon product lines the shipped catalog holds (campaign aetheria-release, cut product-lines-weapons; rulings +// catalog-grows-generic-designs-branded-products, maker-design-tiers-allowed): a sold weapon design has several brands, a +// brand name is one product's, and every row of the command's table is in the catalog with the quality its maker's profile gives. +// The rows come from the command's own table and the profiles from its own maker-profile table, so a row edited in one place +// and not shipped fails here. +public sealed class ProductLinesCatalogTests +{ + // A design with any product is sold; the Smoke test designs have none and stay exempt. + [Fact] + public void EveryWeaponDesignWithAProductHasTwoMakers() + { + using var cache = RestoredHullsTests.OpenCatalog(); + var products = cache.GetAll().ToArray(); + var sold = 0; + foreach (var design in cache.GetAll()) + { + var key = cache.RefOf(design).Key; + var makers = products.Where(p => p.Design.Key.Equals(key)).Select(p => p.Manufacturer.Key).Distinct().Count(); + if (makers == 0) continue; + sold++; + Assert.True(makers >= 2, $"the weapon design \"{design.Name}\" is sold by {makers} maker"); + } + Assert.True(sold >= 20, "the catalog should sell many weapon designs"); + } + + [Fact] + public void NoTwoProductsShareAName() + { + using var cache = RestoredHullsTests.OpenCatalog(); + var shared = cache.GetAll() + .GroupBy(p => p.Name, StringComparer.OrdinalIgnoreCase) + .Where(group => group.Count() > 1) + .Select(group => group.Key) + .ToArray(); + Assert.Empty(shared); + } + + [Fact] + public void WeaponLineRowsAreShipped() + { + using var cache = RestoredHullsTests.OpenCatalog(); + var table = ProductLinesCommand.Table("weapons"); + Assert.Equal(20, table.Count); + foreach (var row in table) + { + var product = Assert.Single(cache.GetAll(), p => p.Name == row.Name); + var design = cache.Get(product.Design); + Assert.Equal(row.Design, design.Name); + Assert.Equal(row.Maker, cache.Get(product.Manufacturer).ShortName); + Assert.Equal(row.Description, product.Description); + var (mean, dev) = Program.MakerProfiles[row.Maker]; + Assert.Equal( + design.Roles.Select(r => r.Name).OrderBy(n => n, StringComparer.Ordinal), + product.Roles.Select(r => r.Role).OrderBy(n => n, StringComparer.Ordinal)); + foreach (var role in product.Roles) + { + Assert.Equal(mean, role.Mean, 4); + Assert.Equal(dev, role.StandardDeviation, 4); + } + } + } +} diff --git a/tests/Aetheria.Shared.Tests/ProductLinesCommandTests.cs b/tests/Aetheria.Shared.Tests/ProductLinesCommandTests.cs new file mode 100644 index 000000000..ffa69d59e --- /dev/null +++ b/tests/Aetheria.Shared.Tests/ProductLinesCommandTests.cs @@ -0,0 +1,169 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using GameCult.Caching; +using GameCult.Caching.MessagePack; +using Xunit; + +// AetherDb's product-lines command (cut product-lines-weapons), run on a scratch copy of the shipped catalog with the weapon +// line's twenty products removed from it. Apply must write what the shipped catalog holds, only its own records, finish a +// catalog that holds part of the line, refuse a name that is another product's, and accept a maker that already sells the design. +public sealed class ProductLinesCommandTests : IDisposable +{ + private static readonly string[] Names = ProductLinesCommand.Table("weapons").Select(r => r.Name).ToArray(); + private static readonly string[] Line = { "weapons" }; + private static readonly string[] LineApply = { "weapons", "apply" }; + + private readonly string _root = Path.Combine(Path.GetTempPath(), "product-lines-command-" + Guid.NewGuid().ToString("N")); + private string CatalogPath => Path.Combine(_root, "GameData", "Aetheria.cc"); + + // The rows as the shipped catalog holds them, taken before they are removed from the scratch copy. + private readonly string[] _shipped; + + private static string Describe(CultCache cache, FactionProductData product) => + $"{cache.Get(product.Design).Name} | {product.Name} | {cache.Get(product.Manufacturer).ShortName} | {product.Description} | " + + string.Join(",", product.Roles.OrderBy(r => r.Role, StringComparer.Ordinal).Select(r => $"{r.Role}:{r.Mean}:{r.StandardDeviation}")); + + public ProductLinesCommandTests() + { + Directory.CreateDirectory(Path.Combine(_root, "GameData")); + Directory.CreateDirectory(Path.Combine(_root, "Assets", "Resources")); + var shipped = AetherDb.FindRoot(); + File.Copy(Path.Combine(shipped, "GameData", "Aetheria.cc"), CatalogPath); + File.Copy(Path.Combine(shipped, "Assets", "Resources", "Settings.asset"), Path.Combine(_root, "Assets", "Resources", "Settings.asset")); + using var cache = Open(); + var products = cache.GetAll().Where(p => Names.Contains(p.Name)).ToArray(); + Assert.Equal(Names.Length, products.Length); + _shipped = products.Select(p => Describe(cache, p)).OrderBy(x => x, StringComparer.Ordinal).ToArray(); + foreach (var product in products) Assert.True(cache.Remove(cache.RefOf(product).Key)); + cache.FlushAsync().Wait(); + } + + public void Dispose() => Directory.Delete(_root, true); + + private CultCache Open() + { + var cache = new CultCache(TestCatalog.Registry()); + cache.AddBackingStore(new SingleFileMessagePackBackingStore(CatalogPath), AetheriaStores.CatalogTypes); + return cache; + } + + private string[] Line20() + { + using var cache = Open(); + return cache.GetAll().Where(p => Names.Contains(p.Name)).Select(p => Describe(cache, p)).OrderBy(x => x, StringComparer.Ordinal).ToArray(); + } + + [Fact] + public void A_dry_run_writes_nothing_and_apply_lands_the_shipped_rows_once() + { + var before = File.ReadAllBytes(CatalogPath); + Assert.Empty(Line20()); + + Assert.Equal(0, ProductLinesCommand.Run(Line, _root)); + Assert.Equal(before, File.ReadAllBytes(CatalogPath)); + Assert.Empty(Line20()); + + Assert.Equal(0, ProductLinesCommand.Run(LineApply, _root)); + var landed = File.ReadAllBytes(CatalogPath); + Assert.NotEqual(before, landed); + // Record keys are CultCache's to mint and differ run to run; the rows do not. + Assert.Equal(_shipped, Line20()); + + // A second apply finds everything authored and writes nothing; so does a dry run. + Assert.Equal(0, ProductLinesCommand.Run(LineApply, _root)); + Assert.Equal(landed, File.ReadAllBytes(CatalogPath)); + Assert.Equal(0, ProductLinesCommand.Run(Line, _root)); + Assert.Equal(landed, File.ReadAllBytes(CatalogPath)); + } + + // Re-spell every record's payload in a longer, equally valid MessagePack form (an array16 header for the fixarray one). A + // write that re-encodes records it did not author shows as changed bytes; one that lands onto the file as it is leaves them. + private int WidenEveryPayload() + { + var snapshot = CultDocumentMessagePackSerialization.DeserializeSnapshot(File.ReadAllBytes(CatalogPath)); + var widened = 0; + foreach (var record in snapshot.Records) + { + var payload = record.Payload; + if (payload.Length == 0 || (payload[0] & 0xF0) != 0x90) continue; + record.Payload = new byte[] { 0xDC, 0x00, (byte)(payload[0] & 0x0F) }.Concat(payload.Skip(1)).ToArray(); + widened++; + } + File.WriteAllBytes(CatalogPath, CultDocumentMessagePackSerialization.SerializeSnapshot(snapshot)); + return widened; + } + + private Dictionary Records() => + CultDocumentMessagePackSerialization.DeserializeSnapshot(File.ReadAllBytes(CatalogPath)).Records.ToDictionary(r => r.Key, StringComparer.Ordinal); + + [Fact] + public void Apply_writes_only_the_records_it_authors() + { + Assert.True(WidenEveryPayload() > 100, "the scratch catalog should hold many records in the widened spelling"); + var before = Records(); + + Assert.Equal(0, ProductLinesCommand.Run(LineApply, _root)); + + var after = Records(); + Assert.Equal(before.Count + Names.Length, after.Count); + foreach (var (key, record) in before) + { + Assert.True(after.TryGetValue(key, out var kept), "a record the command did not author is gone"); + Assert.Equal(record.SchemaId, kept.SchemaId); + Assert.Equal(record.StoredAt, kept.StoredAt); + Assert.True(record.Payload.AsSpan().SequenceEqual(kept.Payload), "a record the command did not author has changed bytes"); + } + } + + [Fact] + public void Apply_finishes_a_catalog_that_holds_part_of_the_line() + { + Assert.Equal(0, ProductLinesCommand.Run(LineApply, _root)); + using (var cache = Open()) + { + foreach (var product in cache.GetAll().Where(p => p.Name == "Merlin" || p.Name == "Tripwire").ToArray()) + Assert.True(cache.Remove(cache.RefOf(product).Key)); + cache.FlushAsync().Wait(); + } + Assert.Equal(Names.Length - 2, Line20().Length); + + Assert.Equal(0, ProductLinesCommand.Run(LineApply, _root)); + Assert.Equal(_shipped, Line20()); + } + + [Fact] + public void Apply_refuses_a_row_name_that_is_another_products_and_writes_nothing() + { + using (var cache = Open()) + { + var other = cache.GetAll().First(w => w.Name == "Light Machine Gun"); + var maker = cache.GetAll().First(f => f.ShortName == "Zhestokost"); + cache.Upsert(new FactionProductData { Name = "Sparrow", Design = new CultRecordRef(cache.RefOf(other).Key), Manufacturer = cache.RefOf(maker) }); + cache.FlushAsync().Wait(); + } + var conflicted = File.ReadAllBytes(CatalogPath); + + Assert.Throws(() => ProductLinesCommand.Run(LineApply, _root)); + Assert.Equal(conflicted, File.ReadAllBytes(CatalogPath)); + } + + // Ruling maker-design-tiers-allowed: a maker that already sells a design may sell another product of it. + [Fact] + public void Apply_accepts_a_maker_that_already_sells_the_design() + { + using (var cache = Open()) + { + var laser = cache.GetAll().First(w => w.Name == "Light Laser"); + var lucent = cache.GetAll().First(f => f.ShortName == "Lucent"); + cache.Upsert(new FactionProductData { Name = "Scratch tier", Design = new CultRecordRef(cache.RefOf(laser).Key), Manufacturer = cache.RefOf(lucent) }); + cache.FlushAsync().Wait(); + } + + Assert.Equal(0, ProductLinesCommand.Run(LineApply, _root)); + Assert.Equal(_shipped, Line20()); + using var after = Open(); + Assert.Contains(after.GetAll(), p => p.Name == "Scratch tier"); + } +} diff --git a/tools/AetherDb/AetherDb.csproj b/tools/AetherDb/AetherDb.csproj index f2b488b9b..d0be0585a 100644 --- a/tools/AetherDb/AetherDb.csproj +++ b/tools/AetherDb/AetherDb.csproj @@ -14,5 +14,6 @@ +