|
| 1 | +use quasar_svm::{Account, Instruction, Pubkey, QuasarSvm}; |
| 2 | +use solana_address::Address; |
| 3 | + |
| 4 | +use quasar_counter_client::{InitializeCounterInstruction, IncrementInstruction}; |
| 5 | + |
| 6 | +fn setup() -> QuasarSvm { |
| 7 | + let elf = include_bytes!("../target/deploy/quasar_counter.so"); |
| 8 | + QuasarSvm::new().with_program(&Pubkey::from(crate::ID), elf) |
| 9 | +} |
| 10 | + |
| 11 | +fn signer(address: Pubkey) -> Account { |
| 12 | + quasar_svm::token::create_keyed_system_account(&address, 10_000_000_000) |
| 13 | +} |
| 14 | + |
| 15 | +fn empty(address: Pubkey) -> Account { |
| 16 | + Account { |
| 17 | + address, |
| 18 | + lamports: 0, |
| 19 | + data: vec![], |
| 20 | + owner: quasar_svm::system_program::ID, |
| 21 | + executable: false, |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +#[test] |
| 26 | +fn test_initialize_counter() { |
| 27 | + let mut svm = setup(); |
| 28 | + |
| 29 | + let payer = Pubkey::new_unique(); |
| 30 | + let system_program = quasar_svm::system_program::ID; |
| 31 | + |
| 32 | + // Derive the counter PDA from ["counter", payer]. |
| 33 | + let (counter, _) = Pubkey::find_program_address( |
| 34 | + &[b"counter", payer.as_ref()], |
| 35 | + &Pubkey::from(crate::ID), |
| 36 | + ); |
| 37 | + |
| 38 | + let instruction: Instruction = InitializeCounterInstruction { |
| 39 | + payer: Address::from(payer.to_bytes()), |
| 40 | + counter: Address::from(counter.to_bytes()), |
| 41 | + system_program: Address::from(system_program.to_bytes()), |
| 42 | + } |
| 43 | + .into(); |
| 44 | + |
| 45 | + let result = svm.process_instruction( |
| 46 | + &instruction, |
| 47 | + &[signer(payer), empty(counter)], |
| 48 | + ); |
| 49 | + |
| 50 | + result.assert_success(); |
| 51 | + |
| 52 | + // Verify the counter account was created with count = 0. |
| 53 | + let counter_account = result.account(&counter).unwrap(); |
| 54 | + // Data: 1 byte discriminator (1) + 8 bytes u64 (0) |
| 55 | + assert_eq!(counter_account.data.len(), 9); |
| 56 | + assert_eq!(counter_account.data[0], 1); // discriminator |
| 57 | + assert_eq!(&counter_account.data[1..], &[0u8; 8]); // count = 0 |
| 58 | +} |
| 59 | + |
| 60 | +#[test] |
| 61 | +fn test_increment() { |
| 62 | + let mut svm = setup(); |
| 63 | + |
| 64 | + let payer = Pubkey::new_unique(); |
| 65 | + let system_program = quasar_svm::system_program::ID; |
| 66 | + |
| 67 | + // Derive the counter PDA. |
| 68 | + let (counter, _) = Pubkey::find_program_address( |
| 69 | + &[b"counter", payer.as_ref()], |
| 70 | + &Pubkey::from(crate::ID), |
| 71 | + ); |
| 72 | + |
| 73 | + // First, initialise the counter. |
| 74 | + let init_instruction: Instruction = InitializeCounterInstruction { |
| 75 | + payer: Address::from(payer.to_bytes()), |
| 76 | + counter: Address::from(counter.to_bytes()), |
| 77 | + system_program: Address::from(system_program.to_bytes()), |
| 78 | + } |
| 79 | + .into(); |
| 80 | + |
| 81 | + let result = svm.process_instruction( |
| 82 | + &init_instruction, |
| 83 | + &[signer(payer), empty(counter)], |
| 84 | + ); |
| 85 | + result.assert_success(); |
| 86 | + |
| 87 | + // Grab updated accounts after init. |
| 88 | + let counter_after_init = result.account(&counter).unwrap().clone(); |
| 89 | + |
| 90 | + // Increment the counter. |
| 91 | + let increment_instruction: Instruction = IncrementInstruction { |
| 92 | + counter: Address::from(counter.to_bytes()), |
| 93 | + } |
| 94 | + .into(); |
| 95 | + |
| 96 | + let result = svm.process_instruction( |
| 97 | + &increment_instruction, |
| 98 | + &[counter_after_init], |
| 99 | + ); |
| 100 | + result.assert_success(); |
| 101 | + |
| 102 | + // Verify count = 1. |
| 103 | + let counter_account = result.account(&counter).unwrap(); |
| 104 | + let count_bytes: [u8; 8] = counter_account.data[1..9].try_into().unwrap(); |
| 105 | + let count = u64::from_le_bytes(count_bytes); |
| 106 | + assert_eq!(count, 1, "counter should be 1 after one increment"); |
| 107 | +} |
0 commit comments