Skip to content

Commit

Permalink
add: Added test for card validity after distribution.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nami-Webisoft committed Nov 28, 2024
1 parent 95e914f commit 439ed79
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 19 deletions.
20 changes: 17 additions & 3 deletions src/tests/integration/test_actions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ use crate::systems::player::{IPlayerSystemDispatcher, IPlayerSystemDispatcherTra
use crate::models::components::{
ComponentGame, ComponentHand, ComponentDeposit, ComponentPlayer, ComponentDeck, ComponentDealer
};
use crate::models::traits::{ComponentPlayerDisplay, IDealer, IAsset, IClaimYield, IHand};
use crate::models::traits::{ComponentPlayerDisplay, ComponentHandDisplay, IDealer, IAsset, IClaimYield, IHand,
PriorityFeeDefault, EnumCardDisplay, EnumCardEq};
use crate::tests::utils::{deploy_world, namespace_def};
use crate::tests::integration::test_game::deploy_game;
use crate::tests::integration::test_player::deploy_player;
Expand Down Expand Up @@ -149,6 +150,9 @@ fn test_play() {
player_system.join("Player 2", addr);
player_system.set_ready(true, addr);

let hand: ComponentHand = world.read_model(first_caller);
assert!(hand.m_cards.len() == 5, "Player should have 5 cards before drawing");

// Set player one as the next caller
starknet::testing::set_contract_address(first_caller);

Expand All @@ -159,16 +163,26 @@ fn test_play() {
let hand: ComponentHand = world.read_model(first_caller);
let card: EnumCard = hand.m_cards.at(0).clone();

println!("Hand size: {}", hand.m_cards.len());
println!("Hand: {}", hand);
assert!(hand.m_cards.len() == 7, "Player should have 7 cards after drawing");

// Play the card
action_system.play(card, addr);
action_system.play(card.clone(), addr);
println!("Card Played: {}", card);

// Verify player state
let player: ComponentPlayer = world.read_model(first_caller);
assert!(player.m_moves_remaining < 3, "Move should be consumed");

// Verify card moved to appropriate location
let hand: ComponentHand = world.read_model(first_caller);
assert!(hand.m_cards.len() == 6, "Card should be removed from hand");

if card == EnumCard::PriorityFee(Default::default()) {
assert!(hand.m_cards.len() == 8, "Player should have 8 cards after playing");
} else {
assert!(hand.m_cards.len() == 6, "Player should have 6 cards after playing");
}
}

#[test]
Expand Down
25 changes: 24 additions & 1 deletion src/tests/integration/test_cards.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ use crate::models::components::{
};
use crate::models::traits::{
IAsset, IBlockchain, IClaimYield, ISandwichAttack, IGasFee, IPriorityFee, IFrontRun,
IFiftyOnePercentAttack, IChainReorg, IHand, IDeck, ComponentDeckDisplay
IFiftyOnePercentAttack, IChainReorg, IHand, IDeck, ComponentDeckDisplay, ComponentHandDisplay,
ComponentDealerDisplay
};
use crate::tests::utils::{deploy_world, namespace_def};
use crate::tests::integration::test_game::deploy_game;
Expand Down Expand Up @@ -432,3 +433,25 @@ fn test_fifty_one_percent_attack_card() {
assert!(player1_deck.m_sets == 1, "Player 1 should gain a set");
assert!(player2_deck_after.m_sets == 0, "Player 2 should lose a set");
}

#[test]
fn test_all_cards() {
let first_caller: ContractAddress = starknet::contract_address_const::<0x0a>();
let second_caller: ContractAddress = starknet::contract_address_const::<0x0b>();
let mut world: WorldStorage = deploy_world();
let (addr, _game_system): (ContractAddress, IGameSystemDispatcher) = deploy_game(ref world);
let player_system = deploy_player(ref world);

// Setup game with two players
starknet::testing::set_contract_address(first_caller);
player_system.join("Player 1", addr);
player_system.set_ready(true, addr);
starknet::testing::set_contract_address(second_caller);
player_system.join("Player 2", addr);
player_system.set_ready(true, addr);

let hand: ComponentHand = world.read_model(first_caller);
println!("Hand: {}", hand);
assert!(hand.m_cards.len() == 5, "Player should have 5 cards");

}
19 changes: 4 additions & 15 deletions src/tests/integration/test_game.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,6 @@ pub fn deploy_game(ref world: WorldStorage) -> (ContractAddress, IGameSystemDisp
.with_writer_of([dojo::utils::bytearray_hash(@"zktt")].span());

world.sync_perms_and_inits([system_def].span());
let cards_in_order = game_system::InternalImpl::_create_cards();
let mut flattened_cards = game_system::InternalImpl::_flatten(ref world, cards_in_order);
let mut dealer: ComponentDealer = IDealer::new(world.dispatcher.contract_address, array![]);

let mut index = 0;
while index < flattened_cards.len() {
dealer.m_cards.append(index);
index += 1;
};
world.write_model(@dealer);

return (contract_address, system);
}

Expand All @@ -73,9 +62,6 @@ fn test_start() {
let (addr, _game_system): (ContractAddress, IGameSystemDispatcher) = deploy_game(ref world);
let player_system: IPlayerSystemDispatcher = deploy_player(ref world);

let mut dealer: ComponentDealer = world.read_model(world.dispatcher.contract_address);
assert!(!dealer.m_cards.is_empty(), "Dealer should have cards!");

// Provide deterministic seed
starknet::testing::set_block_timestamp(240);
starknet::testing::set_nonce(0x111);
Expand All @@ -88,6 +74,9 @@ fn test_start() {
player_system.join("Player 2", addr);
player_system.set_ready(true, addr);

let mut dealer: ComponentDealer = world.read_model(addr);
assert!(!dealer.m_cards.is_empty(), "Dealer should have cards!");

let game: ComponentGame = world.read_model(addr);
assert!(game.m_state == EnumGameState::Started, "Game should have started!");

Expand Down Expand Up @@ -197,7 +186,7 @@ fn test_start_with_one_player() {
}

#[test]
#[should_panic(expected: ("Game has already started or invalid game ID", 'ENTRYPOINT_FAILED'))]
#[should_panic(expected: ("Game has already started", 'ENTRYPOINT_FAILED'))]
fn test_start_game_twice() {
let first_caller: ContractAddress = starknet::contract_address_const::<0x0a>();
let second_caller: ContractAddress = starknet::contract_address_const::<0x0b>();
Expand Down

0 comments on commit 439ed79

Please sign in to comment.