diff --git a/tests/Aetheria.Shared.Tests/ConditionRatioTests.cs b/tests/Aetheria.Shared.Tests/ConditionRatioTests.cs new file mode 100644 index 000000000..2ff03d18f --- /dev/null +++ b/tests/Aetheria.Shared.Tests/ConditionRatioTests.cs @@ -0,0 +1,266 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using CultMath; +using GameCult.Caching; +using Xunit; +using static CultMath.math; + +// Operator ask (2026-09-19, docs/... none yet -- this is Cut 8 on top of Cut 7's brownout work): "you can't +// currently tell when [a thruster is] busted -- the particle system it spawns responds only to thrust intent. +// Should multiply that by actual performance." EquippedItem.ConditionRatio (Entity.cs) is the one number that +// answers "how healthy does this item actually look": the item's resolved value for a stat against what the +// same item, same lot, same modifiers would produce with Heat/Durability/PowerSupply pinned to their identity. +// Thruster.Condition and AetherDrive.Condition (Thruster.cs/AetherDrive.cs) are that ratio read against each +// behaviour's own governing stat (Thrust, Torque); ShipInstance.cs (Unity-side, not covered here) multiplies +// presentation intent by it instead of recomputing any durability/heat/power arithmetic of its own. +// Each rule here has a matching mutation in tests/mutation_tests_condition_ratio_cut8.py. +public sealed class ConditionRatioTests : IDisposable +{ + private readonly string _root = Path.Combine(Path.GetTempPath(), "aetheria-conditionratio-" + Guid.NewGuid().ToString("N")); + private string Catalog => Path.Combine(_root, "Aetheria.cc"); + + public ConditionRatioTests() => Directory.CreateDirectory(_root); + public void Dispose() => Directory.Delete(_root, true); + + private static GameplaySettings Settings() => new GameplaySettings + { + DefaultEntitySettings = new EntitySettings(), + Tiers = new[] { new RarityTier { Name = "Common", Quality = .5f, Rarity = 0, Color = new float3(1, 1, 1) } }, + QualityPriceModifier = new ExponentialLerp() + }; + + private static PerformanceStat Constant(float v) => new PerformanceStat { Min = v, Max = v }; + + // Optimal temperature 280, plateau 400 wide (matches BrownoutTests' own consumer shape): the ambient + // temperature this fixture starts at (280, set explicitly per test rather than relied on as a default) sits + // dead center of the plateau, so ThermalPerformance reads exactly 1 there. + private const float OptimalTemperature = 280f; + + private static EquippableItem Mint(CultCache cache, ItemManager items, ItemData design) => new EquippableItem + { + Data = cache.RefOf(design), + Durability = design is EquippableItemData equippable ? equippable.Durability : 0, + Lot = items.Lots.Add(new Lot { Design = cache.RefOf(design), Origin = new Attributed(), Quality = .5f, Roles = new List() }) + }; + + private static string NewRoot() + { + var root = Path.Combine(Path.GetTempPath(), "aetheria-conditionratio-" + Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(root); + return root; + } + + // Hull, a Reactor (BrownoutTests' own shape: no capacitor, so a tick's grant ratio is a clean + // min(1, generation/demand)), and one Consumer carrying the given ThrusterData. Defaults to this instance's + // own catalog path; a test that needs two independent ships (the power-grant comparison below) passes a + // second, freshly rooted path rather than reopening the same file twice in one process. + private CultCache OpenCatalog(ThrusterData thruster, string catalogPath = null) + { + var cache = AetheriaStores.Open(catalogPath ?? Catalog, catalogWritable: true); + cache.Upsert(new TestCatalogGlobal { Name = "Temperament" }); + var hullShape = new Shape(5, 5); + foreach (var cell in hullShape.AllCoordinates) hullShape[cell] = true; + cache.Upsert(new HullData { Name = "Skiff", HullType = HullType.Ship, Shape = hullShape, Durability = 10, Mass = 1000 }); + cache.Upsert(new GearData + { + Name = "Reactor", Hardpoint = HardpointType.Tool, Shape = new Shape(), Durability = 10, + MinimumTemperature = 0, MaximumTemperature = 1000, OptimalTemperature = OptimalTemperature, PlateauWidth = 400, + Behaviors = { new ReactorData + { + Charge = Constant(0), Efficiency = Constant(1), OverloadEfficiency = Constant(1), ThrottlingFactor = Constant(2) + } } + }); + cache.Upsert(new GearData + { + Name = "Consumer", Hardpoint = HardpointType.Tool, Shape = new Shape(), Durability = 10, + MinimumTemperature = 0, MaximumTemperature = 1000, OptimalTemperature = OptimalTemperature, PlateauWidth = 400, + Behaviors = { thruster } + }); + cache.FlushAsync().Wait(); + return cache; + } + + private Ship BuildShip(CultCache cache, float reactorCharge, out EquippedItem consumer) + { + var items = new ItemManager(cache, new ProvenanceLedger(), Settings(), _ => { }); + var zone = new Zone(items, new PlanetSettings(), new ZonePack(), new GalaxyZone { Name = "Test Zone", Owner = null }, null); + var hull = Mint(cache, items, cache.GetByName("Skiff")); + var ship = new Ship(items, zone, hull, new EntitySettings()); + + var reactorData = cache.GetByName("Reactor"); + ((ReactorData) reactorData.Behaviors[0]).Charge = Constant(reactorCharge); + Assert.True(ship.TryEquip(Mint(cache, items, reactorData))); + Assert.True(ship.TryEquip(Mint(cache, items, cache.GetByName("Consumer")))); + + zone.Entities.Add(ship); + ship.LookDirection = float3(0, 0, 1); // Ship.Update's own steering pass stays a no-op, as BrownoutTests + ship.Activate(); + consumer = ship.Equipment.Single(e => e.Data.Name == "Consumer"); + + // Settle at the plateau's center: two ticks so UpdatePerformance's deltaTemp term reads 0 before a test + // drives its own change, matching HeatResponseTests' own settle pattern. + foreach (var cell in consumer.InsetShape.Coordinates) ship.Temperature[cell.x, cell.y] = OptimalTemperature; + consumer.UpdatePerformance(); + consumer.UpdatePerformance(); + return ship; + } + + // --- 1: a perfect item -- full durability, a grant far exceeding demand -- reads exactly 1, on a stat that + // --- declares Durability and PowerSupply together (both at their identity). The Heat term's own identity + // --- case is pinned by ConditionFallsWithTemperatureAwayFromThePlateauAlone's "atPlateau" reading below; + // --- combining it here too would make this test depend on UpdateTemperature's own ambient drift over the + // --- Update tick PowerSupply needs, which is a real dependency this test has no reason to take on. --- + [Fact] + public void APerfectItemsConditionIsOne() + { + var thrust = new PerformanceStat + { + Min = 0, Max = 100, + Terms = + { + new StatTerm { Source = StatSource.Durability, Exponent = 1 }, + new StatTerm { Source = StatSource.PowerSupply, Exponent = 1 } + } + }; + using var cache = OpenCatalog(new ThrusterData { Thrust = thrust, Visibility = Constant(0), Heat = Constant(0), EnergyUsage = Constant(100) }); + var ship = BuildShip(cache, reactorCharge: 1000, out var consumer); + ship.Update(1f); + + Assert.Equal(1f, ship.GetBehavior().Condition, 4); + } + + // --- 2: durability alone. Full temperature/power, half durability, a stat with only a Durability term. --- + [Fact] + public void ConditionFallsWithDurabilityAlone() + { + var thrust = new PerformanceStat { Min = 0, Max = 100, Terms = { new StatTerm { Source = StatSource.Durability, Exponent = 1 } } }; + using var cache = OpenCatalog(new ThrusterData { Thrust = thrust, Visibility = Constant(0), Heat = Constant(0), EnergyUsage = Constant(100) }); + var ship = BuildShip(cache, reactorCharge: 1000, out var consumer); + + consumer.EquippableItem.Durability = consumer.Data.Durability; // full + consumer.UpdatePerformance(); // direct call, not ship.Update -- no reason to also churn UpdateTemperature + var full = ship.GetBehavior().Condition; + + consumer.EquippableItem.Durability = consumer.Data.Durability * .5f; // half + consumer.UpdatePerformance(); + var half = ship.GetBehavior().Condition; + + Assert.Equal(1f, full, 4); + Assert.True(half > 0f && half < full, $"half={half} should sit strictly between 0 and full={full}"); + } + + // --- 3: temperature alone, away from the plateau. Full durability/power, a stat with only a Heat term. --- + [Fact] + public void ConditionFallsWithTemperatureAwayFromThePlateauAlone() + { + var thrust = new PerformanceStat { Min = 0, Max = 100, Terms = { new StatTerm { Source = StatSource.Heat, Exponent = 1 } } }; + using var cache = OpenCatalog(new ThrusterData { Thrust = thrust, Visibility = Constant(0), Heat = Constant(0), EnergyUsage = Constant(100) }); + var ship = BuildShip(cache, reactorCharge: 1000, out var consumer); + var atPlateau = ship.GetBehavior().Condition; // BuildShip already settled at OptimalTemperature + + // Push the item's own temperature far outside the plateau (center 280, half-width 200 -> outside past + // 480), then read performance directly (not through ship.Update, whose UpdateTemperature would pull the + // value we just set back toward ambient equilibrium within the same tick, before UpdatePerformance ever + // sees it -- exactly HeatResponseTests' own SetTemperature-then-UpdatePerformance pattern). + foreach (var cell in consumer.InsetShape.Coordinates) ship.Temperature[cell.x, cell.y] = 900f; + consumer.UpdatePerformance(); + var offPlateau = ship.GetBehavior().Condition; + + Assert.Equal(1f, atPlateau, 4); + Assert.True(offPlateau > 0f && offPlateau < atPlateau, $"offPlateau={offPlateau} should sit strictly between 0 and atPlateau={atPlateau}"); + } + + // --- 4: power alone. Full durability/temperature, a partial grant, a stat with only a PowerSupply term + // --- (mirrors BrownoutTests' own Curved fixture). --- + [Fact] + public void ConditionFallsWithAPartialPowerGrantAlone() + { + var thrust = new PerformanceStat { Min = 0, Max = 100, Terms = { new StatTerm { Source = StatSource.PowerSupply, Exponent = 1 } } }; + + using var cache = OpenCatalog(new ThrusterData { Thrust = thrust, Visibility = Constant(0), Heat = Constant(0), EnergyUsage = Constant(100) }); + var fullShip = BuildShip(cache, reactorCharge: 1000, out _); // demand 100, generation far exceeds it -> ratio 1 + fullShip.MovementDirection = float2(0, -1); // real thrust demand -- PowerRequest is 0 with no input at all + fullShip.Update(1f); + var full = fullShip.GetBehavior().Condition; + + var secondRoot = NewRoot(); + try + { + using var cache2 = OpenCatalog(new ThrusterData { Thrust = thrust, Visibility = Constant(0), Heat = Constant(0), EnergyUsage = Constant(100) }, catalogPath: Path.Combine(secondRoot, "Aetheria.cc")); + var halfShip = BuildShip(cache2, reactorCharge: 50, out _); // demand 100, generation 50 -> ratio .5 + halfShip.MovementDirection = float2(0, -1); + halfShip.Update(1f); + var half = halfShip.GetBehavior().Condition; + + Assert.Equal(1f, full, 4); + Assert.True(half > 0f && half < full, $"half={half} should sit strictly between 0 and full={full}"); + } + finally { Directory.Delete(secondRoot, true); } + } + + // --- 5: dead ends at 0, not some small positive residue -- a fully destroyed item on a stat with Min=0 + // --- produces nothing, so the ratio itself is 0/0-guarded down to 0 rather than a divide-by-zero or NaN. --- + [Fact] + public void ConditionIsZeroWhenTheItemProducesNothing() + { + var thrust = new PerformanceStat { Min = 0, Max = 100, Terms = { new StatTerm { Source = StatSource.Durability, Exponent = 1 } } }; + using var cache = OpenCatalog(new ThrusterData { Thrust = thrust, Visibility = Constant(0), Heat = Constant(0), EnergyUsage = Constant(100) }); + var ship = BuildShip(cache, reactorCharge: 1000, out var consumer); + + consumer.EquippableItem.Durability = 0f; + ship.Update(1f); + + Assert.Equal(0f, ship.GetBehavior().Condition, 4); + } + + // --- 5b: the guard's real reason to exist -- a stat authored with Max=0 (no terms at all) makes the nominal + // --- side degenerate to exactly 0 too, so actual/nominal is 0/0. Without the guard that is NaN, which is not + // --- equal to 0f (the ConditionIsZeroWhenTheItemProducesNothing case above never hits this: its nominal + // --- stays 100 -- only the numerator goes to 0 there). --- + [Fact] + public void ConditionIsZeroNotNaNWhenTheNominalValueItselfDegeneratesToZero() + { + var thrust = Constant(0); // Min == Max == 0, no Terms -- both actual and nominal evaluate to exactly 0 + using var cache = OpenCatalog(new ThrusterData { Thrust = thrust, Visibility = Constant(0), Heat = Constant(0), EnergyUsage = Constant(100) }); + var ship = BuildShip(cache, reactorCharge: 1000, out _); + ship.Update(1f); + + Assert.Equal(0f, ship.GetBehavior().Condition, 4); + } + + // --- 6: presentation reading Condition must not have moved what the thruster actually does. Exact numbers + // --- from BrownoutTests.ThrusterAtFullGrantIsUnchangedFromToday and ...DegradesByItsAuthoredExponentNotLinearly, + // --- reproduced here against a ship that also reads Condition every tick, to prove Execute()'s own force + // --- math is untouched by this cut. --- + [Fact] + public void ThrustForceForAGivenInputIsUnchangedByThisCut() + { + var thrust = new PerformanceStat { Min = 0, Max = 100, Terms = { new StatTerm { Source = StatSource.PowerSupply, Exponent = 1 } } }; + using var cache = OpenCatalog(new ThrusterData { Thrust = thrust, Visibility = Constant(0), Heat = Constant(0), EnergyUsage = Constant(100) }); + var ship = BuildShip(cache, reactorCharge: 1000, out _); // demand 100, generation far exceeds it -> ratio 1 + ship.MovementDirection = float2(0, -1); // reverse-thruster axis += -MovementDirection.y == 1 + ship.Update(1f); + + // Reading Condition after Update (as presentation would every frame) must not perturb the result. + _ = ship.GetBehavior().Condition; + + Assert.Equal(.1f, -ship.Velocity.y, 3); // 100 thrust / 1000 mass * dt 1, exactly as BrownoutTests pins it + } + + [Fact] + public void ThrustForceDegradesByItsAuthoredExponentUnchangedByThisCut() + { + var thrust = new PerformanceStat { Min = 0, Max = 100, Terms = { new StatTerm { Source = StatSource.PowerSupply, Exponent = 2 } } }; + using var cache = OpenCatalog(new ThrusterData { Thrust = thrust, Visibility = Constant(0), Heat = Constant(0), EnergyUsage = Constant(100) }); + var ship = BuildShip(cache, reactorCharge: 50, out _); // demand 100, generation 50 -> ratio .5 + ship.MovementDirection = float2(0, -1); + ship.Update(1f); + + _ = ship.GetBehavior().Condition; + + // Thrust = lerp(0, 100, pow(.5, 2)) = 25; velocity delta = 25 / 1000 mass * dt 1 = .025. + Assert.Equal(.025f, -ship.Velocity.y, 3); + } +} diff --git a/tests/mutation_tests_condition_ratio_cut8.py b/tests/mutation_tests_condition_ratio_cut8.py new file mode 100644 index 000000000..ba67066e9 --- /dev/null +++ b/tests/mutation_tests_condition_ratio_cut8.py @@ -0,0 +1,194 @@ +#!/usr/bin/env python3 +"""Mutation tests for Cut 8, the operator's condition-ratio ask (2026-09-19): "you can't currently tell when +[a thruster is] busted -- the particle system it spawns responds only to thrust intent. Should multiply that by +actual performance." This follows Cut 7's brownout work (mutation_tests_stats_power_cut7.py) in this repo's own +numbering. + +EquippedItem.ConditionRatio (Entity.cs) is the one number that answers "how healthy does this item actually +look": the item's resolved value for a stat against what the same item, same lot, same modifier stack would +produce with Heat/Durability/PowerSupply pinned to their identity (NominalContext, the private struct right +below it). Thruster.Condition and AetherDrive.Condition (Thruster.cs/AetherDrive.cs) are that ratio read against +each behaviour's own governing stat. This script mutates each rule's own changed line and asserts the matching +ConditionRatioTests.cs Fact goes red for exactly that reason. A no-op control mutation proves the +read/replace/restore path is transparent. + +Usage: + python tests/mutation_tests_condition_ratio_cut8.py --cultlib-root + +Only touches files under this repository; every mutation is reversed before the script exits, including on +failure. +""" +from __future__ import annotations + +import argparse +import subprocess +import sys +from dataclasses import dataclass +from pathlib import Path + +REPO_ROOT = Path(__file__).resolve().parent.parent +TEST_PROJECT = REPO_ROOT / "tests" / "Aetheria.Shared.Tests" + + +@dataclass +class Mutation: + rule: str + file: str + anchor: str + mutated: str + test: str + expect: str # "red" (mutant must be killed) or "green" (the no-op control) + + +MUTATIONS: list[Mutation] = [ + # --- No-op control: proves the anchor/replace/restore mechanism is transparent --- + Mutation( + rule="control (no-op round trip)", + file="Assets/Scripts/ServerShared/Entity.cs", + anchor=" public float ConditionRatio(PerformanceStat stat) => saturate(Evaluate(stat) / stat.Evaluate(new NominalContext(this)));", + mutated=" public float ConditionRatio(PerformanceStat stat) => saturate(Evaluate(stat) / stat.Evaluate(new NominalContext(this)));", + test="ConditionRatioTests.APerfectItemsConditionIsOne", + expect="green", + ), + + # --- The ratio must divide actual by nominal, not the reverse. A stat pinned to the identity on the nominal + # --- side but degraded (half durability) on the actual side proves direction: dividend and divisor swapped + # --- turns "half sits strictly below full" into "both saturate to 1" (nominal/actual > 1 either way, and + # --- saturate clamps it there), which is exactly as wrong as it sounds and just as easy to write by accident. + Mutation( + rule="ConditionRatio must divide actual by nominal, not the reverse", + file="Assets/Scripts/ServerShared/Entity.cs", + anchor=" public float ConditionRatio(PerformanceStat stat) => saturate(Evaluate(stat) / stat.Evaluate(new NominalContext(this)));", + mutated=" public float ConditionRatio(PerformanceStat stat) => saturate(stat.Evaluate(new NominalContext(this)) / Evaluate(stat));", + test="ConditionRatioTests.ConditionFallsWithDurabilityAlone", + expect="red", + ), + + # --- Durability must actually be one of the terms NominalContext pins to identity -- if it is left to read + # --- the item's own live DurabilityFactor instead, a half-durability item's nominal denominator shrinks right + # --- along with its numerator and the ratio never leaves 1. --- + Mutation( + rule="NominalContext must pin DurabilityFactor to 1, not forward the item's live durability", + file="Assets/Scripts/ServerShared/Entity.cs", + anchor=" public float DurabilityFactor(float exponent) => 1f;", + mutated=" public float DurabilityFactor(float exponent) => _item.DurabilityFactor(exponent);", + test="ConditionRatioTests.ConditionFallsWithDurabilityAlone", + expect="red", + ), + + # --- Same shape of bug, on Heat: if NominalContext forwards the item's live thermal performance instead of + # --- pinning it to 1, an off-plateau item's own nominal denominator degrades with it and the ratio never + # --- moves off 1. --- + Mutation( + rule="NominalContext must pin HeatFactor to 1, not forward the item's live thermal performance", + file="Assets/Scripts/ServerShared/Entity.cs", + anchor=" public float HeatFactor(float exponent) => 1f;", + mutated=" public float HeatFactor(float exponent) => _item.HeatFactor(exponent);", + test="ConditionRatioTests.ConditionFallsWithTemperatureAwayFromThePlateauAlone", + expect="red", + ), + + # --- Same shape again, on PowerSupply -- the exact term Cut 7's brownout curve depends on. --- + Mutation( + rule="NominalContext must pin PowerSupplyFactor to 1, not forward the item's live grant", + file="Assets/Scripts/ServerShared/Entity.cs", + anchor=" public float PowerSupplyFactor(float exponent) => 1f;", + mutated=" public float PowerSupplyFactor(float exponent) => _item.PowerSupplyFactor(exponent);", + test="ConditionRatioTests.ConditionFallsWithAPartialPowerGrantAlone", + expect="red", + ), + + # --- Zero must land exactly on zero, not divide-by-zero into NaN, when the nominal denominator itself + # --- degenerates (a Max=0 stat: both actual and nominal evaluate to exactly 0). ConditionRatio relies on + # --- CultMath's own NaN-favoring math.min/max (0/0 saturates to a real 0f, not NaN) rather than a manual + # --- epsilon guard -- dropping the saturate() call removes that protection and 0/0 surfaces as raw NaN, which + # --- is not equal to 0f. ConditionIsZeroWhenTheItemProducesNothing cannot catch this by itself, since its + # --- nominal stays 100 and only the numerator goes to 0 there. --- + Mutation( + rule="ConditionRatio must saturate the division, not return the raw quotient", + file="Assets/Scripts/ServerShared/Entity.cs", + anchor=" public float ConditionRatio(PerformanceStat stat) => saturate(Evaluate(stat) / stat.Evaluate(new NominalContext(this)));", + mutated=" public float ConditionRatio(PerformanceStat stat) => Evaluate(stat) / stat.Evaluate(new NominalContext(this));", + test="ConditionRatioTests.ConditionIsZeroNotNaNWhenTheNominalValueItselfDegeneratesToZero", + expect="red", + ), + + # --- Thruster.Condition must read Thrust, the stat that actually governs this behaviour's force -- reading + # --- some other stat (here, the item's Visibility) would silently condition against the wrong quantity. --- + Mutation( + rule="Thruster.Condition must condition against the Thrust stat, not an unrelated one", + file="Assets/Scripts/ServerShared/Behaviors/Thruster.cs", + anchor=" public float Condition => Item?.ConditionRatio(_data.Thrust) ?? 1f;", + mutated=" public float Condition => Item?.ConditionRatio(_data.Visibility) ?? 1f;", + test="ConditionRatioTests.ConditionFallsWithDurabilityAlone", + expect="red", + ), +] + + +def replace_unique(path: Path, anchor: str, mutated: str) -> None: + original = path.read_text(encoding="utf-8") + count = original.count(anchor) + if count != 1: + raise SystemExit(f"{path}: anchor must occur exactly once, found {count}\n---\n{anchor}") + path.write_text(original.replace(anchor, mutated), encoding="utf-8") + + +def run_test(cultlib_root: str, test: str) -> tuple[int, str]: + proc = subprocess.run( + ["dotnet", "test", str(TEST_PROJECT), f"-p:CultLibRoot={cultlib_root}", + "--filter", f"FullyQualifiedName~{test}"], + capture_output=True, text=True, timeout=300, + ) + return proc.returncode, proc.stdout + proc.stderr + + +def main() -> int: + parser = argparse.ArgumentParser() + parser.add_argument("--cultlib-root", required=True) + args = parser.parse_args() + + failures: list[str] = [] + originals: dict[str, str] = {} + for m in MUTATIONS: + if m.file not in originals: + originals[m.file] = (REPO_ROOT / m.file).read_text(encoding="utf-8") + + def restore_all() -> None: + for file, text in originals.items(): + (REPO_ROOT / file).write_text(text, encoding="utf-8") + + try: + for m in MUTATIONS: + print(f"\n=== {m.rule} (expect {m.expect}) ===") + path = REPO_ROOT / m.file + replace_unique(path, m.anchor, m.mutated) + code, output = run_test(args.cultlib_root, m.test) + passed = "Passed!" in output and code == 0 + if m.expect == "green": + if not passed: + failures.append(f"{m.rule}: expected green (test passing), got failure\n{output[-2000:]}") + else: + print(f"OK: {m.test} passed under a no-op mutation, as expected") + else: + if passed: + failures.append(f"{m.rule}: expected red (mutant killed), but {m.test} still passed") + else: + print(f"OK: {m.test} went red under the mutation, as expected") + # Restore this file immediately so the next mutation starts from a clean tree. + path.write_text(originals[m.file], encoding="utf-8") + finally: + restore_all() + + if failures: + print("\n=== FAILURES ===") + for f in failures: + print(f"- {f}") + return 1 + + print("\nAll mutations behaved as declared.") + return 0 + + +if __name__ == "__main__": + sys.exit(main())