Skip to content

Commit

Permalink
fix: add actions for testing contract
Browse files Browse the repository at this point in the history
  • Loading branch information
posaune0423 committed Aug 7, 2024
1 parent 20b9ac1 commit a2f1aac
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 48 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/ci-contracts.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 0 additions & 2 deletions contracts/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,3 @@ world_address= "0xd4136ca2b9f1b08c6d6857b0f19da3dc8d136105ec46459787a6de0fd92bf8
#account_address = "0x003c4dd268780ef738920c801edc3a75b6337bc17558c74795b530c0ff502486"
#private_key = "0x2bbf4f9fd0bbb2e60b0316c1fe0b76cf7a4d0198bd493ced9b8df2a3a24d68a"
#world_address = "0x00a712727a27defbf77122fae30c91e90ec62bba8f09a2a396eddc29b768386b"
#
#
5 changes: 3 additions & 2 deletions contracts/src/apps/paint/tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
13 changes: 10 additions & 3 deletions contracts/src/apps/snake/tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions contracts/src/core.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ mod models;
mod actions;
mod traits;
mod utils;
mod tests;
74 changes: 33 additions & 41 deletions contracts/src/core/tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,38 @@ 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;

#[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 };

Expand All @@ -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');
}
}

0 comments on commit a2f1aac

Please sign in to comment.