diff --git a/tests/Aetheria.Shared.Tests/CatalogRoleTests.cs b/tests/Aetheria.Shared.Tests/CatalogRoleTests.cs index c93b38d57..d7364b15f 100644 --- a/tests/Aetheria.Shared.Tests/CatalogRoleTests.cs +++ b/tests/Aetheria.Shared.Tests/CatalogRoleTests.cs @@ -172,7 +172,8 @@ public sealed class CatalogRoleTests Assert.Equal(generic, design.Name); Assert.Equal(Program.Renames.Single(r => r.Generic == generic).Functional, design.Description); var products = cache.GetAll().Where(p => p.Design.Key.Equals(cache.RefOf(design).Key)).ToArray(); - Assert.Equal(sold.Select(p => p.Key).OrderBy(k => k, StringComparer.Ordinal), products.Select(p => cache.RefOf(p).Key.Value).OrderBy(k => k, StringComparer.Ordinal)); + // Later product lines add sellers to these designs; the renamed designs keep the products they were renamed with. + Assert.All(sold, product => Assert.Contains(products, p => cache.RefOf(p).Key.Value == product.Key)); foreach (var (key, name, maker) in sold) { var product = products.Single(p => cache.RefOf(p).Key.Value == key); @@ -260,10 +261,7 @@ public sealed class CatalogRoleTests AssertNamed(Design("Muon Fusion Station Reactor"), "Modifier", "core"); // Each seller's means come from its maker's profile; the Pirates are middling and inconsistent. - var profiles = new Dictionary - { - ["Zhestokost"] = (.50f, .08f), ["R&D"] = (.60f, .15f), ["DME"] = (.55f, .20f), ["Lightsail"] = (.55f, .10f), ["Pirates"] = (.45f, .22f), - }; + var profiles = new Dictionary(Program.MakerProfiles) { ["Pirates"] = (.45f, .22f) }; foreach (var design in new[] { autocannon, lrm, srm, mine, drive }) { var sellers = cache.GetAll().Where(p => p.Design.Key.Equals(cache.RefOf(design).Key)).ToArray(); diff --git a/tests/Aetheria.Shared.Tests/ProductLinesCommandTests.cs b/tests/Aetheria.Shared.Tests/ProductLinesCommandTests.cs index ffa69d59e..4c582062b 100644 --- a/tests/Aetheria.Shared.Tests/ProductLinesCommandTests.cs +++ b/tests/Aetheria.Shared.Tests/ProductLinesCommandTests.cs @@ -20,6 +20,7 @@ public sealed class ProductLinesCommandTests : IDisposable // The rows as the shipped catalog holds them, taken before they are removed from the scratch copy. private readonly string[] _shipped; + private readonly string[] _shippedKeys; private static string Describe(CultCache cache, FactionProductData product) => $"{cache.Get(product.Design).Name} | {product.Name} | {cache.Get(product.Manufacturer).ShortName} | {product.Description} | " + @@ -36,6 +37,7 @@ public sealed class ProductLinesCommandTests : IDisposable 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(); + _shippedKeys = products.Select(p => cache.RefOf(p).Key.Value).OrderBy(x => x, StringComparer.Ordinal).ToArray(); foreach (var product in products) Assert.True(cache.Remove(cache.RefOf(product).Key)); cache.FlushAsync().Wait(); } @@ -55,6 +57,12 @@ public sealed class ProductLinesCommandTests : IDisposable return cache.GetAll().Where(p => Names.Contains(p.Name)).Select(p => Describe(cache, p)).OrderBy(x => x, StringComparer.Ordinal).ToArray(); } + private string[] Line20Keys() + { + using var cache = Open(); + return cache.GetAll().Where(p => Names.Contains(p.Name)).Select(p => cache.RefOf(p).Key.Value).OrderBy(x => x, StringComparer.Ordinal).ToArray(); + } + [Fact] public void A_dry_run_writes_nothing_and_apply_lands_the_shipped_rows_once() { @@ -68,8 +76,10 @@ public sealed class ProductLinesCommandTests : IDisposable 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. + // The command derives each record's key from the product's name, so applying the table again lands the keys the + // shipped catalog holds. Assert.Equal(_shipped, Line20()); + Assert.Equal(_shippedKeys, Line20Keys()); // A second apply finds everything authored and writes nothing; so does a dry run. Assert.Equal(0, ProductLinesCommand.Run(LineApply, _root)); diff --git a/tools/AetherDb/ProductLinesCommand.cs b/tools/AetherDb/ProductLinesCommand.cs index aa17159d6..89e44f26a 100644 --- a/tools/AetherDb/ProductLinesCommand.cs +++ b/tools/AetherDb/ProductLinesCommand.cs @@ -5,6 +5,8 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Security.Cryptography; +using System.Text; using GameCult.Caching; // product-lines [apply] (aetheria-release, cut product-lines-weapons; rulings catalog-grows-generic-designs-branded-products, @@ -48,6 +50,11 @@ public static class ProductLinesCommand // A line's rows, for the catalog tests that check the shipped records against the table the command authors from. internal static IReadOnlyList Table(string line) => Lines[line]; + // A product's record key is derived from its name, so the same table lands the same records every time it is applied: the + // replay at a merge reproduces the branch's blob, and nothing that orders products by key reads a different order after it. + private static CultRecordKey KeyOf(Row row) => + new(new Guid(SHA256.HashData(Encoding.UTF8.GetBytes("product-line:" + row.Name)).AsSpan(0, 16)).ToString("N")); + public static int Run(string[] args, string root = null) { var apply = args.Contains("apply"); @@ -120,7 +127,7 @@ public static class ProductLinesCommand Design = db.Cache.RefOf(design), Manufacturer = db.Cache.RefOf(maker), Roles = design.Roles.Select(r => new ProductRole { Role = r.Name, Mean = mean, StandardDeviation = dev }).ToList(), - }); + }, KeyOf(row)); } })) throw new InvalidOperationException("The catalog changed while the product line was being authored; nothing was written. Run it again.");