Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

rules_test.gno

2.66 Kb · 107 lines
  1package chess
  2
  3import (
  4	"testing"
  5)
  6
  7func TestNewInitialBoard(t *testing.T) {
  8	b := NewInitialBoard()
  9
 10	// Check White Backrank
 11	if b[7][4] != "K" || b[7][3] != "Q" {
 12		t.Fatalf("unexpected white backrank: %v", b[7])
 13	}
 14	// Check Black Backrank
 15	if b[0][4] != "k" || b[0][3] != "q" {
 16		t.Fatalf("unexpected black backrank: %v", b[0])
 17	}
 18	// Check Pawns
 19	for c := 0; c < 8; c++ {
 20		if b[6][c] != "P" {
 21			t.Fatalf("expected white pawn at (6,%d), got %s", c, b[6][c])
 22		}
 23		if b[1][c] != "p" {
 24			t.Fatalf("expected black pawn at (1,%d), got %s", c, b[1][c])
 25		}
 26	}
 27}
 28
 29func TestPieceMoveValidations(t *testing.T) {
 30	b := NewInitialBoard()
 31	var rights CastlingRights
 32
 33	// 1. Pawn e2-e4 (from 6,4 to 4,4) is valid
 34	if !IsValidMove(b, 6, 4, 4, 4, true, rights, -1) {
 35		t.Fatal("e2-e4 pawn move should be valid")
 36	}
 37
 38	// 2. Knight b1-c3 (from 7,1 to 5,2) is valid
 39	if !IsValidMove(b, 7, 1, 5, 2, true, rights, -1) {
 40		t.Fatal("knight b1-c3 move should be valid")
 41	}
 42
 43	// 3. Bishop c1-e3 should be blocked initially by pawns
 44	if IsValidMove(b, 7, 2, 5, 4, true, rights, -1) {
 45		t.Fatal("bishop cannot jump over pawns")
 46	}
 47}
 48
 49func TestPromotionNormalization(t *testing.T) {
 50	// White default promotion
 51	p, err := NormalizePromotionPiece("", true)
 52	if err != nil || p != "Q" {
 53		t.Fatalf("expected 'Q', got %s, err: %v", p, err)
 54	}
 55
 56	// Black knight promotion
 57	p, err = NormalizePromotionPiece("n", false)
 58	if err != nil || p != "n" {
 59		t.Fatalf("expected 'n', got %s, err: %v", p, err)
 60	}
 61
 62	// Invalid piece
 63	_, err = NormalizePromotionPiece("K", true)
 64	if err == nil {
 65		t.Fatal("expected error for invalid promotion to King")
 66	}
 67}
 68
 69func TestReplayUCI(t *testing.T) {
 70	// 1. Scholar's Mate -> White checkmates Black
 71	scholarsMate := "e2e4 e7e5 d1h5 b8c6 f1c4 g8f6 h5f7"
 72	_, _, status, err := ReplayUCI(scholarsMate)
 73	if err != nil {
 74		t.Fatalf("unexpected error replaying scholar's mate: %v", err)
 75	}
 76	if status != "WHITE_WON" {
 77		t.Fatalf("expected status WHITE_WON, got %s", status)
 78	}
 79
 80	// 2. Fool's Mate -> Black checkmates White in 2 moves
 81	foolsMate := "f2f3 e7e5 g2g4 d8h4"
 82	_, _, status, err = ReplayUCI(foolsMate)
 83	if err != nil {
 84		t.Fatalf("unexpected error replaying fool's mate: %v", err)
 85	}
 86	if status != "BLACK_WON" {
 87		t.Fatalf("expected status BLACK_WON, got %s", status)
 88	}
 89
 90	// 3. Ongoing game -> status PLAYING
 91	ongoing := "e2e4 e7e5 g1f3"
 92	_, _, status, err = ReplayUCI(ongoing)
 93	if err != nil {
 94		t.Fatalf("unexpected error replaying ongoing game: %v", err)
 95	}
 96	if status != "PLAYING" {
 97		t.Fatalf("expected status PLAYING, got %s", status)
 98	}
 99
100	// 4. Illegal move -> returns error
101	illegal := "e2e5"
102	_, _, _, err = ReplayUCI(illegal)
103	if err == nil {
104		t.Fatal("expected error for illegal move e2e5")
105	}
106}
107