From 8c93f69081f0012e9a97ffc59adb7129bc1593ee Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 1 Nov 2026 18:51:05 +0100 Subject: v4.0.5: login and registration as core commands --- tests/client_core.rs | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) (limited to 'tests/client_core.rs') diff --git a/tests/client_core.rs b/tests/client_core.rs index d070a5b..53a024c 100644 --- a/tests/client_core.rs +++ b/tests/client_core.rs @@ -1487,3 +1487,79 @@ fn logging_out_and_showing_the_lobby_switch_the_screen() { }; assert_eq!(lobby.user, None); } + +#[test] +fn a_login_command_sends_auth_and_sets_the_user() { + let mut h = Harness::new(); + h.core.set_logged_in(None); + + let effects = h.core.command(Command::Login { + user: "alice".to_string(), + credential: "hash".to_string(), + }); + h.apply(effects); + assert_eq!(h.sent.last().map(String::as_str), Some("AUTH alice hash")); + + h.reply("OK alice"); + let Some(View::Lobby(lobby)) = h.core.view() else { + panic!("expected the lobby"); + }; + assert_eq!(lobby.user.as_deref(), Some("alice")); + assert!(lobby.login_error.is_none()); + assert!(h + .screen + .lines + .iter() + .any(|line| line == "Logged in as alice")); +} + +#[test] +fn a_rejected_login_is_shown_on_the_lobby() { + let mut h = Harness::new(); + h.core.set_logged_in(None); + h.core.command(Command::Login { + user: "alice".to_string(), + credential: "bad".to_string(), + }); + h.reply("ERROR Invalid credentials"); + + let Some(View::Lobby(lobby)) = h.core.view() else { + panic!("expected the lobby"); + }; + assert_eq!(lobby.user, None); + assert_eq!(lobby.login_error.as_deref(), Some("Invalid credentials")); +} + +#[test] +fn a_register_command_sends_register_and_reports_the_result() { + let mut h = Harness::new(); + h.core.set_logged_in(None); + + let effects = h.core.command(Command::Register { + user: "bob".to_string(), + credential: "hash".to_string(), + }); + h.apply(effects); + assert_eq!(h.sent.last().map(String::as_str), Some("REGISTER bob hash")); + + h.reply("REGISTERED bob"); + assert!(h + .screen + .lines + .iter() + .any(|line| line == "Account created. Log in to play.")); +} + +#[test] +fn login_requires_a_connection() { + let mut h = Harness::new(); + h.core.set_connected(false); + + let effects = h.core.command(Command::Login { + user: "alice".to_string(), + credential: "hash".to_string(), + }); + h.apply(effects); + + assert!(!h.sent.iter().any(|line| line.starts_with("AUTH"))); +} -- cgit v1.2.3