> ## 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

> Các tiện ích tạo tài khoản, thao tác token, dẫn xuất PDA và quản lý slot

## Tạo tài khoản có sẵn SOL

Nhu cầu kiểm thử phổ biến nhất là tạo các tài khoản có SOL. Trait `TestHelpers`
làm việc này trở nên cực kỳ đơn giản.

### Một tài khoản

```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());
}
```

### Nhiều tài khoản

```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());
    }
}
```

## Thao tác với token

Tạo token mint và tài khoản mà không cần viết nhiều đoạn mã boilerplate thông thường.

### Tạo 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());
}
```

### Tạo token account

```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>
  **Hãy dùng ATA trong hầu hết trường hợp.** Associated Token Account có tính xác định và
  tuân theo các quy ước của Solana. Chỉ dùng token account thông thường khi bạn cần
  nhiều tài khoản cho cùng một cặp mint/owner.
</Info>

### Mint token

```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);
}
```

## Dẫn xuất PDA

Dễ dàng dẫn xuất Program Derived Address cho các bài kiểm thử của bạn.

### Dẫn xuất PDA cơ bản

```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);
}
```

### Chỉ lấy 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>
  Dùng `derive_pda` khi bạn cần bump seed (ví dụ: cho các lệnh gọi CPI). Dùng
  `get_pda` khi bạn chỉ cần địa chỉ.
</Info>

## Quản lý slot

Điều khiển tiến trình thời gian để kiểm thử logic phụ thuộc vào thời gian.

### Lấy slot hiện tại

```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);
}
```

### Tăng slot

```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);
}
```

## Ví dụ hoàn chỉnh

Dưới đây là một ví dụ toàn diện sử dụng kết hợp nhiều test helper:

```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);
}
```
