Skip to content

Add rust tests for gui state management#1682

Merged
hardyjosh merged 5 commits intomainfrom
2025-04-19-state-management-tests
Apr 26, 2025
Merged

Add rust tests for gui state management#1682
hardyjosh merged 5 commits intomainfrom
2025-04-19-state-management-tests

Conversation

@findolor
Copy link
Copy Markdown
Collaborator

@findolor findolor commented Apr 19, 2025

Caution

Do not merge before #1681

Motivation

See issue: #1634

Solution

  • Remove unused functions
  • Add tests

Checks

By submitting this for review, I'm confirming I've done the following:

  • made this PR as small as possible
  • unit-tested any new functionality
  • linked any relevant issues or PRs
  • [ ] included screenshots (if this involves a front-end change)

Summary by CodeRabbit

  • New Features

    • Added comprehensive tests for select token management and GUI state serialization, deserialization, error handling, and callback execution.
  • Bug Fixes

    • Improved error handling in select token operations and state deserialization.
  • Refactor

    • Removed methods related to clearing and checking preset state from the user interface.
  • Tests

    • Expanded test coverage for select tokens and state management.
    • Removed outdated tests related to state clearing and preset checks.

@findolor findolor added test test code rust Related to rust crates labels Apr 19, 2025
@findolor findolor added this to the Test coverage, tech debt milestone Apr 19, 2025
@findolor findolor requested a review from hardyjosh April 19, 2025 10:47
@findolor findolor self-assigned this Apr 19, 2025
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 19, 2025

Walkthrough

This set of changes primarily updates internal testing and struct visibility in the GUI components of the JavaScript API. It adds new test helpers and comprehensive wasm-bindgen tests for select token and state management logic in Rust, modifies the visibility of a struct field, and expands test data in a YAML fixture. Several previously exported methods for state clearing and preset checks are removed from the Rust GUI implementation, and corresponding tests in the TypeScript suite are deleted. Minor test mocks related to a removed method are also cleaned up. No production logic is altered outside of test-only code and struct field visibility.

Changes

File(s) Change Summary
crates/js_api/src/gui/mod.rs Appended a new token entry (token4) to the select-tokens list in the YAML test fixture within the test module.
crates/js_api/src/gui/order_operations.rs Made the inner HashMap<String, Vec<Option<U256>>> field of the IOVaultIds struct publicly accessible by adding the pub modifier.
crates/js_api/src/gui/select_tokens.rs Added test-only helper methods (add_record_to_yaml, remove_record_from_yaml) and a comprehensive test module for select token management, including multiple async wasm-bindgen tests for DotrainOrderGui.
crates/js_api/src/gui/state_management.rs Removed exported methods for state clearing and preset checks; added a new test module with wasm-bindgen tests for state serialization, deserialization, error handling, and state update callback execution.
packages/orderbook/test/js_api/gui.test.ts Removed three test cases related to state clearing and preset checks from the state management tests suite.
packages/ui-components/src/tests/DepositInput.test.ts Removed mocking of DotrainOrderGui.prototype.isDepositPreset in a test case; rest of the test unchanged.
packages/ui-components/test-setup.ts Removed the mocked isDepositPreset method from the DotrainOrderGui class mock setup; other mocks unchanged.

Sequence Diagram(s)

sequenceDiagram
    participant Test as Test Module
    participant DotrainOrderGui as DotrainOrderGui
    participant TokenCfg as TokenCfg
    participant YAML as YAML Config

    Test->>DotrainOrderGui: add_record_to_yaml(...)
    DotrainOrderGui->>TokenCfg: add_record(...)
    TokenCfg->>YAML: Update token records
    YAML-->>TokenCfg: Confirmation
    TokenCfg-->>DotrainOrderGui: Result
    DotrainOrderGui-->>Test: Done

    Test->>DotrainOrderGui: remove_record_from_yaml(...)
    DotrainOrderGui->>TokenCfg: remove_record(...)
    TokenCfg->>YAML: Remove token record
    YAML-->>TokenCfg: Confirmation
    TokenCfg-->>DotrainOrderGui: Result
    DotrainOrderGui-->>Test: Done
Loading

Suggested reviewers

  • hardyjosh

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bda356a and b07a144.

📒 Files selected for processing (2)
  • packages/ui-components/src/__tests__/DepositInput.test.ts (0 hunks)
  • packages/ui-components/test-setup.ts (0 hunks)
💤 Files with no reviewable changes (2)
  • packages/ui-components/test-setup.ts
  • packages/ui-components/src/tests/DepositInput.test.ts
