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 creator chain ID.
38pub fn mock_application_creator_chain_id(
39    _application_creator_chain_id: impl Into<Option<ChainId>>,
40) {
41    unreachable!("{ERROR_MESSAGE}");
42}
43
44/// Sets the mocked application parameters.
45pub fn mock_application_parameters(_application_parameters: &impl Serialize) {
46    unreachable!("{ERROR_MESSAGE}");
47}
48
49/// Sets the mocked chain balance.
50pub fn mock_chain_balance(_chain_balance: impl Into<Option<Amount>>) {
51    unreachable!("{ERROR_MESSAGE}");
52}
53
54/// Sets the mocked system timestamp.
55pub fn mock_system_timestamp(_system_timestamp: impl Into<Option<Timestamp>>) {
56    unreachable!("{ERROR_MESSAGE}");
57}
58
59/// Returns all messages logged so far.
60pub fn log_messages() -> Vec<(log::Level, String)> {
61    unreachable!("{ERROR_MESSAGE}");
62}
63
64/// Sets the mocked application state.
65pub fn mock_application_state(_state: impl Into<Option<Vec<u8>>>) {
66    unreachable!("{ERROR_MESSAGE}");
67}
68
69/// Initializes and returns a view context for using as the mocked key-value store.
70pub fn mock_key_value_store() -> MemoryContext<()> {
71    unreachable!("{ERROR_MESSAGE}");
72}
73
74/// Mocks the `try_query_application` system API.
75pub fn mock_try_query_application<E>(
76    _handler: impl FnMut(ApplicationId, Vec<u8>) -> Result<Vec<u8>, E> + 'static,
77) where
78    E: ToString + 'static,
79{
80    unreachable!("{ERROR_MESSAGE}");
81}