-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.ts
195 lines (162 loc) · 5.09 KB
/
tests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { expect } from "chai";
import { Client, Event, Method, Server } from "./index.js";
import * as t from "io-ts";
import WebSocketServerImpl from "./servers/wss.js";
import { WebSocketServer } from "ws";
import WebSocketImpl from "./sockets/ws.js";
import WebSocket from "isomorphic-ws";
const PORT_FOR_TESTING = 60123;
function createWSServerImpl() {
return new WebSocketServerImpl(
new WebSocketServer({ port: PORT_FOR_TESTING }),
);
}
function createWSServer(): Server {
return new Server({ server: createWSServerImpl(), sessionInit: () => {} });
}
async function createWSClient(): Promise<Client> {
const socket = new WebSocketImpl(new WebSocket("ws://127.0.0.1:" + PORT_FOR_TESTING));
const client = new Client({ socket });
await client.connect();
return client;
}
async function createWSEnvironment(): Promise<{
client: Client;
server: Server;
}> {
const server = createWSServer();
const client = await createWSClient();
server.debugLoggerSend = (s) => console.log(`Server > ${s}`);
server.debugLoggerReceive = (s) => console.log(`Server < ${s}`);
client.debugLoggerSend = (s) => console.log(`Client > ${s}`);
client.debugLoggerReceive = (s) => console.log(`Client < ${s}`);
return { server, client };
}
describe("server and client", () => {
it("Basic ClientToServer and ServerToClient RPC", async () => {
const { server, client } = await createWSEnvironment();
try {
const Hello = Method.new("Hello", t.string, t.string);
server.onMethod(Hello, async (name) => `Hello, ${name} from the server!`);
client.onMethod(Hello, async (name) => `Hello, ${name} from a client!`);
const NAME = "UNIVERSE";
const method = new Hello(NAME);
expect(await method.with(client)).to.equal(
`Hello, ${NAME} from the server!`,
);
expect(await method.withs(server, server.clients.get(0))).to.equal(
`Hello, ${NAME} from a client!`,
);
} finally {
[server, client].forEach((x) => x.close());
}
});
it("basic event dispath", async () => {
const { server, client } = await createWSEnvironment();
try {
const Hello = Event.new("test", t.string);
const value = "xyz";
let counter = 0;
let trigger = () => {};
let promise = new Promise<void>(res => {
trigger = () => {
if (counter === 2)
res();
};
});
client.onEvent(Hello, async (s) => {
s === value ? counter++ : null;
trigger();
});
server.onEvent(Hello, async (s) => {
s === value ? counter++ : null;
trigger();
});
const event = new Hello(value);
await event.with(client);
await event.withs(server, server.clients.get(0));
await promise;
expect(counter).to.equal(2);
} finally {
[server, client].forEach((x) => x.close());
}
});
it("server socket-sessions", async () => {
const server = new Server({
server: createWSServerImpl(),
sessionInit: () => 0,
});
const client = await createWSClient();
try {
const SetValue = Method.new("set-value", t.number, t.void);
const GetValue = Method.new("get-value", t.void, t.number);
server.onMethod(SetValue, async (x, y) => {
y.session = x;
});
server.onMethod(GetValue, async (_, y) => y.session);
await new SetValue(42).with(client);
expect(await new GetValue().with(client)).to.equal(42);
} finally {
[server, client].forEach((x) => x.close());
}
});
it("array+composite types", async () => {
const { server, client } = await createWSEnvironment();
try {
const ServerUsers = [
{
firstname: "Madoka",
lastname: "Kaname",
age: 14,
friends: [1],
},
{
firstname: "Homura",
lastname: "Akemi",
age: 14,
friends: [0],
},
{
firstname: "Kyuubey",
lastname: "[DATA DELETED]",
age: -1,
friends: [],
},
];
// https://github.com/gcanti/io-ts/blob/master/index.md#recursive-types
interface User {
firstname: string;
lastname: string;
age: number;
friends: User[];
}
const User: t.Type<User> = t.recursion("User", () =>
t.type({
firstname: t.string,
lastname: t.string,
age: t.number,
friends: t.array(User),
}),
);
const FindUser = Method.new("FindUser", t.string, User);
server.onMethod(FindUser, async (username) => {
const user = ServerUsers.filter(
(x) => `${x.firstname} ${x.lastname}` === username,
)[0];
return {
...user,
friends: user.friends.map((x) => {
return {
...ServerUsers[x],
friends: [],
};
}),
};
});
const response = await new FindUser("Homura Akemi").with(client);
expect(response.friends[0].firstname).to.equal("Madoka");
} finally {
[server, client].forEach((x) => x.close());
}
});
});