From 20cc69904af00ec12039b5f4c7349ebc97ffa59d Mon Sep 17 00:00:00 2001 From: Darlington02 Date: Sun, 20 Oct 2024 11:23:44 +0100 Subject: [PATCH] chore: little fixes for channel --- src/channel/channel.cairo | 9 +- tests/test_channel.cairo | 178 ++++++++++++++++---------------------- 2 files changed, 78 insertions(+), 109 deletions(-) diff --git a/src/channel/channel.cairo b/src/channel/channel.cairo index f65f627..a0be793 100644 --- a/src/channel/channel.cairo +++ b/src/channel/channel.cairo @@ -144,7 +144,7 @@ pub mod ChannelComponent { channel_id, channel_nft_classhash, channel_id.try_into().unwrap() ); // use channel_id as salt since its unique - // check that caller is a member of the community + // check that owner is a member of the community let (membership_status, _) = community_instance .is_community_member(channel_owner, community_id); assert(membership_status, NOT_COMMUNITY_MEMBER); @@ -161,9 +161,10 @@ pub mod ChannelComponent { // update storage self.channels.write(channel_id, new_channel.clone()); + self.channel_counter.write(channel_id); + // include channel owner as first member self._join_channel(channel_owner, channel_id); - self.channel_counter.write(channel_id); // emit event self @@ -308,7 +309,6 @@ pub mod ChannelComponent { self.channels.write(channel_id, channel); } - /// @notice set the ban status of a profile in the channel /// @param channel_id The id of the channel /// @param profile The address of the profile @@ -331,7 +331,6 @@ pub mod ChannelComponent { self._set_ban_status(channel_id, profiles, ban_statuses); } - /// @notice gets the channel parameters /// @param channel_id The id of the channel /// @return ChannelDetails The channel parameters @@ -479,7 +478,7 @@ pub mod ChannelComponent { // check moderator is a channel member let (is_channel_member, _) = self.is_channel_member(moderator, channel_id); - assert(is_channel_member == true, NOT_COMMUNITY_MEMBER); + assert(is_channel_member == true, NOT_CHANNEL_MEMBER); self.channel_moderators.write((channel_id, moderator), true); diff --git a/tests/test_channel.cairo b/tests/test_channel.cairo index 2becf5c..37b58b0 100644 --- a/tests/test_channel.cairo +++ b/tests/test_channel.cairo @@ -8,22 +8,17 @@ use core::traits::{TryInto, Into}; use snforge_std::{ declare, start_cheat_caller_address, stop_cheat_caller_address, spy_events, - EventSpyAssertionsTrait, ContractClassTrait, DeclareResultTrait, EventSpy + EventSpyAssertionsTrait, ContractClassTrait, DeclareResultTrait }; use core::starknet::{ - ContractAddress, contract_address_const, get_caller_address, get_block_timestamp, ClassHash, - syscalls::deploy_syscall + ContractAddress, contract_address_const, get_block_timestamp }; use karst::channel::channel::ChannelComponent; -use karst::community::community::CommunityComponent; -use karst::base::constants::types::{ChannelDetails, ChannelMember}; +use karst::base::constants::types::{ChannelDetails}; use karst::mocks::interfaces::IChannelComposable::{ IChannelComposableDispatcher, IChannelComposableDispatcherTrait }; -use karst::base::constants::types::CommunityType; -use karst::presets::channel; - const HUB_ADDRESS: felt252 = 'HUB'; const ADMIN: felt252 = 'ADMIN'; const USER_ONE: felt252 = 'BOB'; @@ -31,14 +26,6 @@ const USER_TWO: felt252 = 'ALICE'; const USER_THREE: felt252 = 'ROB'; const USER_FOUR: felt252 = 'DAN'; const USER_FIVE: felt252 = 'RANDY'; -const USER_SIX: felt252 = 'JOE'; -const MODERATOR1: felt252 = 'MOD1'; -const MODERATOR2: felt252 = 'MOD2'; -const NOTOWNER: felt252 = 'NOTOWNER'; -const NOTMODERATOR: felt252 = 'NOTMODERATOR'; -const MEMBER1: felt252 = 'MEMBER1'; -const MEMBER2: felt252 = 'MEMBER2'; - fn __setup__() -> ContractAddress { let community_nft_class_hash = declare("CommunityNFT").unwrap().contract_class().class_hash; @@ -108,7 +95,6 @@ fn test_create_channel_emits_events() { ); } -// test channel reate should be the member of the community #[test] fn test_create_channel_by_community_member() { let channel_contract_address = __setup__(); @@ -134,10 +120,10 @@ fn test_create_channel_by_community_member() { assert(channel_details.channel_total_members == 1, 'invalid total members'); assert(channel_details.channel_censorship == false, 'invalid censorship status'); } -// test channel reate should be the member of the community + #[test] #[should_panic(expected: ('Karst: Not a Community Member',))] -fn test_should_panic_if_create_channel_by_not_community_member() { +fn test_should_panic_if_channel_creator_is_not_community_member() { let channel_contract_address = __setup__(); let dispatcher = IChannelComposableDispatcher { contract_address: channel_contract_address }; start_cheat_caller_address(channel_contract_address, USER_ONE.try_into().unwrap()); @@ -162,7 +148,6 @@ fn test_should_panic_if_create_channel_by_not_community_member() { assert(channel_details.channel_censorship == false, 'invalid censorship status'); } - #[test] fn test_profile_can_join_channel() { let channel_contract_address = __setup__(); @@ -191,10 +176,8 @@ fn test_profile_can_join_channel() { assert(channel_member.channel_token_id != 0, 'Invalid nft mint token '); } -// let's add an additional test to check that when a profile joins a channel, the channel NFT is -// minted to him #[test] -fn test_channel_nft_minted_to_profile() { +fn test_channel_nft_minted_to_profile_on_joining() { let channel_contract_address = __setup__(); let dispatcher = IChannelComposableDispatcher { contract_address: channel_contract_address }; @@ -220,14 +203,13 @@ fn test_channel_nft_minted_to_profile() { // check that nft is minted to the profile start_cheat_caller_address(channel_contract_address, USER_TWO.try_into().unwrap()); - let (is_member, channel_member) = dispatcher + let (_, channel_member) = dispatcher .is_channel_member(USER_TWO.try_into().unwrap(), channel_id); assert(channel_member.channel_token_id != 0, 'Invalid nft mint token '); } -// add an extra test to check that when the profile leaves the channel, his channel nft is burnt #[test] -fn test_channel_nft_burn_on_leaving_channel() { +fn test_channel_nft_is_burnt_on_leaving_channel() { let channel_contract_address = __setup__(); let dispatcher = IChannelComposableDispatcher { contract_address: channel_contract_address }; @@ -256,12 +238,11 @@ fn test_channel_nft_burn_on_leaving_channel() { dispatcher.leave_channel(channel_id); stop_cheat_caller_address(channel_contract_address); - let (is_member, channel_member) = dispatcher + let (_, channel_member) = dispatcher .is_channel_member(USER_TWO.try_into().unwrap(), channel_id); assert(channel_member.channel_token_id == 0, 'nft is burned '); } - #[test] #[should_panic(expected: ('Karst: already a Member',))] fn test_should_panic_if_a_user_joins_one_channel_twice() { @@ -314,7 +295,7 @@ fn test_should_panic_if_banned_members_join_a_channel() { #[test] #[should_panic(expected: ('Karst: banned from channel',))] -fn test_should_panic_if_banned_user_try_to_leave_channel_and_then_rejoin() { +fn test_should_panic_if_banned_user_tries_to_leave_channel_and_then_rejoin() { let channel_contract_address = __setup__(); let dispatcher = IChannelComposableDispatcher { contract_address: channel_contract_address }; start_cheat_caller_address(channel_contract_address, USER_ONE.try_into().unwrap()); @@ -346,6 +327,51 @@ fn test_should_panic_if_banned_user_try_to_leave_channel_and_then_rejoin() { dispatcher.join_channel(channel_id); } +#[test] +fn test_channel_total_members() { + let channel_contract_address = __setup__(); + let dispatcher = IChannelComposableDispatcher { contract_address: channel_contract_address }; + start_cheat_caller_address(channel_contract_address, USER_ONE.try_into().unwrap()); + let community_id = dispatcher.create_community(); + let channel_id = dispatcher.create_channel(community_id); + stop_cheat_caller_address(channel_contract_address); + + start_cheat_caller_address(channel_contract_address, USER_TWO.try_into().unwrap()); + dispatcher.join_community(community_id); + stop_cheat_caller_address(channel_contract_address); + + start_cheat_caller_address(channel_contract_address, USER_TWO.try_into().unwrap()); + dispatcher.join_channel(channel_id); + stop_cheat_caller_address(channel_contract_address); + + start_cheat_caller_address(channel_contract_address, USER_THREE.try_into().unwrap()); + dispatcher.join_community(community_id); + stop_cheat_caller_address(channel_contract_address); + + start_cheat_caller_address(channel_contract_address, USER_THREE.try_into().unwrap()); + dispatcher.join_channel(channel_id); + stop_cheat_caller_address(channel_contract_address); + + start_cheat_caller_address(channel_contract_address, USER_FOUR.try_into().unwrap()); + dispatcher.join_community(community_id); + stop_cheat_caller_address(channel_contract_address); + + start_cheat_caller_address(channel_contract_address, USER_FOUR.try_into().unwrap()); + dispatcher.join_channel(channel_id); + stop_cheat_caller_address(channel_contract_address); + + start_cheat_caller_address(channel_contract_address, USER_FIVE.try_into().unwrap()); + dispatcher.join_community(community_id); + stop_cheat_caller_address(channel_contract_address); + + start_cheat_caller_address(channel_contract_address, USER_FIVE.try_into().unwrap()); + dispatcher.join_channel(channel_id); + stop_cheat_caller_address(channel_contract_address); + + let total_members = dispatcher.get_total_channel_members(channel_id); + assert(total_members == 5, 'invalid total members'); +} + #[test] fn test_joining_channel_emits_event() { let channel_contract_address = __setup__(); @@ -362,7 +388,7 @@ fn test_joining_channel_emits_event() { let mut spy = spy_events(); start_cheat_caller_address(channel_contract_address, USER_TWO.try_into().unwrap()); dispatcher.join_channel(channel_id); - let (is_member, channel_member) = dispatcher + let (_, channel_member) = dispatcher .is_channel_member(USER_TWO.try_into().unwrap(), channel_id); spy .assert_emitted( @@ -429,6 +455,21 @@ fn test_should_panic_if_profile_leaving_is_not_a_member() { stop_cheat_caller_address(channel_contract_address); } +#[test] +#[should_panic(expected: ('Karst: channel has no members',))] +fn test_channel_have_members_before_leaving() { + let channel_contract_address = __setup__(); + let dispatcher = IChannelComposableDispatcher { contract_address: channel_contract_address }; + + start_cheat_caller_address(channel_contract_address, USER_ONE.try_into().unwrap()); + let community_id = dispatcher.create_community(); + let channel_id = dispatcher.create_channel(community_id); + stop_cheat_caller_address(channel_contract_address); + + start_cheat_caller_address(channel_contract_address, USER_ONE.try_into().unwrap()); + dispatcher.leave_channel(channel_id); + stop_cheat_caller_address(channel_contract_address); +} #[test] fn test_leave_channel_emits_event() { @@ -453,8 +494,6 @@ fn test_leave_channel_emits_event() { .is_channel_member(USER_TWO.try_into().unwrap(), channel_id); let channel_token_id = channel_member.channel_token_id; dispatcher.leave_channel(channel_id); - let (is_member, channel_member) = dispatcher - .is_channel_member(USER_TWO.try_into().unwrap(), channel_id); spy .assert_emitted( @array![ @@ -474,7 +513,6 @@ fn test_leave_channel_emits_event() { ); } - #[test] fn test_channel_metadata_uri_with_owner() { let channel_contract_address = __setup__(); @@ -524,7 +562,7 @@ fn test_set_channel_metadata_with_moderator() { #[test] #[should_panic(expected: ('Karst: user unauthorized!',))] -fn test_set_metadata_should_panic_if_not_owner_or_moderator() { +fn test_set_metadata_should_panic_if_caller_is_not_owner_or_moderator() { let channel_contract_address = __setup__(); let dispatcher = IChannelComposableDispatcher { contract_address: channel_contract_address }; start_cheat_caller_address(channel_contract_address, USER_ONE.try_into().unwrap()); @@ -627,7 +665,7 @@ fn test_only_owner_can_add_channel_mod() { } #[test] -#[should_panic(expected: ('Karst: Not a Community Member',))] +#[should_panic(expected: ('Karst: not channel member',))] fn test_should_panic_if_mod_is_not_member() { let channel_contract_address = __setup__(); let dispatcher = IChannelComposableDispatcher { contract_address: channel_contract_address }; @@ -647,7 +685,6 @@ fn test_should_panic_if_mod_is_not_member() { stop_cheat_caller_address(channel_contract_address); } - #[test] fn test_add_channel_mods_emits_event() { let channel_contract_address = __setup__(); @@ -686,7 +723,6 @@ fn test_add_channel_mods_emits_event() { ); } - #[test] fn test_remove_channel_mods() { let channel_contract_address = __setup__(); @@ -763,7 +799,6 @@ fn test_only_owner_can_remove_channel_mod() { stop_cheat_caller_address(channel_contract_address); } - #[test] #[should_panic(expected: ('Karst: not channel moderator',))] fn test_should_panic_if_profile_to_be_removed_is_not_mod() { @@ -878,7 +913,6 @@ fn test_set_channel_censorship_status() { assert(censorship_status == true, 'invalid censorship status'); } - #[test] #[should_panic(expected: ('Karst: not channel owner',))] fn test_set_channel_censorship_status_not_owner() { @@ -1027,7 +1061,6 @@ fn test_set_ban_status_emit_event() { ); } - #[test] #[should_panic(expected: ('Karst: user unauthorized!',))] fn test_should_panic_if_caller_to_set_ban_status_is_not_owner_or_mod() { @@ -1098,7 +1131,7 @@ fn test_can_only_set_ban_status_for_members() { #[test] #[should_panic(expected: ('Karst: array mismatch',))] -fn test_should_set_ban_status_for_invalid_array_length() { +fn test_should_not_set_ban_status_for_invalid_array_length() { let channel_contract_address = __setup__(); let dispatcher = IChannelComposableDispatcher { contract_address: channel_contract_address }; start_cheat_caller_address(channel_contract_address, USER_ONE.try_into().unwrap()); @@ -1125,66 +1158,3 @@ fn test_should_set_ban_status_for_invalid_array_length() { dispatcher.set_channel_ban_status(channel_id, profiles, ban_statuses); } - -#[test] -fn test_joining_channel_total_members() { - let channel_contract_address = __setup__(); - let dispatcher = IChannelComposableDispatcher { contract_address: channel_contract_address }; - start_cheat_caller_address(channel_contract_address, USER_ONE.try_into().unwrap()); - let community_id = dispatcher.create_community(); - let channel_id = dispatcher.create_channel(community_id); - stop_cheat_caller_address(channel_contract_address); - - start_cheat_caller_address(channel_contract_address, USER_TWO.try_into().unwrap()); - dispatcher.join_community(community_id); - stop_cheat_caller_address(channel_contract_address); - - start_cheat_caller_address(channel_contract_address, USER_TWO.try_into().unwrap()); - dispatcher.join_channel(channel_id); - stop_cheat_caller_address(channel_contract_address); - - start_cheat_caller_address(channel_contract_address, USER_THREE.try_into().unwrap()); - dispatcher.join_community(community_id); - stop_cheat_caller_address(channel_contract_address); - - start_cheat_caller_address(channel_contract_address, USER_THREE.try_into().unwrap()); - dispatcher.join_channel(channel_id); - stop_cheat_caller_address(channel_contract_address); - - start_cheat_caller_address(channel_contract_address, USER_FOUR.try_into().unwrap()); - dispatcher.join_community(community_id); - stop_cheat_caller_address(channel_contract_address); - - start_cheat_caller_address(channel_contract_address, USER_FOUR.try_into().unwrap()); - dispatcher.join_channel(channel_id); - stop_cheat_caller_address(channel_contract_address); - - start_cheat_caller_address(channel_contract_address, USER_FIVE.try_into().unwrap()); - dispatcher.join_community(community_id); - stop_cheat_caller_address(channel_contract_address); - - start_cheat_caller_address(channel_contract_address, USER_FIVE.try_into().unwrap()); - dispatcher.join_channel(channel_id); - stop_cheat_caller_address(channel_contract_address); - - let total_members = dispatcher.get_total_channel_members(channel_id); - assert(total_members == 5, 'invalid total members'); -} - -// add an extra test to check that the channel to be left has members in the first place else it -// panics -#[test] -#[should_panic(expected: ('Karst: channel has no members',))] -fn test_channel_have_members_before_leaving() { - let channel_contract_address = __setup__(); - let dispatcher = IChannelComposableDispatcher { contract_address: channel_contract_address }; - - start_cheat_caller_address(channel_contract_address, USER_ONE.try_into().unwrap()); - let community_id = dispatcher.create_community(); - let channel_id = dispatcher.create_channel(community_id); - stop_cheat_caller_address(channel_contract_address); - - start_cheat_caller_address(channel_contract_address, USER_ONE.try_into().unwrap()); - dispatcher.leave_channel(channel_id); - stop_cheat_caller_address(channel_contract_address); -}