test.rs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. use close_account_native_program::processor::MyInstruction;
  9. #[test]
  10. fn test_close_account() {
  11. let mut svm = LiteSVM::new();
  12. let program_id = Pubkey::new_unique();
  13. let program_bytes = include_bytes!("../../tests/fixtures/close_account_native_program.so");
  14. svm.add_program(program_id, program_bytes).unwrap();
  15. let payer = Keypair::new();
  16. svm.airdrop(&payer.pubkey(), LAMPORTS_PER_SOL * 10).unwrap();
  17. let test_account_pubkey =
  18. Pubkey::find_program_address(&[b"USER".as_ref(), &payer.pubkey().as_ref()], &program_id).0;
  19. // create user ix
  20. let data = borsh::to_vec(&MyInstruction::CreateUser(User {
  21. name: "Jacob".to_string(),
  22. }))
  23. .unwrap();
  24. let ix = Instruction {
  25. program_id,
  26. accounts: vec![
  27. AccountMeta::new(test_account_pubkey, false),
  28. AccountMeta::new(payer.pubkey(), true),
  29. AccountMeta::new(solana_system_interface::program::ID, false),
  30. ],
  31. data,
  32. };
  33. let tx = Transaction::new_signed_with_payer(
  34. &[ix],
  35. Some(&payer.pubkey()),
  36. &[&payer],
  37. svm.latest_blockhash(),
  38. );
  39. let _ = svm.send_transaction(tx).is_ok();
  40. // clsose user ix
  41. let data = borsh::to_vec(&MyInstruction::CloseUser).unwrap();
  42. let ix = Instruction {
  43. program_id,
  44. accounts: vec![
  45. AccountMeta::new(test_account_pubkey, false),
  46. AccountMeta::new(payer.pubkey(), true),
  47. AccountMeta::new(solana_system_interface::program::ID, false),
  48. ],
  49. data,
  50. };
  51. let tx = Transaction::new_signed_with_payer(
  52. &[ix],
  53. Some(&payer.pubkey()),
  54. &[&payer],
  55. svm.latest_blockhash(),
  56. );
  57. let _ = svm.send_transaction(tx).is_ok();
  58. }