Skip to content

Add comprehensive tests for Player model functionality#195

Merged
Birdmannn merged 8 commits intoPrometheus-A:v1from
No-bodyq:v1
Aug 6, 2025
Merged

Add comprehensive tests for Player model functionality#195
Birdmannn merged 8 commits intoPrometheus-A:v1from
No-bodyq:v1

Conversation

@No-bodyq
Copy link
Copy Markdown
Contributor

Description

This PR introduces a comprehensive suite of unit tests for the playerImplementation

Related Issues

closes #98

Changes Made

  • Added helper function: mock_poker_game for simulating an active game state
  • Added helper function: mock_allowable_game for simulating a joinable game state
  • Implemented test: test_exit_with_out_true_succeeds
  • Implemented test: test_exit_with_out_false_succeeds
  • Implemented test: test_exit_without_lock_fails
  • Implemented test: test_exit_with_splitted_showdown_returns_locked_chips
  • Implemented test: test_exit_with_zero_player_count_fails
  • Implemented test: test_exit_with_invalid_player_fails
  • Implemented test: test_enter_succeeds_new_player

How to Test

Run the tests using dojo:

sozo test

Checklist

  • My code follows the project's coding style.
  • I have tested these changes locally.
  • Documentation has been updated where necessary.

@No-bodyq No-bodyq marked this pull request as ready for review July 28, 2025 17:17
@No-bodyq
Copy link
Copy Markdown
Contributor Author

Ready for review @Birdmannn

@Birdmannn Birdmannn self-requested a review July 29, 2025 13:30
Copy link
Copy Markdown
Collaborator

@Birdmannn Birdmannn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey champ @No-bodyq, please check this out 👀

Comment on lines +72 to +96
fn PLAYER_1() -> ContractAddress {
starknet::contract_address_const::<'PLAYER_1'>()
}

fn PLAYER_2() -> ContractAddress {
starknet::contract_address_const::<'PLAYER_2'>()
}

fn PLAYER_3() -> ContractAddress {
starknet::contract_address_const::<'PLAYER_3'>()
}

fn PLAYER_4() -> ContractAddress {
starknet::contract_address_const::<'PLAYER_4'>()
}

fn mock_poker_game(ref world: WorldStorage) {
let game = Game {
id: 1,
in_progress: true,
has_ended: false,
current_round: 1,
round_in_progress: true,
current_player_count: 2,
players: array![PLAYER_1(), PLAYER_2(), PLAYER_3()],
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is also in test_actions.cairo. make all functions public there, then import from there please

Comment on lines +97 to +128
deck: array![],
next_player: Option::Some(PLAYER_1()),
community_cards: array![],
pots: array![0],
current_bet: 0,
params: get_default_game_params(),
reshuffled: 0,
should_end: false,
deck_root: 0,
dealt_cards_root: 0,
nonce: 0,
community_dealing: false,
showdown: false,
round_count: 0,
highest_staker: Option::None,
previous_offset: 0,
};

let player_1 = Player {
id: PLAYER_1(),
alias: 'dub_zn',
chips: 2000,
current_bet: 0,
total_rounds: 1,
locked: (true, 1),
is_dealer: false,
in_round: true,
out: (0, 0),
pub_key: 0x1,
locked_chips: 0,
is_blacklisted: false,
eligible_pots: 1,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This too.. and it goes down

Comment on lines +129 to +156
};

let player_2 = Player {
id: PLAYER_2(),
alias: 'Birdmannn',
chips: 5000,
current_bet: 0,
total_rounds: 1,
locked: (true, 2),
is_dealer: false,
in_round: true,
out: (0, 0),
pub_key: 0x2,
locked_chips: 0,
is_blacklisted: false,
eligible_pots: 1,
};

let player_3 = Player {
id: PLAYER_3(),
alias: 'chiscookeke11',
chips: 5000,
current_bet: 0,
total_rounds: 1,
locked: (false, 1),
is_dealer: false,
in_round: true,
out: (0, 0),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this too.. it still goes down

Comment on lines +157 to +165
pub_key: 0x3,
locked_chips: 0,
is_blacklisted: false,
eligible_pots: 1,
};

world.write_model(@game);
world.write_models(array![@player_1, @player_2, @player_3].span());
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think for the first part of your refactor, it'll end here 💯

world.write_models(array![@player_1, @player_2, @player_3].span());
}

fn mock_allowable_game(ref world: WorldStorage) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from here Line167 to Line322, all new mocks would be to refactor the original mock function to take in parameters that target specific fields, and reuse this function.

