diff --git a/tests/Aetheria.Shared.Tests/LaunchSuppressorCatalogTests.cs b/tests/Aetheria.Shared.Tests/LaunchSuppressorCatalogTests.cs index 2f5a2a99b..096845380 100644 --- a/tests/Aetheria.Shared.Tests/LaunchSuppressorCatalogTests.cs +++ b/tests/Aetheria.Shared.Tests/LaunchSuppressorCatalogTests.cs @@ -67,11 +67,6 @@ public sealed class LaunchSuppressorCatalogTests } // Exactly these six products and these makers, each authoring both roles at its maker's own mean and deviation. - var profiles = new Dictionary - { - ["NiteLife"] = (.55f, .12f), ["Finch"] = (.65f, .20f), ["DME"] = (.55f, .20f), ["R&D"] = (.60f, .15f), - ["Lightsail"] = (.55f, .10f), ["Alakrita"] = (.60f, .14f), - }; foreach (var (name, makers) in new[] { (StrongName, new[] { "DME", "Finch", "NiteLife", "R&D" }), @@ -84,7 +79,7 @@ public sealed class LaunchSuppressorCatalogTests Assert.Equal(products.Length, products.Select(p => p.Name).Distinct().Count()); Assert.All(products, product => { - var (mean, dev) = profiles[cache.Get(product.Manufacturer).ShortName]; + var (mean, dev) = Program.MakerProfiles[cache.Get(product.Manufacturer).ShortName]; Assert.Equal(new[] { "baffles", "heat sink" }, product.Roles.Select(r => r.Role).OrderBy(r => r, StringComparer.Ordinal).ToArray()); Assert.All(product.Roles, r => { diff --git a/tests/Aetheria.Shared.Tests/ProductLinesCatalogTests.cs b/tests/Aetheria.Shared.Tests/ProductLinesCatalogTests.cs index a4cfa7ac0..756e22355 100644 --- a/tests/Aetheria.Shared.Tests/ProductLinesCatalogTests.cs +++ b/tests/Aetheria.Shared.Tests/ProductLinesCatalogTests.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using GameCult.Caching; using Xunit; @@ -16,16 +17,20 @@ public sealed class ProductLinesCatalogTests { using var cache = RestoredHullsTests.OpenCatalog(); var products = cache.GetAll().ToArray(); - var sold = 0; + var unsold = new List(); 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++; + if (makers == 0) + { + unsold.Add(design.Name); + continue; + } 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"); + // The designs no product sells are the Smoke test designs and no others: a design that lost all its products is a failure. + Assert.All(unsold, name => Assert.StartsWith("Smoke ", name)); } [Fact] diff --git a/tests/Aetheria.Shared.Tests/ProductLinesCommandTests.cs b/tests/Aetheria.Shared.Tests/ProductLinesCommandTests.cs index 4c582062b..9aa136e16 100644 --- a/tests/Aetheria.Shared.Tests/ProductLinesCommandTests.cs +++ b/tests/Aetheria.Shared.Tests/ProductLinesCommandTests.cs @@ -176,4 +176,87 @@ public sealed class ProductLinesCommandTests : IDisposable using var after = Open(); Assert.Contains(after.GetAll(), p => p.Name == "Scratch tier"); } + + // Master's committed catalog holds records written on a schema older than today's (HullData lost a member after the last + // catalog write). The scratch copy gets that shape: the hull records point at an earlier schema id the loader resolves to + // the current type. + private string MakeHullsStale() + { + var snapshot = CultDocumentMessagePackSerialization.DeserializeSnapshot(File.ReadAllBytes(CatalogPath)); + var current = snapshot.SchemaCatalog.Single(e => e.SchemaName == "aetheria.hulldata"); + var stale = new CultSchemaCatalogEntry + { + SchemaId = "sha256:earlier-hulldata", SchemaName = current.SchemaName, SchemaVersion = current.SchemaVersion, + ContentHash = "sha256:earlier-hulldata", CanonicalSchemaJson = current.CanonicalSchemaJson, Members = current.Members, + }; + snapshot.SchemaCatalog = snapshot.SchemaCatalog.Append(stale).ToArray(); + var hulls = snapshot.Records.Where(r => r.SchemaId == current.SchemaId).ToArray(); + Assert.NotEmpty(hulls); + foreach (var hull in hulls) hull.SchemaId = stale.SchemaId; + File.WriteAllBytes(CatalogPath, CultDocumentMessagePackSerialization.SerializeSnapshot(snapshot)); + return stale.SchemaId; + } + + [Fact] + public void Apply_lands_on_a_catalog_that_holds_records_of_an_older_schema_and_replays_to_the_same_bytes() + { + var stale = MakeHullsStale(); + var before = Records(); + + Assert.Equal(0, ProductLinesCommand.Run(LineApply, _root)); + var landed = File.ReadAllBytes(CatalogPath); + Assert.Equal(_shipped, Line20()); + var after = Records(); + foreach (var (key, record) in before) + Assert.True(record.Payload.AsSpan().SequenceEqual(after[key].Payload) && record.SchemaId == after[key].SchemaId, "a record the command did not author changed"); + Assert.Contains(after.Values, r => r.SchemaId == stale); + + Assert.Equal(0, ProductLinesCommand.Run(LineApply, _root)); + Assert.Equal(landed, File.ReadAllBytes(CatalogPath)); + } + + private void AddProduct(string name, string design, string maker) + { + using var cache = Open(); + var designRecord = cache.GetAll().First(w => w.Name == design); + var makerRecord = cache.GetAll().First(f => f.ShortName == maker); + cache.Upsert(new FactionProductData { Name = name, Design = new CultRecordRef(cache.RefOf(designRecord).Key), Manufacturer = cache.RefOf(makerRecord) }); + cache.FlushAsync().Wait(); + } + + // Sparrow is the Light Laser by Finch. A product of that name is the row only when design and maker both match. + [Theory] + [InlineData("Sparrow", "Light Machine Gun", "Finch")] + [InlineData("Sparrow", "Light Laser", "Zhestokost")] + [InlineData("sparrow", "Light Laser", "Finch")] + public void Apply_refuses_a_name_unless_design_and_maker_both_match_it_ignoring_case(string name, string design, string maker) + { + AddProduct(name, design, maker); + var conflicted = File.ReadAllBytes(CatalogPath); + + Assert.Throws(() => ProductLinesCommand.Run(LineApply, _root)); + Assert.Equal(conflicted, File.ReadAllBytes(CatalogPath)); + } + + [Fact] + public void Apply_refuses_a_row_name_that_is_a_designs_and_writes_nothing() + { + using (var cache = Open()) + { + cache.Upsert(new WeaponItemData { Name = "Tripwire" }); + cache.FlushAsync().Wait(); + } + var conflicted = File.ReadAllBytes(CatalogPath); + + Assert.Throws(() => ProductLinesCommand.Run(LineApply, _root)); + Assert.Equal(conflicted, File.ReadAllBytes(CatalogPath)); + } + + // The command is reached as AetherDb's product-lines word, with the line after it; the shipped catalog holds the line. + [Fact] + public void The_product_lines_word_runs_the_named_line_and_refuses_an_unknown_one() + { + Assert.Equal(0, Program.Main(new[] { "product-lines", "weapons" })); + Assert.Equal(1, Program.Main(new[] { "product-lines", "nonesuch" })); + } } diff --git a/tools/AetherDb/ProductLinesCommand.cs b/tools/AetherDb/ProductLinesCommand.cs index 89e44f26a..d0fd7ce41 100644 --- a/tools/AetherDb/ProductLinesCommand.cs +++ b/tools/AetherDb/ProductLinesCommand.cs @@ -112,11 +112,13 @@ public static class ProductLinesCommand return 0; } - // ExpectUnchanged lands the batch onto the file as it is: every other record keeps the bytes it has, where a plain - // commit would re-encode the whole catalog through today's types. + // The batch lands onto the file as it is: every other record keeps the bytes it has, where a plain commit would + // re-encode the whole catalog through today's types. What it expects is that each key it authors is still absent, the + // one condition the command's own validation rests on. ExpectUnchanged would compare the file's schema ids with the + // loaded ones and refuse any catalog that holds a record written on an older schema, whatever the batch is. if (!db.Cache.Commit(batch => { - batch.ExpectUnchanged(); + foreach (var (row, _, _, _) in missing) batch.Expect(KeyOf(row), null); foreach (var (row, design, maker, _) in missing) { var (mean, dev) = Program.MakerProfiles[row.Maker];