import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; // Mock fetch globally const mockFetch = vi.fn(); vi.stubGlobal("fetch", mockFetch); // Import after mocking import { getLichessPuzzle, getLichessDailyPuzzle, challengeStockfish, getGameStream, } from "../lichess"; // Store original env const originalEnv = { ...process.env }; beforeEach(() => { mockFetch.mockReset(); process.env = { ...originalEnv }; }); afterEach(() => { process.env = originalEnv; }); // -------------------------------------------------------------------------- // Fixture data // -------------------------------------------------------------------------- const MOCK_PUZZLE_RESPONSE = { game: { id: "abc123", perf: { key: "blitz", name: "Blitz" }, rated: true, players: [ { name: "player1", id: "p1", color: "white" }, { name: "player2", id: "p2", color: "black" }, ], pgn: "1. e4 e5 2. Nf3 Nc6", clock: "5+3", }, puzzle: { id: "pzl001", rating: 1500, plays: 42000, solution: ["e2e4", "d7d5", "e4d5"], themes: ["middlegame", "short"], }, }; // -------------------------------------------------------------------------- // getLichessPuzzle // -------------------------------------------------------------------------- describe("getLichessPuzzle", () => { it("returns parsed puzzle response on success", async () => { mockFetch.mockResolvedValueOnce({ ok: true, json: async () => MOCK_PUZZLE_RESPONSE, }); const result = await getLichessPuzzle(); expect(result).toEqual(MOCK_PUZZLE_RESPONSE); expect(result!.puzzle.id).toBe("pzl001"); expect(result!.puzzle.rating).toBe(1500); expect(result!.game.pgn).toBe("1. e4 e5 2. Nf3 Nc6"); }); it("calls the correct Lichess API endpoint", async () => { mockFetch.mockResolvedValueOnce({ ok: true, json: async () => MOCK_PUZZLE_RESPONSE, }); await getLichessPuzzle(); expect(mockFetch).toHaveBeenCalledWith( "https://lichess.org/api/puzzle/next", expect.objectContaining({ headers: expect.objectContaining({ Accept: "application/json" }), }) ); }); it("includes Authorization header when LICHESS_API_TOKEN is set", async () => { process.env.LICHESS_API_TOKEN = "lip_test_token_123"; mockFetch.mockResolvedValueOnce({ ok: true, json: async () => MOCK_PUZZLE_RESPONSE, }); await getLichessPuzzle(); expect(mockFetch).toHaveBeenCalledWith( expect.any(String), expect.objectContaining({ headers: expect.objectContaining({ Authorization: "Bearer lip_test_token_123", }), }) ); }); it("omits Authorization header when no token is set", async () => { delete process.env.LICHESS_API_TOKEN; mockFetch.mockResolvedValueOnce({ ok: true, json: async () => MOCK_PUZZLE_RESPONSE, }); await getLichessPuzzle(); const headers = mockFetch.mock.calls[0][1].headers; expect(headers).not.toHaveProperty("Authorization"); }); it("returns null on non-OK response", async () => { mockFetch.mockResolvedValueOnce({ ok: false, status: 429, statusText: "Too Many Requests", }); const result = await getLichessPuzzle(); expect(result).toBeNull(); }); it("returns null on network error", async () => { mockFetch.mockRejectedValueOnce(new Error("Network timeout")); const result = await getLichessPuzzle(); expect(result).toBeNull(); }); }); // -------------------------------------------------------------------------- // getLichessDailyPuzzle // -------------------------------------------------------------------------- describe("getLichessDailyPuzzle", () => { it("returns the daily puzzle on success", async () => { mockFetch.mockResolvedValueOnce({ ok: true, json: async () => MOCK_PUZZLE_RESPONSE, }); const result = await getLichessDailyPuzzle(); expect(result).toEqual(MOCK_PUZZLE_RESPONSE); }); it("calls the daily puzzle endpoint (not /next)", async () => { mockFetch.mockResolvedValueOnce({ ok: true, json: async () => MOCK_PUZZLE_RESPONSE, }); await getLichessDailyPuzzle(); expect(mockFetch).toHaveBeenCalledWith( "https://lichess.org/api/puzzle/daily", expect.any(Object) ); }); it("does not send Authorization header (public endpoint)", async () => { process.env.LICHESS_API_TOKEN = "lip_should_not_appear"; mockFetch.mockResolvedValueOnce({ ok: true, json: async () => MOCK_PUZZLE_RESPONSE, }); await getLichessDailyPuzzle(); const headers = mockFetch.mock.calls[0][1].headers; expect(headers).not.toHaveProperty("Authorization"); }); it("returns null on HTTP error", async () => { mockFetch.mockResolvedValueOnce({ ok: false, status: 500, statusText: "Internal Server Error", }); const result = await getLichessDailyPuzzle(); expect(result).toBeNull(); }); it("returns null on fetch error", async () => { mockFetch.mockRejectedValueOnce(new Error("DNS resolution failed")); const result = await getLichessDailyPuzzle(); expect(result).toBeNull(); }); }); // -------------------------------------------------------------------------- // challengeStockfish // -------------------------------------------------------------------------- describe("challengeStockfish", () => { it("returns null when LICHESS_API_TOKEN is not set", async () => { delete process.env.LICHESS_API_TOKEN; const result = await challengeStockfish(); expect(result).toBeNull(); expect(mockFetch).not.toHaveBeenCalled(); }); it("creates a Stockfish challenge and returns id + url", async () => { process.env.LICHESS_API_TOKEN = "lip_test_token"; mockFetch.mockResolvedValueOnce({ ok: true, json: async () => ({ id: "game789" }), }); const result = await challengeStockfish(5); expect(result).toEqual({ id: "game789", url: "https://lichess.org/game789", }); }); it("sends POST to the correct endpoint with form data", async () => { process.env.LICHESS_API_TOKEN = "lip_test_token"; mockFetch.mockResolvedValueOnce({ ok: true, json: async () => ({ id: "game001" }), }); await challengeStockfish(3); expect(mockFetch).toHaveBeenCalledWith( "https://lichess.org/api/challenge/stockfish", expect.objectContaining({ method: "POST", headers: expect.objectContaining({ "Content-Type": "application/x-www-form-urlencoded", Authorization: "Bearer lip_test_token", }), }) ); // Verify the body contains the level const body = mockFetch.mock.calls[0][1].body; expect(body).toContain("level=3"); }); it("clamps level to minimum of 1", async () => { process.env.LICHESS_API_TOKEN = "lip_test_token"; mockFetch.mockResolvedValueOnce({ ok: true, json: async () => ({ id: "game001" }), }); await challengeStockfish(-5); const body = mockFetch.mock.calls[0][1].body; expect(body).toContain("level=1"); }); it("clamps level to maximum of 8", async () => { process.env.LICHESS_API_TOKEN = "lip_test_token"; mockFetch.mockResolvedValueOnce({ ok: true, json: async () => ({ id: "game001" }), }); await challengeStockfish(99); const body = mockFetch.mock.calls[0][1].body; expect(body).toContain("level=8"); }); it("defaults to level 4 when no level provided", async () => { process.env.LICHESS_API_TOKEN = "lip_test_token"; mockFetch.mockResolvedValueOnce({ ok: true, json: async () => ({ id: "game001" }), }); await challengeStockfish(); const body = mockFetch.mock.calls[0][1].body; expect(body).toContain("level=4"); }); it("returns null on non-OK response", async () => { process.env.LICHESS_API_TOKEN = "lip_test_token"; mockFetch.mockResolvedValueOnce({ ok: false, status: 403, text: async () => "Forbidden", }); const result = await challengeStockfish(); expect(result).toBeNull(); }); it("returns null on network error", async () => { process.env.LICHESS_API_TOKEN = "lip_test_token"; mockFetch.mockRejectedValueOnce(new Error("Connection refused")); const result = await challengeStockfish(); expect(result).toBeNull(); }); }); // -------------------------------------------------------------------------- // getGameStream // -------------------------------------------------------------------------- describe("getGameStream", () => { it("returns null when LICHESS_API_TOKEN is not set", async () => { delete process.env.LICHESS_API_TOKEN; const result = await getGameStream("game123"); expect(result).toBeNull(); expect(mockFetch).not.toHaveBeenCalled(); }); it("returns the response body stream on success", async () => { process.env.LICHESS_API_TOKEN = "lip_test_token"; const mockStream = new ReadableStream(); mockFetch.mockResolvedValueOnce({ ok: true, body: mockStream, }); const result = await getGameStream("game123"); expect(result).toBe(mockStream); }); it("calls the correct stream endpoint with NDJSON accept header", async () => { process.env.LICHESS_API_TOKEN = "lip_test_token"; mockFetch.mockResolvedValueOnce({ ok: true, body: new ReadableStream(), }); await getGameStream("abc456"); expect(mockFetch).toHaveBeenCalledWith( "https://lichess.org/api/board/game/stream/abc456", expect.objectContaining({ headers: expect.objectContaining({ Accept: "application/x-ndjson", Authorization: "Bearer lip_test_token", }), }) ); }); it("returns null on non-OK response", async () => { process.env.LICHESS_API_TOKEN = "lip_test_token"; mockFetch.mockResolvedValueOnce({ ok: false, status: 404, body: null, }); const result = await getGameStream("nonexistent"); expect(result).toBeNull(); }); it("returns null when response body is null", async () => { process.env.LICHESS_API_TOKEN = "lip_test_token"; mockFetch.mockResolvedValueOnce({ ok: true, body: null, }); const result = await getGameStream("game123"); expect(result).toBeNull(); }); it("returns null on network error", async () => { process.env.LICHESS_API_TOKEN = "lip_test_token"; mockFetch.mockRejectedValueOnce(new Error("Stream interrupted")); const result = await getGameStream("game123"); expect(result).toBeNull(); }); });