LCOV - code coverage report
Current view: top level - vrt - state.go Coverage Total Hit
Test: coverage.lcov Lines: 0.0 % 65 0
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package vrt
       2              : 
       3              : import (
       4              :         "image"
       5              :         "math/rand/v2"
       6              :         "sync"
       7              :         "testing"
       8              : 
       9              :         "github.com/kijimaD/ruins/internal/config"
      10              :         "github.com/kijimaD/ruins/internal/consts"
      11              :         es "github.com/kijimaD/ruins/internal/engine/states"
      12              :         "github.com/kijimaD/ruins/internal/maingame"
      13              :         "github.com/kijimaD/ruins/internal/raw"
      14              :         gs "github.com/kijimaD/ruins/internal/systems"
      15              :         w "github.com/kijimaD/ruins/internal/world"
      16              : 
      17              :         "github.com/kijimaD/ruins/internal/world/gameaction"
      18              :         "github.com/kijimaD/ruins/internal/world/lifecycle"
      19              :         "github.com/stretchr/testify/require"
      20              : )
      21              : 
      22              : // States はステートのスライスをビルダー関数に変換するアダプタ
      23              : func States(states ...es.State[w.World]) func(w.World) []es.State[w.World] {
      24            0 :         return func(_ w.World) []es.State[w.World] { return states }
      25              : }
      26              : 
      27              : // RenderPNG はステートを構築し本番の renderer で描いてPNGを返す。比較はしない、画像保存用。
      28              : // screeneffect のポスト処理まで含めて実画面と同じ絵になる。
      29              : func RenderPNG(t *testing.T, buildStates func(w.World) []es.State[w.World]) []byte {
      30            0 :         t.Helper()
      31            0 :         return encodePNG(t, renderStates(t, buildStates))
      32            0 : }
      33              : 
      34              : // renderStates は world を作りステートを構築し、本番の MainGame.Draw で screen へ描いて NRGBA を返す。
      35              : func renderStates(t *testing.T, buildStates func(w.World) []es.State[w.World]) *image.NRGBA {
      36            0 :         t.Helper()
      37            0 :         world := InitReplayWorld(t)
      38            0 : 
      39            0 :         sm := SetupStateMachine(t, world, buildStates)
      40            0 :         game, err := maingame.NewMainGame(world, sm)
      41            0 :         require.NoError(t, err)
      42            0 :         screen := newScreen(consts.GameWidth, consts.GameHeight)
      43            0 :         game.Draw(screen)
      44            0 :         return captureScreen(screen)
      45            0 : }
      46              : 
      47              : // SetupStateMachine はステートを構築しレイアウト確定までフレームを回す。
      48              : func SetupStateMachine(t *testing.T, world w.World, buildStates func(w.World) []es.State[w.World]) es.StateMachine[w.World] {
      49            0 :         t.Helper()
      50            0 :         states := buildStates(world)
      51            0 :         require.NotEmpty(t, states, "at least one state is required")
      52            0 : 
      53            0 :         stateMachine, err := es.Init(states[0], world)
      54            0 :         require.NoError(t, err)
      55            0 :         require.NoError(t, stateMachine.Update(world))
      56            0 : 
      57            0 :         if len(states) > 1 {
      58            0 :                 require.NoError(t, stateMachine.PushState(world, states[1:]...))
      59            0 :         }
      60              : 
      61              :         // レイアウト確定のためフレームを回す。警告なく失敗を握り潰すと不可解なテスト失敗になるので報告する
      62            0 :         for range 10 {
      63            0 :                 require.NoError(t, stateMachine.Update(world), "layout warm-up failed")
      64            0 :         }
      65            0 :         return stateMachine
      66              : }
      67              : 
      68              : // loadMu は maingame.InitWorld の構築を直列化する。フォント以外にもスプライトシートや
      69              : // システム登録など多数の構築を含み、並行実行の安全は保証されない。
      70              : var loadMu sync.Mutex
      71              : 
      72              : // InitReplayWorld は実プレイの再現に使うフルゲーム構成のワールドを固定シードで初期化する。
      73              : // テスト・ベンチ双方で使えるよう testing.TB を受ける。
      74              : //
      75              : // 構築は loadMu で直列化する。構築後の world は自前の独立フェイスを持つので、
      76              : // 以降の設定と描画はロック無しで並列に走れる。
      77              : func InitReplayWorld(tb testing.TB) w.World {
      78            0 :         tb.Helper()
      79            0 : 
      80            0 :         cfg := &config.Config{Profile: config.ProfileDevelopment}
      81            0 :         cfg.ApplyProfileDefaults()
      82            0 :         cfg.LogLevel = "ignore"
      83            0 :         cfg.Seed = 12345
      84            0 :         cfg.RNG = rand.New(rand.NewPCG(cfg.Seed, 0))
      85            0 :         cfg.DisableAnimation = true
      86            0 :         // ポスト処理を切って撮る。スキャンラインの高周波パターンが無くなり、golden の
      87              :         // 差分がUIの実変化だけを映すようになる。トレランスを絞れる
      88            0 :         cfg.DisableScreenFilter = true
      89            0 :         // 再生は描画確認であって永続化セッションではない。オートセーブの副作用でセーブファイルを
      90              :         // 書かない。UI には影響しないので Load や Save メニュー項目はそのまま出る
      91            0 :         cfg.DisableAutoSave = true
      92            0 :         require.NoError(tb, cfg.Validate())
      93            0 : 
      94            0 :         loadMu.Lock()
      95            0 :         world, err := maingame.InitWorld(cfg)
      96            0 :         loadMu.Unlock()
      97            0 :         require.NoError(tb, err)
      98            0 : 
      99            0 :         name, err := raw.PlayerMemberID(world.Resources.RawMaster)
     100            0 :         require.NoError(tb, err)
     101            0 :         player, err := lifecycle.SpawnPlayer(world, consts.Coord[consts.Tile]{X: 5, Y: 5}, name)
     102            0 :         require.NoError(tb, err)
     103            0 : 
     104            0 :         professions := raw.PtrSlice(world.Resources.RawMaster.Professions)
     105            0 :         if len(professions) > 0 {
     106            0 :                 require.NoError(tb, gameaction.ApplyProfession(world, player, professions[0]))
     107            0 :         }
     108              : 
     109            0 :         for _, updater := range []w.Updater{
     110            0 :                 &gs.StatsChangedSystem{},
     111            0 :                 &gs.WeightDirtySystem{},
     112            0 :         } {
     113            0 :                 if sys, ok := world.Updaters[updater.String()]; ok {
     114            0 :                         require.NoError(tb, sys.Update(world))
     115            0 :                 }
     116              :         }
     117            0 :         return world
     118              : }
        

Generated by: LCOV version 2.0-1