From a2f1aac4fc46c71857d2a43c48c2d70d40a03016 Mon Sep 17 00:00:00 2001 From: Asuma Yamada Date: Wed, 7 Aug 2024 16:05:57 -0400 Subject: [PATCH] fix: add actions for testing contract --- .github/workflows/ci-contracts.yml | 32 ++++++++++++ contracts/Scarb.toml | 2 - contracts/src/apps/paint/tests.cairo | 5 +- contracts/src/apps/snake/tests.cairo | 13 +++-- contracts/src/core.cairo | 1 + contracts/src/core/tests.cairo | 74 +++++++++++++--------------- 6 files changed, 79 insertions(+), 48 deletions(-) create mode 100644 .github/workflows/ci-contracts.yml diff --git a/.github/workflows/ci-contracts.yml b/.github/workflows/ci-contracts.yml new file mode 100644 index 0000000..5121b1a --- /dev/null +++ b/.github/workflows/ci-contracts.yml @@ -0,0 +1,32 @@ +name: ci-contracts + +on: + push: + branches: [main] + paths: + - "contracts/**" + pull_request: + branches: [main] + paths: + - "contracts/**" + +defaults: + run: + working-directory: contracts + +jobs: + sozo-test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - run: curl -L https://install.dojoengine.org | bash + - run: /home/runner/.config/.dojo/bin/dojoup -v v0.7.0-alpha.4 + - run: | + scarb fmt --check + /home/runner/.config/.dojo/bin/sozo test + /home/runner/.config/.dojo/bin/sozo build + if [[ `git status --porcelain` ]]; then + echo The git repo is dirty + echo "Make sure to run \"sozo build\" after changing Scarb.toml" + exit 1 + fi diff --git a/contracts/Scarb.toml b/contracts/Scarb.toml index cdccd02..e66f180 100644 --- a/contracts/Scarb.toml +++ b/contracts/Scarb.toml @@ -57,5 +57,3 @@ world_address= "0xd4136ca2b9f1b08c6d6857b0f19da3dc8d136105ec46459787a6de0fd92bf8 #account_address = "0x003c4dd268780ef738920c801edc3a75b6337bc17558c74795b530c0ff502486" #private_key = "0x2bbf4f9fd0bbb2e60b0316c1fe0b76cf7a4d0198bd493ced9b8df2a3a24d68a" #world_address = "0x00a712727a27defbf77122fae30c91e90ec62bba8f09a2a396eddc29b768386b" -# -# diff --git a/contracts/src/apps/paint/tests.cairo b/contracts/src/apps/paint/tests.cairo index b10cc36..3c03656 100644 --- a/contracts/src/apps/paint/tests.cairo +++ b/contracts/src/apps/paint/tests.cairo @@ -47,8 +47,9 @@ mod tests { let paint_actions = IPaintActionsDispatcher { contract_address: paint_actions_address }; let namespace: ByteArray = "pixelaw"; - let pixelByteArray:ByteArray = "Pixel"; - world.grant_writer(selector_from_names(@namespace, @pixelByteArray), core_actions_address); + let pixel_model_name: ByteArray = "Pixel"; + world + .grant_writer(selector_from_names(@namespace, @pixel_model_name), core_actions_address); (world, core_actions, paint_actions) } diff --git a/contracts/src/apps/snake/tests.cairo b/contracts/src/apps/snake/tests.cairo index e055d54..16636eb 100644 --- a/contracts/src/apps/snake/tests.cairo +++ b/contracts/src/apps/snake/tests.cairo @@ -74,9 +74,16 @@ mod tests { let snake_model_name: ByteArray = "Snake"; let snake_segment_model_name: ByteArray = "SnakeSegment"; - world.grant_writer(selector_from_names(@namespace, @pixel_model_name), core_actions_address); - world.grant_writer(selector_from_names(@namespace, @snake_model_name), snake_actions_address); - world.grant_writer(selector_from_names(@namespace, @snake_segment_model_name), snake_actions_address); + world + .grant_writer(selector_from_names(@namespace, @pixel_model_name), core_actions_address); + world + .grant_writer( + selector_from_names(@namespace, @snake_model_name), snake_actions_address + ); + world + .grant_writer( + selector_from_names(@namespace, @snake_segment_model_name), snake_actions_address + ); (world, core_actions, snake_actions, paint_actions) } diff --git a/contracts/src/core.cairo b/contracts/src/core.cairo index 799ad23..ba86b6c 100644 --- a/contracts/src/core.cairo +++ b/contracts/src/core.cairo @@ -2,3 +2,4 @@ mod models; mod actions; mod traits; mod utils; +mod tests; diff --git a/contracts/src/core/tests.cairo b/contracts/src/core/tests.cairo index 2ac0d84..b4f57c9 100644 --- a/contracts/src/core/tests.cairo +++ b/contracts/src/core/tests.cairo @@ -4,23 +4,16 @@ mod tests { use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; - use dojo::utils::test::{spawn_test_world, deploy_contract}; - - use pixelaw::core::models::app::{app, app_name}; - use pixelaw::core::models::color::color; - use pixelaw::core::models::actions_model::actions_model; - use pixelaw::core::models::alert::alert; - use pixelaw::core::models::owner::owner; - use pixelaw::core::models::owner::Owner; - use pixelaw::core::models::permission::permission; - use pixelaw::core::models::app::app; - use pixelaw::core::models::app::PixelType; - use pixelaw::core::models::position::Position; - use pixelaw::core::models::text::text; - use pixelaw::core::models::timestamp::timestamp; - use pixelaw::core::models::timestamp::Timestamp; + use pixelaw::core::models::registry::{app, app_name, core_actions_address}; - use super::{actions, IActionsDispatcher, IActionsDispatcherTrait}; + use pixelaw::core::models::pixel::{Pixel, PixelUpdate}; + use pixelaw::core::models::pixel::{pixel}; + use pixelaw::core::models::permissions::{permissions}; + use pixelaw::core::utils::{get_core_actions, Direction, Position, DefaultParameters}; + use pixelaw::core::actions::{actions, IActionsDispatcher, IActionsDispatcherTrait}; + use dojo::utils::test::{spawn_test_world, deploy_contract}; + use dojo::utils::{selector_from_names}; + use poseidon::poseidon_hash_span; const SPAWN_PIXEL_ENTRYPOINT: felt252 = 0x01c199924ae2ed5de296007a1ac8aa672140ef2a973769e4ad1089829f77875a; @@ -28,30 +21,21 @@ mod tests { #[test] #[available_gas(30000000)] fn test_process_queue() { - let caller = starknet::contract_address_const::<0x0>(); - - // models let mut models = array![ + pixel::TEST_CLASS_HASH, app::TEST_CLASS_HASH, app_name::TEST_CLASS_HASH, - actions_model::TEST_CLASS_HASH, - owner::TEST_CLASS_HASH, - permission::TEST_CLASS_HASH, - app::TEST_CLASS_HASH, - timestamp::TEST_CLASS_HASH, - text::TEST_CLASS_HASH, - color::TEST_CLASS_HASH, + core_actions_address::TEST_CLASS_HASH, + permissions::TEST_CLASS_HASH, ]; - // deploy world with models let world = spawn_test_world("pixelaw", models); - let class_hash: ClassHash = actions::TEST_CLASS_HASH.try_into().unwrap(); - - // deploy systems contract - let contract_address = world.deploy_contract(0, class_hash, array![].span()); + let core_actions_address = world + .deploy_contract( + 'salt1', actions::TEST_CLASS_HASH.try_into().unwrap(), array![].span() + ); - let actions_system = IActionsDispatcher { contract_address }; - let id = 0; + let core_actions = IActionsDispatcher { contract_address: core_actions_address }; let position = Position { x: 0, y: 0 }; @@ -60,21 +44,29 @@ mod tests { position.serialize(ref calldata); calldata.append('snake'); calldata.append(0); + let id = poseidon_hash_span( + array![ + 0.into(), + core_actions_address.into(), + SPAWN_PIXEL_ENTRYPOINT.into(), + poseidon_hash_span(calldata.span()) + ] + .span() + ); - actions_system.process_queue(id, contract_address, SPAWN_PIXEL_ENTRYPOINT, calldata.span()); + core_actions + .process_queue(id, 0, core_actions_address, SPAWN_PIXEL_ENTRYPOINT, calldata.span()); - let (owner, app, timestamp) = get!(world, (position).into(), (Owner, PixelType, Timestamp)); + let pixel = get!(world, (position).into(), (Pixel)); // check timestamp assert( - timestamp.created_at == starknet::get_block_timestamp(), - 'incorrect timestamp.created_at' + pixel.created_at == starknet::get_block_timestamp(), 'incorrect timestamp.created_at' ); assert( - timestamp.updated_at == starknet::get_block_timestamp(), - 'incorrect timestamp.updated_at' + pixel.updated_at == starknet::get_block_timestamp(), 'incorrect timestamp.updated_at' ); - assert(timestamp.x == position.x, 'incorrect timestamp.x'); - assert(timestamp.y == position.y, 'incorrect timestamp.y'); + assert(pixel.x == position.x, 'incorrect timestamp.x'); + assert(pixel.y == position.y, 'incorrect timestamp.y'); } }