Skip to content

refactor(price-alert): port to Host trait + MockHost tests (BLEU-851)#22

Open
brunota20 wants to merge 1 commit into
chore/rust-idiomatic-passfrom
feat/price-alert-host-trait-bleu-851
Open

refactor(price-alert): port to Host trait + MockHost tests (BLEU-851)#22
brunota20 wants to merge 1 commit into
chore/rust-idiomatic-passfrom
feat/price-alert-host-trait-bleu-851

Conversation

@brunota20

Copy link
Copy Markdown
Collaborator

Summary

Closes the loop opened by BLEU-841 (mock host) + BLEU-846 (example) + BLEU-848 (tutorial). The price-alert module now matches the recipe the tutorial recommends.

```
modules/examples/price-alert/
├── Cargo.toml + shepherd-sdk-test as dev-dep
└── src/
├── lib.rs wit_bindgen::generate! + WitBindgenHost
│ adapter + From conversions + Guest impl
└── strategy.rs pure logic against &impl Host
+ parse_config + scale_threshold + tests
```

What changed

  • Strategy logic moved to strategy.rs and takes &impl shepherd_sdk::host::Host. No direct `nexum::host::*` calls in there.
  • lib.rs carries the wit-bindgen adapter boilerplate (will be folded into a declarative macro in shepherd-sdk in a future cleanup).
  • `parse_config` returns `Result<Settings, shepherd_sdk::host::HostError>` instead of `Result<_, String>`. Carries domain / kind / code / data through to the operator.
  • 16 host tests (was 11). 5 of them run `on_block` against `MockHost` end-to-end (idle, triggers-below, triggers-above, RPC error, decode error, throttle).

Numbers

Before After
price-alert LOC 354 593 (strategy 458 + lib 135)
Tests 11 (pure-fn) 16 (5 new MockHost)
.wasm 206 KB 210 KB

The .wasm grows by 4 KB because of the adapter boilerplate; alloy + shepherd-sdk dedupe so future modules pay zero extra.

Stacks on #21 (rust-idiomatic pass).

Linear: BLEU-851.

Test plan

  • `cargo test -p price-alert` — 16 host tests.
  • `cargo clippy --all-targets --workspace -- -D warnings` clean.
  • `cargo build --target wasm32-wasip2 --release -p price-alert` — 210 KB.

Validates the host-trait pattern from the M3 tutorial end-to-end on
a real module. The price-alert example now matches the recipe the
tutorial recommends:

  modules/examples/price-alert/
  ├── Cargo.toml          adds shepherd-sdk-test as dev-dep
  └── src/
      ├── lib.rs          wit_bindgen::generate! + WitBindgenHost
      │                   adapter + From conversions + Guest impl
      └── strategy.rs     pure logic against `&impl Host`
                          + parse_config + scale_threshold + tests

Strategy logic now takes `&impl shepherd_sdk::host::Host` and never
calls `nexum::host::*` free functions directly. The wit-bindgen
boilerplate (WitBindgenHost struct, ChainHost / LocalStoreHost /
CowApiHost / LoggingHost impls, convert_err / sdk_err_into_wit /
convert_level helpers) lives in lib.rs - mechanical and identical
across modules, a future declarative macro in shepherd-sdk will
elide it.

parse_config now returns `Result<Settings, shepherd_sdk::host::
HostError>` instead of `Result<T, String>`. Carrying the SDK error
through the strategy / adapter / Guest seam means the same domain /
kind / code / message / data fields surface to the operator
verbatim.

Tests: 16 (was 11) - all strategy tests now run against
shepherd_sdk_test::MockHost rather than calling wit-bindgen
directly. The 5 new ones lock the on_block behaviour end-to-end:

  - idle when price is on the safe side of the trigger
  - triggers below threshold (Direction::Below)
  - triggers above threshold (Direction::Above)
  - warns + continues on RPC timeout (no propagation into the
    supervisor)
  - warns on undecodable oracle response
  - respects `every_n_blocks` throttle

cargo clippy --all-targets --workspace -- -D warnings clean. .wasm
210 KB (was 206 KB; +4 KB for the adapter boilerplate, which
deduplicates against shepherd-sdk so future modules add no extra
cost).
@linear-code

linear-code Bot commented Jun 17, 2026

Copy link
Copy Markdown

BLEU-851

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.

1 participant