From d251b54fe40f6f8f95261b4df65bced2fb728c63 Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 1 Nov 2026 19:00:04 +0100 Subject: v4.0.6: move the prompt editor and key mapping out of the core --- tests/client_core.rs | 49 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 17 deletions(-) (limited to 'tests/client_core.rs') diff --git a/tests/client_core.rs b/tests/client_core.rs index 53a024c..722cf0d 100644 --- a/tests/client_core.rs +++ b/tests/client_core.rs @@ -6,7 +6,8 @@ use std::time::Duration; use zagan_bet::blackjack::BlackjackState; -use zagan_bet::client::core::{ClientCore, Effect, InteractiveCommand, Key}; +use zagan_bet::client::core::{ClientCore, Effect, InteractiveCommand}; +use zagan_bet::client::input::{game_input, Editor, GameInput, Input, Key}; use zagan_bet::client::games::response::{ parse_blackjack_response, parse_crash_response, parse_spin_response, }; @@ -66,10 +67,6 @@ impl Screen { self.lines.extend(lines.iter().cloned()); self.region_len = lines.len(); } - Effect::SetPrompt { text, cursor_col } => { - self.prompt = text.clone(); - self.prompt_cursor = *cursor_col; - } Effect::SendLine(line) => sent.push(line.clone()), Effect::Interactive(command) => interactive.push(command.clone()), Effect::Quit | Effect::Disconnect | Effect::ListHosts | Effect::ForgetHost(_) => {} @@ -87,6 +84,7 @@ impl Screen { struct Harness { core: ClientCore, + editor: Editor, screen: Screen, sent: Vec, interactive: Vec, @@ -99,6 +97,7 @@ impl Harness { core.set_logged_in(Some("alice".to_string())); Self { core, + editor: Editor::new(), screen: Screen::default(), sent: Vec::new(), interactive: Vec::new(), @@ -111,11 +110,30 @@ impl Harness { } fn key(&mut self, key: Key) { - let effects = self.core.handle_key(key); + let effects = if self.core.is_in_game() { + match game_input(self.core.current_game_type().unwrap_or(""), key) { + GameInput::Quit => vec![Effect::Quit], + GameInput::Command(command) => self.core.command(command), + GameInput::None => Vec::new(), + } + } else { + match self.editor.handle(key) { + Input::Quit => vec![Effect::Quit], + Input::Submit(line) => self.core.submit_line(&line), + Input::Print(lines) => vec![Effect::Print(lines)], + Input::Prompt | Input::None => Vec::new(), + } + }; self.apply(effects); - if let Some(prompt) = self.core.prompt_effect() { - self.apply(vec![prompt]); + self.apply_prompt(); + } + + fn apply_prompt(&mut self) { + if self.core.is_in_game() { + return; } + self.screen.prompt = self.editor.input().to_string(); + self.screen.prompt_cursor = self.editor.cursor_column(); } fn line(&mut self, line: &str) { @@ -128,9 +146,7 @@ impl Harness { fn reply(&mut self, reply: &str) { let effects = self.core.handle_reply(reply); self.apply(effects); - if let Some(prompt) = self.core.prompt_effect() { - self.apply(vec![prompt]); - } + self.apply_prompt(); } fn board(&self) -> &[String] { @@ -469,16 +485,15 @@ fn play_requires_a_connection_and_a_login() { #[test] fn ctrl_c_quits_from_the_prompt() { - let mut core = ClientCore::new(); - assert_eq!(core.handle_key(Key::CtrlC), vec![Effect::Quit]); + let mut editor = Editor::new(); + assert_eq!(editor.handle(Key::CtrlC), Input::Quit); } #[test] fn ctrl_c_quits_from_a_game() { - let mut h = Harness::new(); - h.enter_slots("50"); - - assert_eq!(h.core.handle_key(Key::CtrlC), vec![Effect::Quit]); + assert_eq!(game_input("slots", Key::CtrlC), GameInput::Quit); + assert_eq!(game_input("roulette", Key::CtrlC), GameInput::Quit); + assert_eq!(game_input("blackjack", Key::CtrlC), GameInput::Quit); } #[test] -- cgit v1.2.3