> ## 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-go のクロック操作で時間依存ロジックをテストする

`WarpToSlot` と `SetClock` を使用して、時間に依存するプログラムロジックをテストする方法を示します。

## クロック操作の基本

```go
package mytest

import (
    "testing"

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

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

    // Read the initial clock.
    initial, err := svm.Clock()
    if err != nil {
        t.Fatal(err)
    }
    t.Logf("initial state:")
    t.Logf("  slot:           %d", initial.Slot)
    t.Logf("  epoch:          %d", initial.Epoch)
    t.Logf("  unix timestamp: %d", initial.UnixTimestamp)

    // Warp to slot 1_000.
    if err := svm.WarpToSlot(1_000); err != nil {
        t.Fatal(err)
    }

    after, err := svm.Clock()
    if err != nil {
        t.Fatal(err)
    }
    t.Logf("after warpToSlot(1000):")
    t.Logf("  slot:  %d", after.Slot)
    t.Logf("  epoch: %d", after.Epoch)

    // Warp further.
    if err := svm.WarpToSlot(100_000); err != nil {
        t.Fatal(err)
    }

    after2, err := svm.Clock()
    if err != nil {
        t.Fatal(err)
    }
    t.Logf("after warpToSlot(100000):")
    t.Logf("  slot:  %d", after2.Slot)
    t.Logf("  epoch: %d", after2.Epoch)
}
```

## タイムロックされたロジックのテスト

```go
package mytest

import (
    "encoding/binary"
    "testing"

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

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

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

    // Fund a payer.
    priv, err := solana.NewRandomPrivateKey()
    if err != nil {
        t.Fatal(err)
    }
    payer := priv.PublicKey()
    if err := svm.Airdrop(payer, 10_000_000_000); err != nil {
        t.Fatal(err)
    }

    // Set up a time-locked vault account.
    // Layout: [u8 discriminator, u64 unlock_slot, u64 amount]
    const unlockSlot = uint64(10_000)
    const lockedAmount = uint64(5_000_000_000)

    vaultData := make([]byte, 1+8+8)
    vaultData[0] = 0x01
    binary.LittleEndian.PutUint64(vaultData[1:9], unlockSlot)
    binary.LittleEndian.PutUint64(vaultData[9:17], lockedAmount)

    programID := solana.NewWallet().PublicKey()
    vaultAddr := solana.NewWallet().PublicKey()
    minBalance, err := svm.MinimumBalanceForRentExemption(len(vaultData))
    if err != nil {
        t.Fatal(err)
    }

    acct, err := litesvm.NewAccount(minBalance+lockedAmount, vaultData, programID, false, 0)
    if err != nil {
        t.Fatal(err)
    }

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

    t.Logf("vault created: unlock slot=%d, locked=%.3f SOL",
        unlockSlot, float64(lockedAmount)/1e9)

    // --- Test 1: Before unlock ---
    cBefore, err := svm.Clock()
    if err != nil {
        t.Fatal(err)
    }
    t.Logf("current slot: %d", cBefore.Slot)
    // In a real test you would build a withdraw instruction here and assert
    // it fails because the current slot < unlock_slot.
    t.Log("withdrawal attempt: EXPECTED TO FAIL (too early)")

    // --- Test 2: After unlock ---
    if err := svm.WarpToSlot(unlockSlot + 100); err != nil {
        t.Fatal(err)
    }

    cAfter, err := svm.Clock()
    if err != nil {
        t.Fatal(err)
    }
    t.Logf("current slot: %d", cAfter.Slot)
    // Build and submit the withdraw instruction; assert it succeeds.
    t.Log("withdrawal attempt: EXPECTED TO SUCCEED (after unlock)")

    // Verify vault state.
    updated := svm.GetAccount(vaultAddr)
    if updated == nil {
        t.Fatal("vault missing")
    }
    t.Logf("vault balance: %d lamports", updated.Lamports)

    _ = payer // payer would be referenced when building the withdraw ix
}
```

## Epoch Schedule の読み取り

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

    es, err := svm.EpochSchedule()
    if err != nil {
        t.Fatal(err)
    }
    t.Logf("slots per epoch:    %d", es.SlotsPerEpoch)
    t.Logf("first normal epoch: %d", es.FirstNormalEpoch)
    t.Logf("first normal slot:  %d", es.FirstNormalSlot)
}
```

<Warning>
  `WarpToSlot` はクロックの `Slot` フィールドを進めますが、`Epoch` や `UnixTimestamp` は自動的には更新**されません**。クロックの各フィールドを完全に制御するには `SetClock` を使用してください。
</Warning>

## クロックの手動操作

プログラムが Clock sysvar から `Epoch` や `UnixTimestamp` を読み取る場合は、`SetClock` でそれらのフィールドを直接設定します。

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

    c, err := svm.Clock()
    if err != nil {
        t.Fatal(err)
    }
    c.Slot = 50_000
    c.Epoch = 5
    c.UnixTimestamp = 1_700_000_000
    if err := svm.SetClock(c); err != nil {
        t.Fatal(err)
    }

    after, err := svm.Clock()
    if err != nil {
        t.Fatal(err)
    }
    t.Logf("slot:  %d", after.Slot)           // 50000
    t.Logf("epoch: %d", after.Epoch)          // 5
    t.Logf("unix:  %d", after.UnixTimestamp)  // 1700000000
}
```

これは Rust の `set_sysvar(&clock)` パターンに相当します。単純に slot を進めるだけなら `WarpToSlot`、epoch やタイムスタンプを制御する必要がある場合は `SetClock` を使用します。

## ユースケース

時間操作は次のようなテストに役立ちます。

| シナリオ                | アプローチ                             |
| ----------------------- | ------------------------------------ |
| トークンのベスティング           | ベスティングのクリフ／マイルストーンを越えてワープする |
| オークションの終了         | オークション終了 slot を越えてワープする           |
| ステーキング報酬         | `SetClock` を使用して epoch を設定する          |
| タイムロック付きの引き出し | ロック解除 slot を越えてワープする                |
| レート制限           | 許可された間隔をまたいでワープする       |
| 注文の期限切れ         | 注文の有効期限を越えてワープする           |

<Info>
  `WarpToSlot` は slot のみを変更します。UNIX タイムスタンプは比例して更新されない場合があります。プログラムが `UnixTimestamp` を読み取る場合は `SetClock` を使用してください。
</Info>

## 重要なポイント

1. **sysvar はデフォルトで有効** - `litesvm.New()` は sysvar が初期化されたハンドルを返します。sysvar を変更した場合は、`SetSysvars()` を呼び出してデフォルトにリセットできます。
2. **前方へワープ** - `WarpToSlot(slot)` は slot フィールドのみを進めます。
3. **完全な制御** - `SetClock(Clock{...})` を使うと、`Epoch`、`UnixTimestamp`、その他のフィールドを設定できます。
4. **読み取り** - `svm.Clock()` で現在の値を読み取ります。
5. **テストパターン** - 時間の境界の前後でテストし、一方では失敗し、もう一方では成功することを確認します。
