Skip to content

Feat/mock platform#258

Draft
decrypto21 wants to merge 7 commits into
worktree-issue-96-rust-core-portfrom
feat/mock-platform
Draft

Feat/mock platform#258
decrypto21 wants to merge 7 commits into
worktree-issue-96-rust-core-portfrom
feat/mock-platform

Conversation

@decrypto21

Copy link
Copy Markdown

Summary

Adds a mock host that swaps the platform seam while running the real Rust core. Tests and demos exercise the actual truapi-server dispatcher, account management, permission service, signing orchestration, and SCALE wire — with device/wallet capabilities supplied by an in-memory, config-driven double instead of a real device or paired wallet.

Why "swap the seam, keep the core"

TrUAPI is one core with two seams: the platform seam (truapi-platform capability traits — the OS/device primitives the core delegates) and the wallet seam (keys in a paired wallet over SSO). The mock replaces only the platform seam. Because every request still flows through the real core, the mock is faithful by construction — it can't drift from real host semantics the way a hand-rolled fake would.

What this delivers

truapi-platformMockPlatform (behind the mock feature, dev-only)

  • MockConfig drives behavior: permission policies (AllowAll / DenyAll), chain behaviors (Silent / Scripted / Closed / ConnectError), confirmation control (ConfirmKind), and fault injection (MockFaults: storage / navigation / preimage / …).
  • Implements every capability trait (ProductStorage, CoreStorage, Navigation, Notifications, Permissions, Features, ChainProvider / JsonRpcConnection, AuthPresenter, UserConfirmation, ThemeHost, PreimageHost) against the core's async_trait surface and truapi::latest types.

truapi-server — through-core proof

  • Tests build the real core from a MockPlatform and assert on the SCALE wire envelopes the dispatcher emits: feature-support dispatch, storage round-trip, device-permission policy (DenyAllgranted: false), preimage confirm-gate, and storage-fault surfacing.

truapi-host-wasmcreateMockHost

  • In-memory HostCallbacks for browser / Web Worker host tests, a headless WASM-bridge test, and CI wiring.

Proven, not asserted

The mock is validated through the real core, never in isolation: the truapi-server tests decode the dispatcher's actual wire output, so a mock that diverged from real host semantics would fail them.

Scope & safety

  • The mock feature is a dev-dependency only — it never enters the default (production WASM) build.
  • No change to real host behavior or the protocol surface.

Stacked on

#104 (Rust-core port). This branch rebases onto #104's current head — review / merge #104 first.

Status

Area State
truapi-platform (MockPlatform) ✅ green — 22 mock unit tests, clippy/fmt clean
truapi-server (through-core) ✅ green — 214 tests incl. 5 through-core
truapi-host-wasm (createMockHost) 🚧 finalizing against #104's reworked worker callbacks + WASM rebuild

Test plan

cargo test -p truapi-platform --features mock
cargo test -p truapi-server --lib
cargo clippy --workspace --all-targets --all-features -- -D warnings
cargo +nightly fmt --check

Tracking

Roadmap and follow-ups under #257.

Add an in-memory MockPlatform implementing all 11 platform capability
traits so it satisfies the blanket Platform: namespaced product/core
storage, per-capability permission policy (device/remote), feature and
theme config, recorded navigation/notifications/confirmations/auth-state
transitions, configurable chain behavior (silent/scripted/connect-error),
fault injection, monotonic notification ids, and a preimage round-trip.

Gated behind a new `mock` cargo feature, so it stays out of the default
build. 21 unit tests; clippy -D warnings and nightly fmt clean.
Enable truapi-platform's `mock` feature for truapi-server test builds via
dev-dependencies, so it stays out of the default (production WASM) build,
and add a core dispatch test that builds TrUApiCore on MockPlatform and
feeds a request frame through receive_from_product. Asserts a configured
feature-supported answer flows through the real dispatcher to the wire,
proving the mock is faithful by construction, not merely trait-complete.
Add createMockHost: a complete, config-driven set of mock HostCallbacks
(the JS sibling of truapi-platform's MockPlatform) plus recordings for
assertions. Handed to createWebWorkerProvider/createIframeHost, it runs
the real truapi-server WASM core against a mocked OS seam: namespaced
in-memory storage, per-capability permission policy, feature/theme
config, recorded navigation/notifications/auth-state, a silent-or-
scripted chain connection, and a preimage round-trip. mockRuntimeConfig
supplies a default HostCoreRuntimeConfig.

The silent chain records outbound requests and never answers, so
login/signing park; storage, permissions, features, theme, navigation,
notifications, and preimage work without a wallet. Promote neverthrow to
a runtime dependency (used by the new source). 9 tests cover callback
behavior and provider init via a fake worker (no real WASM needed).
…in, JS oracle parity

Resolve the senior-review findings:

- Major (coverage): add MockPlatform->core tests that drive product storage
  (write/read/clear round-trip), a DenyAll device-permission (granted:false
  through the real permission service + wire), and preimage submit (auto-confirm
  Ok vs confirm=false Err) through the production dispatcher — proving the seam
  end-to-end, not just in the mock's own unit tests.
- Minor (fail-fast chain): add ChainBehavior::Closed (response stream ends at
  once) so disconnect/timeout paths can be asserted instead of parking like
  Silent; document Silent's park + the timeout guidance. Mirror on the JS side
  with a chainClosed option.
- Minor (JS oracle parity): record confirmations and cancelled notifications in
  createMockHost, with confirmations()/cancelledNotifications() accessors, so the
  web sibling can assert what the Rust mock can. Note the JS key scheme is
  internal and independent from the Rust one by design.
Add a bun test that drives the real truapi-server WASM core with
createMockHost's callbacks (no browser, no worker): it instantiates the
WASM, constructs WasmHostCore with the mock callbacks, and asserts the
core invokes them across the JS↔SCALE↔WASM boundary (the core's startup
session read reaches the mock's readCoreStorage with a decoded key). The
test skips when the WASM artifact is absent, so a plain `bun test` on a
fresh checkout stays green; CI builds the WASM and runs it. No new deps.

Document the mock surface: truapi-platform README gains a mock-feature
section and its trait list is corrected to the full eleven capabilities;
truapi-host-wasm README gains a createMockHost section.
…ment

Address the final-review findings:
- Add a `host-wasm` CI job that downloads the codegen output, installs
  wasm-pack, builds the WASM, and runs `bun test` — so the createMockHost
  and WASM-bridge tests gate against regressions. The codegen job already
  uploaded the host-wasm generated artifact but nothing consumed it. Correct
  the wasm-bridge test comment to reference that job (it previously claimed
  CI ran it, which was not yet true).
- Add a through-core test proving a MockPlatform storage fault surfaces as a
  wire Err envelope through the real dispatcher.
- Align MockPlatform::lookup_preimage to a stay-open subscription (emit then
  pending), matching subscribe_theme, the trait doc, and the JS mock.
Adapt the mock platform to #104's async_trait capability traits and
truapi::latest types: add #[async_trait] to each capability impl, flatten
feature_supported to bare v01 request/response types, update
CoreStorageKey::PermissionAuthorization to { product_id, request }, and add
JsonRpcConnection::close.

Fix the through-core preimage + storage-fault tests to assert the Result
discriminant at wire byte 1: the versioned envelope is
[version_index, result_discriminant, ..inner], and V1's version index is 0,
so the Ok/Err distinction lives at byte 1, not byte 0.
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