Testing the Bot
We have reached the point where the pieces finally come together. The WebSocket connection works, the PlayOK protocol has been decoded, the chess engine can generate moves, and the bridge can translate those moves into something PlayOK understands. Now it is time to stop looking at individual functions and see whether the whole system actually works.
The most useful test is not another unit test or another print statement. It is a real game.
In this chapter, we will run the bot against real players on PlayOK and examine the resulting console output. The goal is not to prove that the bot is a strong chess player. The goal is to verify that all of the moving parts cooperate correctly: connecting, finding tables, joining games, taking a side, starting the game, receiving moves, updating the engine board, generating replies, and continuing to look for another game when the current one ends.
The First Real Test
When the program starts, the first thing we expect to see is a successful login followed by the range of tables that PlayOK assigned to our user session:
PLAYOK: Logged in as "nnk6619g"
PLAYOK: Table range (500, 599)
This already verifies several parts of the system. The WebSocket connection was established, the initial credentials were accepted, the server sent the login information, and our receive loop recognized the corresponding PlayOK message.
Next, the bot starts watching the lobby:
LOBBY: Game (fkk1680g) vs (sheti) at #512
LOBBY: Game () vs (sheti) at #512
LOBBY: Game (gnt8295g) vs () at #542
LOBBY: Game () vs () at #575
These messages are especially useful during testing because they show that the bot is not blindly connecting to a hard-coded table. It is receiving the table activity from PlayOK, examining the state of each table, and looking for an appropriate place to play.
Eventually it finds an empty table and takes a seat:
TABLE #575: () vs () idle
TABLE #575: () vs (nnk6619g) idle
The repeated status messages are not particularly elegant, but they are useful while developing the client. They show the state transitions as PlayOK processes our commands and reports the resulting table state back to us.
During reverse engineering, console output is often much more valuable than it looks. A line such as TABLE #575: (tmt8482g) vs (nnk6619g) play tells us that several separate protocol operations have already succeeded. Keeping useful state information in the log makes it much easier to find the point where something stops behaving as expected.
Game One
The first opponent to join the table is tmt8482g:
TABLE #575: (tmt8482g) vs (nnk6619g) idle
TABLE #575: (tmt8482g) vs (nnk6619g) play
Our bot is playing Black in this game. Once PlayOK reports that the game has started, the receive loop begins processing moves. The important thing here is that these are not moves generated by our program. They are incoming server messages representing what happened on the table.
The bot reports the moves in normal chess notation:
TABLE #575: white played e4
TABLE #575: black played e5
TABLE #575: white played Bc4
TABLE #575: black played c6
TABLE #575: white played Nc3
TABLE #575: black played d5
TABLE #575: white played exd5
TABLE #575: black played b5
TABLE #575: white played Bb3
TABLE #575: black played b4
TABLE #575: white played Na4
TABLE #575: black played cxd5
TABLE #575: white played d4
TABLE #575: black played exd4
TABLE #575: white played Qxd4
TABLE #575: black played Nf6
TABLE #575: white played Bg5
TABLE #575: black played Nc6
TABLE #575: white played Qe3+
TABLE #575: black played Be6
TABLE #575: white played O-O-O
TABLE #575: black played Ng4
TABLE #575: white played Qd2
TABLE #575: black played Qa5
There is a lot happening behind each line. When PlayOK sends the opponent's move, our receive thread identifies the message, extracts the SAN move, and loads it into the python-chess board. When it becomes our turn, the engine searches the current position and produces a move. The bridge then converts that move into PlayOK's integer representation and sends it through the WebSocket.
That means this simple-looking sequence is actually testing the entire chain we built throughout the book:
PlayOK
↓
WebSocket message
↓
receive_messages()
↓
SAN move
↓
python-chess board
↓
ABC UCI engine
↓
UCI move
↓
PlayOK square conversion
↓
encoded integer
↓
WebSocket message
↓
PlayOK
And this is exactly why a real online game is such a valuable test. A problem anywhere in that chain would eventually become visible as an incorrect move, a desynchronized board, a failed WebSocket action, or a stalled game.
The First Game Ends
The first game does not end with a resignation message in our current log. The opponent disconnects.
That is an important distinction. From the point of view of our bot, the game simply stops producing normal move messages. The current implementation does not yet provide a dedicated human-readable message such as OPPONENT DISCONNECTED or GAME WON.
This is one of the limitations that becomes obvious only when we test against the real service. We successfully played the game, but our logging and state handling do not yet expose every possible reason why a game ended.
There is another important observation, though: the bot did not simply stop after the first game.
Finding Another Game
After the first game is finished, the program continues monitoring the lobby. Eventually, it discovers another set of available tables:
LOBBY: Game (dcs9535g) vs () at #530
LOBBY: Game (yito2904) vs () at #562
LOBBY: Game (wpw1345g) vs () at #506
LOBBY: Game (gkg9387g) vs (knm7516g) at #522
LOBBY: Game (yito2904) vs (droppy44) at #562
LOBBY: Game (04damang) vs (fkw3960g) at #540
LOBBY: Game () vs (bmb1000g) at #566
LOBBY: Game () vs (fkw3960g) at #540
LOBBY: Game () vs () at #566
The bot eventually reaches table #566 and takes the White side:
TABLE #566: (nnk6619g) vs () idle
...
TABLE #566: (nnk6619g) vs (dmc1437g) idle
TABLE #566: (nnk6619g) vs (dmc1437g) play
This is one of the most important results of the test. The bot did not need to be restarted manually after the first game. It remained connected, continued processing the lobby, found another game, joined it, and started playing again.
A bot that can play exactly one game is not yet a useful online client. A successful test should include the transition from one game back to the lobby and then into another game. That transition exercises the state-reset and table-discovery logic just as much as the chess moves exercise the engine integration.
Game Two
The second game gives us a much longer test. This time our bot is White and plays a substantial number of moves:
TABLE #566: white played Nf3
TABLE #566: black played d5
TABLE #566: white played d4
TABLE #566: black played c6
TABLE #566: white played g3
TABLE #566: black played Bf5
TABLE #566: white played Bg2
TABLE #566: black played h6
TABLE #566: white played O-O
TABLE #566: black played e6
TABLE #566: white played c4
TABLE #566: black played Nf6
TABLE #566: white played cxd5
TABLE #566: black played cxd5
TABLE #566: white played Be3
TABLE #566: black played Be7
TABLE #566: white played Nc3
TABLE #566: black played a6
TABLE #566: white played Qb3
TABLE #566: black played b5
The game continues for many more moves. The important thing is not any particular opening or tactical sequence. What we are testing is synchronization over a long-running session.
The engine repeatedly receives the current position, searches for a move, and sends the result back to PlayOK. The board on the Python side must remain synchronized with the actual game on the server. If even one move were missed or applied twice, subsequent SAN moves could fail to load or the engine could begin searching an incorrect position.
The log therefore gives us a practical synchronization test:
TABLE #566: white played Qb7
TABLE #566: black played Bc2
TABLE #566: white played Na7
TABLE #566: black played Re8
TABLE #566: white played Nc6
TABLE #566: black played Qd7
TABLE #566: white played Qxd7
TABLE #566: black played Nxd7
TABLE #566: white played Nxe7+
TABLE #566: black played Rxe7
TABLE #566: white played Bd6
TABLE #566: black played Ree8
Captures, checks, castling, and moves involving multiple pieces of the same type all pass through the same basic mechanism. The SAN supplied by PlayOK is loaded into the current python-chess board, and the engine searches that updated position.
A Longer Stress Test
The second game eventually reaches a much deeper position:
TABLE #566: white played e4
TABLE #566: black played Nf6
TABLE #566: white played e5
TABLE #566: black played Nd7
TABLE #566: white played Rfe1
TABLE #566: black played Nb6
TABLE #566: white played Rb1
TABLE #566: black played Na4
TABLE #566: white played Re3
TABLE #566: black played g6
TABLE #566: white played Bf3
TABLE #566: black played h5
TABLE #566: white played h3
TABLE #566: black played Kg7
TABLE #566: white played Kg2
TABLE #566: black played Ra7
TABLE #566: white played Re2
TABLE #566: black played Rc8
TABLE #566: white played Ree1
TABLE #566: black played Rb7
TABLE #566: white played Bb4
TABLE #566: black played Bc2
TABLE #566: white played Rbc1
TABLE #566: black played Bd3
TABLE #566: white played Red1
TABLE #566: black played Nxb2
TABLE #566: white played Rd2
At this point we have exercised a large portion of the complete system. The game has lasted long enough to test repeated engine searches, repeated WebSocket sends, repeated incoming messages, board updates, and the maintenance of PlayOK state.
The second opponent eventually resigned. Our current log does not explicitly report that event either. From the available move output, we can see that the game was progressing normally, but the reason for the final result is not represented in a dedicated status message.
Watching the Games
The games from this test were recorded by PlayOK and can be viewed directly while they remain available. PlayOK only stores games for a limited period, so these links are temporary references to the actual games used during testing.
There is also a video from an earlier stage of development where I was testing the bot online while recording the process:
Watch the earlier online bot test on YouTube
The video is useful for seeing the project as a real running system rather than just source code and console output. The implementation in the video is from an earlier stage, so it should be viewed as part of the development history rather than as a description of the final version from this chapter.
What This Test Actually Proves
It is tempting to look at the two games and simply say that the bot won twice. That is certainly the visible result of the test, but for our purposes the more interesting information is everything that had to work to make those games possible.
- The bot connected to the PlayOK WebSocket.
- The server accepted the initial session information.
- The bot identified its username.
- The bot discovered the active table range.
- The receive loop processed lobby activity.
- The bot found an available table.
- The bot joined a table and selected a side.
- The bot detected when an opponent joined.
- The bot started the game.
- Incoming chess moves were converted into updates to the local chess board.
- The UCI engine generated replies.
- The engine moves were converted into PlayOK's integer representation.
- The moves were sent through the WebSocket.
- The bot remained synchronized through a long sequence of moves.
- The first game ended without requiring a program restart.
- The bot returned to the lobby and found another game.
- The bot successfully played a second game.
That is a much more meaningful test than checking whether an individual function returns the expected integer.
For a network client, successful testing means more than verifying individual functions. We want to know whether the complete chain survives real server traffic, asynchronous events, state changes, and repeated actions. Real interaction is where assumptions that looked reasonable in isolated tests finally get challenged.
What the Log Does Not Tell Us
There is also a useful lesson in what is missing from the log.
The first opponent disconnected, and the second opponent resigned, but the program does not explicitly say so. The bot also does not print a final result such as 1-0, 0-1, or draw.
This is not necessarily a problem with the core chess-playing mechanism. It is simply functionality that we have not implemented yet.
Our receive loop currently understands the messages that are necessary for the basic bot to play games, but it does not yet provide a complete user-facing interpretation of every game-ending event that PlayOK can report.
This is exactly the kind of gap that becomes visible through testing. We did not need to invent a hypothetical requirement. The real game exposed it for us.
An Exercise for the Reader
At this point, the natural next step is to upgrade the bot so that it understands how games end.
Modify the receive-message logic so that it can recognize the relevant PlayOK messages for events such as an opponent disconnecting, resigning, winning, losing, or drawing. Then update the console output so the bot reports the result explicitly.
A useful target would be something like:
TABLE #566: Game finished
TABLE #566: Result: opponent resigned
TABLE #566: Bot wins
The exact PlayOK messages required to implement this feature are left for you to discover. Use the same reverse-engineering process we used throughout the book: open the browser, watch the WebSocket traffic, perform one action at a time, and compare the messages before and after the event.
You will probably find that there is more to learn from the protocol once you start looking for game-ending events. The important part is not to guess what the server should send. Observe what it actually sends.
Testing as Part of Reverse Engineering
There is a larger lesson here. Testing was not something we postponed until the end of the project. In a reverse-engineering project, testing and discovery are tightly connected.
When we first looked at the WebSocket traffic, we did not know what the integer values meant. We observed them.
When we did not know which message represented a table action, we performed an action and watched the traffic.
When we did not know how PlayOK represented chess squares, we compared known moves and worked out the encoding.
And now that the bot can play real games, the games themselves become another source of information about the protocol.
The development cycle is therefore not simply:
write code → test code → finish
It is closer to:
observe
↓
form a hypothesis
↓
implement
↓
test against the real application
↓
observe unexpected behavior
↓
refine the protocol model
↓
update the implementation
↓
test again
That loop is the real skill behind this project.
The Bot Is Now a Working System
At the beginning of the book, PlayOK's WebSocket traffic looked like a stream of unexplained JSON objects containing arbitrary integers, strings, and arrays. We had no formal protocol documentation to work from.
By working from the browser outward, we gradually identified the pieces we needed. We learned how to inspect WebSocket traffic, organize the project, configure the protocol constants, connect to the server, receive messages, track PlayOK state, translate chess moves, communicate with a UCI engine, and send actions back to the server.
Now the result is no longer just a collection of decoded messages. It is a working automated client.
PLAYOK
↕
WebSocket protocol
↕
playok.py
↙ ↘
PlayOK state ChessEngine
↕
python-chess
↕
ABC UCI
The two successful games are the final integration test for the version of the bot we built in this book. The first opponent disconnected. The second opponent resigned. In both cases, the bot successfully played the game, and after the first game it continued running and found another opponent instead of stopping.
There is still plenty that could be improved. Better game-result handling, cleaner logging, more robust error handling, more sophisticated table selection, reconnect logic, and additional PlayOK features would all make the client more complete.
But those are now engineering problems rather than mysteries. We have a working foundation, and more importantly, we have learned how to investigate the protocol ourselves.
The final product of reverse engineering is not merely a collection of protocol constants. It is an understanding of how the system behaves. Once we understand that behavior, we can reproduce it, automate it, test it, and extend it.
Where to Go Next
The project can now continue in whatever direction interests you. You can improve the bot's handling of game results, make the table-selection strategy smarter, add reconnection support, expose more PlayOK commands, improve the console interface, or experiment with a different chess engine.
More importantly, the same methodology can be applied to applications other than PlayOK. Whenever a browser application maintains a WebSocket connection, the browser gives us a valuable starting point: it shows us the messages being exchanged and the actions that cause those messages to appear.
We started with traffic that looked like gibberish. We ended with a bot that can connect to a real online chess server, find games, play chess through a UCI engine, and continue looking for another opponent.
That is the practical result of reverse engineering a WebSocket.