|
| 1 | +(global as any).CLIENT = true; |
| 2 | + |
| 3 | +import WS from "jest-websocket-mock"; |
| 4 | +import { TestGoban } from "../../src/index"; |
| 5 | +import { GobanSocket } from "engine"; |
| 6 | + |
| 7 | +let last_port = 38880; |
| 8 | + |
| 9 | +async function createGobanWithSocket(game_id: number): Promise<any[]> { |
| 10 | + const port = ++last_port; |
| 11 | + const server = new WS(`ws://localhost:${port}`, { jsonProtocol: true }); |
| 12 | + const client = new GobanSocket(`ws://localhost:${port}`, { dont_ping: true, quiet: true }); |
| 13 | + const instance = new TestGoban({ game_id, server_socket: client }); |
| 14 | + (instance as any).post_config_constructor(); |
| 15 | + await server.connected; |
| 16 | + return [server, client, instance]; |
| 17 | +} |
| 18 | + |
| 19 | +describe("OGSConnectivity isInPushedAnalysis guard", () => { |
| 20 | + test("move handler does not log error when isInPushedAnalysis is deleted", async () => { |
| 21 | + const [server, client, instance] = await createGobanWithSocket(123); |
| 22 | + |
| 23 | + const moveHandler = (instance as any).socket_event_bindings.find( |
| 24 | + (binding: any) => binding[0] === "game/123/move", |
| 25 | + )?.[1]; |
| 26 | + |
| 27 | + delete (instance as any).isInPushedAnalysis; |
| 28 | + |
| 29 | + const consoleErrorSpy = jest.spyOn(console, "error").mockImplementation(() => {}); |
| 30 | + moveHandler({ game_id: 123, move: "aa" }); |
| 31 | + |
| 32 | + expect(consoleErrorSpy).not.toHaveBeenCalledWith( |
| 33 | + expect.objectContaining({ |
| 34 | + message: expect.stringContaining("isInPushedAnalysis is not a function"), |
| 35 | + }) |
| 36 | + ); |
| 37 | + |
| 38 | + consoleErrorSpy.mockRestore(); |
| 39 | + client.disconnect(); |
| 40 | + server.close(); |
| 41 | + }); |
| 42 | + |
| 43 | + test("syncReviewMove does not throw when isInPushedAnalysis is deleted", () => { |
| 44 | + const instance = new TestGoban({ review_id: 123 }); |
| 45 | + |
| 46 | + (instance as any).done_loading_review = true; |
| 47 | + (instance as any).isPlayerController = () => true; |
| 48 | + (instance as any).socket = { send: () => {} }; |
| 49 | + |
| 50 | + delete (instance as any).isInPushedAnalysis; |
| 51 | + |
| 52 | + expect(() => { |
| 53 | + (instance as any).syncReviewMove(); |
| 54 | + }).not.toThrow(); |
| 55 | + |
| 56 | + instance.destroy(); |
| 57 | + }); |
| 58 | +}); |
0 commit comments