Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): add tests for event upgrades #2976

Merged
merged 1 commit into from
Jan 30, 2025

Conversation

remybar
Copy link
Contributor

@remybar remybar commented Jan 30, 2025

Add new tests for event upgrade as it has been done for models in #2925.

Summary by CodeRabbit

  • New Features

    • Enhanced event handling capabilities with new event types
    • Added support for model upgrades with member changes
  • Tests

    • Introduced new test functions to validate event and model upgrade processes
    • Added comprehensive test coverage for event system upgrades
  • Enhancements

    • Expanded event structures with more complex member configurations
    • Introduced new enumeration types for improved event handling

Copy link

coderabbitai bot commented Jan 30, 2025

Walkthrough

Ohayo, sensei! This PR introduces enhancements to event handling and model upgrade testing in the Dojo core Cairo test suite. The changes focus on adding new enumerations (MyEnum and AnotherEnum) and event structures (FooEventMemberChanged and FooEventMemberIllegalChange) across multiple test files. The modifications aim to expand the testing capabilities for model and event upgrades, providing more comprehensive validation of system behavior during structural changes.

Changes

File Change Summary
core-cairo-test/src/tests/helpers/event.cairo - Added MyEnum and AnotherEnum enums
- Added FooEventMemberChanged and FooEventMemberIllegalChange event structures
- Updated deploy_world_for_event_upgrades function
core-cairo-test/src/tests/world/event.cairo - Added MyEnum with X and Y variants
- Added AnotherEnum with X variant
- Added FooEventMemberChanged and FooEventMemberIllegalChange structs
- Added test functions for event upgrades
core-cairo-test/src/tests/world/model.cairo - Added test_upgrade_model_with_member_changed() test function

Possibly related PRs

Suggested labels

contributor

Suggested reviewers

  • glihm

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
crates/dojo/core-cairo-test/src/tests/world/model.cairo (1)

Line range hint 235-269: Consider enhancing test coverage for model upgrades.

While the test is well-structured, consider adding assertions for:

  • Pre-upgrade state validation
  • Transition of other fields during upgrade
  • Edge cases with different enum variants

Example enhancement:

 #[test]
 fn test_upgrade_model_with_member_changed() {
     let caller = starknet::contract_address_const::<0xb0b>();
     let world = deploy_world_for_model_upgrades();
     let mut world_storage = dojo::world::WorldStorageTrait::new(world, @"dojo");
+
+    // Verify pre-upgrade state
+    let initial: FooModelMemberChanged = world_storage.read_model(caller);
+    assert!(initial.b == 456, 'unexpected initial state');

     drop_all_events(world.contract_address);
     // ... existing test code ...

     // values previously set in deploy_world_for_model_upgrades
     let read: FooModelMemberChanged = world_storage.read_model(caller);
     assert!(read.a == (MyEnum::X(42), 189, 0));
     assert!(read.b == 456);
+
+    // Test with different enum variant
+    let different_variant = FooModelMemberChanged {
+        caller,
+        a: (MyEnum::Y(100), 189, 0),
+        b: 456,
+    };
+    world_storage.write_model(caller, different_variant);
+    let read_different = world_storage.read_model(caller);
+    assert!(read_different.a == (MyEnum::Y(100), 189, 0));
 }
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 687334d and 0a37b54.

⛔ Files ignored due to path filters (6)
  • crates/dojo/core-cairo-test/Scarb.lock is excluded by !**/*.lock
  • crates/torii/types-test/Scarb.lock is excluded by !**/*.lock
  • examples/simple/Scarb.lock is excluded by !**/*.lock
  • examples/spawn-and-move/Scarb.lock is excluded by !**/*.lock
  • spawn-and-move-db.tar.gz is excluded by !**/*.gz
  • types-test-db.tar.gz is excluded by !**/*.gz
