Skip to content

Commit

Permalink
feat: add tests for cairo feature and modify inspect to return error …
Browse files Browse the repository at this point in the history
…if resource not found
  • Loading branch information
glihm committed Jan 17, 2025
1 parent 051e2e8 commit 89d9f15
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 12 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ jobs:
/tmp/bins/sozo --manifest-path crates/dojo/core/Scarb.toml test
/tmp/bins/sozo --manifest-path crates/dojo/core-cairo-test/Scarb.toml test
dojo-spawn-and-move-example-test:
dojo-examples-test:
needs: build
runs-on: ubuntu-latest
container:
Expand All @@ -150,6 +150,9 @@ jobs:
- uses: actions/checkout@v3
- run: |
chmod +x /tmp/bins/sozo
/tmp/bins/sozo --manifest-path examples/spawn-and-move/Scarb.toml build
/tmp/bins/sozo --manifest-path examples/spawn-and-move/Scarb.toml inspect ns-Flatbow
/tmp/bins/sozo --manifest-path examples/spawn-and-move/Scarb.toml inspect ns-RiverSkale
/tmp/bins/sozo --manifest-path examples/spawn-and-move/Scarb.toml test
/tmp/bins/sozo --manifest-path examples/simple/Scarb.toml test
Expand Down
8 changes: 4 additions & 4 deletions bin/sozo/src/commands/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl InspectArgs {
utils::get_world_diff_and_provider(starknet.clone(), world, &ws).await?;

if let Some(resource) = resource {
inspect_resource(&resource, &world_diff);
inspect_resource(&resource, &world_diff)?;
} else {
inspect_world(&world_diff);
}
Expand Down Expand Up @@ -166,7 +166,7 @@ struct GranteeDisplay {
}

/// Inspects a resource.
fn inspect_resource(resource_name_or_tag: &str, world_diff: &WorldDiff) {
fn inspect_resource(resource_name_or_tag: &str, world_diff: &WorldDiff) -> Result<()> {
let selector = if naming::is_valid_tag(resource_name_or_tag) {
naming::compute_selector_from_tag(resource_name_or_tag)
} else {
Expand All @@ -175,8 +175,7 @@ fn inspect_resource(resource_name_or_tag: &str, world_diff: &WorldDiff) {
let resource_diff = world_diff.resources.get(&selector);

if resource_diff.is_none() {
println!("Resource not found locally.");
return;
return Err(anyhow::anyhow!("Resource not found locally."));
}

let resource_diff = resource_diff.unwrap();
Expand Down Expand Up @@ -243,6 +242,7 @@ fn inspect_resource(resource_name_or_tag: &str, world_diff: &WorldDiff) {

print_table(&writers_disp, Some(Color::FG_BRIGHT_CYAN), Some("\n> Writers"));
print_table(&owners_disp, Some(Color::FG_BRIGHT_MAGENTA), Some("\n> Owners"));
Ok(())
}

/// Inspects the whole world.
Expand Down
13 changes: 9 additions & 4 deletions bin/sozo/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use starknet::core::types::Felt;
use starknet::core::utils as snutils;
use starknet::providers::jsonrpc::HttpTransport;
use starknet::providers::{JsonRpcClient, Provider};
use tracing::{error, trace};
use tracing::{trace, warn};

use crate::commands::options::account::{AccountOptions, SozoAccount};
use crate::commands::options::starknet::StarknetOptions;
Expand Down Expand Up @@ -117,10 +117,15 @@ pub async fn get_world_diff_and_provider(

let (provider, rpc_url) = starknet.provider(env)?;
let provider = Arc::new(provider);
if let Err(e) = provider_utils::health_check_provider(provider.clone()).await {
error!(target: LOG_TARGET,"Provider health check failed during sozo inspect.");
return Err(e);
if (provider_utils::health_check_provider(provider.clone()).await).is_err() {
warn!(target: LOG_TARGET, "Provider health check failed during sozo inspect, inspecting locally and all resources will appeared as `Created`. Remote resources will not be fetched.");
return Ok((
WorldDiff::from_local(world_local)?,
Arc::try_unwrap(provider).map_err(|_| anyhow!("Failed to unwrap Arc"))?,
rpc_url,
));
}

let provider = Arc::try_unwrap(provider).map_err(|_| anyhow!("Failed to unwrap Arc"))?;
trace!(?provider, "Provider initialized.");

Expand Down
6 changes: 6 additions & 0 deletions bin/sozo/tests/test_data/invalid_cairo_version/Scarb.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Code generated by scarb DO NOT EDIT.
version = 1

[[package]]
name = "sozo_test"
version = "0.3.1-rc7"
2 changes: 1 addition & 1 deletion crates/dojo/core-cairo-test/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version = 1

[[package]]
name = "dojo"
version = "1.0.11"
version = "1.0.12"
dependencies = [
"dojo_plugin",
]
Expand Down
2 changes: 1 addition & 1 deletion examples/spawn-and-move/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dependencies = [

[[package]]
name = "dojo_examples"
version = "1.0.11"
version = "1.0.12"
dependencies = [
"armory",
"bestiary",
Expand Down
2 changes: 1 addition & 1 deletion examples/spawn-and-move/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ edition = "2024_07"
sierra-replace-ids = true

[[target.starknet-contract]]
build-external-contracts = [ "dojo::world::world_contract::world" ]
build-external-contracts = [ "dojo::world::world_contract::world", "armory::m_Flatbow", "bestiary::m_RiverSkale" ]

[dependencies]
armory = { path = "../game-lib/armory" }
Expand Down
35 changes: 35 additions & 0 deletions examples/spawn-and-move/src/actions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ mod tests {
};

use super::{actions, IActionsDispatcher, IActionsDispatcherTrait};
use crate::dungeon::dungeon;
use dojo_examples::models::{Position, PositionValue, m_Position, Moves, m_Moves, Direction,};

fn namespace_def() -> NamespaceDef {
Expand Down Expand Up @@ -318,4 +319,38 @@ mod tests {
assert(new_position.vec.x == initial_position.vec.x + 1, 'position x is wrong');
assert(new_position.vec.y == initial_position.vec.y, 'position y is wrong');
}

#[test]
#[available_gas(30000000)]
#[cfg(feature: 'dungeon')]
fn test_feature_dungeon() {
let ndef = NamespaceDef {
namespace: "ns", resources: [
TestResource::Model(armory::m_Flatbow::TEST_CLASS_HASH),
TestResource::Model(bestiary::m_RiverSkale::TEST_CLASS_HASH),
TestResource::Contract(actions::TEST_CLASS_HASH),
TestResource::Contract(dungeon::TEST_CLASS_HASH),
].span()
};

let contract_defs = [
ContractDefTrait::new(@"ns", @"actions")
.with_writer_of([dojo::utils::bytearray_hash(@"ns")].span()),
ContractDefTrait::new(@"ns", @"dungeon")
.with_writer_of([dojo::utils::bytearray_hash(@"ns")].span()),
].span();

let mut world = spawn_test_world([ndef].span());
world.sync_perms_and_inits(contract_defs);

let other = starknet::contract_address_const::<0x1234>();
starknet::testing::set_contract_address(other);

let (dungeon_addr, _) = world.dns(@"dungeon").unwrap();

let (actions_system_addr, _) = world.dns(@"actions").unwrap();
let actions_system = IActionsDispatcher { contract_address: actions_system_addr };

actions_system.enter_dungeon(dungeon_addr);
}
}

0 comments on commit 89d9f15

Please sign in to comment.