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

# Test dei programmi

> Carica e testa programmi Solana personalizzati con litesvm-go

Testare un programma Solana personalizzato da Go significa caricare il relativo file `.so` compilato
nella SVM, configurare gli account che si aspetta e inviargli delle istruzioni.
`litesvm-go` gestisce tutto questo in-process: non serve alcun validator.

## Caricamento di un programma

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

## Configurazione completa del test di un programma

```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.
}
```

## Uso dei programmi predefiniti

Per i test che richiedono programmi standard (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>
  Compila il tuo programma Solana con `cargo build-sbf` per generare il file `.so`.
</Info>

Lo schema è sempre lo stesso:

1. **Configurazione** - abilita sysvar, builtin e precompile secondo necessità.
2. **Caricamento del programma** - `AddProgramFromFile` (oppure `AddProgram` per i byte
   in memoria).
3. **Configurazione degli account** - popola in anticipo gli account dati con `SetAccount`.
4. **Invio** - costruisci l'istruzione con `solana-go`, serializzala e inviala tramite
   `SendLegacyTransaction`.
5. **Verifica** - rileggi gli account con `GetAccount` e controlla log,
   compute unit e stato finale.

Una volta che questo scheletro funziona, puoi scalare aggiungendo più istruzioni, più
account e una funzione di supporto per il codice boilerplate.
