## Universal battle-test runner — CLI entry point. ## ## One command for every garage in the repo: ## nim c -r --path:../../common_libs tools/test_runner/runner.nim -- --bot X_garage ## ## Exit codes: 0 = pass or SKIP, 1 = test failure, 2 = harness error. import std/[os, strutils, parseopt, strformat] import ../../common_libs/test_framework/test_framework import ../../common_libs/test_framework/battle_result # parseServerOutput (offline mode) import ./resolve, ./stage, ./suites, ./report type Cfg = object bots: seq[string] oppSuite: string rounds: int timeout: int jsonOut: string offline: string dryRun: bool serverJar, runnerJar: string const Usage = """ Universal battle-test runner (tools/test_runner) Usage: runner --bot [--bot ...] [options] Options: --bot Bot garage (X_garage) or bot dir. Repeatable. --opponents baseline | all | GAUNTLET | (default: baseline) --rounds Rounds per matchup (default: 10) --timeout Battle runner timeout (default: 120000) --survival-threshold Fail a matchup below this survival rate (default: 0.6) --json Write the report as JSON --offline Parse a captured battle log, no Java, no server --server-jar Override TR_SERVER_JAR (bypasses the env gate) --runner-jar Override TR_BATTLE_RUNNER (bypasses the env gate) --dry-run Resolve and print the matrix, run nothing -h, --help This text Environment: TR_SERVER_JAR, TR_BATTLE_RUNNER (see common_libs/test_framework/README.md). If the JARs are absent the runner prints a SKIP banner and exits 0 — that is a skip, not a failure. Exit codes: 0 pass|skip, 1 test failure, 2 harness error. Caveat: survival vs SittingDuck is NO-SIGNAL (both bots score 0/survive 0 in this harness regardless of play). It is flagged as such and judged on score differential only. Use OscillatorBot/Observer for real survival signal. """ proc parseArgs(): Cfg = result = Cfg(oppSuite: Baseline, rounds: 10, timeout: 120000) var p = initOptParser(commandLineParams()) # ponytail: parseopt does not bind values to options — the value arrives as # the NEXT cmdArgument, so we latch the key and fill it on the way by. var pending = "" for kind, key, val in p.getopt(): case kind of cmdEnd: discard of cmdArgument: if pending.len > 0: case pending of "bot": result.bots.add key of "opponents": result.oppSuite = key of "rounds": result.rounds = parseInt(key) of "timeout": result.timeout = parseInt(key) of "survival-threshold": survivalThreshold = parseFloat(key) of "json": result.jsonOut = key of "offline": result.offline = key of "server-jar": result.serverJar = key of "runner-jar": result.runnerJar = key else: stderr.writeLine "option --" & pending & " needs a value\n" & Usage; quit(ExitHarness) pending = "" elif key.len > 0 and key[0] != '-': result.bots.add key of cmdLongOption, cmdShortOption: if key == "h" or key == "help": echo Usage quit(ExitOk) elif key == "dry-run": result.dryRun = true elif key in ["bot", "opponents", "rounds", "timeout", "survival-threshold", "json", "offline", "server-jar", "runner-jar"]: pending = key else: stderr.writeLine "unknown option: --" & key & "\n" & Usage quit(ExitHarness) proc runOffline(cfg: Cfg, bots: seq[ResolvedBot]): int = ## No Java, no server: parse the captured JSON-lines log. if not fileExists(cfg.offline): stderr.writeLine "offline log not found: " & cfg.offline return ExitHarness let r = parseServerOutput(readFile(cfg.offline)) var m: seq[Matchup] for b in bots: var opponent = "" for n in r.bots: if n != b.stem: opponent = n if opponent.len == 0: stderr.writeLine "offline log has no opponent for " & b.stem return ExitHarness m.add buildMatchup(r, b.stem, opponent, cfg.rounds) echo render(m, bots[0].stem) if cfg.jsonOut.len > 0: writeFile(cfg.jsonOut, toJson(m, bots[0].stem)) exitCode(m) proc main(): int = let cfg = parseArgs() if cfg.bots.len == 0: stderr.writeLine "--bot is required\n" & Usage return ExitHarness var bots: seq[ResolvedBot] for b in cfg.bots: bots.add resolveBot(b) if cfg.offline.len > 0: return runOffline(cfg, bots) let g = gate(cfg.serverJar, cfg.runnerJar) if not g.ok: echo "========================================================" echo " SKIP — integration battle tests not run" echo " reason: ", g.reason echo " set TR_SERVER_JAR / TR_BATTLE_RUNNER (or pass --server-jar" echo " / --runner-jar) to enable. See common_libs/test_framework/README.md" echo "========================================================" return ExitOk for b in bots: echo "bot: ", b.dir, " stem=", b.stem let all = suite(cfg.oppSuite) echo "opponent suite '", all.name, "':" var matrix: seq[tuple[bot: ResolvedBot, opp: string]] for b in bots: var opps = all.opponents if b.opponents.len > 0: opps = b.opponents # .battletest.json override for o in opps: echo " ", b.stem, " vs ", lastPathPart(o) matrix.add (b, o) if cfg.dryRun: echo "\n--dry-run: ", matrix.len, " matchup(s), nothing executed." return ExitOk let stageRoot = getTempDir() / "test_runner_stage" var m: seq[Matchup] try: for (b, oppPath) in matrix: let o = resolveBot(oppPath) let rounds = if b.rounds > 0: b.rounds else: cfg.rounds let stMine = stageOne(stageRoot, b.dir, b.stem) let stOpp = stageOne(stageRoot, o.dir, o.stem) echo &"\n--- {b.stem} vs {o.stem} ({rounds} rounds) ---" let r = runBattle(@[stMine.dir, stOpp.dir], rounds = rounds, timeout = cfg.timeout) m.add buildMatchup(r, b.stem, o.stem, rounds) except CatchableError as e: return harnessExit(e) let primary = bots[0].stem echo render(m, primary) if cfg.jsonOut.len > 0: writeFile(cfg.jsonOut, toJson(m, primary)) exitCode(m) when isMainModule: quit(main())