be4e9549ef
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
119 lines
4.4 KiB
Nim
119 lines
4.4 KiB
Nim
## Integration tests: QBot vs SittingDuck and vs OscillatorBot.
|
|
## Requires a running TR server (set TR_SERVER_JAR / TR_BATTLE_RUNNER env vars).
|
|
## Also includes an offline parseServerOutput unit test — no Java needed.
|
|
|
|
import std/[os, unittest]
|
|
import test_framework/battle_result
|
|
|
|
# ---------- offline unit test (no server needed) ----------
|
|
|
|
suite "parseServerOutput":
|
|
test "parses a 2-round battle fixture":
|
|
const fixture = """
|
|
{"event":"game_started","bots":["QBot","SittingDuck"]}
|
|
{"event":"round_ended","round":1,"results":[{"name":"QBot","score":300,"rank":1,"survived":true},{"name":"SittingDuck","score":0,"rank":2,"survived":false}]}
|
|
{"event":"round_ended","round":2,"results":[{"name":"QBot","score":250,"rank":1,"survived":true},{"name":"SittingDuck","score":0,"rank":2,"survived":false}]}
|
|
{"event":"battle_ended","results":[{"name":"QBot","totalScore":550,"rank":1,"firstPlaces":2,"survivalCount":2},{"name":"SittingDuck","totalScore":0,"rank":2,"firstPlaces":0,"survivalCount":0}]}
|
|
"""
|
|
let r = parseServerOutput(fixture)
|
|
|
|
check r.bots == @["QBot", "SittingDuck"]
|
|
check r.rounds.len == 2
|
|
|
|
# per-round data
|
|
check r.rounds[0].round == 1
|
|
check r.rounds[0].results[0].name == "QBot"
|
|
check r.rounds[0].results[0].score == 300
|
|
check r.rounds[0].results[0].survived == true
|
|
check r.rounds[1].results[1].survived == false
|
|
|
|
# final standings
|
|
check r.results.len == 2
|
|
|
|
var qbot: BotResult
|
|
for b in r.results:
|
|
if b.name == "QBot": qbot = b
|
|
check qbot.name == "QBot" # zero-value guard
|
|
check qbot.totalScore == 550
|
|
check qbot.firstPlaces == 2
|
|
check qbot.survivalCount == 2
|
|
check qbot.rank == 1
|
|
|
|
check r.winners == @["QBot"]
|
|
|
|
# ---------- integration tests (require TR server + BattleRunner JARs) ----------
|
|
|
|
const
|
|
DefaultServerJar = "/home/davide/Projects/tank-royale/server/build/libs/robocode-tankroyale-server-0.35.5-all.jar"
|
|
DefaultRunnerJar = "/home/davide/Projects/tank-royale/runner/examples/lib/robocode-tankroyale-runner.jar"
|
|
|
|
let serverJar = getEnv("TR_SERVER_JAR", DefaultServerJar)
|
|
let runnerJar = getEnv("TR_BATTLE_RUNNER", DefaultRunnerJar)
|
|
if not fileExists(serverJar) or not fileExists(runnerJar):
|
|
echo "Skipping integration tests: JAR files not found (set TR_SERVER_JAR / TR_BATTLE_RUNNER to override)"
|
|
quit(0)
|
|
|
|
import test_framework/test_framework
|
|
|
|
const
|
|
qbotDir = currentSourcePath().parentDir.parentDir
|
|
sittingDuckDir = currentSourcePath().parentDir.parentDir.parentDir /
|
|
"common_libs" / "test_framework" / "adversaries" / "SittingDuck"
|
|
oscillatorDir = currentSourcePath().parentDir.parentDir.parentDir /
|
|
"common_libs" / "test_framework" / "adversaries" / "OscillatorBot"
|
|
|
|
suite "QBot integration battles":
|
|
test "beats SittingDuck in 3 rounds":
|
|
let r = runBattle(@[qbotDir, sittingDuckDir], rounds = 3)
|
|
|
|
check r.rounds.len == 3
|
|
check r.results.len == 2
|
|
|
|
# zero-value guard: catches name mismatch / missing bot
|
|
var qbot, duck: BotResult
|
|
for b in r.results:
|
|
if b.name == "QBot": qbot = b
|
|
if b.name == "SittingDuck": duck = b
|
|
check qbot.name == "QBot"
|
|
check duck.name == "SittingDuck"
|
|
|
|
# overall standings
|
|
check qbot.totalScore > duck.totalScore
|
|
check qbot.rank == 1
|
|
check qbot.firstPlaces > 0
|
|
check qbot.survivalCount > 0
|
|
check "QBot" in r.winners
|
|
|
|
# per-round: QBot should outscore SittingDuck every round
|
|
for rnd in r.rounds:
|
|
var rqbot, rduck: BotRoundResult
|
|
for br in rnd.results:
|
|
if br.name == "QBot": rqbot = br
|
|
if br.name == "SittingDuck": rduck = br
|
|
check rqbot.score > rduck.score
|
|
check rqbot.survived == true
|
|
|
|
test "competes against OscillatorBot in 5 rounds":
|
|
let r = runBattle(@[qbotDir, oscillatorDir], rounds = 5)
|
|
|
|
check r.rounds.len == 5
|
|
check r.results.len == 2
|
|
|
|
var qbot, osc: BotResult
|
|
for b in r.results:
|
|
if b.name == "QBot": qbot = b
|
|
if b.name == "OscillatorBot": osc = b
|
|
check qbot.name == "QBot"
|
|
check osc.name == "OscillatorBot"
|
|
|
|
# QBot is a learning bot — just assert it scored at all
|
|
check qbot.totalScore > 0
|
|
# Both bots should appear in results with valid scores
|
|
check qbot.totalScore + osc.totalScore > 0
|
|
|
|
# per-round: each round has exactly 2 results
|
|
for rnd in r.rounds:
|
|
check rnd.results.len == 2
|
|
for br in rnd.results:
|
|
check br.score >= 0
|