Skip to content

Computer play: Built and tested Stockfish Socket.IO client - #16

Open
Ultree1 wants to merge 6 commits into
mainfrom
feat/stockfish-client
Open

Ultree1 wants to merge 6 commits into
mainfrom
feat/stockfish-client

Conversation

@Ultree1

@Ultree1 Ultree1 commented Oct 11, 2026

Copy link
Copy Markdown
Collaborator

Summary

Resolves #9:
Build a tested Socket.IO client for communicating with the Stockfish chess engine server.

New required env var: add EXPO_PUBLIC_STOCKFISH_SOCKET_URL=http://10.0.2.2:8080 to your .env.local, or the app won't start.

New files:

  • src/features/play/services/StockfishClient.ts - StockfishClient implementation, interfaces, and response validation.
  • test/StockfishClient.test.ts - Tests for StockfishClient.
  • test/mocks/FakeSocket.ts - Fake socket used by the tests in place of a real server connection.
  • docs/stockfish-socket-contract.md - Documentation of the Stockfish server's socket events, payloads, and unresolved details.

Modified files:

  • src/config/env.ts - Validates the new EXPO_PUBLIC_STOCKFISH_SOCKET_URL.
  • .env.example, test/setup.ts, test/env.test.ts - Added the new env var and its tests.

Developers can create a StockfishClient via createStockfishClient(), optionally passing in a url (defaults to the env var), a createSocket factory, and timeoutMs.

Using this client, they can call startSession(), requestMove() with a FEN, player move, and difficulty level 1–5, and disconnect(). The client connects lazily, validates server replies with zod, and rejects on timeouts, connection failures, and server errors without leaving listeners behind. Screens never access the socket directly.

Notes:

  • requestMove() returns only the engine's move. The server's newFEN is wrong whenever a player move is sent (details in the contract doc).
  • Difficulty 1–5 is sent directly as engine search depth.
  • Only one move request can run at a time, because server replies carry no request ID.
  • Auto-reconnect is off. The server deletes the session on disconnect, so callers run startSession() again.
  • Auth and rate limits don't exist on the server. Documented as unresolved.

Tests

Run the new tests with: npm run test -- ./test/StockfishClient.test.ts --verbose.
This should be the result: Tests: 33 passed, 33 total
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Computer play: Build a tested Stockfish Socket.IO client

1 participant