Economy checks: pins from mutation testing; drop two redundant guards

This commit is contained in:
soul 2026-10-23 04:14:42 +02:00
commit c6c8b4465b
4 changed files with 218 additions and 9 deletions

View file

@ -139,7 +139,7 @@ public static class EconomyChecks
var grounded = Depths(catalog, sells);
var recipes = AllRecipes(catalog).ToList();
var used = new HashSet<string>(recipes.SelectMany(p => p.Recipe.Inputs.Concat(p.Recipe.Upkeep)).Select(c => c.Commodity.Key.Value), StringComparer.Ordinal);
var rolesNamed = new HashSet<string>(recipes.Where(p => catalog.Get(p.Recipe.Output) != null && Recipes.IsRoleRecipe(catalog, p.Recipe))
var rolesNamed = new HashSet<string>(recipes.Where(p => Recipes.IsRoleRecipe(catalog, p.Recipe))
.SelectMany(p => Recipes.RolesOf(catalog.Get(p.Recipe.Output))).Select(r => r.Name), StringComparer.Ordinal);
foreach (var (key, recipe) in recipes)
{
@ -316,7 +316,6 @@ public static class EconomyChecks
if (done[start]) continue;
var component = Enumerable.Range(0, n).Where(i => reach[start, i] && reach[i, start]).ToList();
foreach (var i in component) done[i] = true;
if (component.Count == 1 && !(a[start, start] > 0)) continue;
var rho = Irreducible(a, component);
if (rho > best) { best = rho; bestComponent = component; }
}

View file

@ -0,0 +1,207 @@
using System;
using System.Linq;
using GameCult.Caching;
using Xunit;
// Pins found by mutation testing the economy checks: what each fault says, how the rules compose, the order faults come in, and
// the corners of the worst-fill search.
public sealed class EconomyChecksFurtherTests
{
[Fact]
public void OutsideMarketIsKeyedByItsId() =>
Assert.Equal("outside-market:gate", OutsideMarketData.KeyOf("gate").Value);
[Fact]
public void AFaultReadsInvariantRecordMessage() =>
Assert.Equal("L-x rec: said", new EconomyFault("L-x", "rec", "said").ToString());
[Fact]
public void CheckRunsEveryRule()
{
using var f = new EconomyFixture();
f.Good("ore");
f.Good("slag", inputs: new[] { ("ore", 1) });
f.Good("ingot", inputs: new[] { ("slag", 1) });
f.Good("steel", inputs: new[] { ("ingot", 1) });
f.Good("barrel", inputs: new[] { ("steel", 1) }, fills: new[] { "Barrel" });
f.Gear("cannon", new[] { ("Barrel", 1) });
f.Gear("bare", new[] { ("Barrel", 1) }, recipe: false);
f.Good("tar", lore: " ");
f.Good("waste");
f.Good("loopa", inputs: new[] { ("loopb", 1) }, fills: new[] { "Barrel" });
f.Good("loopb", inputs: new[] { ("loopa", 1) });
var report = EconomyChecks.Check(f.Cache);
foreach (var invariant in new[] { EconomyChecks.LRecipe, EconomyChecks.LLore, EconomyChecks.LSource, EconomyChecks.LProductive, EconomyChecks.LDemand })
Assert.Contains(report.Faults, x => x.Invariant == invariant);
Assert.All(report.Faults, x => Assert.True(x.Message.Length > 20, x.ToString()));
Assert.Equal(0, report.Depth["commodity:ore"]);
}
[Fact]
public void FaultsComeOrderedByKeyAndSayWhichRecord()
{
using var f = EconomyFixture.Healthy();
f.Good("zinc");
f.Good("alum");
f.Cache.Remove(RecipeData.KeyOf(new CultRecordKey("commodity:zinc")));
f.Cache.Remove(RecipeData.KeyOf(new CultRecordKey("commodity:alum")));
f.Gear("bare", new[] { ("Plate", 1) }, recipe: false);
var faults = EconomyChecks.Recipe(f.Cache);
Assert.Equal(new[] { "commodity:alum", "commodity:zinc", "gear:bare" }, faults.Select(x => x.Record));
Assert.Contains("alum", faults[0].Message);
Assert.Contains("bare", faults[2].Message);
f.Good("a1", lore: "");
f.Good("b1", lore: "");
Assert.Equal(new[] { "recipe:commodity:a1", "recipe:commodity:b1" }, EconomyChecks.Lore(f.Cache).Select(x => x.Record));
Assert.All(EconomyChecks.Lore(f.Cache), x => Assert.Contains("lore hook", x.Message));
}
[Fact]
public void SourceSinkSaysWhatIsMissingAndReadsUpkeepAsASink()
{
using var f = EconomyFixture.Healthy();
f.Good("coolant");
f.Good("heater", upkeep: new[] { ("coolant", 1) }, fills: new[] { "Plate" });
Assert.Empty(EconomyChecks.SourceSink(f.Cache));
f.Good("waste");
f.Good("loopa", inputs: new[] { ("loopb", 1) }, fills: new[] { "Plate" });
f.Good("loopb", inputs: new[] { ("loopa", 1) }, fills: new[] { "Plate" });
var faults = EconomyChecks.SourceSink(f.Cache);
Assert.Contains(faults, x => x.Record == "recipe:commodity:waste" && x.Message.Contains("sink") && x.Message.Length > 20);
Assert.Contains(faults, x => x.Record == "recipe:commodity:loopa" && x.Message.Contains("source") && x.Message.Length > 20);
}
[Fact]
public void ChecksSurviveARecipeWhoseOutputIsGone()
{
using var f = EconomyFixture.Healthy();
f.EditRecipe(Ore(), r => r.Output = EconomyFixture.Item("commodity:ghost"));
f.Good("shard", inputs: new[] { ("ghost", 1) });
Assert.NotEmpty(EconomyChecks.Recipe(f.Cache));
EconomyChecks.SourceSink(f.Cache);
EconomyChecks.Productive(f.Cache);
EconomyChecks.ConsumerDemand(f.Cache);
EconomyChecks.Deep(f.Cache);
}
private static string Ore() => EconomyFixture.Key("ore");
[Fact]
public void ConsumerDemandReadsAnInputTheCatalogLacks()
{
using var f = new EconomyFixture();
f.Good("ore");
f.Good("steel", inputs: new[] { ("ore", 1), ("ghost", 1) });
f.Good("barrel", inputs: new[] { ("steel", 1), ("ghost", 1) }, fills: new[] { "Barrel" });
f.Gear("cannon", new[] { ("Barrel", 1) });
var faults = EconomyChecks.ConsumerDemand(f.Cache);
Assert.Equal(new[] { "recipe:commodity:steel" }, faults.Select(x => x.Record));
Assert.Contains("steel", faults[0].Message);
Assert.True(faults[0].Message.Length > 20);
}
[Fact]
public void ProductiveSkipsAnEmptyOutputAndUnknownGoods()
{
using var f = Loop();
f.Good("z", inputs: new[] { ("a", 1) }, outputCount: 0, workforce: 1);
f.Good("w", inputs: new[] { ("ghost", 5), ("a", 1) }, outputCount: 1);
f.EditRecipe("commodity:a", r => r.Inputs.Add(new CommodityCount { Commodity = EconomyFixture.Item("commodity:z"), Count = 1 }));
f.EditRecipe("commodity:a", r => r.Inputs.Add(new CommodityCount { Commodity = EconomyFixture.Item("commodity:ghost"), Count = 5 }));
f.EditRecipe("commodity:a", r => r.Upkeep.Add(new CommodityCount { Commodity = EconomyFixture.Item("commodity:ghost"), Count = 5 }));
Assert.Empty(EconomyChecks.Productive(f.Cache));
}
private static EconomyFixture Loop()
{
var f = new EconomyFixture();
f.Good("a", inputs: new[] { ("b", 1) }, outputCount: 2);
f.Good("b", inputs: new[] { ("a", 1) });
return f;
}
[Fact]
public void ProductiveNamesOnlyTheGoodsThatCarryTheRadius()
{
using var f = new EconomyFixture();
f.Good("a", inputs: new[] { ("b", 1) });
f.Good("b", inputs: new[] { ("a", 1) });
f.Good("c", inputs: new[] { ("a", 1) });
f.Good("d", inputs: new[] { ("c", 1) });
var fault = Assert.Single(EconomyChecks.Productive(f.Cache));
Assert.EndsWith("carried by commodity:a -> commodity:b.", fault.Message);
Assert.StartsWith("The goods cannot sustain themselves", fault.Message);
}
[Fact]
public void ProductiveRefusesAGoodThatNeedsItself()
{
using var f = new EconomyFixture();
f.Good("a", inputs: new[] { ("a", 1) });
var fault = Assert.Single(EconomyChecks.Productive(f.Cache));
Assert.EndsWith("carried by commodity:a.", fault.Message);
using var g = new EconomyFixture();
g.Good("a", inputs: new[] { ("a", 1) }, outputCount: 2);
Assert.Empty(EconomyChecks.Productive(g.Cache));
}
[Fact]
public void ProductiveNamesTheCycleInOrderThroughLabour()
{
using var f = new EconomyFixture();
f.Good("meal", roles: new[] { ("Fuel", 1) });
f.Good("afuel", fills: new[] { "Fuel" }, workforce: 3, outputCount: 5);
f.Good("bfuel", fills: new[] { "Fuel" }, workforce: 6, outputCount: 5);
var fault = Assert.Single(EconomyChecks.Productive(f.Cache, new EconomyLoad { LabourNeeds = { { "commodity:meal", 1 } } }));
Assert.EndsWith("carried by commodity:bfuel -> labour -> commodity:meal.", fault.Message);
Assert.Equal("commodity:bfuel", fault.Record);
Assert.Contains("spectral radius 1.06265", fault.Message);
}
[Fact]
public void ProductiveScalesARoleByItsCountAndOutputAndTheInputFactor()
{
using var f = new EconomyFixture();
f.Good("meal", roles: new[] { ("Fuel", 1) }, outputCount: 2);
f.Good("fuel", fills: new[] { "Fuel" }, workforce: 8, outputCount: 5);
var needs = new EconomyLoad { LabourNeeds = { { "commodity:meal", 1 } } };
Assert.Empty(EconomyChecks.Productive(f.Cache, needs));
Assert.Single(EconomyChecks.Productive(f.Cache, new EconomyLoad { LabourNeeds = { { "commodity:meal", 1 } }, InputFactor = 1.3 }));
f.EditItem<CompoundCommodityData>("commodity:meal", m => m.Roles[0].Count = 2);
Assert.Single(EconomyChecks.Productive(f.Cache, needs));
}
[Fact]
public void ProductiveSkipsARoleNothingFills()
{
using var f = new EconomyFixture();
f.Good("meal", roles: new[] { ("Nothing", 1), ("Fuel", 0) });
f.Good("fuel", fills: new[] { "Fuel" }, workforce: 8, outputCount: 5);
Assert.Empty(EconomyChecks.Productive(f.Cache, new EconomyLoad { LabourNeeds = { { "commodity:meal", 1 } } }));
}
[Fact]
public void ProductiveFindsTheWorstFillFromAnAcyclicStart()
{
using var f = new EconomyFixture();
f.Good("meal", roles: new[] { ("Fuel", 1) });
f.Good("afuel", fills: new[] { "Fuel" }, workforce: 0, outputCount: 5);
f.Good("bfuel", fills: new[] { "Fuel" }, workforce: 10, outputCount: 5);
var fault = Assert.Single(EconomyChecks.Productive(f.Cache, new EconomyLoad { LabourNeeds = { { "commodity:meal", 1 } } }));
Assert.Equal("commodity:bfuel", fault.Record);
}
[Fact]
public void ProductiveFollowsAChoiceThatOpensAnotherChoice()
{
using var f = new EconomyFixture();
f.Good("meal", roles: new[] { ("Fuel", 1) });
f.Good("x", fills: new[] { "Fuel" }, workforce: 1, outputCount: 5);
f.Good("y", roles: new[] { ("Core", 1) }, fills: new[] { "Fuel" });
f.Good("c1", fills: new[] { "Core" }, workforce: 0, outputCount: 5);
f.Good("c2", fills: new[] { "Core" }, workforce: 10, outputCount: 5);
var fault = Assert.Single(EconomyChecks.Productive(f.Cache, new EconomyLoad { LabourNeeds = { { "commodity:meal", 1 } } }));
Assert.Contains("commodity:y", fault.Message);
Assert.Contains("commodity:c2", fault.Message);
}
}

View file

@ -39,7 +39,7 @@ public sealed class EconomyChecksTests
public static IEnumerable<object[]> BadShapes()
{
object[] Case(string name, Action<EconomyFixture> mutate, string record, string invariant = "L-recipe") => new object[] { name, mutate, record, invariant };
object[] Case(string name, Action<EconomyFixture> mutate, string record, string invariant = "L-recipe", string says = "") => new object[] { name, mutate, record, invariant, says };
yield return Case("zero output count", f => f.EditRecipe(Ore, r => r.OutputCount = 0), "recipe:" + Ore);
yield return Case("negative output count", f => f.EditRecipe(Ore, r => r.OutputCount = -1), "recipe:" + Ore);
yield return Case("zero cycle", f => f.EditRecipe(Ore, r => r.Cycle = 0), "recipe:" + Ore);
@ -47,8 +47,8 @@ public sealed class EconomyChecksTests
yield return Case("NaN cycle", f => f.EditRecipe(Ore, r => r.Cycle = float.NaN), "recipe:" + Ore);
yield return Case("infinite cycle", f => f.EditRecipe(Ore, r => r.Cycle = float.PositiveInfinity), "recipe:" + Ore);
yield return Case("negative workforce", f => f.EditRecipe(Ore, r => r.Workforce = -1), "recipe:" + Ore);
yield return Case("zero input count", f => f.EditRecipe("commodity:plate", r => r.Inputs[0].Count = 0), "recipe:commodity:plate");
yield return Case("zero upkeep count", f => f.EditRecipe("commodity:plate", r => r.Upkeep.Add(new CommodityCount { Commodity = EconomyFixture.Item(Ore), Count = 0 })), "recipe:commodity:plate");
yield return Case("zero input count", f => f.EditRecipe("commodity:plate", r => r.Inputs[0].Count = 0), "recipe:commodity:plate", says: "Inputs[0] count");
yield return Case("zero upkeep count", f => f.EditRecipe("commodity:plate", r => r.Upkeep.Add(new CommodityCount { Commodity = EconomyFixture.Item(Ore), Count = 0 })), "recipe:commodity:plate", says: "Upkeep[0] count");
yield return Case("input that is not a commodity", f => f.EditRecipe("commodity:plate", r => r.Inputs.Add(new CommodityCount { Commodity = EconomyFixture.Item("gear:laser"), Count = 1 })), "recipe:commodity:plate");
yield return Case("upkeep that is not a commodity", f => f.EditRecipe("commodity:plate", r => r.Upkeep.Add(new CommodityCount { Commodity = EconomyFixture.Item("hull:skiff"), Count = 1 })), "recipe:commodity:plate");
yield return Case("input naming nothing", f => f.EditRecipe("commodity:plate", r => r.Inputs.Add(new CommodityCount { Commodity = EconomyFixture.Item("commodity:ghost"), Count = 1 })), "recipe:commodity:plate");
@ -69,13 +69,16 @@ public sealed class EconomyChecksTests
[Theory]
[MemberData(nameof(BadShapes))]
public void RecipeRefusesBadShape(string name, Action<EconomyFixture> mutate, string record, string invariant)
public void RecipeRefusesBadShape(string name, Action<EconomyFixture> mutate, string record, string invariant, string says)
{
using var f = EconomyFixture.Healthy();
Assert.Empty(EconomyChecks.Recipe(f.Cache));
mutate(f);
var faults = EconomyChecks.Recipe(f.Cache);
Assert.True(faults.Any(x => x.Invariant == invariant && x.Record == record), name + ": " + string.Join(" | ", faults));
var matched = faults.Where(x => x.Invariant == invariant && x.Record == record).ToArray();
Assert.True(matched.Length > 0, name + ": " + string.Join(" | ", faults));
Assert.All(matched, x => Assert.True(x.Message.Length > 20 && x.Message.Contains(says), name + ": " + x.Message));
if (name == "an output naming nothing") Assert.Single(faults.Where(x => x.Record == record));
if (invariant == EconomyChecks.LOneKindList) Assert.DoesNotContain(faults, x => x.Invariant == EconomyChecks.LRecipe);
}
@ -319,7 +322,7 @@ public sealed class EconomyChecksTests
using var f = Cannon();
// barrel is distance 1 (exempt); steel is 2 and ingot 3 (faults); slag is 4 and ore is raw (neither is).
var faults = EconomyChecks.ConsumerDemand(f.Cache);
Assert.Equal(new[] { "recipe:commodity:ingot", "recipe:commodity:steel" }, faults.Select(x => x.Record).OrderBy(x => x, StringComparer.Ordinal));
Assert.Equal(new[] { "recipe:commodity:ingot", "recipe:commodity:steel" }, faults.Select(x => x.Record));
Assert.All(faults, x => Assert.Equal(EconomyChecks.LDemand, x.Invariant));
f.Good("pot", roles: new[] { ("Casing", 1) });

View file

@ -139,7 +139,7 @@ public sealed class EconomyFixture : IDisposable
f.Good("ore");
f.Good("plate", inputs: new[] { ("ore", 2) }, fills: new[] { "Plate" });
f.Gear("laser", new[] { ("Plate", 2) });
f.Hull("skiff", HullType.Ship, new[] { ("Plate", 2) });
f.Hull("skiff", HullType.Ship, new[] { ("Plate", 1) });
return f;
}
}