7d35a25906
Clippy and rustfmt clean, and the scoreboard test now expects vanilla's removeObjective, which keeps holders tracked without scores. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
138 lines
5.8 KiB
Rust
138 lines
5.8 KiB
Rust
//! Headless command runner for `tools/test_command_exec_parity.py`: with
|
|
//! `HORIZON_COMMAND_SCRIPT` and `HORIZON_COMMAND_OUTPUT` set, the opened world
|
|
//! runs without a window. Each script line is a JSON operation: `cmd` runs a
|
|
//! command as the player (Commands.performPrefixedCommand) and records every
|
|
//! system message it produced, as styled spans, with the results reported to
|
|
//! the player's `CommandResultCallback`; `tick` advances the game; `settle`
|
|
//! waits until the terrain around the player is loaded.
|
|
use super::Game;
|
|
use horizon_client::{Result, text::Text};
|
|
use serde_json::{Value, json};
|
|
use std::{
|
|
path::Path,
|
|
time::{Duration, Instant},
|
|
};
|
|
|
|
/// A message as `[text, style]` runs with adjacent equal styles merged; the
|
|
/// style lists colour, bold, italic, underlined, strikethrough, obfuscated
|
|
/// and the suggested command.
|
|
pub(super) fn spans(text: &Text) -> Value {
|
|
let mut out: Vec<(String, Value)> = Vec::new();
|
|
for span in &text.spans {
|
|
if span.text.is_empty() {
|
|
continue;
|
|
}
|
|
let s = &span.style;
|
|
let style = json!([
|
|
s.color,
|
|
s.bold == Some(true),
|
|
s.italic == Some(true),
|
|
s.underlined == Some(true),
|
|
s.strikethrough == Some(true),
|
|
s.obfuscated == Some(true),
|
|
]);
|
|
match out.last_mut() {
|
|
Some((t, last)) if *last == style => t.push_str(&span.text),
|
|
_ => out.push((span.text.clone(), style)),
|
|
}
|
|
}
|
|
Value::Array(out.into_iter().map(|(t, s)| json!([t, s])).collect())
|
|
}
|
|
|
|
impl Game<'_> {
|
|
/// Load terrain until the player stands in lit chunks.
|
|
fn harness_settle(&mut self, deadline: Instant) -> Result<()> {
|
|
loop {
|
|
self.update_world();
|
|
self.spawn_player()?;
|
|
if let Some(p) = &self.player {
|
|
let (cx, cz) = (
|
|
(p.position.x.floor() as i32) >> 4,
|
|
(p.position.z.floor() as i32) >> 4,
|
|
);
|
|
if (-1..=1).all(|dz| (-1..=1).all(|dx| self.streaming.is_lit(cx + dx, cz + dz))) {
|
|
return Ok(());
|
|
}
|
|
}
|
|
if std::env::var_os("HORIZON_HARNESS_DEBUG").is_some() {
|
|
eprintln!(
|
|
"settle: player {:?} center {:?} chunks {} pending {}",
|
|
self.player.as_ref().map(|p| p.position),
|
|
self.streaming.center,
|
|
self.world.chunks.len(),
|
|
self.streaming.pending()
|
|
);
|
|
}
|
|
if Instant::now() > deadline {
|
|
return Err("command harness: terrain did not load".into());
|
|
}
|
|
std::thread::sleep(Duration::from_millis(2));
|
|
}
|
|
}
|
|
pub(super) fn run_command_harness(&mut self, script: &Path, output: &Path) -> Result<()> {
|
|
let deadline = Instant::now()
|
|
+ Duration::from_secs(
|
|
std::env::var("HORIZON_HARNESS_TIMEOUT")
|
|
.ok()
|
|
.and_then(|v| v.parse().ok())
|
|
.unwrap_or(600),
|
|
);
|
|
self.harness_settle(deadline)?;
|
|
let mut out = String::new();
|
|
for line in std::fs::read_to_string(script)?.lines() {
|
|
if line.trim().is_empty() {
|
|
continue;
|
|
}
|
|
let op: Value = serde_json::from_str(line)?;
|
|
match op["op"].as_str().unwrap_or("") {
|
|
"cmd" => {
|
|
let command = op["cmd"].as_str().unwrap_or("");
|
|
self.chat_capture = Some(Vec::new());
|
|
self.cmds.results = Some(Vec::new());
|
|
self.run_command(command.strip_prefix('/').unwrap_or(command));
|
|
let messages = self.chat_capture.take().unwrap_or_default();
|
|
let results = self.cmds.results.take().unwrap_or_default();
|
|
if op["record"].as_bool() != Some(false) {
|
|
let record = json!({
|
|
"cmd": command,
|
|
"messages": messages.iter().map(spans).collect::<Vec<_>>(),
|
|
"results": results.iter().map(|(s, v)| json!([s, v])).collect::<Vec<_>>(),
|
|
});
|
|
out.push_str(&record.to_string());
|
|
out.push('\n');
|
|
}
|
|
}
|
|
"tick" => {
|
|
self.chat_capture = Some(Vec::new());
|
|
// The oracle's player has no client moving it: hold the
|
|
// player where commands put it while the game ticks.
|
|
let held = self.player.as_ref().map(|p| p.position);
|
|
for _ in 0..op["n"].as_u64().unwrap_or(1) {
|
|
self.update_world();
|
|
self.tick()?;
|
|
if let (Some(p), Some(at)) = (self.player.as_mut(), held) {
|
|
p.position = at;
|
|
p.velocity = horizon_core::geometry::Vec3::new(0.0, 0.0, 0.0);
|
|
p.fall_distance = 0.0;
|
|
}
|
|
}
|
|
let messages = self.chat_capture.take().unwrap_or_default();
|
|
if op["record"].as_bool() == Some(true) {
|
|
let record = json!({
|
|
"cmd": "#tick",
|
|
"messages": messages.iter().map(spans).collect::<Vec<_>>(),
|
|
"results": [],
|
|
});
|
|
out.push_str(&record.to_string());
|
|
out.push('\n');
|
|
}
|
|
}
|
|
"settle" => self.harness_settle(deadline)?,
|
|
other => return Err(format!("command harness: unknown op {other}").into()),
|
|
}
|
|
}
|
|
std::fs::write(output, out)?;
|
|
Ok(())
|
|
}
|
|
}
|