use light_token::instruction::{
derive_associated_token_account, get_associated_token_address,
get_associated_token_address_and_bump, get_spl_interface_pda_and_bump, LIGHT_TOKEN_PROGRAM_ID,
};
use solana_pubkey::Pubkey;
#[test]
fn test_derive_ata_single_owner_mint() {
let owner = Pubkey::new_unique();
let mint = Pubkey::new_unique();
let ata = derive_associated_token_account(&owner, &mint);
let (recreated_ata, _recreated_bump) = Pubkey::find_program_address(
&[
owner.as_ref(),
LIGHT_TOKEN_PROGRAM_ID.as_ref(),
mint.as_ref(),
],
&LIGHT_TOKEN_PROGRAM_ID,
);
assert_eq!(ata, recreated_ata);
assert_ne!(ata, owner);
assert_ne!(ata, mint);
assert_ne!(ata, LIGHT_TOKEN_PROGRAM_ID);
}
#[test]
fn test_derive_ata_different_owners_different_result() {
let owner1 = Pubkey::new_unique();
let owner2 = Pubkey::new_unique();
let mint = Pubkey::new_unique();
let ata1 = derive_associated_token_account(&owner1, &mint);
let ata2 = derive_associated_token_account(&owner2, &mint);
assert_ne!(
ata1, ata2,
"Different owners should produce different ATAs for the same mint"
);
}
#[test]
fn test_derive_ata_different_mints_different_result() {
let owner = Pubkey::new_unique();
let mint1 = Pubkey::new_unique();
let mint2 = Pubkey::new_unique();
let ata1 = derive_associated_token_account(&owner, &mint1);
let ata2 = derive_associated_token_account(&owner, &mint2);
assert_ne!(
ata1, ata2,
"Different mints should produce different ATAs for the same owner"
);
}
#[test]
fn test_derive_ata_consistency() {
let owner = Pubkey::new_unique();
let mint = Pubkey::new_unique();
let ata1 = derive_associated_token_account(&owner, &mint);
let ata2 = derive_associated_token_account(&owner, &mint);
let ata3 = derive_associated_token_account(&owner, &mint);
assert_eq!(ata1, ata2);
assert_eq!(ata2, ata3);
}
#[test]
fn test_spl_interface_pda_derivation() {
let mint = Pubkey::new_unique();
let (pda, bump) = get_spl_interface_pda_and_bump(&mint);
let pool_seed: &[u8] = b"pool";
let (recreated_pda, recreated_bump) =
Pubkey::find_program_address(&[pool_seed, mint.as_ref()], &LIGHT_TOKEN_PROGRAM_ID);
assert_eq!(pda, recreated_pda);
assert_eq!(bump, recreated_bump);
assert_ne!(pda, mint);
assert_ne!(pda, LIGHT_TOKEN_PROGRAM_ID);
}
#[test]
fn test_spl_interface_pda_different_mints() {
let mint1 = Pubkey::new_unique();
let mint2 = Pubkey::new_unique();
let (pda1, _bump1) = get_spl_interface_pda_and_bump(&mint1);
let (pda2, _bump2) = get_spl_interface_pda_and_bump(&mint2);
assert_ne!(
pda1, pda2,
"Different mints should produce different SPL interface PDAs"
);
}
#[test]
fn test_spl_interface_pda_consistency() {
let mint = Pubkey::new_unique();
let (pda1, bump1) = get_spl_interface_pda_and_bump(&mint);
let (pda2, bump2) = get_spl_interface_pda_and_bump(&mint);
assert_eq!(pda1, pda2, "Same mint should always produce same PDA");
assert_eq!(bump1, bump2, "Same mint should always produce same bump");
}
#[test]
fn test_get_associated_token_address_matches_derive() {
let owner = Pubkey::new_unique();
let mint = Pubkey::new_unique();
let ata = get_associated_token_address(&owner, &mint);
let (ata_with_bump, _bump) = get_associated_token_address_and_bump(&owner, &mint);
let derived_ata = derive_associated_token_account(&owner, &mint);
assert_eq!(
ata, ata_with_bump,
"get_associated_token_address should match get_associated_token_address_and_bump"
);
assert_eq!(
ata, derived_ata,
"get_associated_token_address should match derive_associated_token_account"
);
}
#[test]
fn test_derive_ata_seed_order() {
let owner = Pubkey::new_from_array([1u8; 32]);
let mint = Pubkey::new_from_array([2u8; 32]);
let ata = derive_associated_token_account(&owner, &mint);
let (ata_swapped, _) = Pubkey::find_program_address(
&[
mint.as_ref(),
LIGHT_TOKEN_PROGRAM_ID.as_ref(),
owner.as_ref(),
],
&LIGHT_TOKEN_PROGRAM_ID,
);
assert_ne!(ata, ata_swapped, "Seed order should affect the derived PDA");
}