using System; using System.Collections.Generic; using System.Linq; using GameCult.Caching; using MessagePack; using Xunit; // The economy checks over small fixture catalogs (aetheria-living-world, cut recipe-schema-and-checks). Each rule is refused by // a catalog that breaks only it. public sealed class EconomyChecksTests { private static string[] Records(IEnumerable faults, string invariant) => faults.Where(f => f.Invariant == invariant).Select(f => f.Record).OrderBy(r => r, StringComparer.Ordinal).ToArray(); private static string Ore => EconomyFixture.Key("ore"); [Fact] public void AHealthyCatalogHasNoFaults() { using var f = EconomyFixture.Healthy(); var report = EconomyChecks.Check(f.Cache); Assert.Empty(report.Faults); Assert.Equal(2, report.Depth["gear:laser"]); Assert.Equal(2, report.Depth["hull:skiff"]); } [Fact] public void RecipeRefusesAGoodWithoutOne() { using var f = EconomyFixture.Healthy(); f.Cache.Remove(RecipeData.KeyOf(new CultRecordKey(Ore))); f.Gear("bare", new[] { ("Plate", 1) }, recipe: false); f.Hull("barehull", HullType.Ship, new[] { ("Plate", 1) }, recipe: false); f.Hull("barestation", HullType.Station, new (string, int)[0], recipe: false); f.Hull("bareturret", HullType.Turret, new (string, int)[0], recipe: false); f.Gear("unoffered", new[] { ("Plate", 1) }, offered: false, recipe: false); Assert.Equal(new[] { "commodity:ore", "gear:bare", "hull:barehull" }, Records(EconomyChecks.Recipe(f.Cache), EconomyChecks.LRecipe)); } public static IEnumerable BadShapes() { object[] Case(string name, Action 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); yield return Case("negative cycle", f => f.EditRecipe(Ore, r => r.Cycle = -2), "recipe:" + Ore); 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", 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"); yield return Case("inputs on a role recipe", f => f.EditRecipe("gear:laser", r => r.Inputs.Add(new CommodityCount { Commodity = EconomyFixture.Item(Ore), Count = 1 })), "recipe:gear:laser"); yield return Case("a role with no filler", f => f.EditItem("gear:laser", g => g.Roles.Add(new ItemRole { Name = "Mystery" })), "recipe:gear:laser"); yield return Case("a role count of zero", f => f.EditItem("gear:laser", g => g.Roles[0].Count = 0), "recipe:gear:laser"); yield return Case("fills on a design", f => f.EditRecipe("gear:laser", r => r.Fills.Add("Plate")), "recipe:gear:laser"); yield return Case("a design with no roles", f => f.EditItem("gear:laser", g => g.Roles.Clear()), "recipe:gear:laser"); yield return Case("an unknown station kind", f => f.EditRecipe(Ore, r => r.StationKind = new CultRecordRef(StationKindData.KeyOf("nowhere"))), "recipe:" + Ore); yield return Case("an unset station kind", f => f.EditRecipe(Ore, r => r.StationKind = default), "recipe:" + Ore); yield return Case("an output naming nothing", f => f.EditRecipe(Ore, r => r.Output = EconomyFixture.Item("commodity:ghost")), "recipe:" + Ore); yield return Case("a kind no faction places", f => { var ghost = f.Station("ghost", "Ghost"); f.EditRecipe(Ore, r => r.StationKind = ghost); }, "recipe:" + Ore, EconomyChecks.LOneKindList); } [Theory] [MemberData(nameof(BadShapes))] public void RecipeRefusesBadShape(string name, Action 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); 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); } [Fact] public void RecipeRefusesARecipeUnderTheWrongKey() { using var f = EconomyFixture.Healthy(); var recipe = f.Cache.Get(RecipeData.KeyOf(new CultRecordKey(Ore))); f.Cache.Remove(RecipeData.KeyOf(new CultRecordKey(Ore))); f.Cache.Upsert(typeof(RecipeData), recipe, new CultRecordKey("recipe:stray")); var faults = EconomyChecks.Recipe(f.Cache); Assert.Contains(faults, x => x.Record == "recipe:stray" && x.Message.Contains("key")); Assert.Contains(faults, x => x.Record == Ore); } [Fact] public void LoreRefusesBlankHookOnCommodities() { using var f = EconomyFixture.Healthy(); Assert.Empty(EconomyChecks.Lore(f.Cache)); f.EditRecipe(Ore, r => r.LoreHook = ""); f.EditRecipe("commodity:plate", r => r.LoreHook = " \t"); f.Good("tar"); f.EditRecipe("commodity:tar", r => r.LoreHook = null); Assert.Equal(new[] { "recipe:commodity:ore", "recipe:commodity:plate", "recipe:commodity:tar" }, Records(EconomyChecks.Lore(f.Cache), EconomyChecks.LLore)); Assert.DoesNotContain(EconomyChecks.Lore(f.Cache), x => x.Record.Contains("gear") || x.Record.Contains("hull")); } [Fact] public void SourceSinkCountsTheGate() { using var f = EconomyFixture.Healthy(); 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.All(faults, x => Assert.Equal(EconomyChecks.LSource, x.Invariant)); Assert.Equal(new[] { "recipe:commodity:loopa", "recipe:commodity:loopb" }, faults.Where(x => x.Message.Contains("source")).Select(x => x.Record).OrderBy(x => x, StringComparer.Ordinal)); Assert.Equal(new[] { "recipe:commodity:waste" }, faults.Where(x => x.Message.Contains("sink")).Select(x => x.Record)); f.Gate(("commodity:waste", true, false), ("commodity:loopa", false, true)); Assert.Empty(EconomyChecks.SourceSink(f.Cache)); f.Gate(("commodity:waste", false, true), ("commodity:loopa", true, false)); Assert.Equal(new[] { "recipe:commodity:loopa", "recipe:commodity:loopb", "recipe:commodity:waste" }, EconomyChecks.SourceSink(f.Cache).Select(x => x.Record).Distinct().OrderBy(x => x, StringComparer.Ordinal)); } [Fact] public void SourceSinkLeavesAConsumerGoodWithoutASink() { using var f = EconomyFixture.Healthy(); f.Good("kettle", roles: new[] { ("Plate", 1) }); Assert.Empty(EconomyChecks.SourceSink(f.Cache)); } [Fact] public void SourceSinkTakesARoleSomeDesignNamesAsASink() { using var f = EconomyFixture.Healthy(); f.Good("rivet", fills: new[] { "Plate" }); f.Good("spare", fills: new[] { "Unwanted" }); Assert.Equal(new[] { "recipe:commodity:spare" }, EconomyChecks.SourceSink(f.Cache).Select(x => x.Record).ToArray()); } private static EconomyFixture Loop(int aCount, int aOut, int bCount = 1, int bOut = 1) { var f = new EconomyFixture(); f.Good("a", inputs: new[] { ("b", aCount) }, outputCount: aOut); f.Good("b", inputs: new[] { ("a", bCount) }, outputCount: bOut); return f; } [Fact] public void ProductiveRefusesALoopAtOne() { using var f = Loop(1, 1); var fault = Assert.Single(EconomyChecks.Productive(f.Cache)); Assert.Equal("L-productive", fault.Invariant); Assert.Equal(EconomyFixture.Key("a"), fault.Record); Assert.Contains("spectral radius 1 ", fault.Message); Assert.Contains("commodity:a -> commodity:b.", fault.Message); using var gentle = Loop(9, 10); Assert.Empty(EconomyChecks.Productive(gentle.Cache)); using var feed = Loop(1, 1); feed.Good("c", inputs: new[] { ("a", 1) }); Assert.Single(EconomyChecks.Productive(feed.Cache)); } [Fact] public void ProductiveRefusesJustBelowTheLine() { using var f = Loop(1999999999, 2000000000); Assert.Single(EconomyChecks.Productive(f.Cache)); using var g = Loop(1999999900, 2000000000); Assert.Empty(EconomyChecks.Productive(g.Cache)); } [Fact] public void ProductiveIncludesInputsAndTheInputFactor() { using var f = Loop(1, 2); Assert.Empty(EconomyChecks.Productive(f.Cache)); Assert.Single(EconomyChecks.Productive(f.Cache, new EconomyLoad { InputFactor = 2 })); Assert.Empty(EconomyChecks.Productive(f.Cache, new EconomyLoad { UpkeepFactor = 4, WorkforceFactor = 4 })); } [Fact] public void ProductiveIncludesUpkeepAndItsFactor() { using var f = new EconomyFixture(); f.Good("a", upkeep: new[] { ("b", 3) }, outputCount: 4); f.Good("b", upkeep: new[] { ("a", 1) }); Assert.Empty(EconomyChecks.Productive(f.Cache)); Assert.Single(EconomyChecks.Productive(f.Cache, new EconomyLoad { UpkeepFactor = 2 })); Assert.Empty(EconomyChecks.Productive(f.Cache, new EconomyLoad { InputFactor = 4, WorkforceFactor = 4 })); f.EditRecipe("commodity:a", r => r.Upkeep[0].Count = 4); Assert.Single(EconomyChecks.Productive(f.Cache)); } [Fact] public void ProductiveIncludesLabourAndTheWorkforceFactor() { using var f = new EconomyFixture(); f.Good("a", outputCount: 2, workforce: 1, cycle: 1); var needs = new EconomyLoad { LabourNeeds = { { "commodity:a", 1.5 } } }; Assert.Empty(EconomyChecks.Productive(f.Cache)); Assert.Empty(EconomyChecks.Productive(f.Cache, needs)); Assert.Single(EconomyChecks.Productive(f.Cache, new EconomyLoad { LabourNeeds = { { "commodity:a", 1.5 } }, WorkforceFactor = 2 })); Assert.Empty(EconomyChecks.Productive(f.Cache, new EconomyLoad { LabourNeeds = { { "commodity:a", 1.5 } }, InputFactor = 4, UpkeepFactor = 4 })); f.EditRecipe("commodity:a", r => r.Cycle = 3); var fault = Assert.Single(EconomyChecks.Productive(f.Cache, needs)); Assert.Contains("labour", fault.Message); f.EditRecipe("commodity:a", r => { r.Cycle = 3; r.Workforce = 0; }); Assert.Empty(EconomyChecks.Productive(f.Cache, needs)); } [Fact] public void ProductiveRefusesFactorsBelowOne() { using var f = new EconomyFixture(); Assert.Contains("at least 1", Assert.Throws(() => EconomyChecks.Productive(f.Cache, new EconomyLoad { InputFactor = 0.5 })).Message); Assert.Throws(() => EconomyChecks.Productive(f.Cache, new EconomyLoad { WorkforceFactor = 0.5 })); Assert.Throws(() => EconomyChecks.Productive(f.Cache, new EconomyLoad { UpkeepFactor = double.NaN })); Assert.Empty(EconomyChecks.Productive(f.Cache, new EconomyLoad { InputFactor = 1, WorkforceFactor = 1, UpkeepFactor = 1 })); } // Labour needs a meal, the meal takes one filler of "Fuel", the filler takes labour: the loop closes through the role. private static EconomyFixture MealLoop(int firstWorkers, int secondWorkers) { var f = new EconomyFixture(); f.Good("meal", roles: new[] { ("Fuel", 1) }); f.Good("afuel", fills: new[] { "Fuel" }, workforce: firstWorkers, outputCount: 5); f.Good("bfuel", fills: new[] { "Fuel" }, workforce: secondWorkers, outputCount: 5); return f; } private static EconomyLoad MealNeeds => new EconomyLoad { LabourNeeds = { { "commodity:meal", 1 } } }; [Fact] public void ProductiveTakesTheWorstSingleFill() { foreach (var (first, second) in new[] { (3, 6), (6, 3) }) { using var f = MealLoop(first, second); var fault = Assert.Single(EconomyChecks.Productive(f.Cache, MealNeeds)); Assert.Contains(first > second ? "commodity:afuel" : "commodity:bfuel", fault.Message); } using var even = MealLoop(3, 3); Assert.Empty(EconomyChecks.Productive(even.Cache, MealNeeds)); Assert.Empty(EconomyChecks.Productive(even.Cache)); using var better = MealLoop(2, 3); Assert.Empty(EconomyChecks.Productive(better.Cache, MealNeeds)); } [Fact] public void ProductiveFindsTheWorstFillAmongSeveralRoles() { using var f = new EconomyFixture(); f.Good("meal", roles: new[] { ("Fuel", 1), ("Spice", 1) }); f.Good("fuela", fills: new[] { "Fuel" }, workforce: 1, outputCount: 5); f.Good("fuelb", fills: new[] { "Fuel" }, workforce: 1, outputCount: 5); f.Good("spicea", fills: new[] { "Spice" }, workforce: 1, outputCount: 5); f.Good("spiceb", fills: new[] { "Spice" }, workforce: 3, outputCount: 5); Assert.Empty(EconomyChecks.Productive(f.Cache, MealNeeds)); f.EditItem("commodity:meal", m => m.Roles[1].Count = 2); var fault = Assert.Single(EconomyChecks.Productive(f.Cache, new EconomyLoad { LabourNeeds = { { "commodity:meal", 1.3 } } })); Assert.Contains("commodity:spiceb", fault.Message); } [Fact] public void DeepIsMinOverFillersMaxOverInputs() { using var f = new EconomyFixture(); f.Good("ore"); f.Good("bar", inputs: new[] { ("ore", 1) }); f.Good("cores", inputs: new[] { ("ore", 1) }, fills: new[] { "Core" }); f.Good("cored", inputs: new[] { ("bar", 1) }, fills: new[] { "Core" }); f.Good("alloy", inputs: new[] { ("bar", 1), ("cored", 1) }, fills: new[] { "Cover" }); f.Gear("rifle", new[] { ("Core", 1) }); f.Hull("frigate", HullType.Ship, new[] { ("Core", 1), ("Cover", 1) }); f.Good("loopa", inputs: new[] { ("loopb", 1) }); f.Good("loopb", inputs: new[] { ("loopa", 1) }); var depth = EconomyChecks.Deep(f.Cache); Assert.Equal(0, depth["commodity:ore"]); Assert.Equal(1, depth["commodity:bar"]); Assert.Equal(1, depth["commodity:cores"]); Assert.Equal(2, depth["commodity:cored"]); Assert.Equal(3, depth["commodity:alloy"]); Assert.Equal(2, depth["gear:rifle"]); Assert.Equal(4, depth["hull:frigate"]); Assert.Null(depth["commodity:loopa"]); Assert.Null(depth["commodity:loopb"]); } [Fact] public void DeepReportsAndNeverRefuses() { using var f = EconomyFixture.Healthy(); var report = EconomyChecks.Check(f.Cache); Assert.Equal(2, report.Depth["hull:skiff"]); Assert.DoesNotContain(report.Faults, x => x.Invariant.Contains("deep", StringComparison.OrdinalIgnoreCase)); Assert.Empty(report.Faults); } private static EconomyFixture Cannon() { 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), ("ore", 1) }, fills: new[] { "Barrel" }); f.Gear("cannon", new[] { ("Barrel", 1) }); return f; } [Fact] public void ConsumerDemandCoversTheSharedLayer() { 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)); Assert.All(faults, x => Assert.Equal(EconomyChecks.LDemand, x.Invariant)); f.Good("pot", roles: new[] { ("Casing", 1) }); f.EditRecipe("commodity:steel", r => r.Fills.Add("Casing")); Assert.Equal(new[] { "recipe:commodity:ingot" }, EconomyChecks.ConsumerDemand(f.Cache).Select(x => x.Record)); f.EditRecipe("commodity:ingot", r => r.Fills.Add("Casing")); Assert.Empty(EconomyChecks.ConsumerDemand(f.Cache)); f.Gate(("commodity:slag", true, false), ("commodity:steel", true, false)); f.EditRecipe("commodity:steel", r => r.Fills.Clear()); Assert.Equal(new[] { "recipe:commodity:steel" }, EconomyChecks.ConsumerDemand(f.Cache).Select(x => x.Record)); } [Fact] public void ConsumerDemandReadsHullsAsWellAsGear() { using var f = new EconomyFixture(); f.Good("ore"); f.Good("ingot", inputs: new[] { ("ore", 1) }); f.Good("plate", inputs: new[] { ("ingot", 1) }, fills: new[] { "Plate" }); f.Hull("tug", HullType.Ship, new[] { ("Plate", 1) }); Assert.Equal(new[] { "recipe:commodity:ingot" }, EconomyChecks.ConsumerDemand(f.Cache).Select(x => x.Record)); } [Fact] public void ChecksArePure() { using var f = EconomyFixture.Healthy(); f.Good("loopa", inputs: new[] { ("loopb", 1) }); f.Good("loopb", inputs: new[] { ("loopa", 1) }); f.EditRecipe(Ore, r => r.LoreHook = ""); var options = GameCult.Caching.MessagePack.CultDocumentMessagePackSerialization.OptionsFor(typeof(ItemData).Assembly); string[] Snapshot() => f.Cache.AllStoredDocuments .Select(d => d.Key.Value + "|" + Convert.ToBase64String(MessagePackSerializer.Serialize(d.Document.GetType(), d.Document, options))) .OrderBy(s => s, StringComparer.Ordinal).ToArray(); var before = Snapshot(); var first = EconomyChecks.Check(f.Cache); var second = EconomyChecks.Check(f.Cache); Assert.NotEmpty(first.Faults); Assert.Equal(first.Faults.Select(x => x.ToString()), second.Faults.Select(x => x.ToString())); Assert.Equal(first.Depth.OrderBy(p => p.Key, StringComparer.Ordinal), second.Depth.OrderBy(p => p.Key, StringComparer.Ordinal)); Assert.Equal(before, Snapshot()); } }