diff --git a/jest.setup.ts b/jest.setup.ts index 71139e9..a8e845c 100644 --- a/jest.setup.ts +++ b/jest.setup.ts @@ -5,6 +5,7 @@ if (typeof window !== 'undefined') { // Mock scrollIntoView for JSDOM window.HTMLElement.prototype.scrollIntoView = jest.fn(); window.HTMLMediaElement.prototype.play = () => Promise.resolve(); + window.HTMLMediaElement.prototype.pause = jest.fn(); } // Mock react-markdown to avoid ESM issues in Jest diff --git a/src/components/ChessGame.test.tsx b/src/components/ChessGame.test.tsx index 02c7394..bcd0719 100644 --- a/src/components/ChessGame.test.tsx +++ b/src/components/ChessGame.test.tsx @@ -143,15 +143,17 @@ describe("ChessGame Component", () => { }); it("restores a PGN game and persists save data without apiKey", async () => { - render( - {}} - /> - ); + await act(async () => { + render( + {}} + /> + ); + }); await waitFor(() => { const saved = JSON.parse(localStorage.getItem("chess_tutor_save") || "{}"); @@ -196,6 +198,16 @@ describe("ChessGame Component", () => { /> ); + await waitFor(() => { + expect(stockfishMock.evaluate).toHaveBeenCalled(); + }); + + await waitFor(() => { + expect(screen.getByTestId("tutor")).toHaveTextContent("Eval P0: 0.5"); + }); + + stockfishMock.evaluate.mockClear(); + await act(async () => { fireEvent.click(screen.getByTestId("chessboard")); fireEvent.click(screen.getByTestId("chessboard")); @@ -203,10 +215,10 @@ describe("ChessGame Component", () => { }); await waitFor(() => { - expect(stockfishMock.evaluate.mock.calls.length).toBeGreaterThanOrEqual(2); + expect(stockfishMock.evaluate.mock.calls.length).toBeGreaterThanOrEqual(1); }); const playerTriggeredEvaluations = stockfishMock.evaluate.mock.calls.filter(([fen]: [string]) => typeof fen === "string"); - expect(playerTriggeredEvaluations.length).toBeLessThanOrEqual(3); + expect(playerTriggeredEvaluations.length).toBeLessThanOrEqual(2); }); }); diff --git a/src/components/ChessGame.tsx b/src/components/ChessGame.tsx index 36748a0..812e18f 100644 --- a/src/components/ChessGame.tsx +++ b/src/components/ChessGame.tsx @@ -289,7 +289,6 @@ export default function ChessGame({ gameId, initialFen, initialPgn, initialPerso fen, language, selectedPersonality, - apiKey, playerColor, // Save player color too pgn: gameRef.current.pgn(), updatedAt: Date.now(), @@ -302,7 +301,7 @@ export default function ChessGame({ gameId, initialFen, initialPgn, initialPerso upsertSavedGame(saveData); localStorage.setItem("chess_tutor_save", JSON.stringify(saveData)); - }, [fen, language, selectedPersonality, apiKey, playerColor, gameId, evalP0]); + }, [fen, language, selectedPersonality, playerColor, gameId, evalP0]); // Game Over Detection useEffect(() => {