Skip to content

Commit

Permalink
Test WeechatConnection close / reconnect on error
Browse files Browse the repository at this point in the history
  • Loading branch information
mhoran committed Feb 28, 2024
1 parent ba87e9e commit 045dbcf
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 5 deletions.
71 changes: 70 additions & 1 deletion __tests__/lib/weechat/connection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import WeechatConnection from '../../../src/lib/weechat/connection';
import WeechatConnection, {
ConnectionError
} from '../../../src/lib/weechat/connection';

const mockWebSocket = jest.fn(function () {
this.send = jest.fn();
this.close = jest.fn(() => this.onclose());
return this;
});

Expand Down Expand Up @@ -88,4 +91,70 @@ describe(WeechatConnection, () => {
payload: '4.1.2'
});
});

describe('close', () => {
it('closes the WebSocket', () => {
const dispatch = jest.fn();
const connection = new WeechatConnection(
dispatch,
'example.com',
'changeme',
true,
jest.fn(),
jest.fn()
);
connection.connect();

expect(mockWebSocket.mock.instances).toHaveLength(1);

mockWebSocket.mock.instances[0].onopen();
mockWebSocket.mock.instances[0].onmessage({
data: Buffer.from(
'\x00\x00\x00\x27\x00\x00\x00\x00\x07\x76\x65\x72\x73\x69\x6f\x6e\x69\x6e\x66\x00\x00\x00\x07\x76\x65\x72\x73\x69\x6f\x6e\x00\x00\x00\x05\x34\x2e\x31\x2e\x32'
)
} as WebSocketMessageEvent);

connection.close();

expect(mockWebSocket.mock.instances[0].send).toHaveBeenCalledWith(
'quit\n'
);
expect(mockWebSocket.mock.instances[0].close).toHaveBeenCalled();
expect(dispatch).toHaveBeenNthCalledWith(2, {
type: 'DISCONNECT'
});
expect(mockWebSocket.mock.instances).toHaveLength(1);
});

it('reconnects on error', () => {
const dispatch = jest.fn();
const onError = jest.fn();
const connection = new WeechatConnection(
dispatch,
'example.com',
'changeme',
true,
jest.fn(),
onError
);
connection.connect();

expect(mockWebSocket.mock.instances).toHaveLength(1);

mockWebSocket.mock.instances[0].onopen();
mockWebSocket.mock.instances[0].onmessage({
data: Buffer.from(
'\x00\x00\x00\x27\x00\x00\x00\x00\x07\x76\x65\x72\x73\x69\x6f\x6e\x69\x6e\x66\x00\x00\x00\x07\x76\x65\x72\x73\x69\x6f\x6e\x00\x00\x00\x05\x34\x2e\x31\x2e\x32'
)
} as WebSocketMessageEvent);
mockWebSocket.mock.instances[0].onerror();
mockWebSocket.mock.instances[0].close();

expect(onError).toHaveBeenCalledWith(true, ConnectionError.Socket);
expect(dispatch).toHaveBeenNthCalledWith(2, {
type: 'DISCONNECT'
});
expect(mockWebSocket.mock.instances).toHaveLength(2);
});
});
});
11 changes: 7 additions & 4 deletions src/lib/weechat/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default class WeechatConnection {
this.websocket.onopen = () => this.onopen();
this.websocket.onmessage = (event) => this.onmessage(event);
this.websocket.onerror = (event) => this.handleError(event);
this.websocket.onclose = () => this.close();
this.websocket.onclose = () => this.handleClose();
}

onopen(): void {
Expand All @@ -77,15 +77,13 @@ export default class WeechatConnection {
this.onError(this.reconnect, ConnectionError.Socket);
}

close(): void {
handleClose(): void {
if (this.authenticating) {
this.onError(false, ConnectionError.Authentication);
return;
}

this.connected = false;
this.send('quit');
this.websocket?.close();
this.dispatch({
type: 'DISCONNECT'
});
Expand All @@ -96,6 +94,11 @@ export default class WeechatConnection {
}
}

close(): void {
this.send('quit');
this.websocket?.close();
}

onmessage(event: WebSocketMessageEvent): void {
const parsed = protocol.parse(event.data) as WeechatResponse<unknown>;

Expand Down

0 comments on commit 045dbcf

Please sign in to comment.