Skip to content

Move common js_api file to main js_api directory and add tests #1715

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

Merged
merged 20 commits into from
May 16, 2025

Conversation

findolor
Copy link
Collaborator

@findolor findolor commented Apr 30, 2025

Motivation

See issue: #1621

Solution

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)

fix #1621

Summary by CodeRabbit

  • New Features

    • Added a new WebAssembly (WASM) module for generating calldata for adding and removing orders, now accessible via JavaScript bindings.
    • Exposed new functions for generating add/remove order calldata from JavaScript, with improved error handling and user-friendly error messages.
  • Bug Fixes

    • Improved error handling in the order removal modal to display clear messages if calldata generation fails.
  • Refactor

    • Unified and simplified WASM trait derivations and macro usage across subgraph types for better interoperability.
    • Reorganized test helpers and conditional module imports for improved maintainability.
  • Tests

    • Added comprehensive tests for the new WASM bindings, covering both successful and error scenarios.
    • Updated and migrated tests to reflect the new structure and error handling mechanisms.
    • Simplified and refocused existing test suites to align with the updated API.
  • Chores

    • Updated test data and configuration samples to match new expected formats and values.

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

coderabbitai bot commented Apr 30, 2025

Important

Review skipped

Review was skipped due to path filters

⛔ Files ignored due to path filters (1)
  • tauri-app/src-tauri/Cargo.lock is excluded by !**/*.lock

CodeRabbit blocks several paths by default. You can override this behavior by explicitly including those paths in the path filters. For example, including **/dist/** will override the default block on the dist directory, by removing the pattern from both the lists.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

This change refactors the WASM JavaScript API for generating order calldata by moving it from the common crate to a new common module within the js_api crate. It updates test data and assertions to match new expected values, adjusts module imports and conditional compilation, and expands WASM trait derivations in the subgraph crate. New and updated tests are added for both Rust and JavaScript, ensuring proper error handling and output validation.

Changes

File(s) Change Summary
crates/common/src/js_api/mod.rs Removed the WASM JS API module for order calldata generation, including structs, error types, and async functions for generating add/remove order calldata.
crates/common/src/lib.rs Removed conditional inclusion of the js_api module for WASM; made test_helpers module always included.
crates/js_api/src/common/mod.rs Added new WASM JS API module for order calldata generation, defining AddOrderCalldata, RemoveOrderCalldata, error handling, and async functions for add/remove order calldata. Includes comprehensive Rust tests for both WASM and non-WASM targets.
crates/js_api/src/lib.rs Publicly exposes the new common module for WASM and test targets; restricts gui module to WASM only.
crates/common/src/frontmatter.rs
crates/common/src/test_helpers.rs
Updated test data and assertions: changed scenario binding values from strings to integers, updated test Rainlang script structure and labels. No changes to exported entities.
crates/subgraph/src/types/common.rs Unconditionally derives Tsify for all relevant structs/enums, removes conditional compilation for WASM traits, and moves impl_wasm_traits! macro invocations directly after type definitions for relevant types.
packages/orderbook/test/common/test.test.ts Removed all tests related to getAddOrderCalldata and mock server setup; retained only tests for composing Rainlang code from deployments and scenarios. Adjusted imports and assertion library.
packages/orderbook/test/js_api/common.test.ts Added new test suite for the Rain Orderbook Common Package Bindgen, covering getAddOrderCalldata and getRemoveOrderCalldata with mock server for RPC, error handling, and output validation.
packages/webapp/src/tests/OrderRemoveModal.test.ts
packages/webapp/src/lib/components/OrderRemoveModal.svelte
Updated to handle new return structure from getRemoveOrderCalldata, adding explicit error handling and passing only the value property to the transaction handler. Adjusted tests to match new API and mock setup.

Sequence Diagram(s)

sequenceDiagram
    participant JS as JavaScript (WASM consumer)
    participant WASM as WASM Bindgen (js_api/common)
    participant Parser as Frontmatter Parser
    participant RPC as RPC Client

    JS->>WASM: getAddOrderCalldata(dotrain, deployment)
    WASM->>Parser: parseFrontmatter(dotrain)
    Parser-->>WASM: config
    WASM->>RPC: fetch deployment info (by key)
    RPC-->>WASM: deployment details
    WASM->>WASM: prepare add order args
    WASM->>WASM: generate calldata bytes
    WASM-->>JS: AddOrderCalldata(bytes)

    JS->>WASM: getRemoveOrderCalldata(order)
    WASM->>WASM: prepare remove order args
    WASM->>WASM: generate calldata bytes
    WASM-->>JS: RemoveOrderCalldata(bytes)
Loading

Assessment against linked issues

Objective Addressed Explanation
Wasm bindings need rust tests (#1621)

Possibly related PRs

Suggested reviewers

  • hardyjosh

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.

❤️ Share
🪧 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need 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)

  • @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 generate sequence diagram to generate a sequence diagram of the changes in 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
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: 7

🔭 Outside diff range comments (2)
packages/orderbook/test/common/test.test.ts (2)

77-84: 🧹 Nitpick (assertive)

Trim whitespace before comparison to avoid brittle failures

DotrainOrder.composeDeploymentToRainlang might emit or omit trailing/leading new-lines depending on microscopic changes in the compiler or running OS.
A strict equality check will break the build for purely cosmetic newline differences.

-		assert.equal(result, expected);
+		assert.equal(result.trim(), expected.trim());

Do the same for the second expectation below to keep both tests robust.


97-104: 🧹 Nitpick (assertive)

See the note above – apply .trim() here as well to make the test future-proof.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 9c4c519 and 44912bc.

⛔ 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 (15)
  • Cargo.toml (1 hunks)
  • crates/common/src/frontmatter.rs (1 hunks)
  • crates/common/src/js_api/mod.rs (0 hunks)
  • crates/common/src/lib.rs (1 hunks)
  • crates/common/src/test_helpers.rs (2 hunks)
  • crates/js_api/Cargo.toml (1 hunks)
  • crates/js_api/src/common/mod.rs (1 hunks)
  • crates/js_api/src/gui/state_management.rs (1 hunks)
  • crates/js_api/src/lib.rs (1 hunks)
  • crates/subgraph/Cargo.toml (0 hunks)
  • crates/subgraph/src/types/common.rs (22 hunks)
  • packages/orderbook/test/common/test.test.ts (1 hunks)
  • packages/orderbook/test/js_api/common.test.ts (1 hunks)
  • packages/webapp/src/__tests__/OrderRemoveModal.test.ts (1 hunks)
  • packages/webapp/src/lib/components/OrderRemoveModal.svelte (1 hunks)
💤 Files with no reviewable changes (2)
  • crates/subgraph/Cargo.toml
  • crates/common/src/js_api/mod.rs
🧰 Additional context used
🧠 Learnings (1)
packages/webapp/src/lib/components/OrderRemoveModal.svelte (1)
Learnt from: hardingjam
PR: rainlanguage/rain.orderbook#1512
File: packages/ui-components/src/__tests__/OrderDetail.test.ts:0-0
Timestamp: 2025-04-09T09:28:05.097Z
Learning: The OrderDetail component has been refactored to use an `onRemove` callback approach instead of the previous `handleOrderRemoveModal` pattern for order removal functionality, as part of PR #1512.
🪛 Biome (1.9.4)
packages/orderbook/test/js_api/common.test.ts

[error] 145-145: This let declares a variable that is only assigned once.

'order' is never reassigned.

Safe fix: Use const instead.

(lint/style/useConst)

⏰ Context from checks skipped due to timeout of 90000ms (16)
  • GitHub Check: build-tauri (ubuntu-22.04, true)
  • GitHub Check: git-clean
  • GitHub Check: test
  • GitHub Check: test
  • GitHub Check: test
  • GitHub Check: standard-tests (ubuntu-latest, test-js-bindings)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-test)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-wasm-artifacts)
  • GitHub Check: Deploy-Preview
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-static)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-rs-artifacts, true)
  • GitHub Check: standard-tests (ubuntu-latest, ob-rs-test, true)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-wasm-test)
  • 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-rs-static)
🔇 Additional comments (14)
crates/js_api/src/gui/state_management.rs (1)

291-291: Good move restricting JS interop utilities to test code.

Importing js_sys::{eval, Reflect} only within the test module is a better practice than having it in the main module scope, as it limits the exposure of JS interop to testing code.

crates/common/src/frontmatter.rs (1)

164-164: Test assertions updated to match numeric values.

The test assertions have been correctly updated to reflect the changes in test data, where string values like "value1" and "value2" were replaced with numeric values "10" and "20".

Also applies to: 170-170

Cargo.toml (1)

56-56:

✅ Verification successful

Consider trade-offs of Git dependency versus published crate.

Switching from a versioned crate "0.0.7" to a Git repository with a specific commit hash ensures reproducible builds but may make it harder to track and update dependencies.


🏁 Script executed:

#!/bin/bash
# Check if wasm-bindgen-utils is used in other repos that might be affected by this change

# Look for the package in the workspace
echo "Checking usage of wasm-bindgen-utils in the workspace:"
rg "wasm-bindgen-utils" --type rust

# Check if any other dependencies are using git references
echo -e "\nChecking for other git dependencies in Cargo.toml:"
rg "git =" Cargo.toml

Length of output: 801


No action needed: Git dependency is consistent with other internal crates

This workspace already pulls all internal rainlanguage crates directly from Git (ensuring reproducible builds), including:

  • alloy-ethers-typecast
  • rain-erc
  • rain-error-decoding
  • wasm-bindgen-utils

Switching wasm-bindgen-utils to a Git reference aligns with the existing practice. No changes required.

packages/webapp/src/__tests__/OrderRemoveModal.test.ts (1)

8-10: Mock updated to match the new return type structure.

The mock implementation has been correctly updated to return a structured object with a value property, which aligns with the new WASM bindings return type in getRemoveOrderCalldata.

crates/js_api/Cargo.toml (1)

36-38: Good addition of conditional dev-dependency for testing

Adding httpmock as a dev-dependency only for non-WASM targets is a good approach. This will allow for proper HTTP mocking in tests while keeping the WASM build clean.

crates/js_api/src/lib.rs (1)

1-2: Appropriate conditional compilation for improved testability

Exposing the common module for both WASM and test environments is a good approach. This ensures the module can be properly tested without needing to compile for WASM specifically.

packages/webapp/src/lib/components/OrderRemoveModal.svelte (1)

27-36: Improved error handling for the new API structure

The updated code properly handles the new return structure from getRemoveOrderCalldata, checking for errors before proceeding with the transaction. This is a good improvement that provides clearer error reporting to users.

crates/common/src/test_helpers.rs (1)

60-66: Values changed to numbers – looks good

Bindings key1 and key2 were migrated from strings to numbers and the YAML / front-matter stay in sync with the updated Rust tests.

packages/orderbook/test/js_api/common.test.ts (1)

8-9: Await mockServer.start/stop for deterministic setup & teardown

mockttp returns a Promise from both start and stop. Although Vitest​ handles returned Promises, being explicit avoids foot-guns if the function signature ever changes.

-beforeEach(() => mockServer.start(8080));
-afterEach(() => mockServer.stop());
+beforeEach(async () => { await mockServer.start(8080); });
+afterEach(async () => { await mockServer.stop(); });
crates/subgraph/src/types/common.rs (5)

3-3: LGTM: Unconditional import of wasm_bindgen_utils.

The import is now unconditional rather than being wrapped in a #[cfg(target_family = "wasm")] directive, which aligns with the refactoring effort to centralize WASM-related functionality.


5-5: LGTM: Standardizing Tsify derivation.

The Tsify derive macro is now applied unconditionally to all relevant structs and enums, which ensures consistent TypeScript type definitions. This change supports the centralized WASM binding approach in the js_api crate.

Also applies to: 11-11, 21-21, 29-29, 40-40, 51-51, 60-60, 73-73, 82-82, 103-103, 110-110, 118-118, 127-127, 134-134, 144-144, 155-155, 175-175, 182-182, 191-191, 206-206, 219-219, 235-235, 251-251, 267-267, 284-284, 291-291, 304-304, 310-310, 323-323, 333-333, 340-340, 347-347, 354-354, 361-361, 365-365


101-101: LGTM: Relocated WASM trait implementations.

The impl_wasm_traits! macro invocations for SgOrder and SgVault have been moved from the conditional impls module to appear directly after their respective struct definitions. This improves code readability by keeping related implementations together.

Also applies to: 173-173


338-338: LGTM: New WASM trait implementations for order operations.

New impl_wasm_traits! invocations for SgAddOrder and SgRemoveOrder support the js_api crate's functions for generating order calldata. This enables proper serialization/deserialization when these types cross the Rust/JavaScript boundary.

Also applies to: 345-345


657-679: LGTM: Remaining WASM trait implementations are still conditionally defined.

The module containing additional impl_wasm_traits! invocations is still conditionally compiled only for WASM targets. This is appropriate for types that aren't directly used by the centralized js_api functions but still need WASM interoperability when targeting WASM.

Copy link
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

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 44912bc and 93948c1.

📒 Files selected for processing (2)
  • Cargo.toml (1 hunks)
  • crates/js_api/src/gui/state_management.rs (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (16)
  • GitHub Check: test
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-legal)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-wasm-test)
  • GitHub Check: standard-tests (ubuntu-latest, test-js-bindings)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-artifacts)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-test)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-wasm-artifacts)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-static)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-rs-static)
  • 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: Deploy-Preview
  • GitHub Check: git-clean
  • GitHub Check: test
🔇 Additional comments (1)
crates/js_api/src/gui/state_management.rs (1)

293-293: Restrict js_sys imports to test scope
Great refactor: moving use js_sys::{eval, Reflect}; into the #[cfg(test)] module confines unsafe/interpreter‐specific APIs to tests only, reducing surface for runtime issues in production.

Copy link
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: 2

♻️ Duplicate comments (1)
crates/js_api/src/common/mod.rs (1)

15-22: (Repeat) Optional ergonomic helper still missing

The .0 field access on AddOrderCalldata / RemoveOrderCalldata remains slightly clunky for downstream users. Implementing Deref<Target = Bytes> (or an as_ref() method) would remove that friction.

I’m not reopening the discussion—just documenting that the earlier suggestion hasn’t been adopted.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 93948c1 and a088a83.

📒 Files selected for processing (2)
  • crates/common/src/lib.rs (1 hunks)
  • crates/js_api/src/common/mod.rs (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
crates/js_api/src/common/mod.rs (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.848Z
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 (16)
  • GitHub Check: git-clean
  • GitHub Check: test
  • 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-wasm-artifacts)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-wasm-test)
  • GitHub Check: test
  • GitHub Check: standard-tests (ubuntu-latest, rainix-rs-static)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-artifacts)
  • 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: standard-tests (ubuntu-latest, rainix-sol-test)
  • GitHub Check: build-tauri (ubuntu-22.04, true)
  • GitHub Check: Deploy-Preview
  • GitHub Check: test
🔇 Additional comments (1)
crates/common/src/lib.rs (1)

23-24: Conditional compilation for test_helpers successfully re-instated

Re-applying the #[cfg(test)] gate keeps test-only utilities out of release builds and public docs, addressing the concern raised in the earlier review.

Copy link
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: 2

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between a088a83 and 097a1a4.

📒 Files selected for processing (1)
  • crates/js_api/src/common/mod.rs (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
crates/js_api/src/common/mod.rs (2)
Learnt from: findolor
PR: rainlanguage/rain.orderbook#1715
File: crates/js_api/src/common/mod.rs:111-118
Timestamp: 2025-04-30T09:28:36.848Z
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.
Learnt from: findolor
PR: rainlanguage/rain.orderbook#1715
File: crates/js_api/src/common/mod.rs:101-105
Timestamp: 2025-04-30T10:32:53.857Z
Learning: In the rain.orderbook project, the `get_remove_order_calldata` function takes `SgOrder` by value rather than by reference, as per maintainer preference.
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-rs-static)
  • GitHub Check: standard-tests (ubuntu-latest, test-js-bindings)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-test)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-static)

@hardyjosh hardyjosh requested a review from 0xgleb May 7, 2025 22:51
@hardyjosh
Copy link
Contributor

@findolor i'm a bit confused by this PR - the issue is just about missing tests, but then there are a bunch of other changes

@findolor
Copy link
Collaborator Author

findolor commented May 9, 2025

@hardyjosh while writing the tests, i've also implemented the wasm macro and moved the logic to be inside the js api. that's why there are other changes. otherwise we needed to do a second round of PR to update the tests again with wasm export in mind

Copy link
Collaborator

@0xgleb 0xgleb left a comment

Choose a reason for hiding this comment

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

Would be good to add non-happy path test cases in Rust. Aside from that everything looks good

Copy link
Collaborator

Choose a reason for hiding this comment

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

We should probably have non-happy path tests

Copy link
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: 2

♻️ Duplicate comments (2)
crates/js_api/src/common/mod.rs (2)

11-12: std::ops::Deref import and .deref() call remain unnecessary

deployment_ref is already a &Deployment; invoking .deref() and keeping the Deref import add no extra value and make the call-site slightly noisier. This was pointed out in an earlier review and intentionally left unchanged, so marking as duplicate.

Also applies to: 84-86


70-94: Minor copy optimisation remains but acknowledged

Bytes::copy_from_slice(&calldata) incurs an extra allocation; returning calldata.into() would avoid it. You explicitly preferred the current approach earlier, so not re-raising—just noting for completeness.

Also applies to: 101-105

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 29806a2 and b6fc95b.

⛔ Files ignored due to path filters (1)
  • tauri-app/src-tauri/Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (1)
  • crates/js_api/src/common/mod.rs (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
crates/js_api/src/common/mod.rs (3)
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.
Learnt from: findolor
PR: rainlanguage/rain.orderbook#1744
File: crates/subgraph/src/orderbook_client.rs:44-46
Timestamp: 2025-05-14T05:13:59.637Z
Learning: In the rain.orderbook project, WASM dependencies are intentionally made available in non-WASM targets to facilitate testing of WASM-related functionality, so conditional compilation guards like `#[cfg(target_family = "wasm")]` should not be added to imports or implementations that may be needed for tests.
Learnt from: findolor
PR: rainlanguage/rain.orderbook#1715
File: crates/js_api/src/common/mod.rs:101-105
Timestamp: 2025-04-30T10:32:53.866Z
Learning: In the rain.orderbook project, the `get_remove_order_calldata` function takes `SgOrder` by value rather than by reference, as per maintainer preference.
⏰ Context from checks skipped due to timeout of 90000ms (8)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-legal)
  • GitHub Check: test
  • GitHub Check: standard-tests (ubuntu-latest, ob-rs-test, true)
  • GitHub Check: Deploy-Preview
  • GitHub Check: test
  • GitHub Check: test
  • GitHub Check: git-clean
  • GitHub Check: build-tauri (ubuntu-22.04, true)

@findolor findolor requested a review from 0xgleb May 14, 2025 05:51
Copy link
Collaborator

@0xgleb 0xgleb left a comment

Choose a reason for hiding this comment

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

Copy link
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: 2

♻️ Duplicate comments (3)
crates/js_api/src/common/mod.rs (3)

11-12: Unnecessary Deref import and call

deployment_ref is already a &Deployment; cloning it directly is sufficient.
The extra Deref import and .deref() call add noise without functional gain.

Also applies to: 85-86


55-59: Expose the readable error message to JS callers

Using value.to_string() surfaces the enum variant name, whereas to_readable_msg() was designed for human-friendly text. Forwarding the readable variant improves DX for browser consumers.


75-94: Avoid double cloning of the dotrain string

dotrain.to_string() is invoked twice (lines 79 and 85), allocating two identical Strings.
Taking ownership once and re-using it trims a small allocation and simplifies the code.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between b6fc95b and 1a8dc31.

📒 Files selected for processing (4)
  • crates/js_api/src/common/mod.rs (1 hunks)
  • crates/js_api/src/lib.rs (1 hunks)
  • packages/webapp/src/__tests__/OrderRemoveModal.test.ts (3 hunks)
  • packages/webapp/src/lib/components/OrderRemoveModal.svelte (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
packages/webapp/src/lib/components/OrderRemoveModal.svelte (1)
Learnt from: hardingjam
PR: rainlanguage/rain.orderbook#1512
File: packages/ui-components/src/__tests__/OrderDetail.test.ts:0-0
Timestamp: 2025-04-09T09:28:05.097Z
Learning: The OrderDetail component has been refactored to use an `onRemove` callback approach instead of the previous `handleOrderRemoveModal` pattern for order removal functionality, as part of PR #1512.
crates/js_api/src/common/mod.rs (5)
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.
Learnt from: findolor
PR: rainlanguage/rain.orderbook#1744
File: crates/subgraph/src/orderbook_client.rs:44-46
Timestamp: 2025-05-14T05:13:59.713Z
Learning: In the rain.orderbook project, WASM dependencies are intentionally made available in non-WASM targets to facilitate testing of WASM-related functionality, so conditional compilation guards like `#[cfg(target_family = "wasm")]` should not be added to imports or implementations that may be needed for tests.
Learnt from: findolor
PR: rainlanguage/rain.orderbook#1715
File: crates/js_api/src/common/mod.rs:101-105
Timestamp: 2025-04-30T10:32:53.866Z
Learning: In the rain.orderbook project, the `get_remove_order_calldata` function takes `SgOrder` by value rather than by reference, as per maintainer preference.
Learnt from: findolor
PR: rainlanguage/rain.orderbook#1715
File: crates/js_api/src/common/mod.rs:15-22
Timestamp: 2025-05-14T05:52:04.270Z
Learning: The project doesn't require `#[repr(transparent)]` for newtype wrappers in WASM contexts such as `AddOrderCalldata` and `RemoveOrderCalldata` as the current implementation is working as expected without it.
Learnt from: findolor
PR: rainlanguage/rain.orderbook#1715
File: crates/js_api/src/common/mod.rs:55-59
Timestamp: 2025-05-14T05:51:50.277Z
Learning: In the Rain Orderbook project, the error handling for WASM exported functions uses WasmEncodedError which includes both the original error message and a human-readable version, making direct modification of the JsValue conversion unnecessary.
🔇 Additional comments (1)
crates/js_api/src/lib.rs (1)

3-5: LGTM – makes common available for unit tests

Exposing the new common module behind cfg(any(target_family = "wasm", test)) keeps the WASM bindings accessible in both browser and native test contexts.
No issues spotted.

@findolor findolor enabled auto-merge May 16, 2025 09:15
@findolor findolor merged commit 78ffb36 into main May 16, 2025
17 checks passed
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.

Missing test - Common crate - js_api/mod.rs
3 participants