Skip to content

Commit ba16155

Browse files
Merge branch 'jhlywa:master' into master
2 parents c57e42d + daf4eec commit ba16155

3 files changed

Lines changed: 80 additions & 5 deletions

File tree

__tests__/load.test.ts

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
import { Chess, DEFAULT_POSITION, SEVEN_TAG_ROSTER } from '../src/chess'
21
import { expect, test } from 'vitest'
2+
import {
3+
BLACK,
4+
Chess,
5+
DEFAULT_POSITION,
6+
KING,
7+
QUEEN,
8+
SEVEN_TAG_ROSTER,
9+
WHITE,
10+
} from '../src/chess'
311

412
test('load - default position', () => {
513
const chess = new Chess()
@@ -56,6 +64,66 @@ test('load - missing FEN tokens (no castling rights, ep square, or move numbers)
5664
expect(() => chess.load(fen)).not.toThrow()
5765
})
5866

67+
test('load - full castling rights but white king moved', () => {
68+
const chess = new Chess()
69+
const fen = 'r3k2r/8/8/8/8/8/8/R2K3R w KQkq - 0 1'
70+
chess.load(fen)
71+
expect(chess.getCastlingRights(WHITE)).toEqual({
72+
[KING]: false,
73+
[QUEEN]: false,
74+
})
75+
})
76+
77+
test('load - full castling rights but white kingside rook moved', () => {
78+
const chess = new Chess()
79+
const fen = 'r3k2r/8/8/8/8/8/8/R3K1R1 w KQkq - 0 1'
80+
chess.load(fen)
81+
expect(chess.getCastlingRights(WHITE)).toEqual({
82+
[KING]: false,
83+
[QUEEN]: true,
84+
})
85+
})
86+
87+
test('load - full castling rights but white queenside rook moved', () => {
88+
const chess = new Chess()
89+
const fen = 'r3k2r/8/8/8/8/8/8/1R2K2R w KQkq - 0 1'
90+
chess.load(fen)
91+
expect(chess.getCastlingRights(WHITE)).toEqual({
92+
[KING]: true,
93+
[QUEEN]: false,
94+
})
95+
})
96+
97+
test('load - full castling rights but black king moved', () => {
98+
const chess = new Chess()
99+
const fen = 'r2k3r/8/8/8/8/8/8/R3K2R w KQkq - 0 1'
100+
chess.load(fen)
101+
expect(chess.getCastlingRights(BLACK)).toEqual({
102+
[KING]: false,
103+
[QUEEN]: false,
104+
})
105+
})
106+
107+
test('load - full castling rights but black kingside rook moved', () => {
108+
const chess = new Chess()
109+
const fen = 'r3k1r1/8/8/8/8/8/8/R3K2R w KQkq - 0 1'
110+
chess.load(fen)
111+
expect(chess.getCastlingRights(BLACK)).toEqual({
112+
[KING]: false,
113+
[QUEEN]: true,
114+
})
115+
})
116+
117+
test('load - full castling rights but black queenside rook moved', () => {
118+
const chess = new Chess()
119+
const fen = '1r2k2r/8/8/8/8/8/8/R3K2R w KQkq - 0 1'
120+
chess.load(fen)
121+
expect(chess.getCastlingRights(BLACK)).toEqual({
122+
[KING]: true,
123+
[QUEEN]: false,
124+
})
125+
})
126+
59127
test('load - preserveHeaders = false', () => {
60128
const chess = new Chess()
61129
chess.setHeader('White', 'Magnus Carlsen')

__tests__/regression.test.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,25 @@ describe('Regression Tests', () => {
3333
const chess = new Chess()
3434
const pgn = [
3535
'[SetUp "1"]',
36-
'[FEN "7k/5K2/4R3/8/8/8/8/8 w KQkq - 0 1"]',
36+
'[FEN "7k/5K2/4R3/8/8/8/8/8 w - - 0 1"]',
3737
'',
3838
'1. Rh6#',
3939
]
4040
chess.loadPgn(pgn.join('\n'))
41-
expect(chess.fen()).toBe('7k/5K2/7R/8/8/8/8/8 b KQkq - 1 1')
41+
expect(chess.fen()).toBe('7k/5K2/7R/8/8/8/8/8 b - - 1 1')
4242
})
4343

4444
it('Github Issue #85 (black) - SetUp and FEN should be accepted in loadPgn', () => {
4545
const chess = new Chess()
4646
const pgn = [
4747
'[SetUp "1"]',
48-
'[FEN "r4r1k/1p4b1/3p3p/5qp1/1RP5/6P1/3NP3/2Q2RKB b KQkq - 0 1"]',
48+
'[FEN "r4r1k/1p4b1/3p3p/5qp1/1RP5/6P1/3NP3/2Q2RKB b - - 0 1"]',
4949
'',
5050
'1. ... Qc5+',
5151
]
5252
chess.loadPgn(pgn.join('\n'))
5353
expect(chess.fen()).toBe(
54-
'r4r1k/1p4b1/3p3p/2q3p1/1RP5/6P1/3NP3/2Q2RKB w KQkq - 1 2',
54+
'r4r1k/1p4b1/3p3p/2q3p1/1RP5/6P1/3NP3/2Q2RKB w - - 1 2',
5555
)
5656
})
5757

@@ -303,4 +303,9 @@ describe('Regression Tests', () => {
303303

304304
expect(chess.moveNumber()).toBe(31)
305305
})
306+
307+
it('Github Issue #552 - ignore invalid castling rights', () => {
308+
const chess = new Chess('kb4r1/p2n3P/1PP5/1P6/8/8/6p1/R3KR2 b KQkq - 0 19')
309+
expect(chess.isGameOver()).toBe(false) // this was crashing due to invalid castling moves being generated
310+
})
306311
})

src/chess.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,8 @@ export class Chess {
811811
this._castling.b |= BITS.QSIDE_CASTLE
812812
}
813813

814+
this._updateCastlingRights()
815+
814816
this._epSquare = tokens[3] === '-' ? EMPTY : Ox88[tokens[3] as Square]
815817
this._fenEpSquare = this._epSquare
816818
this._halfMoves = parseInt(tokens[4], 10)

0 commit comments

Comments
 (0)