/** * 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(); }); });