📒 Files selected for processing (3)
  • crates/dojo/core-cairo-test/src/tests/helpers/event.cairo (2 hunks)
  • crates/dojo/core-cairo-test/src/tests/world/event.cairo (3 hunks)
  • crates/dojo/core-cairo-test/src/tests/world/model.cairo (1 hunks)
🔇 Additional comments (4)
crates/dojo/core-cairo-test/src/tests/helpers/event.cairo (2)

105-106: LGTM! Event registration is properly configured.

The new event types are correctly added to the namespace definition.


66-78: ⚠️ Potential issue

Ohayo sensei! Ensure enum variants match between helper and test files.

The MyEnum in this file only has variant X: u8, while the test file's version includes Y: u16. This mismatch could cause issues during event upgrades.

Apply this diff to align with the test file:

 enum MyEnum {
     X: u8,
+    Y: u16,
 }

Likely invalid or redundant comment.

crates/dojo/core-cairo-test/src/tests/world/event.cairo (2)

201-228: LGTM! Well-structured test for event member changes.

The test thoroughly validates the event upgrade process by checking:

  • Event emission
  • Selector correctness
  • Class hash verification
  • Address validation

281-291: LGTM! Proper validation of illegal changes.

The test correctly verifies that attempting to upgrade with incompatible member types triggers the expected error.

Comment on lines +80 to +92
#[derive(Introspect, Copy, Drop, Serde)]
enum AnotherEnum {
X: u8,
}

#[derive(Introspect, Copy, Drop, Serde)]
#[dojo::event]
struct FooEventMemberIllegalChange {
#[key]
pub caller: ContractAddress,
pub a: AnotherEnum,
pub b: u128,
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Type mismatch in AnotherEnum between files.

The AnotherEnum::X variant is u8 here but bool in the test file. This inconsistency will cause upgrade failures.

Apply this diff to match the test file:

 enum AnotherEnum {
-    X: u8,
+    X: bool,
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#[derive(Introspect, Copy, Drop, Serde)]
enum AnotherEnum {
X: u8,
}
#[derive(Introspect, Copy, Drop, Serde)]
#[dojo::event]
struct FooEventMemberIllegalChange {
#[key]
pub caller: ContractAddress,
pub a: AnotherEnum,
pub b: u128,
}
#[derive(Introspect, Copy, Drop, Serde)]
enum AnotherEnum {
X: bool,
}
#[derive(Introspect, Copy, Drop, Serde)]
#[dojo::event]
struct FooEventMemberIllegalChange {
#[key]
pub caller: ContractAddress,
pub a: AnotherEnum,
pub b: u128,
}

Comment on lines +48 to +61
#[derive(Introspect, Copy, Drop, Serde, PartialEq)]
enum MyEnum {
X: u8,
Y: u16,
}

#[derive(Introspect, Copy, Drop, Serde)]
#[dojo::event]
struct FooEventMemberChanged {
#[key]
pub caller: ContractAddress,
pub a: (MyEnum, u8, u32),
pub b: u128,
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Tuple type mismatch between event implementations.

The FooEventMemberChanged.a tuple includes an additional u32 compared to the helper version:

  • Helper: (MyEnum, u8)
  • Test: (MyEnum, u8, u32)

Update the helper version to match:

 struct FooEventMemberChanged {
     #[key]
     pub caller: ContractAddress,
-    pub a: (MyEnum, u8),
+    pub a: (MyEnum, u8, u32),
     pub b: u128,
 }

Committable suggestion skipped: line range outside the PR's diff.

Copy link

codecov bot commented Jan 30, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 57.12%. Comparing base (687334d) to head (0a37b54).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2976      +/-   ##
==========================================
- Coverage   57.13%   57.12%   -0.01%     
==========================================
  Files         424      424              
  Lines       56198    56198              
==========================================
- Hits        32107    32102       -5     
- Misses      24091    24096       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@glihm glihm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @remybar for the addition of those tests. 👍

@glihm glihm merged commit db840b7 into dojoengine:main Jan 30, 2025
14 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants