diff --git a/pkgs/id/src/commands/world.rs b/pkgs/id/src/commands/world.rs index f9b4ad2f1e..c12ae5359f 100644 --- a/pkgs/id/src/commands/world.rs +++ b/pkgs/id/src/commands/world.rs @@ -1828,24 +1828,22 @@ where Some(line) => { let line = line.trim(); if line.is_empty() { continue; } - let frame = if command_mode { - match pump_command(line, capability) { - PumpLine::Send(frame) => frame, - PumpLine::Say { level, message } => { - if json { - println!("{}", local_transcript_record(level, &message)); - } else { - eprintln!("{message}"); - } - continue; - } - PumpLine::Quit => break Ok(()), - } + let action = if command_mode { + pump_command(line, capability) } else { - match line.strip_prefix("/input ") { - Some(hex) => serde_json::json!({"type": "input", "data_hex": hex.trim()}), - None => serde_json::json!({"type": "chat", "text": line}), + pump_plain(line) + }; + let frame = match action { + PumpLine::Send(frame) => frame, + PumpLine::Say { level, message } => { + if json { + println!("{}", local_transcript_record(level, &message)); + } else { + eprintln!("{message}"); + } + continue; } + PumpLine::Quit => break Ok(()), }; if let Err(e) = write_frame_to(&mut send, frame.to_string().as_bytes()).await { break Err(e); @@ -1892,7 +1890,7 @@ fn check_contract(text: &str) -> Result { ) } -/// What one stdin line does in the `id world join --command-mode` pump. +/// What one stdin line does in the `id world join` pump. #[derive(Debug, PartialEq, Eq)] enum PumpLine { /// Send this session frame. @@ -1958,6 +1956,29 @@ fn pump_command(line: &str, capability: &str) -> PumpLine { }) } +/// Map the plain `id world join` input line, validating `/input` while +/// leaving every other line as chat. +fn pump_plain(line: &str) -> PumpLine { + use crate::world_line::{WorldLine, parse_line}; + + if line != "/input" && !line.starts_with("/input ") { + return PumpLine::Send(serde_json::json!({"type": "chat", "text": line})); + } + match parse_line(line) { + Some(WorldLine::Input(hex)) => { + PumpLine::Send(serde_json::json!({"type": "input", "data_hex": hex})) + } + Some(WorldLine::Message(message)) => PumpLine::Say { + level: "error", + message, + }, + _ => PumpLine::Say { + level: "info", + message: "usage: /input ".to_owned(), + }, + } +} + #[cfg(test)] #[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] mod tests { @@ -2186,6 +2207,36 @@ mod tests { )); } + #[test] + fn the_plain_pump_validates_input_and_preserves_chat() { + use super::pump_plain; + use serde_json::json; + + for hex in ["00af", "00AF"] { + assert_eq!( + pump_plain(&format!("/input {hex}")), + PumpLine::Send(json!({"type": "input", "data_hex": "00af"})) + ); + } + assert_eq!( + pump_plain("/input 00AF"), + PumpLine::Send(json!({"type": "input", "data_hex": "00af"})) + ); + for line in ["/input ZZ", "/input 0", "/input"] { + assert_eq!( + pump_plain(line), + PumpLine::Say { + level: "error", + message: "usage: /input ".to_owned() + } + ); + } + assert_eq!( + pump_plain("hello world"), + PumpLine::Send(json!({"type": "chat", "text": "hello world"})) + ); + } + #[test] fn the_contract_check_reports_conformance_or_every_violation() { let good = [