Skip to content

Commit

Permalink
get address from username (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xaxxoo authored Dec 27, 2024
1 parent 0e8feea commit dd7ccec
Showing 1 changed file with 61 additions and 30 deletions.
91 changes: 61 additions & 30 deletions onchain/src/tests/test_game.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,31 @@ mod tests {
GameActions, IGameActionsDispatcher, IGameActionsDispatcherTrait
};
use starkludo::models::game::{Game, m_Game};
use starkludo::models::player::{Player, m_Player, AddressToUsername, UsernameToAddress, m_AddressToUsername, m_UsernameToAddress};
use starkludo::models::player::{
Player, m_Player, AddressToUsername, UsernameToAddress, m_AddressToUsername,
m_UsernameToAddress
};

use starkludo::models::game::{GameMode, GameStatus};
use starkludo::errors::Errors;

/// Defines the namespace configuration for the Starkludo game system
/// Returns a NamespaceDef struct containing namespace name and associated resources
fn namespace_def() -> NamespaceDef {

// Creates a new NamespaceDef struct with:
// Namespace name "starkludo"
// Array of TestResource enums for models, contracts and events
let ndef = NamespaceDef {
namespace: "starkludo", resources: [

// Register the Game model's class hash
TestResource::Model(m_Game::TEST_CLASS_HASH),

// Register the Player model's class hash
TestResource::Model(m_Player::TEST_CLASS_HASH),
TestResource::Model(m_AddressToUsername::TEST_CLASS_HASH),
TestResource::Model(m_UsernameToAddress::TEST_CLASS_HASH),

// Register the main contract containing game actions

TestResource::Contract(GameActions::TEST_CLASS_HASH),

// Register the GameCreated event's class hash
TestResource::Event(GameActions::e_GameCreated::TEST_CLASS_HASH),
TestResource::Event(GameActions::e_GameStarted::TEST_CLASS_HASH),
Expand All @@ -56,10 +54,9 @@ mod tests {
// Create a new contract definition for the StarKLudo game's actions
// using the ContractDefTrait builder pattern
ContractDefTrait::new(@"starkludo", @"GameActions")

// Configure write permissions by specifying which addresses can modify the contract
// Here, only the address derived from hashing "starkludo" has write access
.with_writer_of([dojo::utils::bytearray_hash(@"starkludo")].span())
// Configure write permissions by specifying which addresses can modify the contract
// Here, only the address derived from hashing "starkludo" has write access
.with_writer_of([dojo::utils::bytearray_hash(@"starkludo")].span())
].span() // Convert the array to a Span container for return
}

Expand Down Expand Up @@ -105,28 +102,25 @@ mod tests {

#[test]
fn test_get_username_from_address() {

let ndef = namespace_def();
let mut world = spawn_test_world([ndef].span());
world.sync_perms_and_inits(contract_defs());

let (contract_address, _) = world.dns(@"GameActions").unwrap();
let game_action_system = IGameActionsDispatcher { contract_address };

let test_address1 = starknet::contract_address_const::<'test_user1'>();
let test_address2 = starknet::contract_address_const::<'test_user2'>();
let username1: felt252 = 'alice';
let username2: felt252 = 'bob';

let address_to_username1 = AddressToUsername {
address: test_address1,
username: username1
let address_to_username1 = AddressToUsername {
address: test_address1, username: username1
};
let address_to_username2 = AddressToUsername {
address: test_address2,
username: username2
let address_to_username2 = AddressToUsername {
address: test_address2, username: username2
};

world.write_model(@address_to_username1);
world.write_model(@address_to_username2);

Expand All @@ -137,11 +131,12 @@ mod tests {
assert(retrieved_username2 == username2, 'Wrong username for address2');

let non_existent_address = starknet::contract_address_const::<'non_existent'>();
let retrieved_username3 = game_action_system.get_username_from_address(non_existent_address);
let retrieved_username3 = game_action_system
.get_username_from_address(non_existent_address);
assert(retrieved_username3 == 0, 'Non-existent should return 0');
}

fn test_start_game_success() {
fn test_start_game_success() {
// Setup world and contract
let caller = starknet::contract_address_const::<'Mr_T'>();
let ndef = namespace_def();
Expand All @@ -159,14 +154,15 @@ fn test_start_game_success() {
world.write_model(@username_address);

// Create new game
let game_id = game_action_system.create(
GameMode::MultiPlayer,
username, // green player (creator)
'player2',
'player3',
'player4',
4
);
let game_id = game_action_system
.create(
GameMode::MultiPlayer,
username, // green player (creator)
'player2',
'player3',
'player4',
4
);

// Start game
game_action_system.start();
Expand All @@ -177,4 +173,39 @@ fn test_start_game_success() {
assert(game.next_player == game.player_green, 'Green should be next player');
}

#[test]
fn test_get_address_from_username() {
let ndef = namespace_def();
let mut world = spawn_test_world([ndef].span());
world.sync_perms_and_inits(contract_defs());

let (contract_address, _) = world.dns(@"GameActions").unwrap();
let game_action_system = IGameActionsDispatcher { contract_address };

let bob_address = starknet::contract_address_const::<'bob'>();
let alice_address = starknet::contract_address_const::<'alice'>();
let bob_username: felt252 = 'bob';
let alice_username: felt252 = 'alice';

let address_to_username1 = UsernameToAddress {
username: bob_username, address: bob_address,
};
let address_to_username2 = UsernameToAddress {
username: alice_username, address: alice_address,
};

world.write_model(@address_to_username1);
world.write_model(@address_to_username2);

let address_1 = game_action_system.get_address_from_username(bob_username);
let address_2 = game_action_system.get_address_from_username(alice_username);

assert(address_1 == bob_address, 'Wrong address 1');
assert(address_2 == alice_address, 'Wrong address 2');

let non_existent_address = starknet::contract_address_const::<'non_existent'>();
let retrieved_username3 = game_action_system
.get_username_from_address(non_existent_address);
assert(retrieved_username3 == 0, 'Non-existent should return 0');
}
}

0 comments on commit dd7ccec

Please sign in to comment.