⏰ Context from checks skipped due to timeout of 90000ms (16)
  • GitHub Check: standard-tests (ubuntu-latest, test-js-bindings)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-legal)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-artifacts)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-wasm-test)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-rs-static)
  • GitHub Check: Deploy-Preview
  • GitHub Check: standard-tests (ubuntu-latest, rainix-wasm-artifacts)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-test)
  • GitHub Check: standard-tests (ubuntu-latest, ob-rs-test, true)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-static)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-rs-artifacts, true)
  • GitHub Check: test
  • GitHub Check: test
  • GitHub Check: git-clean
  • GitHub Check: build-tauri (ubuntu-22.04, true)
  • GitHub Check: test

🪧 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.
  • @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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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
Copy Markdown
Contributor

@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: 1

🔭 Outside diff range comments (4)
crates/js_api/src/gui/select_tokens.rs (2)

76-81: ⚠️ Potential issue

Potential data‑loss if the on‑chain lookup fails

save_select_token removes an existing token record before the async call to the RPC/ERC20::token_info.
If that call errors (network hic‑cup, invalid address, CORS, …), the original record is already deleted, leaving the YAML in an inconsistent state.

Move the removal after the on‑chain fetch succeeds, or wrap both operations in a rollback‑capable routine.

-        if TokenCfg::parse_from_yaml(self.dotrain_order.dotrain_yaml().documents, &key, None)
-            .is_ok()
-        {
-            TokenCfg::remove_record_from_yaml(self.dotrain_order.orderbook_yaml().documents, &key)?;
-        }
-
-        let address = Address::from_str(&address)?;
+        let address = Address::from_str(&address)?;+        // Safe to replace now that we have verified on‑chain info
+        if TokenCfg::parse_from_yaml(self.dotrain_order.dotrain_yaml().documents, &key, None)
+            .is_ok()
+        {
+            TokenCfg::remove_record_from_yaml(
+                self.dotrain_order.orderbook_yaml().documents,
+                &key,
+            )?;
+        }

61-108: 🧹 Nitpick (assertive)

Long‑running network call on the critical path

save_select_token performs an unconditional erc20.token_info(None).await?, which can add noticeable latency to the UI.
Consider:

  • Caching previously fetched token‑info by (chainId,address).
  • Allowing the caller to supply known decimals/name/symbol and skipping the RPC in that case.
crates/js_api/src/gui/state_management.rs (2)

244-249: 🛠️ Refactor suggestion

Deserialization does not notify the front‑end

After loading a saved state, deserialize_state updates self but never triggers execute_state_update_callback.
Any connected UI will remain stale until the next manual change.

 self.state_update_callback = dotrain_order_gui.state_update_callback;
 
+// Notify listeners that the state has been refreshed
+self.execute_state_update_callback()?;
 
 Ok(())

32-36: 🧹 Nitpick (assertive)

Hash stability concern

get_dotrain_hash hashes the bincode representation of the dotrain string.
Bincode’s output can change with crate version or compile‑time features, producing different hashes for identical strings across builds.
Hashing the raw UTF‑8 bytes (or a canonicalised YAML) is safer and reproducible.

-let dotrain_bytes = bincode::serialize(dotrain)?;
-let hash = Sha256::digest(&dotrain_bytes);
+let hash = Sha256::digest(dotrain.as_bytes());
📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 39b8d47 and bda356a.

📒 Files selected for processing (5)
  • crates/js_api/src/gui/mod.rs (1 hunks)
  • crates/js_api/src/gui/order_operations.rs (1 hunks)
  • crates/js_api/src/gui/select_tokens.rs (1 hunks)
  • crates/js_api/src/gui/state_management.rs (2 hunks)
  • packages/orderbook/test/js_api/gui.test.ts (0 hunks)
💤 Files with no reviewable changes (1)
  • packages/orderbook/test/js_api/gui.test.ts
⏰ Context from checks skipped due to timeout of 90000ms (16)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-wasm-artifacts)
  • GitHub Check: test
  • GitHub Check: standard-tests (ubuntu-latest, test-js-bindings)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-static)
  • GitHub Check: git-clean
  • GitHub Check: standard-tests (ubuntu-latest, rainix-rs-static)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-wasm-test)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-test)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-legal)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-artifacts)
  • GitHub Check: Deploy-Preview
  • GitHub Check: standard-tests (ubuntu-latest, rainix-rs-artifacts, true)
  • GitHub Check: standard-tests (ubuntu-latest, ob-rs-test, true)
  • GitHub Check: build-tauri (ubuntu-22.04, true)
  • GitHub Check: test
  • GitHub Check: test
🔇 Additional comments (1)
crates/js_api/src/gui/mod.rs (1)

543-546: YAML addition looks good

token4 is introduced in the select‑tokens list with correct indentation and the same schema as the other entries.
No further action required.

Comment thread crates/js_api/src/gui/order_operations.rs
@hardyjosh hardyjosh enabled auto-merge April 26, 2025 01:13
@hardyjosh hardyjosh merged commit ecdf149 into main Apr 26, 2025
17 checks passed
@coderabbitai coderabbitai Bot mentioned this pull request Aug 22, 2025
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

rust Related to rust crates test test code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants