Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
zsluedem committed Mar 24, 2024
1 parent 1ca2f80 commit b9021cb
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 31 deletions.
1 change: 0 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ jobs:
- run: sozo build
- run: sozo test
- run: sozo --profile ${{ env.TARGET_NAME }} migrate
- run: ls target
- uses: actions/upload-artifact@v4
with:
name: manifest.json
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ const leaves = data.map((d) =>
const tree = new merkle.MerkleTree(leaves, hash.computePoseidonHash);
console.log("root: ", tree.root)

const h = hash_posei([5, 1, 70, 40, 60, 20, 2, 2500, 100])
const h = hash_posei([8, 1, 210, 140, 260, 120, 4, 2000, 100])
console.log("h: ", h)
const proof = tree.getProof(h);
console.log("proof: ", proof)
2 changes: 1 addition & 1 deletion scripts/default_auth.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ echo "Default authorizations have been successfully set."

echo "Setting pay address for upgrading"
export ADMIN_ADDRESS=$(cat ./target/$TARGET_NAME/manifest.json | jq -r '.contracts[] | select(.name == "kingdom_lord::admin::kingdom_lord_admin" ).address')
sozo execute 0x17acb0793d3bfdf9b8058d6ba25215bed0df3949007d0b7676ad335736e444f set_config --calldata 0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7,0,100,0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03
sozo execute 0x17acb0793d3bfdf9b8058d6ba25215bed0df3949007d0b7676ad335736e444f set_config --calldata 0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7,0,100,0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03,0x2e3aa949c5d014218a1194b9d5c84c7457a027fc34826547b8dfe5b52d72220
echo "Setting pay address for upgrading done"
9 changes: 7 additions & 2 deletions src/components/barrack.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct Barrack{
building_id: u64,
level: Level,
bonus: u64,
population:u64
population:u64,
}

#[derive(Model, Copy, Drop, Serde)]
Expand Down Expand Up @@ -279,9 +279,14 @@ mod barrack_component{
let world = self.get_contract().world();
let current_time = get_current_time();
let player = get_caller_address();
let barrack = get!(world, (player), (Barrack));
let barrack: Barrack = get!(world, (player), (Barrack));

let required_time = barrack.bonus * required_time / 100;
if barrack.population == 0{
return Result::Err(Error::NoBarrackConstructed);
}
let mut index = 0;

let mut res: Result<UnderTraining, Error> = Result::Err(Error::UnknownedError('start upgrading failed'));
loop {
if index == UNDER_TRAINING_COUNT {
Expand Down
2 changes: 2 additions & 0 deletions src/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ enum Error{
InvalidProof,
TrainingNotFinished,
TrainingListFull,
NoBarrackConstructed
}

#[starknet::interface]
trait IKingdomLord<TState>{
// read function
Expand Down
10 changes: 5 additions & 5 deletions src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ mod helpers {
}

mod tests {
// mod test_spawn;
// mod test_storage;
// mod test_upgrade;
// mod test_city_hall;
mod test_spawn;
mod test_storage;
mod test_upgrade;
mod test_city_hall;
mod test_barrack;
// mod test_pay_upgrade;
mod test_pay_upgrade;
mod utils;
}

Expand Down
49 changes: 29 additions & 20 deletions src/tests/test_barrack.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ mod tests {

// import test utils
use dojo::test_utils::{spawn_test_world, deploy_contract};
use kingdom_lord::tests::utils::{setup_world, assert_troop, city_hall_level2_proof, city_hall_level1_proof, assert_resource};
use kingdom_lord::tests::utils::{
setup_world, assert_troop, city_hall_level2_proof, city_hall_level1_proof, assert_resource,
construct_barrack, increase_time
};
use kingdom_lord::interface::{
IKingdomLord, IKingdomLordDispatcher, IKingdomLordLibraryDispatcherImpl, Error
};
Expand All @@ -23,37 +26,43 @@ mod tests {
let context = setup_world();

context.kingdom_lord.spawn();

construct_barrack(context);
let caller = get_caller_address();

let err = context
.kingdom_lord
.start_training(0)
.unwrap_err();
assert(err == Error::ResourceNotEnough, 'not enough resource');
set_block_number(100);
assert_resource(context, caller, 1000,1000,1000,1000);
assert_resource(context, caller, 1000, 1000, 1000, 1000);
let training_id1 = context.kingdom_lord.start_training(0).unwrap();
assert_resource(context, caller, 880,900,850,970);
assert_resource(context, caller, 880, 900, 850, 970);
let training_id2 = context.kingdom_lord.start_training(0).unwrap();
assert_resource(context, caller, 760,800,700,940);
assert_resource(context, caller, 760, 800, 700, 940);
let training_id3 = context.kingdom_lord.start_training(1).unwrap();
assert_resource(context, caller, 660,670,540,870);
assert_resource(context, caller, 660, 670, 540, 870);

set_block_number(150);
println!("block number");
increase_time(150);
let err = context.kingdom_lord.finish_training(training_id1).unwrap_err();
assert_eq!(err, Error::TrainingNotFinished, "training not finished");
set_block_number(1700);
increase_time(1450);
context.kingdom_lord.finish_training(training_id1).unwrap();
assert_troop(context, caller, 1,0,0,0,0,0);

assert_troop(context, caller, 1, 0, 0, 0, 0, 0);
context.kingdom_lord.finish_training(training_id2).unwrap();
assert_troop(context, caller, 2,0,0,0,0,0);
println!("block number 2");
assert_troop(context, caller, 2, 0, 0, 0, 0, 0);
let err = context.kingdom_lord.finish_training(training_id3).unwrap_err();
assert_eq!(err, Error::TrainingNotFinished, "training not finished");
set_block_number(1860);
increase_time(160);
let res = context.kingdom_lord.finish_training(training_id3).unwrap();
assert_troop(context, caller, 2,1,0,0,0,0);
assert_troop(context, caller, 2, 1, 0, 0, 0, 0);
}


#[test]
#[available_gas(300000000000)]
fn test_no_barrack_training() {
let context = setup_world();

context.kingdom_lord.spawn();
let caller = get_caller_address();
increase_time(100);
let err = context.kingdom_lord.start_training(0).unwrap_err();
assert(err == Error::NoBarrackConstructed, 'barrack have not built');
}
}
26 changes: 25 additions & 1 deletion src/tests/utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ use kingdom_lord::components::outer_city::OuterCityTraitDispatcher;
use starknet::contract_address_const;
use openzeppelin::presets::erc20::ERC20;
use openzeppelin::token::erc20::interface::IERC20Dispatcher;
use starknet::testing::{set_caller_address, set_contract_address};
use kingdom_lord::models::time::get_current_time;
use starknet::testing::{set_caller_address, set_contract_address, set_block_number};
trait SerializedAppend<T> {
fn append_serde(ref self: Array<felt252>, value: T);
}
Expand Down Expand Up @@ -119,6 +120,29 @@ fn assert_troop(
assert_eq!(troop.heavy_knights, heavy_knights, "heavy_knights should be {} but got {}", troop.heavy_knights, heavy_knights);
}

fn increase_time(time: u64) {
let current_time = get_current_time();
set_block_number(current_time + time);
}


fn construct_barrack(context: TestContext){
increase_time(100);
let res = context.kingdom_lord.start_upgrade(19, 8, 1, 210, 140, 260, 120, 4, 2000, 100, array![
0x3e6b3f2c7624525e03ed0c96ff43d0fe1dafa24a61eaca42b1e9dd00a9bf2b9,
0x2e42f1daa91953d844c066e52c9355208979c982e0cea128e7ea54db6dd1d75,
0x5eeef9158bbb0ed60b496e2dd18f1b50f2efd44a619a1e4f4b312562fd86202,
0x76a918559603e1db782e6b05119251069e444c6f0aa1fa6e9f2c21a607fe648,
0x5a39d476538786b9849535508804dfe168518b1f6c2d65541109b2534791136,
0x0,
0x0,
0x5f0dde256e23129e6713acf8c87088898a0632c9d3cddcd77fc58c2c1a8922b
]);
increase_time(2000);
let res = context.kingdom_lord.finish_upgrade(0);
res.unwrap();
}


#[cfg(test)]
fn setup_world() -> TestContext {
Expand Down

0 comments on commit b9021cb

Please sign in to comment.