File size: 9,285 Bytes
9580dce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
 * Tests for src/lib/chess/pgn.ts
 *
 * Covers:
 *   parseUci
 *     - standard 4-char quiet move
 *     - standard 4-char capture move
 *     - promotion move (5 chars) — all four promotion pieces
 *     - no promotion field for 4-char moves
 *     - from/to squares are typed correctly
 *
 *   isValidUci
 *     - valid 4-char moves (quiet, capture)
 *     - valid 5-char promotion moves (all pieces)
 *     - rejects wrong length (3, 6, empty)
 *     - rejects invalid file (letters outside a-h)
 *     - rejects invalid rank (numbers outside 1-8, 0, 9)
 *     - rejects invalid promotion piece
 *     - rejects uppercase letters
 *
 *   fenFromPgn
 *     - valid PGN returns a non-empty FEN string
 *     - returned FEN is a valid chess position (parseable by Chess)
 *     - returns null for empty string
 *     - returns null for whitespace-only string
 *     - returns null for malformed PGN
 *     - short PGN (1 move) returns correct FEN
 *     - FEN after 1.e4 is the standard e4-pawn position
 *
 *   sideToMove
 *     - starting position is white
 *     - known FEN after 1.e4 is black to move
 *     - returns null for empty string
 *     - returns null for invalid FEN
 *
 *   puzzlePlayerColor
 *     - when it's white's turn (opponent moves first as white), player is black
 *     - when it's black's turn (opponent moves first as black), player is white
 *     - returns null for invalid FEN
 *     - returns null for empty FEN
 */

import { describe, it, expect } from "vitest";
import {
  parseUci,
  isValidUci,
  fenFromPgn,
  sideToMove,
  puzzlePlayerColor,
} from "@/lib/chess/pgn";

// ---------------------------------------------------------------------------
// parseUci
// ---------------------------------------------------------------------------

describe("parseUci", () => {
  it("parses a standard quiet move (e2e4)", () => {
    const move = parseUci("e2e4");
    expect(move.from).toBe("e2");
    expect(move.to).toBe("e4");
    expect(move.promotion).toBeUndefined();
  });

  it("parses a capture move (d5e4)", () => {
    const move = parseUci("d5e4");
    expect(move.from).toBe("d5");
    expect(move.to).toBe("e4");
    expect(move.promotion).toBeUndefined();
  });

  it("parses a queen promotion (e7e8q)", () => {
    const move = parseUci("e7e8q");
    expect(move.from).toBe("e7");
    expect(move.to).toBe("e8");
    expect(move.promotion).toBe("q");
  });

  it("parses a rook promotion (a7a8r)", () => {
    const move = parseUci("a7a8r");
    expect(move.promotion).toBe("r");
  });

  it("parses a bishop promotion (h7h8b)", () => {
    const move = parseUci("h7h8b");
    expect(move.promotion).toBe("b");
  });

  it("parses a knight promotion (b2b1n)", () => {
    const move = parseUci("b2b1n");
    expect(move.from).toBe("b2");
    expect(move.to).toBe("b1");
    expect(move.promotion).toBe("n");
  });

  it("does not set promotion for a 4-char king-side castle move (e1g1)", () => {
    const move = parseUci("e1g1");
    expect(move.promotion).toBeUndefined();
  });

  it("from and to are exactly 2-character strings", () => {
    const move = parseUci("h8a1");
    expect(move.from.length).toBe(2);
    expect(move.to.length).toBe(2);
  });
});

// ---------------------------------------------------------------------------
// isValidUci
// ---------------------------------------------------------------------------

describe("isValidUci", () => {
  it("accepts a valid 4-char move (e2e4)", () => {
    expect(isValidUci("e2e4")).toBe(true);
  });

  it("accepts a valid 4-char move spanning the whole board (a1h8)", () => {
    expect(isValidUci("a1h8")).toBe(true);
  });

  it("accepts a valid queen promotion (e7e8q)", () => {
    expect(isValidUci("e7e8q")).toBe(true);
  });

  it("accepts a valid rook promotion (a2a1r)", () => {
    expect(isValidUci("a2a1r")).toBe(true);
  });

  it("accepts a valid bishop promotion (h7h8b)", () => {
    expect(isValidUci("h7h8b")).toBe(true);
  });

  it("accepts a valid knight promotion (d2d1n)", () => {
    expect(isValidUci("d2d1n")).toBe(true);
  });

  it("rejects an empty string", () => {
    expect(isValidUci("")).toBe(false);
  });

  it("rejects a 3-character string", () => {
    expect(isValidUci("e2e")).toBe(false);
  });

  it("rejects a 6-character string", () => {
    expect(isValidUci("e2e4qq")).toBe(false);
  });

  it("rejects invalid file letter (i-file does not exist)", () => {
    expect(isValidUci("i2e4")).toBe(false);
  });

  it("rejects rank 0 (out of range)", () => {
    expect(isValidUci("e0e4")).toBe(false);
  });

  it("rejects rank 9 (out of range)", () => {
    expect(isValidUci("e2e9")).toBe(false);
  });

  it("rejects uppercase file letters", () => {
    expect(isValidUci("E2e4")).toBe(false);
  });

  it("rejects invalid promotion piece (king cannot promote)", () => {
    expect(isValidUci("e7e8k")).toBe(false);
  });

  it("rejects invalid promotion piece (uppercase Q)", () => {
    expect(isValidUci("e7e8Q")).toBe(false);
  });
});

