|
| 1 | +use borsh::BorshDeserialize; |
| 2 | +use counter_solana_native::Counter; |
| 3 | +use litesvm::LiteSVM; |
| 4 | +use solana_instruction::{AccountMeta, Instruction}; |
| 5 | +use solana_keypair::{Keypair, Signer}; |
| 6 | +use solana_native_token::LAMPORTS_PER_SOL; |
| 7 | +use solana_pubkey::Pubkey; |
| 8 | +use solana_rent::Rent; |
| 9 | +use solana_system_interface::instruction::create_account; |
| 10 | +use solana_transaction::Transaction; |
| 11 | + |
| 12 | +#[test] |
| 13 | +fn test_counter() { |
| 14 | + let program_id = Pubkey::new_unique(); |
| 15 | + let program_bytes = include_bytes!("../../../../../target/deploy/counter_solana_native.so"); |
| 16 | + |
| 17 | + let mut svm = LiteSVM::new(); |
| 18 | + svm.add_program(program_id, program_bytes).unwrap(); |
| 19 | + |
| 20 | + let payer = Keypair::new(); |
| 21 | + let counter_account = Keypair::new(); |
| 22 | + |
| 23 | + svm.airdrop(&payer.pubkey(), LAMPORTS_PER_SOL * 10).unwrap(); |
| 24 | + |
| 25 | + let counter_account_size = std::mem::size_of::<Counter>(); |
| 26 | + |
| 27 | + let create_ix = create_account( |
| 28 | + &payer.pubkey(), |
| 29 | + &counter_account.pubkey(), |
| 30 | + Rent::default().minimum_balance(counter_account_size), |
| 31 | + counter_account_size as u64, |
| 32 | + &program_id, |
| 33 | + ); |
| 34 | + |
| 35 | + let tx = Transaction::new_signed_with_payer( |
| 36 | + &[create_ix], |
| 37 | + Some(&payer.pubkey()), |
| 38 | + &[&payer, &counter_account], |
| 39 | + svm.latest_blockhash(), |
| 40 | + ); |
| 41 | + svm.send_transaction(tx).unwrap(); |
| 42 | + |
| 43 | + let ix = Instruction { |
| 44 | + program_id, |
| 45 | + accounts: vec![AccountMeta::new(counter_account.pubkey(), false)], |
| 46 | + data: vec![0], |
| 47 | + }; |
| 48 | + |
| 49 | + let tx = Transaction::new_signed_with_payer( |
| 50 | + &[ix], |
| 51 | + Some(&payer.pubkey()), |
| 52 | + &[payer], |
| 53 | + svm.latest_blockhash(), |
| 54 | + ); |
| 55 | + |
| 56 | + dbg!(&[0].split_at(1)); |
| 57 | + |
| 58 | + let _ = svm.send_transaction(tx).is_ok(); |
| 59 | + |
| 60 | + let counter_account_data = svm.get_account(&counter_account.pubkey()).unwrap().data; |
| 61 | + let couneter = Counter::try_from_slice(&counter_account_data).unwrap(); |
| 62 | + assert_eq!(couneter.count, 1); |
| 63 | +} |
0 commit comments