Skip to content

Commit d015017

Browse files
committed
test(lesy-pilot-ui): add tests for socket class
1 parent 2255ffc commit d015017

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// tslint:disable-next-line: import-name
2+
import WebSocket from "ws";
3+
import { WebSocketBus } from "../pilot.socket";
4+
5+
describe("web socket", () => {
6+
let wsServer: WebSocketBus;
7+
let wsClient: WebSocket;
8+
9+
beforeAll((done) => {
10+
wsServer = new WebSocketBus().startServer("localhost", 1234).init({
11+
requestRunCommand: (payload) => {
12+
return {
13+
success: true,
14+
data: payload,
15+
};
16+
},
17+
});
18+
19+
wsServer["ws"].on("connection", () => wsClient.on("open", done));
20+
wsClient = new WebSocket(`ws://localhost:1234`);
21+
});
22+
23+
afterAll((done) => {
24+
wsClient.close();
25+
wsServer.close(done);
26+
});
27+
28+
it("should exchange messages", (done) => {
29+
wsClient.on("message", (m) => {
30+
expect(m).toEqual(
31+
JSON.stringify({
32+
success: true,
33+
data: "echo 123",
34+
}),
35+
);
36+
done();
37+
});
38+
39+
wsClient.send(
40+
JSON.stringify({ REQUEST: "requestRunCommand", PAYLOAD: "echo 123" }),
41+
);
42+
});
43+
});

packages/plugins/lesy-plugin-pilot/src/pilot.command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export default class PilotCommand {
6565
[currCtx.feature.pkg.name]: currCtx,
6666
};
6767
global.lesySelectedProject = currCtx.feature.pkg.name;
68-
const projectsPaths = await this.fetchProjectPaths();
68+
const projectsPaths = await this.fetchProjectPaths(); // todo: remove same project dup
6969
for (let i = 0; i < projectsPaths.length; i = i + 1) {
7070
const p = await require(projectsPaths[i]).default;
7171
global.lesyWorkspace[p.feature.pkg.name] = p.localState;

packages/plugins/lesy-plugin-pilot/src/pilot.socket.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ export class WebSocketBus {
3737
return this.receiver.asObservable();
3838
}
3939

40-
close() {
41-
this.ws.close();
40+
close(cb?: (param: any) => void) {
41+
this.sender.unsubscribe();
42+
this.receiver.unsubscribe();
43+
this.ws.close(cb);
4244
}
4345

4446
private onConnectionOpen() {

0 commit comments

Comments
 (0)