import { describe, it, expect, vi, beforeEach } from "vitest"; // Mock fetch globally const mockFetch = vi.fn(); vi.stubGlobal("fetch", mockFetch); // Import after mocking import { getPlayerStats, getPlayerGames, getBestRating, type ChessComPlayerStats, } from "../chesscom"; beforeEach(() => { mockFetch.mockReset(); }); // -------------------------------------------------------------------------- // Fixture data // -------------------------------------------------------------------------- const MOCK_PLAYER_STATS: ChessComPlayerStats = { chess_rapid: { last: { rating: 1200, date: 1700000000 }, best: { rating: 1350, date: 1690000000 }, record: { win: 50, loss: 30, draw: 10 }, }, chess_blitz: { last: { rating: 1100, date: 1700000000 }, best: { rating: 1250, date: 1695000000 }, record: { win: 100, loss: 80, draw: 20 }, }, chess_bullet: { last: { rating: 950, date: 1700000000 }, best: { rating: 1050, date: 1698000000 }, record: { win: 25, loss: 40, draw: 5 }, }, tactics: { highest: { rating: 2100, date: 1699000000 }, lowest: { rating: 1800, date: 1680000000 }, }, }; const MOCK_GAMES = { games: [ { url: "https://chess.com/game/live/12345", pgn: "1. d4 d5 2. c4 e6", time_control: "600", end_time: 1700000000, rated: true, time_class: "rapid" as const, rules: "chess", white: { rating: 1200, result: "win", username: "testplayer" }, black: { rating: 1180, result: "loss", username: "opponent1" }, }, { url: "https://chess.com/game/live/12346", pgn: "1. e4 c5", time_control: "180", end_time: 1700001000, rated: true, time_class: "blitz" as const, rules: "chess", white: { rating: 1300, result: "loss", username: "opponent2" }, black: { rating: 1100, result: "win", username: "testplayer" }, }, ], }; // -------------------------------------------------------------------------- // getPlayerStats // -------------------------------------------------------------------------- describe("getPlayerStats", () => { it("returns parsed player stats on success", async () => { mockFetch.mockResolvedValueOnce({ ok: true, json: async () => MOCK_PLAYER_STATS, }); const result = await getPlayerStats("testplayer"); expect(result).toEqual(MOCK_PLAYER_STATS); expect(result!.chess_rapid!.last.rating).toBe(1200); }); it("calls the correct Chess.com API endpoint with lowercase username", async () => { mockFetch.mockResolvedValueOnce({ ok: true, json: async () => MOCK_PLAYER_STATS, }); await getPlayerStats("TestPlayer"); expect(mockFetch).toHaveBeenCalledWith( "https://api.chess.com/pub/player/testplayer/stats", expect.objectContaining({ headers: expect.objectContaining({ Accept: "application/json", "User-Agent": expect.stringContaining("rotnot"), }), }) ); }); it("URL-encodes special characters in username", async () => { mockFetch.mockResolvedValueOnce({ ok: true, json: async () => MOCK_PLAYER_STATS, }); await getPlayerStats("user name"); expect(mockFetch).toHaveBeenCalledWith( "https://api.chess.com/pub/player/user%20name/stats", expect.any(Object) ); }); it("returns null for 404 (unknown username)", async () => { mockFetch.mockResolvedValueOnce({ ok: false, status: 404, }); const result = await getPlayerStats("nonexistentuser12345"); expect(result).toBeNull(); }); it("returns null on non-404 HTTP error", async () => { mockFetch.mockResolvedValueOnce({ ok: false, status: 500, }); const result = await getPlayerStats("testplayer"); expect(result).toBeNull(); }); it("returns null on network error", async () => { mockFetch.mockRejectedValueOnce(new Error("Connection refused")); const result = await getPlayerStats("testplayer"); expect(result).toBeNull(); }); it("returns null for empty username", async () => { const result = await getPlayerStats(""); expect(result).toBeNull(); expect(mockFetch).not.toHaveBeenCalled(); }); it("returns null for whitespace-only username", async () => { const result = await getPlayerStats(" "); expect(result).toBeNull(); expect(mockFetch).not.toHaveBeenCalled(); }); }); // -------------------------------------------------------------------------- // getPlayerGames // -------------------------------------------------------------------------- describe("getPlayerGames", () => { it("returns parsed game array on success", async () => { mockFetch.mockResolvedValueOnce({ ok: true, json: async () => MOCK_GAMES, }); const result = await getPlayerGames("testplayer", 2024, 11); expect(result).toHaveLength(2); expect(result[0].white.username).toBe("testplayer"); expect(result[1].time_class).toBe("blitz"); }); it("calls the correct endpoint with zero-padded month", async () => { mockFetch.mockResolvedValueOnce({ ok: true, json: async () => MOCK_GAMES, }); await getPlayerGames("testplayer", 2024, 3); expect(mockFetch).toHaveBeenCalledWith( "https://api.chess.com/pub/player/testplayer/games/2024/03", expect.any(Object) ); }); it("does not zero-pad two-digit months", async () => { mockFetch.mockResolvedValueOnce({ ok: true, json: async () => MOCK_GAMES, }); await getPlayerGames("testplayer", 2024, 12); expect(mockFetch).toHaveBeenCalledWith( "https://api.chess.com/pub/player/testplayer/games/2024/12", expect.any(Object) ); }); it("returns empty array on HTTP error", async () => { mockFetch.mockResolvedValueOnce({ ok: false, status: 404, }); const result = await getPlayerGames("testplayer", 2024, 11); expect(result).toEqual([]); }); it("returns empty array on network error", async () => { mockFetch.mockRejectedValueOnce(new Error("Timeout")); const result = await getPlayerGames("testplayer", 2024, 11); expect(result).toEqual([]); }); it("returns empty array for empty username", async () => { const result = await getPlayerGames("", 2024, 11); expect(result).toEqual([]); expect(mockFetch).not.toHaveBeenCalled(); }); it("handles response with missing games property", async () => { mockFetch.mockResolvedValueOnce({ ok: true, json: async () => ({}), }); const result = await getPlayerGames("testplayer", 2024, 11); expect(result).toEqual([]); }); it("lowercases username in URL", async () => { mockFetch.mockResolvedValueOnce({ ok: true, json: async () => MOCK_GAMES, }); await getPlayerGames("MagnusCarlsen", 2024, 1); expect(mockFetch).toHaveBeenCalledWith( "https://api.chess.com/pub/player/magnuscarlsen/games/2024/01", expect.any(Object) ); }); }); // -------------------------------------------------------------------------- // getBestRating // -------------------------------------------------------------------------- describe("getBestRating", () => { it("returns rapid rating as first priority", () => { const result = getBestRating(MOCK_PLAYER_STATS); expect(result).toBe(1200); // rapid last.rating }); it("falls back to blitz when rapid is missing", () => { const stats: ChessComPlayerStats = { chess_blitz: { last: { rating: 1100, date: 1700000000 }, best: { rating: 1250, date: 1695000000 }, record: { win: 10, loss: 10, draw: 5 }, }, }; const result = getBestRating(stats); expect(result).toBe(1100); }); it("falls back to bullet when rapid and blitz are missing", () => { const stats: ChessComPlayerStats = { chess_bullet: { last: { rating: 900, date: 1700000000 }, best: { rating: 950, date: 1698000000 }, record: { win: 5, loss: 10, draw: 1 }, }, }; const result = getBestRating(stats); expect(result).toBe(900); }); it("falls back to daily as last resort", () => { const stats: ChessComPlayerStats = { chess_daily: { last: { rating: 1400, date: 1700000000 }, best: { rating: 1500, date: 1690000000 }, record: { win: 200, loss: 100, draw: 50 }, }, }; const result = getBestRating(stats); expect(result).toBe(1400); }); it("returns null when no time class ratings exist", () => { const stats: ChessComPlayerStats = { tactics: { highest: { rating: 2100, date: 1699000000 }, lowest: { rating: 1800, date: 1680000000 }, }, }; const result = getBestRating(stats); expect(result).toBeNull(); }); it("returns null for completely empty stats", () => { const result = getBestRating({}); expect(result).toBeNull(); }); it("skips time classes with zero rating", () => { const stats: ChessComPlayerStats = { chess_rapid: { last: { rating: 0, date: 1700000000 }, best: { rating: 0, date: 1700000000 }, record: { win: 0, loss: 0, draw: 0 }, }, chess_blitz: { last: { rating: 1100, date: 1700000000 }, best: { rating: 1250, date: 1695000000 }, record: { win: 10, loss: 10, draw: 5 }, }, }; const result = getBestRating(stats); // 0 is falsy, so it should skip rapid and return blitz expect(result).toBe(1100); }); });