Skip to content

feat(shepherd-sdk-test): in-memory host mocks (BLEU-841)#15

Open
brunota20 wants to merge 1 commit into
feat/m2-modules-on-sdk-bleu-843from
feat/shepherd-sdk-test-bleu-841
Open

feat(shepherd-sdk-test): in-memory host mocks (BLEU-841)#15
brunota20 wants to merge 1 commit into
feat/m2-modules-on-sdk-bleu-843from
feat/shepherd-sdk-test-bleu-841

Conversation

@brunota20

Copy link
Copy Markdown
Collaborator

Summary

Two-part deliverable:

1. Host traits in `shepherd_sdk::host`

The seam between strategy logic and the per-cdylib wit-bindgen shims:

Trait Mirrors Methods
`ChainHost` `nexum:host/chain` `request(chain_id, method, params)`
`LocalStoreHost` `nexum:host/local-store` `get` / `set` / `delete` / `list_keys`
`CowApiHost` `shepherd:cow/cow-api` `submit_order(chain_id, body)`
`LoggingHost` `nexum:host/logging` `log(level, message)`
`Host` supertrait blanket impl over the four

Rides on a host-neutral `HostError` (same field shape as wit-bindgen's), with `HostErrorKind` and `LogLevel` mirroring the WIT enums. Modules bridge to their own wit-bindgen `HostError` with one-liner `From` impls each direction — the BLEU-848 tutorial documents the adapter pattern.

2. `shepherd-sdk-test` crate (dev-only)

In-memory implementations + assertion helpers:

  • `MockChain`: programmable `(method, params)` → result map; records every call (chain_id, method, params); `last_call` / `call_count` accessors.
  • `MockLocalStore`: HashMap-backed; `list_keys` prefix scan with sorted output.
  • `MockCowApi`: single programmable response; records each submission's body; `last_body_as_json` convenience parse.
  • `MockLogging`: buffers lines + level; `contains` / `count_at` helpers.
  • `MockHost`: composes all four; impls `Host` (and the four component traits) so a strategy taking `&impl Host` runs against it directly.

Unconfigured calls return `HostErrorKind::Unsupported` so a forgotten programming step fails the test fast rather than silently passing on a default.

Adoption

Opt-in. Existing M2 modules keep their pure-function tests for now. BLEU-848 (tutorial) demonstrates the strategy-takes-`Host` pattern with `MockHost` end-to-end.

Stacks on #14 (BLEU-843 module refactor).

Linear: BLEU-841.

Test plan

  • `cargo test -p shepherd-sdk-test` — 8 host tests + 1 module-level doctest.
  • `cargo check --workspace` clean.
  • `cargo check --target wasm32-wasip2 -p shepherd-sdk -p twap-monitor -p ethflow-watcher` clean (mocks not pulled into wasm bundle).
  • `cargo clippy -p shepherd-sdk -p shepherd-sdk-test --tests -- -Dwarnings` clean.

…841)

Two-part deliverable:

1. New `shepherd_sdk::host` module exposing the trait seam between
   strategy logic and the wit-bindgen shims a module generates per-
   cdylib:

   - `ChainHost`     — request(chain_id, method, params)
   - `LocalStoreHost`— get / set / delete / list_keys
   - `CowApiHost`    — submit_order(chain_id, body)
   - `LoggingHost`   — log(level, message)
   - `Host`          — supertrait bundling all four (blanket impl
                       so callers only need the supertrait bound)

   The traits ride on a host-neutral `HostError` (same field shape
   as wit-bindgen's), with `HostErrorKind` and `LogLevel` mirroring
   the WIT enums verbatim. Modules bridge their own wit-bindgen
   `HostError` to the SDK's with a one-liner `From` impl on each
   side; the M3 tutorial (BLEU-848) documents the adapter pattern.

2. New `shepherd-sdk-test` crate (dev-only, host-only) supplying
   in-memory implementations for every trait + assertion helpers:

   - `MockHost { chain, store, cow_api, logging }`
   - `MockChain`: programmable `(method, params)` -> result map;
     records every call with `chain_id`, `method`, `params`.
   - `MockLocalStore`: HashMap-backed; `list_keys` does a prefix
     scan (sorted output for stable assertions).
   - `MockCowApi`: single programmable response shared across
     calls; records each submission's `chain_id` + body bytes;
     `last_body_as_json` helper for inline assertions.
   - `MockLogging`: buffers all lines with their level; `contains`
     / `count_at` helpers.

   Unconfigured calls return `HostErrorKind::Unsupported` so an
   unprogrammed test fails fast instead of silently passing on a
   default value.

Tests: 8 host tests on `shepherd-sdk-test` + 1 module-level doctest
locking the recommended usage pattern. Workspace + wasm32-wasip2
check still clean.

Adoption is opt-in: existing M2 modules keep their pure-function
tests for now. BLEU-848 (tutorial) will demonstrate the new
strategy-takes-Host pattern with `MockHost` end-to-end.
@linear-code

linear-code Bot commented Jun 17, 2026

Copy link
Copy Markdown

BLEU-841

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