From de3a285cb3b0dce06e8f5a6548e3a04759103b91 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Tue, 13 Oct 2026 01:07:19 +0200 Subject: [PATCH] Headless Horizon client joining a vanilla dedicated server, checked by the harness Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 4 + crates/horizon-client/src/game/net_harness.rs | 90 +++++++++++++++++++ tools/test_dedicated_server_parity.py | 55 +++++++++++- 3 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 crates/horizon-client/src/game/net_harness.rs diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index fe1857dd7..ea7f6d153 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -22,6 +22,7 @@ mod copper_golems; mod creaking; mod creatures; mod dedicated; +mod net_harness; mod crossbow; mod dispense_fixture; mod dispenser; @@ -8452,6 +8453,9 @@ pub fn run() -> Result<()> { game.options.player_name.clone(), ); game.connect_server(&address, &name)?; + if let Some(report) = std::env::var_os("HORIZON_NET_REPORT") { + return game.run_net_harness(report.as_ref()); + } } Some(folder) => { let (seed, creative) = (game.options.seed, game.options.creative); diff --git a/crates/horizon-client/src/game/net_harness.rs b/crates/horizon-client/src/game/net_harness.rs new file mode 100644 index 000000000..f8346fc98 --- /dev/null +++ b/crates/horizon-client/src/game/net_harness.rs @@ -0,0 +1,90 @@ +//! Headless multiplayer client for `tools/test_dedicated_server_parity.py`: +//! with `--server` and `HORIZON_NET_REPORT` set, the client joins without a +//! window, waits until it stands in the world with the chunks around it, +//! sends a chat message and `/list`, collects the chat it is shown, leaves +//! and writes a JSON report (position, chunks, chat lines, whether the +//! server kept it connected throughout). +use super::Game; +use horizon_client::Result; +use serde_json::json; +use std::{ + path::Path, + time::{Duration, Instant}, +}; + +impl Game<'_> { + pub(super) fn run_net_harness(&mut self, report: &Path) -> Result<()> { + let deadline = Instant::now() + Duration::from_secs(120); + let mut next = Instant::now(); + let mut spawned_at: Option = None; + let mut last_chunks = 0; + let mut quiet_since = Instant::now(); + let mut step = 0; + let mut step_at = Instant::now(); + let mut position = None; + let mut chunks = 0; + let mut error = None; + while Instant::now() < deadline { + self.update_world(); + if self.remote.is_none() { + error = Some("disconnected".to_owned()); + break; + } + if Instant::now() >= next { + next += Duration::from_millis(50); + self.tick()?; + } + let now = Instant::now(); + if let Some(p) = &self.player { + spawned_at.get_or_insert(now); + position.get_or_insert([p.position.x, p.position.y, p.position.z]); + } + let count = self.world.chunks.len(); + if count != last_chunks { + last_chunks = count; + quiet_since = now; + } + match step { + 0 if spawned_at.is_some() && count > 0 && now - quiet_since > Duration::from_secs(3) => { + chunks = count; + self.send_chat("hello from the horizon client"); + step = 1; + step_at = now; + } + 1 if now - step_at > Duration::from_millis(1500) => { + self.send_chat("/list"); + step = 2; + step_at = now; + } + 2 if now - step_at > Duration::from_millis(1500) => { + step = 3; + break; + } + _ => {} + } + std::thread::sleep(Duration::from_millis(5)); + } + if step < 3 && error.is_none() { + error = Some(format!("timed out at step {step}")); + } + let chat: Vec = self + .remote + .as_ref() + .map(|r| r.chat.iter().map(|(t, _)| t.clone()).collect()) + .unwrap_or_default(); + if self.remote.is_some() { + self.leave_server(None)?; + } + std::fs::write( + report, + json!({ + "position": position, + "chunks": chunks, + "chat": chat, + "error": error, + }) + .to_string(), + )?; + Ok(()) + } +} diff --git a/tools/test_dedicated_server_parity.py b/tools/test_dedicated_server_parity.py index b3a64b007..044d75bee 100644 --- a/tools/test_dedicated_server_parity.py +++ b/tools/test_dedicated_server_parity.py @@ -247,6 +247,56 @@ def run_side(label, command, directory, client): PROPERTIES_PORTS = {} +def horizon_client_joins(vanilla_server, binary, world, build, reference): + """Horizon's client joins a vanilla dedicated server headless: it must + land where the vanilla client did, receive as many chunks, see its chat + and /list reply, and leave as a vanilla client does (the server's log).""" + label = 'vanilla-for-horizon-client' + PROPERTIES_PORTS[label] = port = free_port() + directory = build / label + write_server_dir(directory, port, world) + client_dir = build / 'horizon-client' + shutil.rmtree(client_dir, ignore_errors=True) + client_dir.mkdir() + report = build / 'horizon-client-report.json' + report.unlink(missing_ok=True) + server = Server(vanilla_server, directory, label) + try: + server.wait_for(r'Done \(') + env = dict(os.environ, HORIZON_NO_AUDIO='1', HORIZON_NET_REPORT=str(report)) + with (build / 'horizon-client-run.log').open('w') as log: + subprocess.run([str(c) for c in [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--render-distance', 2, + '--server', f'127.0.0.1:{port}', '--name', PLAYER]], + cwd=client_dir, env=env, stdout=log, stderr=subprocess.STDOUT, timeout=300, check=True) + server.wait_for(r'left the game', timeout=30) + finally: + server.stop() + got = json.loads(report.read_text()) + move = next(l for l in reference if l.startswith('play-move')) + want_position = [float(v) for v in re.match(r'play-move \(([^,]+), ([^,]+), ([^)]+)\)', move).groups()] + want_chunks = int(next(l for l in reference if l.startswith('play-counts')).split()[-1]) + expected = { + 'connected throughout': None, + 'spawn position': want_position, + 'chunks received': want_chunks, + 'own chat shown': True, + '/list reply shown': True, + 'server log': ['logged in', 'Tester joined the game', '[Not Secure] hello from the horizon client', + 'Tester lost connection: Disconnected', 'Tester left the game'], + } + log = '\n'.join(server.lines) + actual = { + 'connected throughout': got['error'], + 'spawn position': got['position'], + 'chunks received': got['chunks'], + 'own chat shown': any(' hello from the horizon client' in c for c in got['chat']), + '/list reply shown': any('There are 1 of a max of 5 players online: Tester' in c for c in got['chat']), + 'server log': [k for k in expected['server log'] if k in log], + } + return [f'horizon-client {k}: {"ok" if actual[k] == v else f"expected {v!r}, got {actual[k]!r}"}' + for k, v in expected.items()] + + def main(): lock, jars, libraries = base.verify_artifacts() build = lab.LOCAL / NAME @@ -303,12 +353,15 @@ def main(): write_server_dir(directory, PROPERTIES_PORTS[label], world) results[label] = run_side(label, command, directory, client) (build / f'{label}.txt').write_text('\n'.join(results[label]) + '\n', encoding='utf-8') + client_checks = horizon_client_joins(vanilla_server, binary, world, build, results['vanilla']) vanilla, horizon = results['vanilla'], results['horizon'] gaps = sorted({gap(l) for l in vanilla if gap(l)}) vanilla = [l for l in vanilla if not gap(l)] for g in gaps: print(f'known gap: {KNOWN_GAPS[g]}') - errors = [l for l in vanilla + horizon if l.startswith('error')] + errors = [l for l in vanilla + horizon if l.startswith('error')] + [l for l in client_checks if not l.endswith(': ok')] + for line in client_checks: + print(line) differences = [] import difflib for line in difflib.unified_diff(vanilla, horizon, 'vanilla', 'horizon', n=0, lineterm=''):