|
| 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 | + |
| 10 | +#[test] |
| 11 | +fn test_realloc() { |
| 12 | + let mut svm = LiteSVM::new(); |
| 13 | + |
| 14 | + let program_id = Pubkey::new_unique(); |
| 15 | + let program_bytes = include_bytes!("../../../../../target/deploy/realloc_program.so"); |
| 16 | + |
| 17 | + svm.add_program(program_id, program_bytes).unwrap(); |
| 18 | + |
| 19 | + let payer = Keypair::new(); |
| 20 | + svm.airdrop(&payer.pubkey(), LAMPORTS_PER_SOL).unwrap(); |
| 21 | + |
| 22 | + let test_account = Keypair::new(); |
| 23 | + |
| 24 | + let address_info = AddressInfo { |
| 25 | + name: "Jacob".to_string(), |
| 26 | + house_number: 123, |
| 27 | + street: "Main St.".to_string(), |
| 28 | + city: "Chicago".to_string(), |
| 29 | + }; |
| 30 | + |
| 31 | + let data = borsh::to_vec(&ReallocInstruction::Create(address_info)).unwrap(); |
| 32 | + |
| 33 | + let ix = Instruction { |
| 34 | + program_id, |
| 35 | + accounts: vec![ |
| 36 | + AccountMeta::new(test_account.pubkey(), true), |
| 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, &test_account], |
| 47 | + svm.latest_blockhash(), |
| 48 | + ); |
| 49 | + |
| 50 | + let _ = svm.send_transaction(tx).is_ok(); |
| 51 | + |
| 52 | + let data = borsh::to_vec(&ReallocInstruction::ReallocateWithoutZeroInit( |
| 53 | + EnhancedAddressInfoExtender { |
| 54 | + state: "Illinois".to_string(), |
| 55 | + zip: 12345, |
| 56 | + }, |
| 57 | + )) |
| 58 | + .unwrap(); |
| 59 | + |
| 60 | + let ix = Instruction { |
| 61 | + program_id, |
| 62 | + accounts: vec![ |
| 63 | + AccountMeta::new(test_account.pubkey(), false), |
| 64 | + AccountMeta::new(payer.pubkey(), true), |
| 65 | + AccountMeta::new(solana_system_interface::program::ID, false), |
| 66 | + ], |
| 67 | + data, |
| 68 | + }; |
| 69 | + |
| 70 | + let tx = Transaction::new_signed_with_payer( |
| 71 | + &[ix], |
| 72 | + Some(&payer.pubkey()), |
| 73 | + &[&payer], |
| 74 | + svm.latest_blockhash(), |
| 75 | + ); |
| 76 | + |
| 77 | + let _ = svm.send_transaction(tx).is_ok(); |
| 78 | + |
| 79 | + let data = borsh::to_vec(&ReallocInstruction::ReallocateZeroInit(WorkInfo { |
| 80 | + name: "Pete".to_string(), |
| 81 | + position: "Engineer".to_string(), |
| 82 | + company: "Solana Labs".to_string(), |
| 83 | + years_employed: 2, |
| 84 | + })) |
| 85 | + .unwrap(); |
| 86 | + |
| 87 | + let ix = Instruction { |
| 88 | + program_id, |
| 89 | + accounts: vec![AccountMeta::new(test_account.pubkey(), false)], |
| 90 | + data, |
| 91 | + }; |
| 92 | + |
| 93 | + let tx = Transaction::new_signed_with_payer( |
| 94 | + &[ix], |
| 95 | + Some(&payer.pubkey()), |
| 96 | + &[&payer], |
| 97 | + svm.latest_blockhash(), |
| 98 | + ); |
| 99 | + |
| 100 | + let _ = svm.send_transaction(tx).is_ok(); |
| 101 | +} |
0 commit comments