Files

96 lines
4.0 KiB
Nim

## Tests for the CHIMERA debug overlay (chimera/debugdraw.nim).
##
## Run from `ChimeraBot_garage/src` (as the nimble `test` task does):
## nim c -r -d:chimeraDebug ../tests/test_debugdraw.nim
##
## Two concerns:
## (i) the pure primitive builder is deterministic and respects the per-tick
## budget, and produces the right primitives for the interesting states;
## (ii) THE REGRESSION ASSERTION: rendering the overlay does not perturb the
## command intent. We run a synthetic tick pipeline twice on the same
## input — once with the overlay drawn and once without — and assert the
## serialised `BotIntent` is identical once `debugGraphics` (the whole
## point of the overlay) is removed.
##
## The test is compiled WITH `-d:chimeraDebug` so `renderOverlay` actually draws;
## without the define it would be an empty stub and the assertion vacuous.
import std/[json, strutils]
import robocode_tankroyale_botapi
import chimera/debugdraw
var failed = 0
proc check(name: string, cond: bool, detail: string = "") =
if cond: echo " ok ", name
else: echo " FAIL ", name, " [", detail, "]"; inc failed
proc sampleInput(): OverlayInput =
OverlayInput(
selfX: 100.0, selfY: 200.0,
kernelName: "ORBIT_BALLIST", kernelP: 0.96, entropy: 0.21,
pFire5: 0.42,
enemyX: 500.0, enemyY: 300.0, haveEnemy: true,
predX: 530.0, predY: 320.0, havePred: true,
fresh: true, censored: false, scanGap: 1,
probeLabel: "pkReversal", probeFlash: true)
block:
echo "debugdraw: pure builder determinism + budget"
let inp = sampleInput()
let a = buildOverlay(inp)
let b = buildOverlay(inp)
check("buildOverlay deterministic (byte-identical reruns)", a == b)
check("budget respected (<= MaxDebugPrims)", a.len <= MaxDebugPrims,
"got " & $a.len)
check("full-featured input draws every layer (>= 7 prims)", a.len >= 7,
"got " & $a.len)
# Deprived states must not crash and must stay within budget.
var bare = OverlayInput(selfX: 1.0, selfY: 1.0)
check("empty input -> only scan-health (<= 1 prim)", buildOverlay(bare).len <= 1)
var noPred = inp
noPred.havePred = false
check("no prediction -> no counterfactual line",
buildOverlay(noPred).len < buildOverlay(inp).len)
var censored = inp
censored.censored = true
check("censor tick -> counterfactual skipped; scan-health shown",
buildOverlay(censored).len <= MaxDebugPrims)
block:
echo "debugdraw: regression — overlay must not change the command intent"
# Deterministic stand-in for one tick's command pipeline. It uses the REAL
# botapi command setters and the REAL buildIntentJson, so this is exactly the
# state that `go()` would serialise.
proc syntheticTick(withOverlay: bool): JsonNode =
setGunTurnRate(0.0)
setRadarTurnRate(45.0)
setTargetSpeed(3.0)
setTurnRate(-2.5)
if withOverlay:
renderOverlay(buildOverlay(sampleInput()))
result = parseJson(buildIntentJson())
if result.hasKey("debugGraphics"):
result.delete("debugGraphics") # overlay's only intended effect
let withoutOverlay = syntheticTick(withOverlay = false)
let withOverlay = syntheticTick(withOverlay = true)
check("command intent identical with/without overlay",
withoutOverlay == withOverlay,
"without=" & $withoutOverlay & " with=" & $withOverlay)
check("turn rate is untouched", $withOverlay["turnRate"] == $(-2.5))
check("target speed is untouched", $withOverlay["targetSpeed"] == $3.0)
when defined(chimeraDebug):
# Prove the overlay really drew under the gate (so the comparison above was
# not vacuous): the global SVG buffer must hold the drawn fragment.
check("overlay produced SVG under -d:chimeraDebug", svgOutput().len > 0)
check("SVG carries our primitives",
svgOutput().contains("P(fire5)") and svgOutput().contains("probe:"))
else:
echo " note: built without -d:chimeraDebug (renderOverlay is an empty stub)"
echo ""
if failed == 0: echo "all debugdraw tests passed"
else: echo failed, " debugdraw test(s) FAILED"; quit(1)