Skip to content

Commit c20ccde

Browse files
authored
Merge pull request #211 from pygeek/main
Add guard checks for isInPushedAnalysis to prevent undefined errors
2 parents 6fdf3de + 17187f7 commit c20ccde

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

src/Goban/OGSConnectivity.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,10 @@ export abstract class OGSConnectivity extends GobanInteractive {
548548
}
549549
const move = move_obj.move;
550550

551-
if (this.isInPushedAnalysis()) {
551+
if (
552+
typeof this.isInPushedAnalysis === "function" &&
553+
this.isInPushedAnalysis()
554+
) {
552555
this.leavePushedAnalysis();
553556
}
554557

@@ -1166,7 +1169,7 @@ export abstract class OGSConnectivity extends GobanInteractive {
11661169
(this.isPlayerOwner() && msg_override && msg_override.controller)) &&
11671170
this.done_loading_review
11681171
) {
1169-
if (this.isInPushedAnalysis()) {
1172+
if (typeof this.isInPushedAnalysis === "function" && this.isInPushedAnalysis()) {
11701173
return;
11711174
}
11721175

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

0 commit comments

Comments
 (0)