## Report rendering + exit-code policy. ## ## PRIMARY metric = survivalCount (these bots are survival movers), then ## firstPlaces, then score. SITTING-DUCK CAVEAT: survival vs SittingDuck is ## NO-SIGNAL (the harness reports 0/0 for both bots regardless of play), so it ## is flagged as such and never used for pass/fail. import std/[strutils, json, sequtils, net] # net: TimeoutError import ../../common_libs/test_framework/test_framework var survivalThreshold* = 0.6 ## CLI --survival-threshold const ExitOk* = 0 ## pass OR skip ExitFail* = 1 ## a matchup failed its threshold ExitHarness* = 2 ## OSError / IOError / TimeoutError — the harness itself broke type Matchup* = object opponent*: string myScore*, oppScore*: int rank*: int firstPlaces*, survivalCount*: int survivedRounds*, rounds*: int noSignal*: bool ## SittingDuck: survival column is meaningless incomplete*: bool ## fewer rounds recorded than requested missing*: bool ## bot name absent from r.results (zero-value trap) passed*: bool note*: string proc buildMatchup*(r: BattleResult, myName, oppName: string, roundsWanted: int): Matchup = result.opponent = oppName result.rounds = r.rounds.len result.incomplete = r.rounds.len < roundsWanted var mine, theirs: BotResult var foundMine = false for b in r.results: if b.name == myName: mine = b; foundMine = true elif b.name == oppName: theirs = b result.missing = not foundMine # zero-value BotResult trap guard result.rank = mine.rank result.firstPlaces = mine.firstPlaces result.survivalCount = mine.survivalCount result.myScore = mine.totalScore result.oppScore = theirs.totalScore for rd in r.rounds: for br in rd.results: if br.name == myName and br.survived: inc result.survivedRounds result.noSignal = oppName == "SittingDuck" if result.missing: result.passed = false result.note = "name '" & myName & "' absent from results — bot never booted?" elif result.noSignal: # Degenerate harness: both bots report ~0 regardless of play, so survival # AND score are meaningless. Never fail on it — flag as no-signal only. result.passed = true result.note = "NO-SIGNAL vs SittingDuck (score " & $result.myScore & " vs " & $result.oppScore & ") — informational, not a gate" else: let rate = if result.rounds == 0: 0.0 else: result.survivedRounds.float / result.rounds.float result.passed = rate >= survivalThreshold and not result.incomplete if result.incomplete: result.note = "only " & $result.rounds & "/" & $roundsWanted & " rounds recorded" else: result.note = "survivalRate=" & formatFloat(rate, ffDecimal, 2) proc pad(s: string, n: int): string = if s.len >= n: s[0 ..< n] else: s & repeat(' ', n - s.len) proc render*(m: seq[Matchup], myName: string): string = var s = "\n== " & myName & " — battle-test report ==\n" s.add pad("opponent", 20) & pad("score", 8) & pad("rank", 6) & pad("1st", 6) & pad("surv", 16) & "status\n" s.add repeat('-', 80) & "\n" for x in m: let survCol = if x.noSignal: "n/a(no-signal)" elif x.missing: "-" else: $x.survivedRounds & "/" & $x.rounds let status = if x.missing: "FAIL (missing from results)" elif x.noSignal: "NO-SIGNAL (not a gate)" elif x.passed: "PASS" else: "FAIL" s.add pad(x.opponent, 20) & pad($x.myScore, 8) & pad($x.rank, 6) & pad($x.firstPlaces, 6) & pad(survCol, 16) & status if x.note.len > 0: s.add " (" & x.note & ")" s.add "\n" let failed = m.filterIt(not it.passed).len s.add repeat('-', 80) & "\n" s.add (if failed == 0: "ALL MATCHUPS PASSED\n" else: $failed & " MATCHUP(S) FAILED\n") result = s proc toJson*(m: seq[Matchup], myName: string): string = var root = %*{"bot": myName, "survivalThreshold": survivalThreshold, "matchups": newJArray()} for x in m: root["matchups"].add %*{ "opponent": x.opponent, "score": x.myScore, "oppScore": x.oppScore, "rank": x.rank, "firstPlaces": x.firstPlaces, "survivalCount": x.survivalCount, "survivedRounds": x.survivedRounds, "rounds": x.rounds, "noSignal": x.noSignal, "incomplete": x.incomplete, "missing": x.missing, "passed": x.passed, "note": x.note} result = $root proc exitCode*(m: seq[Matchup]): int = if m.anyIt(not it.passed): ExitFail else: ExitOk ## Map framework exceptions to exit 2 with a message that says what actually ## broke (see test_framework README: OSError = compile, IOError = runner exit, ## TimeoutError = server startup / compile / runner timeout). proc harnessExit*(e: ref CatchableError): int = if e of OSError: stderr.writeLine "HARNESS ERROR [bot compile failed]: " & e.msg elif e of IOError: stderr.writeLine "HARNESS ERROR [battle runner exited non-zero / bad setup]: " & e.msg elif e of TimeoutError: stderr.writeLine "HARNESS ERROR [timeout: server startup, bot compile, or battle runner]: " & e.msg else: stderr.writeLine "HARNESS ERROR [unexpected]: " & e.msg ExitHarness