From e9f4d78952480de09e81505595aefe0ac4630233 Mon Sep 17 00:00:00 2001 From: jake Date: Fri, 5 Jun 2020 09:19:36 -0600 Subject: [PATCH] move common test functions to another file --- tests/common.rs | 83 +++++++++++++++++++++++++++++++++++++++ tests/test_item_pickup.rs | 71 ++------------------------------- 2 files changed, 86 insertions(+), 68 deletions(-) create mode 100644 tests/common.rs diff --git a/tests/common.rs b/tests/common.rs new file mode 100644 index 0000000..e3d187f --- /dev/null +++ b/tests/common.rs @@ -0,0 +1,83 @@ +use std::time::SystemTime; + +use elseware::common::serverstate::{ClientId, ServerState}; +use elseware::entity::gateway::{EntityGateway, InMemoryGateway}; +use elseware::entity::account::{UserAccountEntity, NewUserAccountEntity, NewUserSettingsEntity}; +use elseware::entity::character::{CharacterEntity, NewCharacterEntity}; +//use elseware::entity::item::{NewItemEntity, ItemDetail, ItemLocation}; +use elseware::entity::item; +use elseware::ship::ship::{ShipServerState, RecvShipPacket}; +use elseware::ship::items::{ClientItemId, ActiveItemEntityId, HeldItemType, FloorItemType}; + +use libpso::packet::ship::*; +use libpso::packet::messages::*; +use libpso::packet::login::{Login, Session}; +use libpso::{utf8_to_array, utf8_to_utf16_array}; + + +pub async fn new_user_character(entity_gateway: &mut EG, username: &str, password: &str) -> (UserAccountEntity, CharacterEntity) { + let new_user = NewUserAccountEntity { + username: username.into(), + password: bcrypt::hash(password, 5).unwrap(), + guildcard: 1, + team_id: None, + banned: false, + muted_until: SystemTime::now(), + created_at: SystemTime::now(), + flags: 0, + }; + + let user = entity_gateway.create_user(new_user).await.unwrap(); + let new_settings = NewUserSettingsEntity::new(user.id); + let _settings = entity_gateway.create_user_settings(new_settings).await.unwrap(); + let new_character = NewCharacterEntity::new(user.id); + let character = entity_gateway.create_character(new_character).await.unwrap(); + + (user, character) +} + +pub async fn log_in_char(ship: &mut ShipServerState, id: ClientId, username: &str, password: &str) { + let username = username.to_string(); + let password = password.to_string(); + ship.handle(id, &RecvShipPacket::Login(Login { + tag: 0, + guildcard: 0, + version: 0, + unknown1: [0; 6], + team: 0, + username: utf8_to_array!(username, 16), + unknown2: [0; 32], + password: utf8_to_array!(password, 16), + unknown3: [0; 40], + hwinfo: [0; 8], + session: Session::new(), + })).await.unwrap().for_each(drop); +} + +pub async fn join_lobby(ship: &mut ShipServerState, id: ClientId) { + ship.handle(id, &RecvShipPacket::CharData(CharData { + _unknown: [0; 0x828] + })).await.unwrap().for_each(drop); +} + +pub async fn create_room(ship: &mut ShipServerState, id: ClientId, name: &str, password: &str) { + ship.handle(id, &RecvShipPacket::CreateRoom(CreateRoom { + unknown: [0; 2], + name: utf8_to_utf16_array!(name, 16), + password: utf8_to_utf16_array!(password, 16), + difficulty: 0, + battle: 0, + challenge: 0, + episode: 1, + single_player: 0, + padding: [0; 3], + })).await.unwrap().for_each(drop); + ship.handle(id, &RecvShipPacket::DoneBursting(DoneBursting {})).await.unwrap().for_each(drop); +} + +pub async fn join_room(ship: &mut ShipServerState, id: ClientId, room_id: u32) { + ship.handle(id, &RecvShipPacket::MenuSelect(MenuSelect { + menu: ROOM_MENU_ID, + item: room_id, + })).await.unwrap().for_each(drop); +} diff --git a/tests/test_item_pickup.rs b/tests/test_item_pickup.rs index c3e4c77..233af69 100644 --- a/tests/test_item_pickup.rs +++ b/tests/test_item_pickup.rs @@ -14,74 +14,9 @@ use libpso::packet::messages::*; use libpso::packet::login::{Login, Session}; use libpso::{utf8_to_array, utf8_to_utf16_array}; - -pub async fn new_user_character(entity_gateway: &mut EG, username: &str, password: &str) -> (UserAccountEntity, CharacterEntity) { - let new_user = NewUserAccountEntity { - username: username.into(), - password: bcrypt::hash(password, 5).unwrap(), - guildcard: 1, - team_id: None, - banned: false, - muted_until: SystemTime::now(), - created_at: SystemTime::now(), - flags: 0, - }; - - let user = entity_gateway.create_user(new_user).await.unwrap(); - let new_settings = NewUserSettingsEntity::new(user.id); - let _settings = entity_gateway.create_user_settings(new_settings).await.unwrap(); - let new_character = NewCharacterEntity::new(user.id); - let character = entity_gateway.create_character(new_character).await.unwrap(); - - (user, character) -} - -pub async fn log_in_char(ship: &mut ShipServerState, id: ClientId, username: &str, password: &str) { - let username = username.to_string(); - let password = password.to_string(); - ship.handle(id, &RecvShipPacket::Login(Login { - tag: 0, - guildcard: 0, - version: 0, - unknown1: [0; 6], - team: 0, - username: utf8_to_array!(username, 16), - unknown2: [0; 32], - password: utf8_to_array!(password, 16), - unknown3: [0; 40], - hwinfo: [0; 8], - session: Session::new(), - })).await.unwrap().for_each(drop); -} - -pub async fn join_lobby(ship: &mut ShipServerState, id: ClientId) { - ship.handle(id, &RecvShipPacket::CharData(CharData { - _unknown: [0; 0x828] - })).await.unwrap().for_each(drop); -} - -pub async fn create_room(ship: &mut ShipServerState, id: ClientId, name: &str, password: &str) { - ship.handle(id, &RecvShipPacket::CreateRoom(CreateRoom { - unknown: [0; 2], - name: utf8_to_utf16_array!(name, 16), - password: utf8_to_utf16_array!(password, 16), - difficulty: 0, - battle: 0, - challenge: 0, - episode: 1, - single_player: 0, - padding: [0; 3], - })).await.unwrap().for_each(drop); - ship.handle(id, &RecvShipPacket::DoneBursting(DoneBursting {})).await.unwrap().for_each(drop); -} - -pub async fn join_room(ship: &mut ShipServerState, id: ClientId, room_id: u32) { - ship.handle(id, &RecvShipPacket::MenuSelect(MenuSelect { - menu: ROOM_MENU_ID, - item: room_id, - })).await.unwrap().for_each(drop); -} - +#[path = "common.rs"] +mod common; +use common::*; #[async_std::test] async fn test_pick_up_item_stack_of_items_already_in_inventory() {