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

# Kiểm thử chương trình

> Tải và kiểm thử các chương trình Solana tùy chỉnh bằng litesvm-go

Kiểm thử một chương trình Solana tùy chỉnh từ Go nghĩa là tải tệp `.so` đã biên dịch của chương trình
vào SVM, thiết lập các tài khoản mà chương trình mong đợi, rồi gửi các instruction
tới chương trình đó. `litesvm-go` xử lý toàn bộ quy trình này ngay trong tiến trình - không cần validator.

## Tải một chương trình

```go
package mytest

import (
    "os"
    "testing"

    litesvm "github.com/LiteSVM/litesvm-go"
    solana "github.com/gagliardetto/solana-go"
)

func TestLoadProgram(t *testing.T) {
    svm, err := litesvm.New()
    if err != nil {
        t.Fatal(err)
    }
    defer svm.Close()

    // Configure for program testing. Setters are infallible in practice;
    // discarding the return keeps the example readable.
    _ = svm.SetSigverify(false)
    _ = svm.SetBlockhashCheck(false)
    _ = svm.SetSysvars()
    _ = svm.SetBuiltins()

    // Load program from .so file.
    programID := solana.NewWallet().PublicKey()
    soPath := "./target/deploy/my_program.so"

    if _, err := os.Stat(soPath); err != nil {
        t.Skipf("program file not found: %s (build with: cargo build-sbf)", soPath)
    }

    if err := svm.AddProgramFromFile(programID, soPath); err != nil {
        t.Fatal(err)
    }
    t.Logf("program loaded: %s", programID)

    // Verify the program account.
    acct := svm.GetAccount(programID)
    if acct == nil {
        t.Fatal("program account missing after load")
    }

    t.Logf("  executable: %v", acct.Executable)
    t.Logf("  data size:  %d bytes", len(acct.Data))
}
```

## Thiết lập kiểm thử chương trình hoàn chỉnh

```go
package mytest

import (
    "os"
    "testing"

    litesvm "github.com/LiteSVM/litesvm-go"
    solana "github.com/gagliardetto/solana-go"
)

func TestProgramEndToEnd(t *testing.T) {
    svm, err := litesvm.New()
    if err != nil {
        t.Fatal(err)
    }
    defer svm.Close()

    // Wide-open config: faster tests, less plumbing. Setters are infallible
    // in practice; discarding the return keeps the example readable.
    _ = svm.SetSigverify(false)
    _ = svm.SetBlockhashCheck(false)
    _ = svm.SetSysvars()
    _ = svm.SetBuiltins()
    _ = svm.SetPrecompiles()
    _ = svm.SetTransactionHistory(100)

    // Load the program.
    programID := solana.NewWallet().PublicKey()
    soPath := "./target/deploy/my_program.so"
    if _, err := os.Stat(soPath); err != nil {
        t.Skipf("missing %s", soPath)
    }
    if err := svm.AddProgramFromFile(programID, soPath); err != nil {
        t.Fatal(err)
    }

    // Seed any data accounts the program expects.
    dataAddr := solana.NewWallet().PublicKey()
    dataPayload := make([]byte, 100)

    rent, err := svm.MinimumBalanceForRentExemption(len(dataPayload))
    if err != nil {
        t.Fatal(err)
    }
    acct, err := litesvm.NewAccount(rent, dataPayload, programID, false, 0)
    if err != nil {
        t.Fatal(err)
    }

    if err := svm.SetAccount(dataAddr, acct); err != nil {
        t.Fatal(err)
    }

    // Fund a payer that will sign the test instruction.
    priv, err := solana.NewRandomPrivateKey()
    if err != nil {
        t.Fatal(err)
    }
    payer := priv.PublicKey()
    if err := svm.Airdrop(payer, 1_000_000_000); err != nil {
        t.Fatal(err)
    }

    // Build an instruction against the program.
    bh, err := svm.LatestBlockhash()
    if err != nil {
        t.Fatal(err)
    }
    ix := &solana.GenericInstruction{
        ProgID: programID,
        AccountValues: solana.AccountMetaSlice{
            {PublicKey: dataAddr, IsWritable: true, IsSigner: false},
            {PublicKey: payer, IsWritable: true, IsSigner: true},
        },
        DataBytes: []byte{0x01 /* discriminator + args */},
    }

    tx, err := solana.NewTransaction(
        []solana.Instruction{ix},
        bh,
        solana.TransactionPayer(payer),
    )
    if err != nil {
        t.Fatal(err)
    }
    if _, err := tx.Sign(func(k solana.PublicKey) *solana.PrivateKey {
        if k.Equals(payer) {
            return &priv
        }
        return nil
    }); err != nil {
        t.Fatal(err)
    }
    txBytes, err := tx.MarshalBinary()
    if err != nil {
        t.Fatal(err)
    }

    // Execute and inspect.
    out, err := svm.SendLegacyTransaction(txBytes)
    if err != nil {
        t.Fatal(err)
    }

    if !out.IsOk() {
        t.Fatalf("program failed: %s\nlogs: %v", out.Error(), out.Logs())
    }

    t.Logf("logs: %v", out.Logs())
    t.Logf("compute: %d CU", out.ComputeUnits())

    // Verify state changes by reading the data account back.
    updated := svm.GetAccount(dataAddr)
    if updated == nil {
        t.Fatal("data account missing after execution")
    }
    t.Logf("data account: %d bytes", len(updated.Data))
    // Decode `updated.Data` and assert as needed.
}
```

## Sử dụng các chương trình mặc định

Đối với các bài kiểm thử cần các chương trình tiêu chuẩn (SPL Token, Memo, ...):

```go
svm, err := litesvm.New()
if err != nil {
    t.Fatal(err)
}
defer svm.Close()

if err := svm.SetDefaultPrograms(); err != nil { // SPL Token, Memo, etc.
    t.Fatal(err)
}
if err := svm.WithNativeMints(); err != nil {    // wrapped-SOL mint
    t.Fatal(err)
}
```

<Info>
  Hãy build chương trình Solana của bạn bằng `cargo build-sbf` để tạo tệp `.so`.
</Info>

Mô hình thực hiện luôn giống nhau:

1. **Cấu hình** - bật sysvar, builtin, precompile khi cần.
2. **Tải chương trình** - `AddProgramFromFile` (hoặc `AddProgram` đối với byte
   trong bộ nhớ).
3. **Thiết lập tài khoản** - điền sẵn dữ liệu cho các tài khoản dữ liệu bằng `SetAccount`.
4. **Gửi giao dịch** - tạo instruction bằng `solana-go`, marshal, rồi gửi qua
   `SendLegacyTransaction`.
5. **Xác minh** - đọc lại các tài khoản bằng `GetAccount` và kiểm tra log,
   compute unit và trạng thái sau khi thực thi.

Khi bộ khung này đã hoạt động, hãy mở rộng bằng cách thêm nhiều instruction hơn, nhiều
tài khoản hơn và một hàm hỗ trợ cho phần mã lặp lại.
