-
Notifications
You must be signed in to change notification settings - Fork 7
Add rust tests for quote wasm bindings #1710
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
Conversation
…uote-bindings-test
WalkthroughThe changes refactor WASM-related Rust modules and TypeScript bindings across several crates, improving type safety, error handling, and test coverage. Conditional compilation for WASM and test targets is simplified or removed, making WASM traits and dependencies unconditional. Result types and error handling in quoting APIs are made explicit and structured, with corresponding updates to downstream TypeScript and Svelte code and tests to match new result object shapes. Changes
Sequence Diagram(s)sequenceDiagram
participant UI as Svelte UI
participant TS as TypeScript Layer
participant WASM as WASM Quote API
participant Rust as Rust Quote Logic
UI->>TS: orderQuoteQuery()
TS->>WASM: getOrderQuote(...)
WASM->>Rust: parse inputs, perform quote
Rust-->>WASM: Result<DoOrderQuoteResult, QuoteBindingsError>
WASM-->>TS: { value: [...], error?: string }
alt error present
TS-->>UI: throw Error(error)
else success
TS-->>UI: value
end
Assessment against linked issues
Possibly related PRs
Suggested reviewers
Tip ⚡️ Faster reviews with caching
Enjoy the performance boost—your workflow just got faster. ✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. 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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 6
📜 Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro
⛔ Files ignored due to path filters (2)
Cargo.lock
is excluded by!**/*.lock
tauri-app/src-tauri/Cargo.lock
is excluded by!**/*.lock
📒 Files selected for processing (11)
Cargo.toml
(1 hunks)crates/js_api/src/gui/state_management.rs
(1 hunks)crates/quote/Cargo.toml
(1 hunks)crates/quote/src/js_api/mod.rs
(1 hunks)crates/quote/src/lib.rs
(1 hunks)crates/quote/src/quote.rs
(2 hunks)crates/subgraph/Cargo.toml
(0 hunks)crates/subgraph/src/types/common.rs
(2 hunks)packages/orderbook/test/quote/test.test.ts
(8 hunks)packages/ui-components/src/__tests__/TanstackOrderQuote.test.ts
(5 hunks)packages/ui-components/src/lib/components/detail/TanstackOrderQuote.svelte
(1 hunks)
💤 Files with no reviewable changes (1)
- crates/subgraph/Cargo.toml
🧰 Additional context used
🪛 Biome (1.9.4)
packages/orderbook/test/quote/test.test.ts
[error] 192-192: Template literals are preferred over string concatenation.
Unsafe fix: Use a template literal.
(lint/style/useTemplate)
[error] 334-334: Template literals are preferred over string concatenation.
Unsafe fix: Use a template literal.
(lint/style/useTemplate)
[error] 345-345: Template literals are preferred over string concatenation.
Unsafe fix: Use a template literal.
(lint/style/useTemplate)
⏰ Context from checks skipped due to timeout of 90000ms (4)
- GitHub Check: build-tauri (ubuntu-22.04, true)
- GitHub Check: test
- GitHub Check: standard-tests (ubuntu-latest, rainix-rs-artifacts, true)
- GitHub Check: test
🔇 Additional comments (14)
crates/js_api/src/gui/state_management.rs (1)
291-291
: Improved code organization by moving imports to the tests module.Moving
js_sys::{eval, Reflect}
from the main module scope to the tests module is a good practice. This confines potentially unsafe functionality likeeval
to test code only and keeps imports localized to where they're actually used.crates/quote/Cargo.toml (2)
31-31
: Making wasm-bindgen-utils available unconditionally.Moving
wasm-bindgen-utils
from target-specific dependencies to general dependencies ensures it's available for all target environments. This is necessary to support the expanded compilation conditions inlib.rs
.
45-46
: Added test dependencies to support wasm testing.Adding
rain_orderbook_common
andwasm-bindgen-test
to dev-dependencies provides the necessary tools to test WebAssembly bindings, directly supporting the PR's objective to enhance test coverage.crates/quote/src/lib.rs (1)
9-9
: Expanded compilation condition to include tests.Changing the
js_api
module compilation condition from#[cfg(target_family = "wasm")]
to#[cfg(any(target_family = "wasm", test))]
is key to achieving the PR objective. This change enables thejs_api
module to be compiled and included during test builds regardless of the target family, allowing proper testing of the WebAssembly bindings even in non-wasm environments.packages/ui-components/src/lib/components/detail/TanstackOrderQuote.svelte (1)
46-51
: Improved error handling with explicit error propagationThe updated query function correctly handles the new result structure from the
getOrderQuote
function. This change properly propagates errors by throwing an exception when an error is detected, making debugging and error handling more straightforward.packages/ui-components/src/__tests__/TanstackOrderQuote.test.ts (5)
15-25
: Test updated to match new API result structureThe mock correctly reflects the updated return type of
getOrderQuote
, which now returns an object with avalue
property containing the array of results.
50-67
: Test updated to match new API result structureThe mock correctly reflects the updated return type structure with wrapped values.
92-109
: Test updated to match new API result structureThe mock updates here match the new return type pattern consistently with the other test cases.
130-140
: Test updated to match new API result structureError handling case properly updated to match the new return type structure.
162-172
: Test updated to match new API result structureZero ratio test case correctly updated with the new structure.
crates/subgraph/src/types/common.rs (1)
90-109
: Unconditional WASM trait implementation for SgOrderThe changes here remove the conditional compilation for the
Tsify
derive macro and theimpl_wasm_traits!
macro invocation, making them always available regardless of the compilation target. This is a good simplification that aligns with the PR's goal of improving test coverage for quote WASM bindings.By making these traits unconditionally available, we can ensure consistent behavior across all environments and make testing easier.
crates/quote/src/quote.rs (2)
106-110
: Unconditional WASM trait implementation for BatchQuoteTargetRemoving the conditional compilation for the
Tsify
derive macro and making the WASM trait implementation unconditional is a good approach. This simplifies the code and ensures consistent behavior across all compilation targets.
202-206
: Unconditional WASM trait implementation for BatchQuoteSpecSimilar to the change for
BatchQuoteTarget
, removing the conditional compilation here simplifies the code and ensures consistent behavior across all targets. This change supports the goal of improved test coverage for WASM bindings.crates/quote/src/js_api/mod.rs (1)
57-60
: Hex parsing oforder_hash
relies on implementation details
U256::from_str
currently accepts both decimal and0x
-prefixed hex, but this is implementation-defined and may change.
Parsing explicitly as hex makes intent clear and avoids silent breakage:-let order_hash = U256::from_str(order_hash)?; +let order_hash = U256::from_hex(order_hash)?;(Requires
use alloy::hex::FromHex
).Please verify tests still pass.
Cargo.toml
Outdated
@@ -53,7 +53,7 @@ test-context = "0.3.0" | |||
portpicker = "0.1.1" | |||
rain-erc = { git = "https://github.com/rainlanguage/rain.erc", rev = "0106e645ebd49334addc698c5aad9a85370eb54d" } | |||
rain-error-decoding = { git = "https://github.com/rainlanguage/rain.error", rev = "72d9577fdaf7135113847027ba951f9a43b41827" } | |||
wasm-bindgen-utils = "0.0.7" | |||
wasm-bindgen-utils = { git = "https://github.com/rainlanguage/rain.wasm", rev = "4379d24a671742a58ec5cdab2c39fecbda59619d" } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
Using git dependency for more precise version control.
Switching from a fixed version to a specific git commit for wasm-bindgen-utils
provides better control over the exact implementation being used. This helps ensure consistency across the workspace, particularly important for WebAssembly bindings.
🏁 Script executed:
#!/bin/bash
# Check if this commit is used in other workspace crates
grep -r "wasm-bindgen-utils.*4379d24a671742a58ec5cdab2c39fecbda59619d" --include="Cargo.toml" ./crates/
Length of output: 103
🏁 Script executed:
#!/bin/bash
# Search all Cargo.toml files in the repository for any reference to wasm-bindgen-utils
rg "wasm-bindgen-utils" --glob Cargo.toml -n .
Length of output: 687
🏁 Script executed:
#!/bin/bash
# Show the section around the git override in the root manifest
sed -n '45,70p' Cargo.toml
# Show the dependency section in the Tauri app manifest
sed -n '30,60p' tauri-app/src-tauri/Cargo.toml
Length of output: 2092
Ensure Tauri app picks up the git‐pinned wasm-bindgen-utils
version
You’ve added wasm-bindgen-utils = { git = …, rev = … }
to the workspace dependencies, but the Tauri app’s WASM target is still locked to version "0.0"
. To have it use the exact commit you specified above, change its dependency to reference the workspace entry:
--- tauri-app/src-tauri/Cargo.toml
@@ [target.'cfg(target_family = "wasm")'.dependencies]
-wasm-bindgen-utils = "0.0"
+wasm-bindgen-utils = { workspace = true }
This ensures the Tauri‐side WASM build also uses the Git commit override you added.
📝 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.
wasm-bindgen-utils = { git = "https://github.com/rainlanguage/rain.wasm", rev = "4379d24a671742a58ec5cdab2c39fecbda59619d" } | |
# tauri-app/src-tauri/Cargo.toml | |
[target.'cfg(target_family = "wasm")'.dependencies] | |
wasm-bindgen-utils = { workspace = true } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are going to update this dependency later on
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@findolor I understand, you're planning to update the wasm-bindgen-utils
dependency in the Tauri app in a future PR. That makes sense to keep the current PR focused on adding Rust tests for the quote wasm bindings. Let me know if you need any assistance with the current implementation.
There was a problem hiding this 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
📜 Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro
⛔ Files ignored due to path filters (1)
Cargo.lock
is excluded by!**/*.lock
📒 Files selected for processing (1)
Cargo.toml
(1 hunks)
🔇 Additional comments (1)
Cargo.toml (1)
56-56
: Approve: Bumpwasm-bindgen-utils
to 0.0.8
This version bump aligns with the unconditional inclusion of WASM utilities across thecrates/quote
andcrates/subgraph
modules.
There seems to be lots of changes to the source as well as adding tests - can this PR be for backfilling only? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added one non-critical suggestion. overall looks good
subgraph_url: String, | ||
rpc_url: String, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: taking &str
is generally preferable over String
unless you need something specific to the String
wrapper. Also, .do_quote
method takes &str
anyway, so it looks like this introduces a little bit of overhead for no reason
There was a problem hiding this 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
📜 Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro
📒 Files selected for processing (3)
crates/js_api/src/gui/state_management.rs
(1 hunks)crates/quote/Cargo.toml
(1 hunks)crates/subgraph/Cargo.toml
(0 hunks)
💤 Files with no reviewable changes (1)
- crates/subgraph/Cargo.toml
🧰 Additional context used
🧠 Learnings (1)
crates/quote/Cargo.toml (1)
Learnt from: findolor
PR: rainlanguage/rain.orderbook#1715
File: crates/js_api/src/common/mod.rs:111-118
Timestamp: 2025-04-30T09:28:36.960Z
Learning: In the rain.orderbook repository, the WASM tests are already properly configured with conditional compilation using `#[cfg(target_family = "wasm")]` and `#[cfg(not(target_family = "wasm"))]`, and don't require additional `wasm_bindgen_test_configure!(run_in_browser)` directives.
⏰ Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: test
🔇 Additional comments (2)
crates/js_api/src/gui/state_management.rs (1)
293-293
: Good practice moving JS imports to the test moduleMoving
use js_sys::{eval, Reflect}
from the main module scope to the test module improves code organization and safety by restricting these potentially unsafe or environment-specific imports to where they're actually used. This change aligns with the PR's goal of improving WASM bindings safety and modularity.crates/quote/Cargo.toml (1)
44-46
:❌ Incorrect review comment
Confirm dev-dependencies for wasm tests
Moving
rain_orderbook_common
andwasm-bindgen-test
into[dev-dependencies]
makes them available across all test targets. Ensure that:
- Your Rust tests remain correctly gated (e.g.,
#[cfg(target_family = "wasm")]
where needed).- No redundant
wasm_bindgen_test_configure!
directives are introduced (per previous learnings, those aren’t necessary).rain_orderbook_common
is only referenced in test code and not accidentally used in production modules.You can verify
rain_orderbook_common
usage with:
🏁 Script executed:
#!/bin/bash # Find occurrences of rain_orderbook_common in this crate rg -n "rain_orderbook_common" -A2 crates/quoteLength of output: 821
🏁 Script executed:
#!/bin/bash set -e echo "Checking for module declarations in lib.rs:" rg -n "mod quote_debug" -A2 crates/quote/src/lib.rs || echo "quote_debug not declared in lib.rs" rg -n "mod order_quotes" -A2 crates/quote/src/lib.rs || echo "order_quotes not declared in lib.rs" echo "" echo "Inspecting the top of quote_debug.rs for cfg attributes:" head -n 20 crates/quote/src/quote_debug.rs echo "" echo "Inspecting the top of order_quotes.rs for cfg attributes:" head -n 20 crates/quote/src/order_quotes.rsLength of output: 1872
rain_orderbook_common must remain a normal dependency
The
rain_orderbook_common
crate is imported by library modules, not just tests, so moving it to[dev-dependencies]
would break your build:
crates/quote/src/quote_debug.rs
(usesrain_orderbook_common::add_order::AddOrderArgs
anddotrain_order::DotrainOrder
)crates/quote/src/order_quotes.rs
(usesrain_orderbook_common::{add_order::AddOrderArgs, dotrain_order::DotrainOrder}
)Dev-dependencies are only for test-only code. Please keep
rain_orderbook_common
under[dependencies]
. Thewasm-bindgen-test
crate can live in[dev-dependencies]
if you actually have#[cfg(target_family = "wasm")]
test modules; otherwise consider removing it.Likely an incorrect or invalid review comment.
@@ -27,21 +27,19 @@ anyhow = { workspace = true } | |||
reqwest = { workspace = true } | |||
clap = { workspace = true, features = ["env"]} | |||
tracing-subscriber = { workspace = true, features = ['env-filter'] } | |||
wasm-bindgen-utils = { workspace = true } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
Review unconditional inclusion of wasm-bindgen-utils
You’ve moved wasm-bindgen-utils
into [dependencies]
, making it compile for all targets. This broadening may introduce unnecessary bloat or break non-wasm builds if unguarded usages exist. Please verify that all calls to this crate are properly protected by #[cfg(target_family = "wasm")]
or consider putting it behind a dedicated Cargo feature (e.g., wasm
) to avoid linking it in non-wasm contexts.
To check for any unguarded imports or macro invocations:
🏁 Script executed:
#!/bin/bash
# Search for unguarded uses of wasm-bindgen-utils in source
rg -n "wasm_bindgen_utils" -A2 crates/quote/src
Length of output: 906
Guard wasm-bindgen-utils
behind a cfg or Cargo feature
You’ve added wasm-bindgen-utils
as an unconditional dependency, but it’s imported unguardedly in several modules:
- crates/quote/src/quote.rs (line 19)
- crates/quote/src/order_quotes.rs (line 13)
- crates/quote/src/js_api/mod.rs (line 15)
- crates/quote/src/error.rs (line 10)
This will bloat non-WASM builds and may break them if any of those imports or macros run on non-wasm targets. To fix this, either:
-
Wrap each import (and any associated code) in:
#[cfg(target_family = "wasm")] use wasm_bindgen_utils::…;
-
Or make it an optional dependency and gate it behind a new feature (e.g.
wasm
):[dependencies] wasm-bindgen-utils = { workspace = true, optional = true } [features] default = [] wasm = ["wasm-bindgen-utils"]
Then wrap your modules or imports with:
#![cfg(feature = "wasm")]
Please update the imports or Cargo.toml as needed to ensure non-WASM targets don’t pull in this crate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we don't need to add this as a feature as we are using this dep in our repo without any issues. As another point sometimes our non wasm targets need this so this needs to stay
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@findolor Thanks for the clarification! I understand now that you're intentionally making wasm-bindgen-utils
an unconditional dependency because:
- You're already using it in the repo without issues
- Some of your non-wasm targets (likely tests) need this dependency too
This aligns with the other changes I noticed in your PR where you're making wasm-related code available for testing purposes. Your approach makes sense - I'll approve this change.
✏️ Learnings added
Learnt from: findolor
PR: rainlanguage/rain.orderbook#1710
File: crates/quote/Cargo.toml:30-30
Timestamp: 2025-05-14T04:49:14.581Z
Learning: In the rain.orderbook repository, wasm-bindgen-utils is intentionally used as an unconditional dependency because non-wasm targets (like tests) sometimes need to use it.
Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.
Motivation
See issue: #1601 and #1635
Solution
Checks
By submitting this for review, I'm confirming I've done the following:
[ ] included screenshots (if this involves a front-end change)fix #1635
Summary by CodeRabbit
Refactor
Tests
Bug Fixes
Chores