linera_sdk/test/
mock_stubs.rs

1// Copyright (c) Zefchain Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Stub functions for the API exported to unit tests.
5//!
6//! These can be used by mistake if running unit tests targeting the host architecture, and the
7//! default compiler error of missing functions isn't very helpful. Instead, these allow
8//! compilation to succeed and fails with a more helpful message *if* one of the functions is
9//! called.
10
11use linera_base::{
12    data_types::{Amount, Timestamp},
13    identifiers::{ApplicationId, ChainId},
14};
15use linera_views::context::MemoryContext;
16use serde::Serialize;
17
18/// A helpful error message to explain why the mock API isn't available.
19const ERROR_MESSAGE: &str =
20    "The mock API is only available for unit tests running inside a WebAssembly virtual machine. \
21    Please check that the unit tests are executed with `linera project test` or with \
22    `cargo test --target wasm32-unknown-unknown`. \
23    Also ensure that the unit tests (or the module containing them) has a \
24    `#[cfg(target_arch = \"wasm32-unknown-unknown\")]` attribute so that they don't get compiled \
25    in for the integration tests";
26
27/// Sets the mocked chain ID.
28pub fn mock_chain_id(_chain_id: impl Into<Option<ChainId>>) {
29    unreachable!("{ERROR_MESSAGE}");
30}
31
32/// Sets the mocked application ID.
33pub fn mock_application_id(_application_id: impl Into<Option<ApplicationId>>) {
34    unreachable!("{ERROR_MESSAGE}");
35}
36
37/// Sets the mocked application parameters.
38pub fn mock_application_parameters(_application_parameters: &impl Serialize) {
39    unreachable!("{ERROR_MESSAGE}");
40}
41
42/// Sets the mocked chain balance.
43pub fn mock_chain_balance(_chain_balance: impl Into<Option<Amount>>) {
44    unreachable!("{ERROR_MESSAGE}");
45}
46
47/// Sets the mocked system timestamp.
48pub fn mock_system_timestamp(_system_timestamp: impl Into<Option<Timestamp>>) {
49    unreachable!("{ERROR_MESSAGE}");
50}
51
52/// Returns all messages logged so far.
53pub fn log_messages() -> Vec<(log::Level, String)> {
54    unreachable!("{ERROR_MESSAGE}");
55}
56
57/// Sets the mocked application state.
58pub fn mock_application_state(_state: impl Into<Option<Vec<u8>>>) {
59    unreachable!("{ERROR_MESSAGE}");
60}
61
62/// Initializes and returns a view context for using as the mocked key-value store.
63pub fn mock_key_value_store() -> MemoryContext<()> {
64    unreachable!("{ERROR_MESSAGE}");
65}
66
67/// Mocks the `try_query_application` system API.
68pub fn mock_try_query_application<E>(
69    _handler: impl FnMut(ApplicationId, Vec<u8>) -> Result<Vec<u8>, E> + 'static,
70) where
71    E: ToString + 'static,
72{
73    unreachable!("{ERROR_MESSAGE}");
74}