Files
SirRoboGarage/ChimeraBot_garage/tests/test_influence.nim
T

80 lines
3.4 KiB
Nim

## Tier A unit tests for the CHIMERA influence / fallback layer
## (chimera/influence.nim). Run: from ChimeraBot_garage/src, `nimble test`.
import std/math
import chimera/fusion
import chimera/influence
var failed = 0
proc check(name: string, cond: bool, detail: string = "") =
if cond: echo " ok ", name
else: echo " FAIL ", name, " [", detail, "]"; inc failed
proc fusedPath(points: seq[tuple[x, y: float64]], pFire = 0.4): FusedPrediction =
result.horizon = points.len
result.points = points
for _ in points: result.pFire.add pFire
result.fireWithin = 0.8
result.confidence = 0.7
block:
echo "test 1: influence ramp"
check("no samples => no influence", influenceScalar(0.0, 0) == 0.0)
check("low loss => full influence", influenceScalar(0.1, 5) == 1.0)
check("high loss => zero influence", influenceScalar(5.0, 5) == 0.0)
let mid = influenceScalar((GoodLoss + BadLoss) / 2.0, 5)
check("mid loss => mid influence", abs(mid - 0.5) < 1e-9, $mid)
check("monotone decreasing",
influenceScalar(0.6, 1) >= influenceScalar(0.7, 1) and
influenceScalar(0.7, 1) >= influenceScalar(0.8, 1))
block:
echo "test 2: reflex gate — never touch an active bullet dodge"
let fp = fusedPath(@[(60.0, 100.0), (60.0, 130.0), (60.0, 160.0)])
let adv = overlay(1.0, 6.0, 0.0, 100.0, 100.0, 90.0, 6.0, fp, reflexBusy = true)
check("not applied while dodging", not adv.applied)
check("command untouched", adv.speed == 6.0 and adv.turnRate == 0.0)
block:
echo "test 3: zero influence => no steering"
let fp = fusedPath(@[(60.0, 100.0), (60.0, 130.0), (60.0, 160.0)])
let adv = overlay(0.0, 6.0, 0.0, 100.0, 100.0, 90.0, 6.0, fp, reflexBusy = false)
check("not applied", not adv.applied)
check("command untouched", adv.speed == 6.0 and adv.turnRate == 0.0)
block:
echo "test 4: steering toward safety + safe margins"
# Collision course: our straight path (heading north through x=100) runs
# straight into the fused predicted enemy (a stationary point at y=130), so
# any turn buys clearance before we arrive.
let fp = fusedPath(@[(100.0, 130.0), (100.0, 130.0), (100.0, 130.0),
(100.0, 130.0), (100.0, 130.0)])
let adv = overlay(1.0, 6.0, 0.0, 100.0, 100.0, 90.0, 6.0, fp, reflexBusy = false)
check("applied when it improves clearance", adv.applied, $adv.gain)
check("turns away from the predicted path", adv.turnRate < 0.0, $adv.turnRate)
check("turn within cap", abs(adv.turnRate - 0.0) <= MaxTurnBias + 1e-9,
$adv.turnRate)
check("speed within cap", abs(adv.speed - 6.0) <= MaxSpeedDelta + 1e-9,
$adv.speed)
block:
echo "test 5: no nudge when the baseline is already safe"
let far = fusedPath(@[(100.0, 10000.0), (100.0, 10030.0), (100.0, 10060.0)])
let adv = overlay(1.0, 6.0, 0.0, 100.0, 100.0, 90.0, 6.0, far,
reflexBusy = false)
check("not applied (gain below margin)", not adv.applied, $adv.gain)
block:
echo "test 6: determinism"
let fp = fusedPath(@[(100.0, 130.0), (100.0, 130.0), (100.0, 130.0),
(100.0, 130.0), (100.0, 130.0)])
let a = overlay(1.0, 6.0, 0.0, 100.0, 100.0, 90.0, 6.0, fp, false)
let b = overlay(1.0, 6.0, 0.0, 100.0, 100.0, 90.0, 6.0, fp, false)
check("bit-identical advisory",
a.speed == b.speed and a.turnRate == b.turnRate and
a.applied == b.applied and a.gain == b.gain)
echo ""
if failed == 0: echo "all influence tests passed"
else: echo failed, " influence test(s) FAILED"; quit(1)