SO basically all functions mocking the player state must be merged into one player function that takes in the specific parameters you wish to mock.
For all functions mocking the game state, then you must refactor the original function in test_actions.cairo to take in the specific mock parameters and adjust the tests there to pass.

Please, I hope you understand.

};

world.write_model(@game);
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

@No-bodyq
Copy link
Copy Markdown
Contributor Author

all done ser @Birdmannn

@No-bodyq No-bodyq requested a review from Birdmannn July 30, 2025 10:40
@Birdmannn
Copy link
Copy Markdown
Collaborator

all done ser @Birdmannn

Just to ask.... you normally use copilot to assist you, right? @No-bodyq

@No-bodyq
Copy link
Copy Markdown
Contributor Author

No-bodyq commented Aug 1, 2025

all done ser @Birdmannn

Just to ask.... you normally use copilot to assist you, right? @No-bodyq

Yes ser, why? @Birdmannn

@Birdmannn
Copy link
Copy Markdown
Collaborator

Birdmannn commented Aug 1, 2025

Yes ser, why? @Birdmannn

@No-bodyq Your code is bloated. Too much lines of code for little matters. And it causes an overhead during review when I want to optimize those lines but you and copilot are not helping matters.

I'll review this code one more time, and I'll show you something...
Maybe if you had worked with Oleh, MSGhais and GianM, you might've understood.

@No-bodyq
Copy link
Copy Markdown
Contributor Author

No-bodyq commented Aug 1, 2025

Okay ser, let me know what I should change and I'll do it manually
I apologise

Copy link
Copy Markdown
Collaborator

@Birdmannn Birdmannn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good @No-bodyq. One last set of adjustments, if you do it well, we merge in 1. Arigato. 🙏🏻

Comment on lines +90 to +96
false,
true,
(0, 0),
0x3,
0,
false,
1,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, something like this is repeated. including Line87 and Line88. Don't bother about the public key, you never tested it. This is what I wanted to show you. In the mock_player function you created, what should enter the params are the variables that are actually tested and that they vary for each player

So your function would resemble something like this

fn mock_player(id: ContractAddress, alias: felt252, chips: u256, out: (bool, u16)) -> Player {
    let mut player: Player = Default::default();
    player.id = id;
    player.chips = chips;
    player.alias = alias;
    player.out = out;
    player
}

Implement this type of stuff too when mocking the game.
You see how neat and easy to read that is? That's how it should probably look... I didn't say that is the exact function... I'm not sure if I omitted some fields or parameters

Comment on lines +98 to +125
mock_poker_game_flex(
ref world,
2,
true,
false,
1,
false,
2,
array![PLAYER_1(), PLAYER_2(), PLAYER_3()],
array![],
Option::Some(PLAYER_1()),
array![],
array![0],
0,
get_default_game_params(),
0,
false,
0,
0,
0,
false,
false,
0,
Option::None,
0,
array![player_1, player_2, player_3],
);
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do the same with this function too. It takes in too many default fields... and yes, the pots might not be passed in as a parameter, you can just set the game.pots = array![0] since that is always the case for the pots, and the game.pots must be set like this inside the function because Default::default() makes it an empty array which is not a valid pot.



#[test]
fn test_exit_with_out_true_succeeds() {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

#[test]
fn test_exit_with_out_false_succeeds() {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let (mut world, _) = deploy_contracts(array![CoreContract::Actions]);
mock_poker_game_flex(
ref world,
3,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by the way, if the id is not useful in mocking, you can default it to 1 in the mock function, and remove it as a parameter in the function's signature

@No-bodyq No-bodyq requested a review from Birdmannn August 3, 2025 19:12
@No-bodyq
Copy link
Copy Markdown
Contributor Author

No-bodyq commented Aug 3, 2025

I think we finally have it ser @Birdmannn

@Birdmannn
Copy link
Copy Markdown
Collaborator

Trés bien, Mister Asher @No-bodyq. Thanks for keeping up with this kind of review, and yes, thank you for your contribution.🫡. Ohayo 🧘🏻‍♂️

@Birdmannn Birdmannn merged commit 4d5af0c into Prometheus-A:v1 Aug 6, 2025
1 check passed
@No-bodyq
Copy link
Copy Markdown
Contributor Author

No-bodyq commented Aug 6, 2025

Thank you ser @Birdmannn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Test][Lab Chef Needed] Test PlayerTrait

2 participants