Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TAdev0 committed Apr 10, 2024
1 parent 66a31f0 commit 3508021
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 34 deletions.
24 changes: 12 additions & 12 deletions onchain/src/art_peace.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub mod ArtPeace {
pub main_quests: Span<ContractAddress>,
}

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) {
Expand Down Expand Up @@ -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::<u32> {
// TODO: hardcoded 3 daily quests
3
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions onchain/src/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ pub trait IArtPeace<TContractState> {
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::<u32>;
fn get_daily_quest(
Expand All @@ -57,9 +60,6 @@ pub trait IArtPeace<TContractState> {
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(
Expand Down
66 changes: 47 additions & 19 deletions onchain/src/tests/art_peace.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<u8>) -> 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() };
Expand Down Expand Up @@ -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<u8>) -> 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() };
Expand Down Expand Up @@ -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


0 comments on commit 3508021

Please sign in to comment.