test.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. use litesvm::LiteSVM;
  2. use realloc_program::state::{AddressInfo, WorkInfo};
  3. use realloc_program::{processor::ReallocInstruction, state::EnhancedAddressInfoExtender};
  4. use solana_instruction::Instruction;
  5. use solana_keypair::{Keypair, Signer};
  6. use solana_native_token::LAMPORTS_PER_SOL;
  7. use solana_pubkey::Pubkey;
  8. use solana_transaction::{AccountMeta, Transaction};
  9. #[test]
  10. fn test_realloc() {
  11. let mut svm = LiteSVM::new();
  12. let program_id = Pubkey::new_unique();
  13. let program_bytes = include_bytes!("../../../../../target/deploy/realloc_program.so");
  14. svm.add_program(program_id, program_bytes).unwrap();
  15. let payer = Keypair::new();
  16. svm.airdrop(&payer.pubkey(), LAMPORTS_PER_SOL).unwrap();
  17. let test_account = Keypair::new();
  18. let address_info = AddressInfo {
  19. name: "Jacob".to_string(),
  20. house_number: 123,
  21. street: "Main St.".to_string(),
  22. city: "Chicago".to_string(),
  23. };
  24. let data = borsh::to_vec(&ReallocInstruction::Create(address_info)).unwrap();
  25. let ix = Instruction {
  26. program_id,
  27. accounts: vec![
  28. AccountMeta::new(test_account.pubkey(), true),
  29. AccountMeta::new(payer.pubkey(), true),
  30. AccountMeta::new(solana_system_interface::program::ID, false),
  31. ],
  32. data,
  33. };
  34. let tx = Transaction::new_signed_with_payer(
  35. &[ix],
  36. Some(&payer.pubkey()),
  37. &[&payer, &test_account],
  38. svm.latest_blockhash(),
  39. );
  40. assert!(svm.send_transaction(tx).is_ok());
  41. let data = borsh::to_vec(&ReallocInstruction::ReallocateWithoutZeroInit(
  42. EnhancedAddressInfoExtender {
  43. state: "Illinois".to_string(),
  44. zip: 12345,
  45. },
  46. ))
  47. .unwrap();
  48. let ix = Instruction {
  49. program_id,
  50. accounts: vec![
  51. AccountMeta::new(test_account.pubkey(), false),
  52. AccountMeta::new(payer.pubkey(), true),
  53. AccountMeta::new(solana_system_interface::program::ID, false),
  54. ],
  55. data,
  56. };
  57. let tx = Transaction::new_signed_with_payer(
  58. &[ix],
  59. Some(&payer.pubkey()),
  60. &[&payer],
  61. svm.latest_blockhash(),
  62. );
  63. assert!(svm.send_transaction(tx).is_ok());
  64. let data = borsh::to_vec(&ReallocInstruction::ReallocateZeroInit(WorkInfo {
  65. name: "Pete".to_string(),
  66. position: "Engineer".to_string(),
  67. company: "Solana Labs".to_string(),
  68. years_employed: 2,
  69. }))
  70. .unwrap();
  71. let ix = Instruction {
  72. program_id,
  73. accounts: vec![AccountMeta::new(test_account.pubkey(), false)],
  74. data,
  75. };
  76. let tx = Transaction::new_signed_with_payer(
  77. &[ix],
  78. Some(&payer.pubkey()),
  79. &[&payer],
  80. svm.latest_blockhash(),
  81. );
  82. assert!(svm.send_transaction(tx).is_ok());
  83. }