|
| 1 | +use close_account_native_program::state::user::User; |
| 2 | +use litesvm::LiteSVM; |
| 3 | +use solana_instruction::{AccountMeta, Instruction}; |
| 4 | +use solana_keypair::{Keypair, Signer}; |
| 5 | +use solana_native_token::LAMPORTS_PER_SOL; |
| 6 | +use solana_pubkey::Pubkey; |
| 7 | +use solana_transaction::Transaction; |
| 8 | + |
| 9 | +use close_account_native_program::processor::MyInstruction; |
| 10 | + |
| 11 | +#[test] |
| 12 | +fn test_close_account() { |
| 13 | + let mut svm = LiteSVM::new(); |
| 14 | + |
| 15 | + let program_id = Pubkey::new_unique(); |
| 16 | + let program_bytes = |
| 17 | + include_bytes!("../../../../../target/deploy/close_account_native_program.so"); |
| 18 | + |
| 19 | + svm.add_program(program_id, program_bytes).unwrap(); |
| 20 | + |
| 21 | + let payer = Keypair::new(); |
| 22 | + svm.airdrop(&payer.pubkey(), LAMPORTS_PER_SOL * 10).unwrap(); |
| 23 | + |
| 24 | + let test_account_pubkey = |
| 25 | + Pubkey::find_program_address(&[b"USER".as_ref(), &payer.pubkey().as_ref()], &program_id).0; |
| 26 | + |
| 27 | + // create user ix |
| 28 | + let data = borsh::to_vec(&MyInstruction::CreateUser(User { |
| 29 | + name: "Jacob".to_string(), |
| 30 | + })) |
| 31 | + .unwrap(); |
| 32 | + |
| 33 | + let ix = Instruction { |
| 34 | + program_id, |
| 35 | + accounts: vec![ |
| 36 | + AccountMeta::new(test_account_pubkey, false), |
| 37 | + AccountMeta::new(payer.pubkey(), true), |
| 38 | + AccountMeta::new(solana_system_interface::program::ID, false), |
| 39 | + ], |
| 40 | + data, |
| 41 | + }; |
| 42 | + |
| 43 | + let tx = Transaction::new_signed_with_payer( |
| 44 | + &[ix], |
| 45 | + Some(&payer.pubkey()), |
| 46 | + &[&payer], |
| 47 | + svm.latest_blockhash(), |
| 48 | + ); |
| 49 | + |
| 50 | + let _ = svm.send_transaction(tx).is_ok(); |
| 51 | + |
| 52 | + // clsose user ix |
| 53 | + let data = borsh::to_vec(&MyInstruction::CloseUser).unwrap(); |
| 54 | + |
| 55 | + let ix = Instruction { |
| 56 | + program_id, |
| 57 | + accounts: vec![ |
| 58 | + AccountMeta::new(test_account_pubkey, false), |
| 59 | + AccountMeta::new(payer.pubkey(), true), |
| 60 | + AccountMeta::new(solana_system_interface::program::ID, false), |
| 61 | + ], |
| 62 | + data, |
| 63 | + }; |
| 64 | + |
| 65 | + let tx = Transaction::new_signed_with_payer( |
| 66 | + &[ix], |
| 67 | + Some(&payer.pubkey()), |
| 68 | + &[&payer], |
| 69 | + svm.latest_blockhash(), |
| 70 | + ); |
| 71 | + |
| 72 | + let _ = svm.send_transaction(tx).is_ok(); |
| 73 | +} |
0 commit comments