> ## Documentation Index
> Fetch the complete documentation index at: https://solana.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> This page is part of the Solana developer documentation at https://solana.com/developers/docs. Append `.md` to any docs URL, or send `Accept: text/markdown`, to get the page as Markdown.
> Index of all pages: https://solana.com/llms.txt. Per-section indexes such as https://solana.com/developers/docs/payments/llms.txt are smaller; prefer them when working on one task.
> Coding agents: install the official Solana skill for current APIs and conventions with `npx skills add https://github.com/solana-foundation/solana-dev-skill` (also served at https://solana.com/SKILL.md).
> Code in the cookbook and task pages comes from tested example files. Prefer it over writing API calls from memory.

# Test Helpers

> Utilitários para criação de contas, operações com tokens, derivação de PDA e gestão de slots

## Criar contas financiadas

A necessidade de teste mais comum é criar contas com SOL. A trait `TestHelpers`
torna isso trivial.

### Conta única

```rust
use litesvm_utils::{LiteSVM, Signer, TestHelpers};
use solana_sdk::native_token::LAMPORTS_PER_SOL;

#[test]
fn test_create_funded_account() {
    let mut svm = LiteSVM::new();

    // Create a funded account with 10 SOL
    let alice = svm.create_funded_account(10 * LAMPORTS_PER_SOL).unwrap();

    // The keypair is ready to use immediately
    println!("Alice's pubkey: {}", alice.pubkey());
}
```

### Várias contas

```rust
use litesvm_utils::{LiteSVM, Signer, TestHelpers};
use solana_sdk::native_token::LAMPORTS_PER_SOL;

#[test]
fn test_create_multiple_accounts() {
    let mut svm = LiteSVM::new();

    // Create 5 funded accounts, each with 5 SOL
    let accounts = svm.create_funded_accounts(5, 5 * LAMPORTS_PER_SOL).unwrap();

    assert_eq!(accounts.len(), 5);

    for (i, account) in accounts.iter().enumerate() {
        println!("Account {}: {}", i, account.pubkey());
    }
}
```

## Operações com tokens

Crie token mints e contas sem o boilerplate habitual.

### Criar um token mint

```rust
use litesvm_utils::{LiteSVM, Signer, TestHelpers};
use solana_sdk::native_token::LAMPORTS_PER_SOL;

#[test]
fn test_create_token_mint() {
    let mut svm = LiteSVM::new();

    // Create authority account
    let authority = svm.create_funded_account(10 * LAMPORTS_PER_SOL).unwrap();

    // Create a token mint with 9 decimals (returns Keypair)
    let mint = svm.create_token_mint(&authority, 9).unwrap();

    println!("Mint address: {}", mint.pubkey());
}
```

### Criar token accounts

```rust
use litesvm_utils::{LiteSVM, Signer, TestHelpers};
use solana_sdk::native_token::LAMPORTS_PER_SOL;

#[test]
fn test_create_token_accounts() {
    let mut svm = LiteSVM::new();

    let authority = svm.create_funded_account(10 * LAMPORTS_PER_SOL).unwrap();
    let user = svm.create_funded_account(5 * LAMPORTS_PER_SOL).unwrap();

    // Create mint (returns Keypair)
    let mint = svm.create_token_mint(&authority, 9).unwrap();

    // Create a regular token account (returns Keypair)
    let token_account = svm.create_token_account(&mint.pubkey(), &user).unwrap();

    // Or create an Associated Token Account — ATA (returns Pubkey)
    let ata = svm.create_associated_token_account(&mint.pubkey(), &user).unwrap();

    println!("Token account: {}", token_account.pubkey());
    println!("ATA: {}", ata);
}
```

<Info>
  **Use ATAs na maioria dos casos.** As Associated Token Accounts são determinísticas e
  seguem as convenções da Solana. Use token accounts comuns apenas quando precisar de
  várias contas para o mesmo par mint/proprietário.
</Info>

### Cunhar tokens

```rust
use litesvm_utils::{AssertionHelpers, LiteSVM, Signer, TestHelpers};
use solana_sdk::native_token::LAMPORTS_PER_SOL;

#[test]
fn test_mint_tokens() {
    let mut svm = LiteSVM::new();

    let authority = svm.create_funded_account(10 * LAMPORTS_PER_SOL).unwrap();
    let user = svm.create_funded_account(5 * LAMPORTS_PER_SOL).unwrap();

    // Create mint with authority
    let mint = svm.create_token_mint(&authority, 9).unwrap();

    // Create ATA for user (returns Pubkey)
    let user_ata = svm.create_associated_token_account(&mint.pubkey(), &user).unwrap();

    // Mint 1000 tokens to user's ATA
    svm.mint_to(&mint.pubkey(), &user_ata, &authority, 1000).unwrap();

    // Verify the balance
    svm.assert_token_balance(&user_ata, 1000);
}
```

## Derivação de PDA

Derive Program Derived Addresses com facilidade para os seus testes.

### Derivação básica de PDA

```rust
use litesvm_utils::{LiteSVM, Pubkey, TestHelpers};

#[test]
fn test_derive_pda() {
    let mut svm = LiteSVM::new();

    let program_id = Pubkey::new_unique();
    let user = Pubkey::new_unique();

    // Get PDA with bump
    let (pda, bump) = svm.derive_pda(
        &[b"user_account", user.as_ref()],
        &program_id,
    );

    println!("PDA: {}, Bump: {}", pda, bump);
}
```

### Obter apenas o PDA

```rust
use litesvm_utils::{LiteSVM, Pubkey, TestHelpers};

#[test]
fn test_get_pda() {
    let mut svm = LiteSVM::new();

    let program_id = Pubkey::new_unique();

    // When you don't need the bump
    let pda = svm.get_pda(&[b"config"], &program_id);

    println!("Config PDA: {}", pda);
}
```

<Info>
  Use `derive_pda` quando precisar do bump seed (p. ex., para chamadas CPI). Use
  `get_pda` quando precisar apenas do endereço.
</Info>

## Gestão de slots

Controle a progressão do tempo para testar lógica dependente do tempo.

### Obter o slot atual

```rust
use litesvm_utils::{LiteSVM, TestHelpers};

#[test]
fn test_get_current_slot() {
    let mut svm = LiteSVM::new();

    let slot = svm.get_current_slot();
    println!("Current slot: {}", slot);
}
```

### Avançar slots

```rust
use litesvm_utils::{LiteSVM, TestHelpers};

#[test]
fn test_advance_slots() {
    let mut svm = LiteSVM::new();

    let initial_slot = svm.get_current_slot();

    // Advance by 100 slots
    svm.advance_slot(100);

    let new_slot = svm.get_current_slot();
    assert_eq!(new_slot, initial_slot + 100);
}
```

## Exemplo completo

Aqui está um exemplo abrangente que usa vários test helpers em conjunto:

```rust
use litesvm_utils::{AssertionHelpers, LiteSVM, Signer, TestHelpers};
use solana_sdk::native_token::LAMPORTS_PER_SOL;

#[test]
fn test_complete_setup() {
    let mut svm = LiteSVM::new();

    // Create accounts
    let admin = svm.create_funded_account(100 * LAMPORTS_PER_SOL).unwrap();
    let users = svm.create_funded_accounts(3, 10 * LAMPORTS_PER_SOL).unwrap();

    // Create token infrastructure (returns Keypair)
    let mint = svm.create_token_mint(&admin, 6).unwrap(); // 6 decimals like USDC

    // Create ATAs and mint tokens to each user
    for (i, user) in users.iter().enumerate() {
        // create_associated_token_account returns Pubkey
        let ata = svm.create_associated_token_account(&mint.pubkey(), user).unwrap();
        let amount = (i as u64 + 1) * 1_000_000; // 1, 2, 3 tokens
        svm.mint_to(&mint.pubkey(), &ata, &admin, amount).unwrap();

        // Verify
        svm.assert_token_balance(&ata, amount);
    }

    // Verify total supply
    let total_supply = 1_000_000 + 2_000_000 + 3_000_000;
    svm.assert_mint_supply(&mint.pubkey(), total_supply);

    // Test time-based logic
    let start_slot = svm.get_current_slot();
    svm.advance_slot(1000);
    assert!(svm.get_current_slot() > start_slot);
}
```