// ---------------------------------------------------------------------------
// fenFromPgn
// ---------------------------------------------------------------------------

// A simple 2-move PGN for testing: 1.e4 e5
const TWO_MOVE_PGN = "1. e4 e5";

// Starting FEN for reference
const START_FEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";

// FEN after 1.e4 — chess.js normalises ep square to "-" when no capture possible
const AFTER_E4_FEN = "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1";

// FEN after 1.e4 e5 — no en-passant capture possible, so "-"
const AFTER_E4_E5_FEN = "rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2";

describe("fenFromPgn", () => {
  it("returns a non-empty string for a valid PGN", () => {
    const fen = fenFromPgn(TWO_MOVE_PGN);
    expect(typeof fen).toBe("string");
    expect((fen as string).length).toBeGreaterThan(0);
  });

  it("returns the correct FEN after 1.e4 e5", () => {
    const fen = fenFromPgn(TWO_MOVE_PGN);
    expect(fen).toBe(AFTER_E4_E5_FEN);
  });

  it("returns the correct FEN after 1.e4 only", () => {
    const fen = fenFromPgn("1. e4");
    expect(fen).toBe(AFTER_E4_FEN);
  });

  it("returns the starting FEN for an empty move list PGN", () => {
    // A PGN with header but no moves — chess.js returns the start position
    const fen = fenFromPgn(
      '[Event "Test"]\n[Site "?"]\n[Date "????.??.??"]\n[Result "*"]\n\n*',
    );
    expect(fen).toBe(START_FEN);
  });

  it("returns null for an empty string", () => {
    expect(fenFromPgn("")).toBeNull();
  });

  it("returns null for a whitespace-only string", () => {
    expect(fenFromPgn("   \n\t")).toBeNull();
  });

  it("returns null for a malformed PGN that chess.js cannot load", () => {
    expect(fenFromPgn("this is not a PGN at all!!!")).toBeNull();
  });

  it("returned FEN is parseable by Chess constructor (valid position)", async () => {
    const { Chess } = await import("chess.js");
    const fen = fenFromPgn(TWO_MOVE_PGN);
    expect(() => new Chess(fen ?? undefined)).not.toThrow();
  });
});

// ---------------------------------------------------------------------------
// sideToMove
// ---------------------------------------------------------------------------

describe("sideToMove", () => {
  it("returns 'w' for the starting position", () => {
    expect(sideToMove(START_FEN)).toBe("w");
  });

  it("returns 'b' for a position where black is to move (after 1.e4)", () => {
    expect(sideToMove(AFTER_E4_FEN)).toBe("b");
  });

  it("returns 'w' for a position where white is to move (after 1.e4 e5)", () => {
    expect(sideToMove(AFTER_E4_E5_FEN)).toBe("w");
  });

  it("returns null for an empty string", () => {
    expect(sideToMove("")).toBeNull();
  });

  it("returns null for a whitespace-only string", () => {
    expect(sideToMove("   ")).toBeNull();
  });

  it("returns null for an invalid FEN string", () => {
    expect(sideToMove("not/a/fen")).toBeNull();
  });
});

// ---------------------------------------------------------------------------
// puzzlePlayerColor
// ---------------------------------------------------------------------------

describe("puzzlePlayerColor", () => {
  it("returns 'black' when it is white's turn (opponent plays white first)", () => {
    // START_FEN: white to move → opponent is white → player is black
    expect(puzzlePlayerColor(START_FEN)).toBe("black");
  });

  it("returns 'white' when it is black's turn (opponent plays black first)", () => {
    // AFTER_E4_FEN: black to move → opponent is black → player is white
    expect(puzzlePlayerColor(AFTER_E4_FEN)).toBe("white");
  });

  it("agrees with sideToMove (player is always the opposite of active side)", () => {
    const fens = [START_FEN, AFTER_E4_FEN, AFTER_E4_E5_FEN];
    for (const fen of fens) {
      const turn = sideToMove(fen);
      const player = puzzlePlayerColor(fen);
      if (turn === "w") {
        expect(player).toBe("black");
      } else {
        expect(player).toBe("white");
      }
    }
  });

  it("returns null for an empty string", () => {
    expect(puzzlePlayerColor("")).toBeNull();
  });

  it("returns null for an invalid FEN", () => {
    expect(puzzlePlayerColor("garbage fen string")).toBeNull();
  });
});