From 439ed795cea99c0cc026d1e1dfd04639f174838f Mon Sep 17 00:00:00 2001 From: "Nami R." Date: Thu, 28 Nov 2024 16:48:34 -0500 Subject: [PATCH] add: Added test for card validity after distribution. --- src/tests/integration/test_actions.cairo | 20 ++++++++++++++++--- src/tests/integration/test_cards.cairo | 25 +++++++++++++++++++++++- src/tests/integration/test_game.cairo | 19 ++++-------------- 3 files changed, 45 insertions(+), 19 deletions(-) diff --git a/src/tests/integration/test_actions.cairo b/src/tests/integration/test_actions.cairo index 79e4779..1e0f864 100644 --- a/src/tests/integration/test_actions.cairo +++ b/src/tests/integration/test_actions.cairo @@ -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; @@ -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); @@ -159,8 +163,13 @@ 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); @@ -168,7 +177,12 @@ fn test_play() { // 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] diff --git a/src/tests/integration/test_cards.cairo b/src/tests/integration/test_cards.cairo index f91fef2..513d4ac 100644 --- a/src/tests/integration/test_cards.cairo +++ b/src/tests/integration/test_cards.cairo @@ -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; @@ -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"); + +} diff --git a/src/tests/integration/test_game.cairo b/src/tests/integration/test_game.cairo index 01b3f8a..fdae585 100644 --- a/src/tests/integration/test_game.cairo +++ b/src/tests/integration/test_game.cairo @@ -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); } @@ -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); @@ -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!"); @@ -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>();