From 2e64d9edb09bc7711492e7927e0deb0dccc7fbb2 Mon Sep 17 00:00:00 2001 From: akorchyn Date: Tue, 3 Sep 2024 17:51:39 +0300 Subject: [PATCH] questionable test --- .../src/environment/mock/mocked_blockchain.rs | 64 ++++++++++++++++++- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/near-sdk/src/environment/mock/mocked_blockchain.rs b/near-sdk/src/environment/mock/mocked_blockchain.rs index 135a1bc58..34d8bafc2 100644 --- a/near-sdk/src/environment/mock/mocked_blockchain.rs +++ b/near-sdk/src/environment/mock/mocked_blockchain.rs @@ -33,10 +33,10 @@ where pub fn test_vm_config() -> near_parameters::vm::Config { let store = RuntimeConfigStore::test(); - let config = store.get_config(PROTOCOL_VERSION).wasm_config.clone(); + let config = store.get_config(PROTOCOL_VERSION).wasm_config.as_ref().to_owned(); near_parameters::vm::Config { vm_kind: config.vm_kind.replace_with_wasmtime_if_unsupported(), - ..config.as_ref().to_owned() + ..config } } @@ -188,6 +188,7 @@ fn sdk_context_to_vm_context( #[cfg(not(target_arch = "wasm32"))] mod mock_chain { use near_vm_runner::logic::{errors::VMLogicError, VMLogic}; + fn with_mock_interface(f: F) -> R where F: FnOnce(&mut VMLogic) -> Result, @@ -601,3 +602,62 @@ mod mock_chain { with_mock_interface(|b| b.alt_bn128_pairing_check(value_len, value_ptr)) } } + +#[cfg(test)] +mod tests { + + use near_gas::NearGas; + use near_primitives::types::GasWeight; + + use crate::{ + env, + test_utils::{accounts, get_created_receipts, get_logs}, + testing_env, + }; + + use super::*; + + #[test] + fn test_mocked_blockchain_api() { + let public_key: crate::types::PublicKey = + "ed25519:H3C2AVAWKq5Qm7FkyDB5cHKcYKHgbiiB2BzX8DQX8CJ".parse().unwrap(); + let context = VMContextBuilder::new() + .signer_account_id(accounts(0)) + .signer_account_pk(public_key.clone()) + .build(); + + testing_env!(context.clone()); + assert_eq!(env::signer_account_id(), accounts(0)); + assert_eq!(env::signer_account_pk(), public_key); + + env::storage_write(b"smile", b"hello_worlds"); + assert_eq!(env::storage_read(b"smile").unwrap(), b"hello_worlds"); + assert!(env::storage_has_key(b"smile")); + env::storage_remove(b"smile"); + assert!(!env::storage_has_key(b"smile")); + + assert_eq!(env::promise_results_count(), 1); + + env::log_str("logged"); + + let logs = get_logs(); + assert_eq!(logs.len(), 1); + assert_eq!(logs[0], "logged"); + + let actions = get_created_receipts(); + assert_eq!(actions.len(), 1); + assert_eq!(actions[0].receiver_id, accounts(0)); + assert_eq!(actions[0].actions.len(), 1); + assert_eq!( + actions[0].actions[0], + MockAction::FunctionCallWeight { + receipt_index: 0, + method_name: b"hehe".to_vec(), + args: [].to_vec(), + attached_deposit: NearToken::from_millinear(1), + prepaid_gas: NearGas::from_tgas(1), + gas_weight: GasWeight(0) + } + ); + } +}