From 35080214025d5475ad2531c4a43639395c2e47dc Mon Sep 17 00:00:00 2001 From: TAdev0 Date: Thu, 11 Apr 2024 01:07:03 +0200 Subject: [PATCH] tests --- onchain/src/art_peace.cairo | 24 +++++------ onchain/src/interface.cairo | 6 +-- onchain/src/tests/art_peace.cairo | 66 ++++++++++++++++++++++--------- 3 files changed, 62 insertions(+), 34 deletions(-) diff --git a/onchain/src/art_peace.cairo b/onchain/src/art_peace.cairo index 1f276251..288604f8 100644 --- a/onchain/src/art_peace.cairo +++ b/onchain/src/art_peace.cairo @@ -79,7 +79,7 @@ pub mod ArtPeace { pub main_quests: Span, } - const day_time: u64 = consteval_int!(60 * 60 * 24); + const DAY_IN_SECONDS: u64 = consteval_int!(60 * 60 * 24); #[constructor] fn constructor(ref self: ContractState, init_params: InitParams) { @@ -257,6 +257,17 @@ pub mod ArtPeace { self.day_index.read() } + fn increase_day_index(ref self: ContractState) { + let block_timestamp = starknet::get_block_timestamp(); + let start_day_time = self.start_day_time.read(); + + assert(block_timestamp >= start_day_time + DAY_IN_SECONDS, 'day has not passed'); + + self.day_index.write(self.day_index.read() + 1); + self.start_day_time.write(block_timestamp); + self.emit(NewDay { day_index: self.day_index.read() }); + } + fn get_daily_quest_count(self: @ContractState) -> core::zeroable::NonZero:: { // TODO: hardcoded 3 daily quests 3 @@ -357,17 +368,6 @@ pub mod ArtPeace { } } - fn increase_day_index(ref self: ContractState) { - let block_timestamp = starknet::get_block_timestamp(); - let start_day_time = self.start_day_time.read(); - - assert(block_timestamp > start_day_time + day_time, 'day has not passed'); - - self.day_index.write(self.day_index.read() + 1); - self.start_day_time.write(block_timestamp); - self.emit(NewDay { day_index: self.day_index.read() }); - } - fn get_user_pixels_placed(self: @ContractState, user: ContractAddress) -> u32 { let mut i = 0; let mut total = 0; diff --git a/onchain/src/interface.cairo b/onchain/src/interface.cairo index a26a1483..9882ff36 100644 --- a/onchain/src/interface.cairo +++ b/onchain/src/interface.cairo @@ -40,6 +40,9 @@ pub trait IArtPeace { fn get_end_time(self: @TContractState) -> u64; fn get_day(self: @TContractState) -> u32; + // Start a new day + fn increase_day_index(ref self: TContractState); + // Get quest info fn get_daily_quest_count(self: @TContractState) -> core::zeroable::NonZero::; fn get_daily_quest( @@ -57,9 +60,6 @@ pub trait IArtPeace { fn claim_today_quest(ref self: TContractState, quest_id: u32); fn claim_main_quest(ref self: TContractState, quest_id: u32); - // Start a new day - fn increase_day_index(ref self: TContractState); - // Stats fn get_user_pixels_placed(self: @TContractState, user: starknet::ContractAddress) -> u32; fn get_user_pixels_placed_day( diff --git a/onchain/src/tests/art_peace.cairo b/onchain/src/tests/art_peace.cairo index ce0fa0a9..eff84e67 100644 --- a/onchain/src/tests/art_peace.cairo +++ b/onchain/src/tests/art_peace.cairo @@ -5,10 +5,14 @@ use art_peace::templates::{ ITemplateVerifierDispatcherTrait, TemplateMetadata }; +use core::poseidon::PoseidonTrait; +use core::hash::{HashStateTrait, HashStateExTrait}; + use snforge_std as snf; use snforge_std::{CheatTarget, ContractClassTrait}; use starknet::{ContractAddress, contract_address_const}; +const DAY_IN_SECONDS: u64 = consteval_int!(60 * 60 * 24); const WIDTH: u128 = 100; const HEIGHT: u128 = 100; const TIME_BETWEEN_PIXELS: u64 = 10; @@ -98,6 +102,21 @@ fn warp_to_next_available_time(art_peace: IArtPeaceDispatcher) { snf::start_warp(CheatTarget::One(art_peace.contract_address), last_time + TIME_BETWEEN_PIXELS); } +fn compute_template_hash(template: Span) -> felt252 { + let template_len = template.len(); + if template_len == 0 { + return 0; + } + + let mut hasher = PoseidonTrait::new(); + let mut i = 0; + while i < template_len { + hasher = hasher.update_with(*template.at(i)); + i += 1; + }; + hasher.finalize() +} + #[test] fn deploy_test() { let art_peace = IArtPeaceDispatcher { contract_address: deploy_contract() }; @@ -212,25 +231,6 @@ fn pixel_quest_test() { ); } -use core::poseidon::PoseidonTrait; -use core::hash::{HashStateTrait, HashStateExTrait}; - -// TODO: Move to src -fn compute_template_hash(template: Span) -> felt252 { - let template_len = template.len(); - if template_len == 0 { - return 0; - } - - let mut hasher = PoseidonTrait::new(); - let mut i = 0; - while i < template_len { - hasher = hasher.update_with(*template.at(i)); - i += 1; - }; - hasher.finalize() -} - #[test] fn template_full_basic_test() { let art_peace = IArtPeaceDispatcher { contract_address: deploy_contract() }; @@ -310,6 +310,34 @@ fn template_full_basic_test() { "Template is not completed after it should be" ); } + +#[test] +fn increase_day_test() { + let art_peace_address = deploy_contract(); + let art_peace = IArtPeaceDispatcher { contract_address: art_peace_address }; + + let current_day_index = art_peace.get_day(); + assert!(current_day_index == 0, "day index wrongly initialized"); + + snf::start_warp(CheatTarget::One(art_peace_address), DAY_IN_SECONDS); + art_peace.increase_day_index(); + let current_day_index = art_peace.get_day(); + assert!(current_day_index == 1, "day index not updated"); + snf::stop_warp(CheatTarget::One(art_peace_address)); +} + +#[test] +#[should_panic(expected: ('day has not passed',))] +fn increase_day_panic_test() { + let art_peace_address = deploy_contract(); + let art_peace = IArtPeaceDispatcher { contract_address: art_peace_address }; + + let current_day_index = art_peace.get_day(); + assert!(current_day_index == 0, "day index wrongly initialized"); + + snf::start_warp(CheatTarget::One(art_peace_address), DAY_IN_SECONDS - 1); + art_peace.increase_day_index(); +} // TODO: test invalid template inputs