Recipe schema and catalog checks: RecipeData, OutsideMarketData, role counts, EconomyChecks over fixture catalogs
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
d233194cb2
commit
bacb56c613
9 changed files with 1155 additions and 4 deletions
366
tests/Aetheria.Shared.Tests/EconomyChecksTests.cs
Normal file
366
tests/Aetheria.Shared.Tests/EconomyChecksTests.cs
Normal file
|
|
@ -0,0 +1,366 @@
|
|||
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<EconomyFault> 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<object[]> BadShapes()
|
||||
{
|
||||
object[] Case(string name, Action<EconomyFixture> mutate, string record, string invariant = "L-recipe") => new object[] { name, mutate, record, invariant };
|
||||
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");
|
||||
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("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<GearData>("gear:laser", g => g.Roles.Add(new ItemRole { Name = "Mystery" })), "recipe:gear:laser");
|
||||
yield return Case("a role count of zero", f => f.EditItem<GearData>("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<GearData>("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>(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<EconomyFixture> mutate, string record, string invariant)
|
||||
{
|
||||
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));
|
||||
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>(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.Throws<ArgumentException>(() => EconomyChecks.Productive(f.Cache, new EconomyLoad { InputFactor = 0.5 }));
|
||||
Assert.Throws<ArgumentException>(() => EconomyChecks.Productive(f.Cache, new EconomyLoad { WorkforceFactor = 0.5 }));
|
||||
Assert.Throws<ArgumentException>(() => 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<CompoundCommodityData>("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).OrderBy(x => x, StringComparer.Ordinal));
|
||||
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());
|
||||
}
|
||||
}
|
||||
145
tests/Aetheria.Shared.Tests/EconomyFixture.cs
Normal file
145
tests/Aetheria.Shared.Tests/EconomyFixture.cs
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using GameCult.Caching;
|
||||
|
||||
// A small writable catalog for the economy tests: one maker that places one station kind (Mill), and builders for commodities,
|
||||
// gear, hulls, recipes and the outside market. Keys are readable: commodity:<id>, gear:<id>, hull:<id>.
|
||||
internal sealed class EconomyFixture : IDisposable
|
||||
{
|
||||
private readonly string _dir = Path.Combine(Path.GetTempPath(), "aetheria-economy-" + Guid.NewGuid().ToString("N"));
|
||||
public string CatalogPath => Path.Combine(_dir, "Aetheria.cc");
|
||||
public CultCache Cache { get; private set; }
|
||||
public CultRecordRef<Faction> Maker { get; private set; }
|
||||
public CultRecordRef<StationKindData> Mill { get; private set; }
|
||||
|
||||
public EconomyFixture()
|
||||
{
|
||||
Directory.CreateDirectory(_dir);
|
||||
Cache = AetheriaStores.Open(CatalogPath, catalogWritable: true, registry: TestCatalog.Registry());
|
||||
Cache.Upsert(new VerseGrammar { Revision = 1 });
|
||||
Cache.Upsert(new InputLayout { Rows = new InputLayoutRow[] { new InputLayoutRowSpacer { Height = 1 } } });
|
||||
Maker = new CultRecordRef<Faction>(Cache.Upsert(new Faction { Name = "Maker", ShortName = "MKR" }).Key);
|
||||
Mill = Station("mill", "Mill");
|
||||
Sell(Cache.Upsert(typeof(HullData), new HullData { Name = "Yard", HullType = HullType.Station, Kind = Mill, Shape = new Shape(), Price = 1 }, new CultRecordKey("hull:yard")));
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Cache.Dispose();
|
||||
try { Directory.Delete(_dir, true); } catch (IOException) { }
|
||||
}
|
||||
|
||||
// Closes the catalog and opens the file again, as the game does.
|
||||
public void Reopen()
|
||||
{
|
||||
Cache.FlushAsync().GetAwaiter().GetResult();
|
||||
Cache.Dispose();
|
||||
Cache = AetheriaStores.Open(CatalogPath, catalogWritable: true, registry: TestCatalog.Registry());
|
||||
}
|
||||
|
||||
public static CultRecordRef<ItemData> Item(string key) => new CultRecordRef<ItemData>(new CultRecordKey(key));
|
||||
public static string Key(string id) => "commodity:" + id;
|
||||
|
||||
public CultRecordRef<StationKindData> Station(string id, string name)
|
||||
{
|
||||
var key = StationKindData.KeyOf(id);
|
||||
Cache.Upsert(typeof(StationKindData), new StationKindData { Name = name }, key);
|
||||
return new CultRecordRef<StationKindData>(key);
|
||||
}
|
||||
|
||||
private void Sell(CultRecordKey design) =>
|
||||
Cache.Upsert(new FactionProductData { Name = design.Value, Design = new CultRecordRef<CraftedItemData>(design), Manufacturer = Maker });
|
||||
|
||||
// A commodity and its recipe. Roles make it a consumer good (a role recipe); Inputs make it a compound commodity.
|
||||
public string Good(string id, (string Id, int Count)[] inputs = null, string[] fills = null, int outputCount = 1, float cycle = 1,
|
||||
int workforce = 0, (string Id, int Count)[] upkeep = null, string lore = "a good", (string Role, int Count)[] roles = null, CultRecordRef<StationKindData>? kind = null)
|
||||
{
|
||||
var key = new CultRecordKey(Key(id));
|
||||
var roleList = (roles ?? Array.Empty<(string, int)>()).Select(r => new ItemRole { Name = r.Role, Count = r.Count }).ToList();
|
||||
if (inputs == null && roles == null) Cache.Upsert(typeof(SimpleCommodityData), new SimpleCommodityData { Name = id }, key);
|
||||
else Cache.Upsert(typeof(CompoundCommodityData), new CompoundCommodityData { Name = id, Roles = roleList }, key);
|
||||
Cache.Upsert(typeof(RecipeData), new RecipeData
|
||||
{
|
||||
Output = Item(key.Value),
|
||||
OutputCount = outputCount,
|
||||
Cycle = cycle,
|
||||
Workforce = workforce,
|
||||
Inputs = (inputs ?? Array.Empty<(string, int)>()).Select(i => new CommodityCount { Commodity = Item(Key(i.Id)), Count = i.Count }).ToList(),
|
||||
Upkeep = (upkeep ?? Array.Empty<(string, int)>()).Select(i => new CommodityCount { Commodity = Item(Key(i.Id)), Count = i.Count }).ToList(),
|
||||
StationKind = kind ?? Mill,
|
||||
LoreHook = lore,
|
||||
Fills = (fills ?? Array.Empty<string>()).ToList()
|
||||
}, RecipeData.KeyOf(key));
|
||||
return key.Value;
|
||||
}
|
||||
|
||||
// Gear taking the given roles, with its recipe, sold by the maker unless offered is false.
|
||||
public string Gear(string id, (string Role, int Count)[] roles, bool offered = true, bool recipe = true)
|
||||
{
|
||||
var key = new CultRecordKey("gear:" + id);
|
||||
Cache.Upsert(typeof(GearData), new GearData
|
||||
{
|
||||
Name = id, Hardpoint = HardpointType.Tool, Shape = new Shape(), Price = 1,
|
||||
Roles = roles.Select(r => new ItemRole { Name = r.Role, Count = r.Count }).ToList()
|
||||
}, key);
|
||||
Design(key, offered, recipe);
|
||||
return key.Value;
|
||||
}
|
||||
|
||||
public string Hull(string id, HullType type, (string Role, int Count)[] roles, bool offered = true, bool recipe = true)
|
||||
{
|
||||
var key = new CultRecordKey("hull:" + id);
|
||||
Cache.Upsert(typeof(HullData), new HullData
|
||||
{
|
||||
Name = id, HullType = type, Shape = new Shape(), Price = 1, Kind = type == HullType.Station ? Mill : default,
|
||||
Roles = roles.Select(r => new ItemRole { Name = r.Role, Count = r.Count }).ToList()
|
||||
}, key);
|
||||
Design(key, offered, recipe);
|
||||
return key.Value;
|
||||
}
|
||||
|
||||
private void Design(CultRecordKey key, bool offered, bool recipe)
|
||||
{
|
||||
Cache.Upsert(new FactionProductData
|
||||
{
|
||||
Name = key.Value, Design = new CultRecordRef<CraftedItemData>(key), Manufacturer = Maker, NotOffered = !offered
|
||||
});
|
||||
if (recipe)
|
||||
Cache.Upsert(typeof(RecipeData), new RecipeData { Output = Item(key.Value), StationKind = Mill }, RecipeData.KeyOf(key));
|
||||
}
|
||||
|
||||
public void EditRecipe(string outputKey, Action<RecipeData> edit)
|
||||
{
|
||||
var key = RecipeData.KeyOf(new CultRecordKey(outputKey));
|
||||
var recipe = Cache.Get<RecipeData>(key);
|
||||
edit(recipe);
|
||||
Cache.Upsert(typeof(RecipeData), recipe, key);
|
||||
}
|
||||
|
||||
public void EditItem<T>(string key, Action<T> edit) where T : class
|
||||
{
|
||||
var item = Cache.Get<T>(new CultRecordKey(key));
|
||||
edit(item);
|
||||
Cache.Upsert(typeof(T), item, new CultRecordKey(key));
|
||||
}
|
||||
|
||||
public void Gate(params (string Commodity, bool Buys, bool Sells)[] rows) =>
|
||||
Cache.Upsert(typeof(OutsideMarketData), new OutsideMarketData
|
||||
{
|
||||
Name = "Gate",
|
||||
Rows = rows.Select(r => new OutsideMarketRow { Commodity = Item(r.Commodity), Price = 10, Buys = r.Buys, Sells = r.Sells, RatePerHour = 5 }).ToList()
|
||||
}, OutsideMarketData.KeyOf("gate"));
|
||||
|
||||
// ore (raw) -> plate (fills Plate) -> a laser and a skiff that each take two plates: no fault anywhere.
|
||||
public static EconomyFixture Healthy()
|
||||
{
|
||||
var f = new EconomyFixture();
|
||||
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) });
|
||||
return f;
|
||||
}
|
||||
}
|
||||
94
tests/Aetheria.Shared.Tests/RecipeSchemaTests.cs
Normal file
94
tests/Aetheria.Shared.Tests/RecipeSchemaTests.cs
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
using System.Linq;
|
||||
using GameCult.Caching;
|
||||
using MessagePack;
|
||||
using Xunit;
|
||||
|
||||
// Recipe records and role counts as catalog data (aetheria-living-world, cut recipe-schema-and-checks).
|
||||
public sealed class RecipeSchemaTests
|
||||
{
|
||||
[Fact]
|
||||
public void RecipeIsKeyedByItsOutput()
|
||||
{
|
||||
using var f = new EconomyFixture();
|
||||
var ore = f.Good("ore", lore: "first");
|
||||
Assert.Equal("recipe:commodity:ore", RecipeData.KeyOf(new CultRecordKey(ore)).Value);
|
||||
Assert.Equal("first", Recipes.RecipeOf(f.Cache, f.Cache.Get<ItemData>(new CultRecordKey(ore))).LoreHook);
|
||||
f.Good("ore", lore: "second");
|
||||
var recipes = f.Cache.GetAll<RecipeData>().ToArray();
|
||||
Assert.Single(recipes);
|
||||
Assert.Equal("second", recipes[0].LoreHook);
|
||||
Assert.Null(Recipes.RecipeOf(f.Cache, null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OldRoleReadsCountOne()
|
||||
{
|
||||
var options = GameCult.Caching.MessagePack.CultDocumentMessagePackSerialization.OptionsFor(typeof(ItemRole).Assembly);
|
||||
var old = MessagePackSerializer.Serialize(new[] { "Lens" });
|
||||
var role = MessagePackSerializer.Deserialize<ItemRole>(old, options);
|
||||
Assert.Equal("Lens", role.Name);
|
||||
Assert.Equal(1, role.Count);
|
||||
var counted = MessagePackSerializer.Deserialize<ItemRole>(MessagePackSerializer.Serialize(new ItemRole { Name = "Lens", Count = 4 }, options), options);
|
||||
Assert.Equal(4, counted.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RoleCountSurvivesTheStore()
|
||||
{
|
||||
using var f = new EconomyFixture();
|
||||
f.Gear("laser", new[] { ("Lens", 3), ("Frame", 1) });
|
||||
f.Reopen();
|
||||
var laser = f.Cache.Get<GearData>(new CultRecordKey("gear:laser"));
|
||||
Assert.Equal(new[] { 3, 1 }, laser.Roles.Select(r => r.Count));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecipeAndMarketRoundTrip()
|
||||
{
|
||||
using var f = new EconomyFixture();
|
||||
f.Good("ore");
|
||||
f.Good("plate", inputs: new[] { ("ore", 2) }, fills: new[] { "Plate", "Cover" }, outputCount: 3, cycle: 12.5f, workforce: 4,
|
||||
upkeep: new[] { ("ore", 1) }, lore: "forged in the Mill");
|
||||
f.Gate(("commodity:ore", true, false), ("commodity:plate", false, true));
|
||||
f.Reopen();
|
||||
var recipe = f.Cache.Get<RecipeData>(RecipeData.KeyOf(new CultRecordKey("commodity:plate")));
|
||||
Assert.Equal("commodity:plate", recipe.Output.Key.Value);
|
||||
Assert.Equal(3, recipe.OutputCount);
|
||||
Assert.Equal(12.5f, recipe.Cycle);
|
||||
Assert.Equal(4, recipe.Workforce);
|
||||
Assert.Equal(("commodity:ore", 2), (recipe.Inputs.Single().Commodity.Key.Value, recipe.Inputs.Single().Count));
|
||||
Assert.Equal(("commodity:ore", 1), (recipe.Upkeep.Single().Commodity.Key.Value, recipe.Upkeep.Single().Count));
|
||||
Assert.Equal(f.Mill.Key.Value, recipe.StationKind.Key.Value);
|
||||
Assert.Equal("forged in the Mill", recipe.LoreHook);
|
||||
Assert.Equal(new[] { "Plate", "Cover" }, recipe.Fills);
|
||||
var market = f.Cache.Get<OutsideMarketData>(OutsideMarketData.KeyOf("gate"));
|
||||
Assert.Equal("Gate", market.Name);
|
||||
Assert.Equal(2, market.Rows.Count);
|
||||
var row = market.Rows[1];
|
||||
Assert.Equal(("commodity:plate", 10, false, true, 5f), (row.Commodity.Key.Value, row.Price, row.Buys, row.Sells, row.RatePerHour));
|
||||
Assert.True(market.Rows[0].Buys);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecipesReadDerivedFacts()
|
||||
{
|
||||
using var f = new EconomyFixture();
|
||||
f.Good("zinc", fills: new[] { "Plate" });
|
||||
f.Good("alum", fills: new[] { "Plate", "Cover" });
|
||||
f.Good("ore");
|
||||
f.Good("tin", inputs: new[] { ("ore", 1) });
|
||||
f.Good("kettle", roles: new[] { ("Plate", 1) });
|
||||
var gear = f.Gear("laser", new[] { ("Plate", 1) });
|
||||
Assert.Equal(new[] { "commodity:alum", "commodity:zinc" }, Recipes.Fillers(f.Cache, "Plate").Select(k => k.Value));
|
||||
Assert.Empty(Recipes.Fillers(f.Cache, "Nothing"));
|
||||
RecipeData Of(string key) => f.Cache.Get<RecipeData>(RecipeData.KeyOf(new CultRecordKey(key)));
|
||||
Assert.True(Recipes.IsRaw(f.Cache, Of("commodity:ore")));
|
||||
Assert.False(Recipes.IsRaw(f.Cache, Of("commodity:tin")));
|
||||
Assert.False(Recipes.IsRaw(f.Cache, Of("commodity:kettle")));
|
||||
Assert.True(Recipes.IsRoleRecipe(f.Cache, Of(gear)));
|
||||
Assert.False(Recipes.IsRoleRecipe(f.Cache, Of("commodity:tin")));
|
||||
Assert.True(Recipes.IsConsumerGood(f.Cache, Of("commodity:kettle")));
|
||||
Assert.False(Recipes.IsConsumerGood(f.Cache, Of(gear)));
|
||||
Assert.False(Recipes.IsConsumerGood(f.Cache, Of("commodity:tin")));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue