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

# 程序测试

> 使用 LiteSVM 加载并测试自定义 Solana 程序

测试自定义 Solana 程序，需要将其编译后的 `.so` 文件加载到 SVM 中，设置程序所需的账户，然后向其发送指令。LiteSVM 在进程内完成所有这些操作——无需 validator。我们建议使用 [Codama JS Renderer](https://github.com/codama-idl/renderers-js) 生成程序客户端，这样就可以使用兼容的 Kit Plugin。

## 加载程序

<Note>
  `@my-program/sdk` 导入只是一个占位符，代表由 Codama 为你自己的程序生成的客户端。这两个示例仅为说明用的骨架代码：运行之前，请将其中的插件、指令和账户名称替换为你自己的名称。
</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");
}
```

## 完整的程序测试设置

<Note>
  说明用的骨架代码：`myProgramPlugin`、`testInstruction` 和 `myAccount` 均来自你自己生成的客户端。
</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
```

## 使用默认程序

`litesvm()` 客户端已加载标准程序：System Program、BPF 加载器、Vote、Compute Budget、Stake、Address Lookup Table、Token、Token-2022、Associated Token Account、Memo，以及 ed25519、secp256k1 和 secp256r1 预编译程序。只需加载测试中调用的其他程序：

```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>
  使用 `cargo build-sbf` 构建你的 Solana 程序，以生成 `.so` 文件。
</Info>

流程始终相同：

1. **加载程序**：使用 `addProgramFromFile` 加载已编译的程序，并从你的客户端导入 myProgramPlugin()
2. **配置**：根据需要更改设置，例如签名验证；sysvar、内置程序、预编译程序和默认程序均已加载
3. **账户设置**：使用 `setAccount` 预先填充数据账户
4. **发送**：使用 `client.myProgram.testInstruction({ /* args */ }).sendTransaction()`——无需手动构建交易
5. **验证**：执行后检查账户状态（`client.myProgram.myAccount.fetch`）

当这个骨架可以正常运行后，你就可以通过添加更多指令和断言，构建完整的测试套件。
