From 9952124e1ebb58e4e9efd0defa4154bf9db57b92 Mon Sep 17 00:00:00 2001 From: Ebina Perelyn Okoh Date: Mon, 27 Oct 2025 19:24:18 +0100 Subject: [PATCH] add rust litesvm test for close account example --- Cargo.lock | 6 ++ .../close-account/native/program/Cargo.toml | 8 ++ .../native/program/tests/test.rs | 73 +++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 basics/close-account/native/program/tests/test.rs diff --git a/Cargo.lock b/Cargo.lock index f4696c4a..e3f78040 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -876,8 +876,14 @@ version = "0.1.0" dependencies = [ "borsh 1.5.7", "borsh-derive 1.5.7", + "litesvm", + "solana-instruction 3.0.0", + "solana-keypair", + "solana-native-token 3.0.0", "solana-program 3.0.0", + "solana-pubkey 3.0.0", "solana-system-interface 2.0.0", + "solana-transaction", ] [[package]] diff --git a/basics/close-account/native/program/Cargo.toml b/basics/close-account/native/program/Cargo.toml index 419687a7..8122abd2 100644 --- a/basics/close-account/native/program/Cargo.toml +++ b/basics/close-account/native/program/Cargo.toml @@ -19,3 +19,11 @@ custom-panic = [] [lints.rust] unexpected_cfgs = { level = "warn", check-cfg = ['cfg(target_os, values("solana"))'] } + +[dev-dependencies] +litesvm = "0.8.1" +solana-instruction = "3.0.0" +solana-keypair = "3.0.1" +solana-native-token = "3.0.0" +solana-pubkey = "3.0.0" +solana-transaction = "3.0.1" diff --git a/basics/close-account/native/program/tests/test.rs b/basics/close-account/native/program/tests/test.rs new file mode 100644 index 00000000..b4ca5a98 --- /dev/null +++ b/basics/close-account/native/program/tests/test.rs @@ -0,0 +1,73 @@ +use close_account_native_program::state::user::User; +use litesvm::LiteSVM; +use solana_instruction::{AccountMeta, Instruction}; +use solana_keypair::{Keypair, Signer}; +use solana_native_token::LAMPORTS_PER_SOL; +use solana_pubkey::Pubkey; +use solana_transaction::Transaction; + +use close_account_native_program::processor::MyInstruction; + +#[test] +fn test_close_account() { + let mut svm = LiteSVM::new(); + + let program_id = Pubkey::new_unique(); + let program_bytes = + include_bytes!("../../../../../target/deploy/close_account_native_program.so"); + + svm.add_program(program_id, program_bytes).unwrap(); + + let payer = Keypair::new(); + svm.airdrop(&payer.pubkey(), LAMPORTS_PER_SOL * 10).unwrap(); + + let test_account_pubkey = + Pubkey::find_program_address(&[b"USER".as_ref(), &payer.pubkey().as_ref()], &program_id).0; + + // create user ix + let data = borsh::to_vec(&MyInstruction::CreateUser(User { + name: "Jacob".to_string(), + })) + .unwrap(); + + let ix = Instruction { + program_id, + accounts: vec![ + AccountMeta::new(test_account_pubkey, false), + AccountMeta::new(payer.pubkey(), true), + AccountMeta::new(solana_system_interface::program::ID, false), + ], + data, + }; + + let tx = Transaction::new_signed_with_payer( + &[ix], + Some(&payer.pubkey()), + &[&payer], + svm.latest_blockhash(), + ); + + let _ = svm.send_transaction(tx).is_ok(); + + // clsose user ix + let data = borsh::to_vec(&MyInstruction::CloseUser).unwrap(); + + let ix = Instruction { + program_id, + accounts: vec![ + AccountMeta::new(test_account_pubkey, false), + AccountMeta::new(payer.pubkey(), true), + AccountMeta::new(solana_system_interface::program::ID, false), + ], + data, + }; + + let tx = Transaction::new_signed_with_payer( + &[ix], + Some(&payer.pubkey()), + &[&payer], + svm.latest_blockhash(), + ); + + let _ = svm.send_transaction(tx).is_ok(); +}