Skip to main content

Crate portaki_test_utils

Crate portaki_test_utils 

Source
Expand description

In-process test harness for Portaki module unit tests.

§Role in the SDK stack

portaki-test-utils complements portaki_sdk for dev-dependencies in module crates. Production Wasm modules call host functions through portaki_sdk::host (KV, i18n, connectors, repo, …). This crate supplies MockHostFunctions, a portaki_sdk::host::HostBackend that runs entirely in memory on the test thread.

Typical test flow:

  1. Build a portaki_sdk::Context with MockContextBuilder::guest or MockContextBuilder::host.
  2. Seed translations, KV bytes, and connector JSON stubs.
  3. Call MockContextBuilder::run to install the mock backend via portaki_sdk::host::with_host and execute module code.
  4. Assert rendered SDUI with SurfaceAssertions. It walks every primitive of the contract, so a module needs no contains_component_type helper of its own:
use portaki_sdk::sdui::primitives::{Card, EmptyState, Stack};
use portaki_sdk::sdui::surface::Surface;
use portaki_test_utils::SurfaceAssertions;

let surface = Surface::new(Stack::new().child(Card::new()));
let tree = SurfaceAssertions::new(&surface);

// Was: assert!(contains_component_type(&surface, "Card"));
assert!(tree.contains_type("Card"));
assert!(!tree.contains_primitive::<EmptyState>());

§Relationship to portaki-connectors

When module code calls portaki_connectors::OpenWeather::current, the mock host returns JSON registered with MockContextBuilder::with_connector_response. No network or gateway process is required.

§Example

use portaki_sdk::capability::{core, external};
use portaki_test_utils::{MockContext, Property};

MockContext::guest()
    .with_property(Property::default())
    .with_capabilities(&[core::STORAGE, external::OPEN_WEATHER_POOL])
    .with_connector_response(
        "open-weather",
        "current",
        r#"{"main":{"temp":21.5,"humidity":55},"weather":[{"main":"Clear"}]}"#,
    )
    .run(|_ctx| {
        // Module code calling portaki_connectors::OpenWeather::current reads the stub.
    });

§Layout

Modules§

conformance
The conformance battery every Portaki module runs — one call, seven checks.
scenarios
The seven pathological cases the sandbox replays after every deploy, for cargo test.

Macros§

conformance
Generates the conformance tests of the module this integration test belongs to.
link_module
Links the module’s library into this integration test, so its #[surface]s are declared — for a test that names nothing of it, like one that only asserts on the battery’s findings.

Structs§

Booking
Sample booking window for event-handler tests.
ConnectorCall
One host::connectors::call, as the module made it.
GuestIdentityFixture
Guest session fixture convertible to GuestIdentity.
LogLine
One host::log line, as the module wrote it.
MockContextBuilder
Fluent builder for a test Context and MockHostFunctions backend.
MockHostFunctions
Thread-safe in-memory implementation of HostBackend.
Property
Sample rental property for tests.
SurfaceAssertions
Borrowed wrapper for depth-first SDUI tree queries.

Traits§

PrimitiveTag
Associates a type with the Component nodes it matches, for SurfaceAssertions queries.

Functions§

assert_declared_error
Asserts that code is declared by portaki_module!(error = "…") in a module linked into this test binary.
assert_refused
Asserts that result is the refusal code, and that the module declares it.

Type Aliases§

MockContext
Alias for MockContextBuilder — preferred name in module test code.