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

# Khẳng định (Assertions)

> Xác minh trạng thái tài khoản, số dư và quyền sở hữu bằng các hàm hỗ trợ khẳng định

## Sự tồn tại của tài khoản

Xác minh xem tài khoản có tồn tại hay đã bị đóng hay chưa.

### Khẳng định tài khoản tồn tại

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

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

    let alice = svm.create_funded_account(10 * LAMPORTS_PER_SOL).unwrap();

    // This will pass - account exists
    svm.assert_account_exists(&alice.pubkey());
}
```

### Khẳng định tài khoản đã đóng

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

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

    // A random pubkey that was never created
    let nonexistent = Pubkey::new_unique();

    // This will pass - account doesn't exist
    svm.assert_account_closed(&nonexistent);
}
```

<Info>
  `assert_account_closed` sẽ thành công khi tài khoản không tồn tại, hoặc có số
  lamport bằng 0 và dữ liệu trống. Hãy dùng hàm này để xác minh việc dọn dẹp sau khi đóng tài khoản.
</Info>

## Khẳng định số dư

Xác minh số dư SOL và token.

### Khẳng định số dư SOL

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

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

    let alice = svm.create_funded_account(10 * LAMPORTS_PER_SOL).unwrap();

    // Verify Alice has 10 SOL
    svm.assert_sol_balance(&alice.pubkey(), 10 * LAMPORTS_PER_SOL);
}
```

### Khẳng định số dư token

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

#[test]
fn test_assert_token_balance() {
    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 and token account
    let mint = svm.create_token_mint(&authority, 9).unwrap();
    let user_ata = svm.create_associated_token_account(&mint.pubkey(), &user).unwrap();

    // Mint tokens
    svm.mint_to(&mint.pubkey(), &user_ata, &authority, 5000).unwrap();

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

### Khẳng định tổng cung của mint

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

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

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

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

    // Create ATAs and mint tokens
    let user1_ata = svm.create_associated_token_account(&mint.pubkey(), &user1).unwrap();
    let user2_ata = svm.create_associated_token_account(&mint.pubkey(), &user2).unwrap();

    svm.mint_to(&mint.pubkey(), &user1_ata, &authority, 1000).unwrap();
    svm.mint_to(&mint.pubkey(), &user2_ata, &authority, 2000).unwrap();

    // Verify total supply
    svm.assert_mint_supply(&mint.pubkey(), 3000);
}
```

## Thuộc tính tài khoản

Xác minh quyền sở hữu tài khoản và các thuộc tính dữ liệu.

### Khẳng định chủ sở hữu tài khoản

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

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

    let alice = svm.create_funded_account(10 * LAMPORTS_PER_SOL).unwrap();

    // Regular accounts are owned by the System Program
    svm.assert_account_owner(&alice.pubkey(), &system_program::id());
}
```

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

#[test]
fn test_assert_token_account_owner() {
    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();

    let mint = svm.create_token_mint(&authority, 9).unwrap();
    let user_ata = svm.create_associated_token_account(&mint.pubkey(), &user).unwrap();

    // Token accounts are owned by the Token Program
    svm.assert_account_owner(&user_ata, &spl_token::id());
}
```

### Khẳng định độ dài dữ liệu tài khoản

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

#[test]
fn test_assert_account_data_len() {
    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();

    let mint = svm.create_token_mint(&authority, 9).unwrap();
    let user_ata = svm.create_associated_token_account(&mint.pubkey(), &user).unwrap();

    // Token mint accounts are 82 bytes
    svm.assert_account_data_len(&mint.pubkey(), 82);

    // Token accounts are 165 bytes
    svm.assert_account_data_len(&user_ata, 165);
}
```

<Info>
  Kích thước tài khoản phổ biến: - **Mint Account**: 82 byte - **token account**: 165
  byte - **System Account**: 0 byte (chỉ có lamport)
</Info>

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

Dưới đây là một bài kiểm thử toàn diện minh họa nhiều khẳng định:

```rust
use litesvm_utils::{AssertionHelpers, LiteSVM, Signer, TestHelpers, TransactionHelpers};
use solana_sdk::native_token::LAMPORTS_PER_SOL;
use solana_system_interface::instruction as system_instruction;

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

    // Setup
    let admin = svm.create_funded_account(100 * LAMPORTS_PER_SOL).unwrap();
    let alice = svm.create_funded_account(10 * LAMPORTS_PER_SOL).unwrap();
    let bob = svm.create_funded_account(0).unwrap();

    // Verify initial SOL balances
    svm.assert_sol_balance(&admin.pubkey(), 100 * LAMPORTS_PER_SOL);
    svm.assert_sol_balance(&alice.pubkey(), 10 * LAMPORTS_PER_SOL);
    svm.assert_sol_balance(&bob.pubkey(), 0);

    // Transfer SOL from Alice to Bob
    let transfer_ix = system_instruction::transfer(
        &alice.pubkey(),
        &bob.pubkey(),
        5 * LAMPORTS_PER_SOL,
    );
    svm.send_instruction(transfer_ix, &[&alice]).unwrap().assert_success();

    // Verify balances after transfer (Alice paid fees too)
    svm.assert_sol_balance(&bob.pubkey(), 5 * LAMPORTS_PER_SOL);

    // Create token infrastructure (mint returns Keypair, ATAs return Pubkey)
    let mint = svm.create_token_mint(&admin, 6).unwrap();
    let alice_ata = svm.create_associated_token_account(&mint.pubkey(), &alice).unwrap();
    let bob_ata = svm.create_associated_token_account(&mint.pubkey(), &bob).unwrap();

    // Verify account properties
    svm.assert_account_exists(&mint.pubkey());
    svm.assert_account_exists(&alice_ata);
    svm.assert_account_owner(&mint.pubkey(), &spl_token::id());
    svm.assert_account_data_len(&mint.pubkey(), 82);
    svm.assert_account_data_len(&alice_ata, 165);

    // Mint and verify
    svm.mint_to(&mint.pubkey(), &alice_ata, &admin, 1_000_000).unwrap();
    svm.mint_to(&mint.pubkey(), &bob_ata, &admin, 500_000).unwrap();

    svm.assert_token_balance(&alice_ata, 1_000_000);
    svm.assert_token_balance(&bob_ata, 500_000);
    svm.assert_mint_supply(&mint.pubkey(), 1_500_000);

    println!("All assertions passed!");
}
```

## Xử lý lỗi khẳng định

Khi một khẳng định thất bại, chương trình sẽ panic kèm theo thông báo mô tả:

```rust
// This will panic with a message like:
// "Expected token balance 1000, but got 500"
svm.assert_token_balance(&some_ata, 1000); // when balance is actually 500
```

<Warning>
  Các khẳng định được thiết kế để thất bại nhanh kèm thông báo lỗi rõ ràng. Hãy
  sử dụng chúng thoải mái trong suốt các bài kiểm thử để phát hiện sớm sự cố.
</Warning>
