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

# 测试辅助工具

> 账户创建、代币操作、PDA 派生和 slot 管理工具

## 创建已充值账户

最常见的测试需求是创建持有 SOL 的账户。`TestHelpers`
trait 让这件事变得非常简单。

### 单个账户

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

### 多个账户

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

## 代币操作

无需编写常见的样板代码，即可创建代币 mint 和账户。

### 创建代币 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());
}
```

### 创建 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>
  **大多数情况下请使用 ATA。** Associated Token Accounts 具有确定性，
  并遵循 Solana 约定。仅当你需要为同一 mint/所有者组合创建
  多个账户时，才使用常规 token account。
</Info>

### 铸造代币

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

## PDA 派生

在测试中轻松派生 Program Derived Address。

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

### 仅获取 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>
  当你需要 bump seed（例如用于 CPI 调用）时，请使用 `derive_pda`。
  如果只需要地址，请使用 `get_pda`。
</Info>

## Slot 管理

控制时间推进，以测试依赖时间的逻辑。

### 获取当前 slot

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

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

## 完整示例

下面是一个综合使用多个测试辅助工具的完整示例：

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