## Unit tests for the ONLINE-learning gun (elm_gun.nim): the delayed-label ## queue, the novelty gate, and the per-opponent reset. ## Run: nim c -r tests/test_online.nim (from DevControlBot_garage/) import std/math import modules/ELM/elm_gun import modules/ELM/elm_features import modules/ELM/elm # for Elm.infer in the assertions var failed = 0 proc check(name: string, cond: bool, detail: string = "") = if cond: echo " ok ", name else: echo " FAIL ", name, " [", detail, "]"; inc failed # ── 1. neutral start: no prior, gate closed ───────────────────────────────── block: echo "test 1: neutral start" let g = initElmGun(priorScale = 0.0) check("weights loaded", g.loaded) check("prior stored", g.priorBeta.len > 0) check("nOnline == 0", g.nOnline == 0) check("gate closed at start", not g.gateOpen) # ── 2. online learning opens the gate once it beats zero ──────────────────── block: echo "test 2: online learning closes then opens the novelty gate" let g = initElmGun(priorScale = 0.0) let feat = newSeq[float64](FeatureDim) # constant feature vector for i in 0 ..< 60: g.feedSample(feat, 0.3) # constant true correction check("accumulated samples", g.nOnline == 60) check("model beats zero (modelErr < zeroErr)", g.modelErr < g.zeroErr, $g.modelErr & " vs " & $g.zeroErr) check("gate OPEN after learning", g.gateOpen) let corr = g.model.infer(feat)[0] check("correction near label", abs(corr - 0.3) < 0.1, $corr) # ── 3. a model that cannot beat zero keeps the gate shut ──────────────────── block: echo "test 3: no-signal data keeps the gate closed" let g = initElmGun(priorScale = 0.0) let feat = newSeq[float64](FeatureDim) for i in 0 ..< 40: g.feedSample(feat, 0.0) # label is exactly zero check("gate stays closed on zero labels", not g.gateOpen) # ── 4. delayed-label queue: keyed by dueTick, needs the trace ─────────────── block: echo "test 4: pending queue resolves at dueTick" let g = initElmGun(priorScale = 0.0) let feat: array[FeatureDim, float64] = default(array[FeatureDim, float64]) let due = 50 g.noteEnemyAt(due, 100.0, 0.0) # target straight ahead of (0,0) g.recordShot(feat, 0.0, 0.0, 0.0, due) # base bearing 0, due at tick 50 g.drainPending(due - 1) check("nothing resolved before dueTick", g.nOnline == 0 and g.pending.len == 1) g.drainPending(due) check("resolved at dueTick", g.nOnline == 1 and g.pending.len == 0) # ── 5. reset returns to the neutral start ─────────────────────────────────── block: echo "test 5: resetOnline clears per-opponent state" let g = initElmGun(priorScale = 0.0) let feat = newSeq[float64](FeatureDim) for i in 0 ..< 60: g.feedSample(feat, 0.3) g.noteEnemyAt(1, 1.0, 1.0) g.recordShot(default(array[FeatureDim, float64]), 0.0, 0.0, 0.0, 5) g.resetOnline() check("nOnline cleared", g.nOnline == 0) check("gate closed after reset", not g.gateOpen) check("pending cleared", g.pending.len == 0) check("trace cleared", g.traceTick.len == 0) if failed > 0: echo "\n", failed, " check(s) FAILED" quit(1) echo "\nall online checks passed"