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

Kiểm thử một chương trình Solana tùy chỉnh 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 yêu cầu, rồi gửi các lệnh (instruction) đến chương trình.
LiteSVM xử lý toàn bộ quy trình này ngay trong tiến trình — không cần validator. Chúng tôi khuyến nghị
tạo các client chương trình bằng
[Codama JS Renderer](https://github.com/codama-idl/renderers-js) để bạn có thể
truy cập một Kit Plugin tương thích.

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

<Note>
  Phần import `@my-program/sdk` là một giá trị giữ chỗ cho client do Codama tạo ra
  cho chương trình của riêng bạn. Hai ví dụ này chỉ là các khung mẫu minh họa: hãy thay
  tên plugin, instruction và account bằng tên của bạn trước khi chạy.
</Note>

```typescript
import { createClient, address, generateKeyPairSigner } from "@solana/kit";
import { litesvm } from "@solana/kit-plugin-litesvm";
import { signer } from "@solana/kit-plugin-signer";
import { myProgramPlugin } from "@my-program/sdk";
import * as fs from "node:fs";

const mySigner = await generateKeyPairSigner();
const client = createClient()
  .use(signer(mySigner))
  .use(litesvm())
  .use(myProgramPlugin());

// Configure for program testing
client.svm
  .withSigverify(false)
  .withBlockhashCheck(false)
  .withSysvars()
  .withBuiltins();

// Load program from .so file
const programId = (await generateKeyPairSigner()).address;
const soPath = "/path/to/target/deploy/my_program.so";

if (fs.existsSync(soPath)) {
  client.svm.addProgramFromFile(programId, soPath);
  console.log("Program loaded:", programId);

  // Verify program account
  const programAccount = client.svm.getAccount(programId);
  if (programAccount.exists) {
    console.log("  Executable:", programAccount.executable);
    console.log("  Data size:", programAccount.data.length, "bytes");
  }
} else {
  console.log("Program file not found:", soPath);
  console.log("Build with: cargo build-sbf");
}
```

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

<Note>
  Khung mẫu minh họa: `myProgramPlugin`, `testInstruction` và `myAccount`
  đến từ client do chính bạn tạo ra.
</Note>

```typescript
import { createClient, address, generateKeyPairSigner, lamports } from '@solana/kit';
import { litesvm } from '@solana/kit-plugin-litesvm';
import { signer } from '@solana/kit-plugin-signer';
import { myProgramPlugin } from '@my-program/sdk';
import * as fs from 'node:fs';

const mySigner = await generateKeyPairSigner();
const client = createClient()
    .use(signer(mySigner))
    .use(litesvm())
    .use(myProgramPlugin());

client.svm
    .withSigverify(false)
    .withBlockhashCheck(false)
    .withSysvars()
    .withBuiltins()
    .withPrecompiles()
    .withTransactionHistory(100n);

// Load program
const programId = (await generateKeyPairSigner()).address;
const soPath = '/path/to/target/deploy/my_program.so';

if (fs.existsSync(soPath)) {
    client.svm.addProgramFromFile(programId, soPath);
}

// Set up any required data accounts
const dataAccountAddress = (await generateKeyPairSigner()).address;
const minBalance = client.svm.minimumBalanceForRentExemption(100n);
client.svm.setAccount({
    address: dataAccountAddress,
    data: new Uint8Array(100),
    executable: false,
    lamports: lamports(minBalance),
    programAddress: programId,
    space: 100n,
});

// Send the transaction
await client.myProgram.testInstruction({ /* args */ }).sendTransaction();

// Verify state changes
const updatedAccount = client.myProgram.myAccount.fetch(dataAccountAddress);
// assert on decoded account
```

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

Một client `litesvm()` đã được tải sẵn các chương trình tiêu chuẩn: System
Program, các BPF loader, Vote, Compute Budget, Stake, Address Lookup Table,
Token, Token-2022, Associated Token Account, Memo, cùng các precompile ed25519, secp256k1
và secp256r1. Chỉ cần tải các chương trình khác mà bài kiểm thử của bạn gọi đến:

```typescript
const mySigner = await generateKeyPairSigner();
const client = createClient().use(signer(mySigner)).use(litesvm());

// Token, Token-2022, Associated Token Account and Memo are already loaded.
// Add any other program your program calls from its .so file
client.svm.addProgramFromFile(otherProgramId, "/path/to/other_program.so");
```

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

Mô hình luôn giống nhau:

1. **Tải chương trình**: Dùng `addProgramFromFile` để tải các chương trình đã biên dịch và
   import myProgramPlugin() từ client của bạn
2. **Cấu hình**: Thay đổi các thiết lập như xác minh chữ ký nếu
   cần; sysvar, builtin, precompile và các chương trình mặc định đã được
   tải sẵn
3. **Thiết lập tài khoản**: Điền sẵn dữ liệu cho các tài khoản bằng `setAccount`
4. **Gửi giao dịch**: Dùng `client.myProgram.testInstruction({ /* args */ }).sendTransaction()` —
   không cần tự xây dựng giao dịch
5. **Xác minh**: Kiểm tra trạng thái tài khoản sau khi thực thi
   (`client.myProgram.myAccount.fetch`)

Khi khung mẫu này đã hoạt động, bạn có thể xây dựng một bộ kiểm thử đầy đủ bằng cách
thêm nhiều instruction và assertion hơn.
