Testing a game
Three tests cover almost everything. The template (cig new) ships all three.
// 1. Automated play-testing: random legal games, every rule checked.
func TestExplore(t *testing.T) {
cig2test.Explore(t, mygame.Name, []cig2.PlayerID{"p1", "p2"})
}
// 2. A scripted game through the real session path.
func TestAGame(t *testing.T) {
tbl := cig2test.Start(t, mygame.Name, []cig2.PlayerID{"p1", "p2"}, cig2.WithSeed(1))
st := tbl.Act("p1", "play", map[string]string{"card": uuid})
if st.Err != nil {
t.Fatal(st.Err)
}
hand, _ := st.Snapshot("p2").Root.FindZone("hand-p1")
_ = hand
}
// 3. Golden snapshots: exactly what each seat is sent, UUIDs renamed.
// See cig2test.Golden.What runs automatically
Every cig2test.Start table:
- Strict rejection checking. A rejected action that changed state fails the step (E201).
- The journal replay check. When the test ends, the table’s journal is replayed into a fresh session and must reach the same state at every step (E507). This finds
time.Now, map-order randomness and package state.- It is skipped, with a log line, after a
Session.Dothat changed state, or forWithRand. tbl.SkipReplayCheck()opts out.
- It is skipped, with a log line, after a
Explore adds:
- leak checks (E501, E502)
- hint consistency (E503)
- deadlock and endless-game detection (E504, E505;
MayNotEndfor a game random play need not finish, such as a solitaire)
A failure is shrunk and printed as a test to paste.
Rules
- Never call
t.FatalinsideSession.Door a game callback (E205). Read into locals, then assert. - Stage preconditions through actions where you can. A mutating
Doworks but turns off the replay check for that table. - Use
WithSeedfor reproducible deals. Card UUIDs differ between runs unless the test also passesWithIDSeed. Compare by identity and position, never by UUID. - Drive timers in tests with
tbl.Fire(Session.ApplyTimer), not sleeps.