2023-04-08 01:47:04 +02:00
|
|
|
import WS from "jest-websocket-mock";
|
|
|
|
import Stream from "../src/streaming";
|
2023-04-07 16:25:22 +02:00
|
|
|
|
2023-04-08 01:47:04 +02:00
|
|
|
describe("Streaming", () => {
|
|
|
|
test("useChannel", async () => {
|
|
|
|
const server = new WS("wss://misskey.test/streaming");
|
|
|
|
const stream = new Stream("https://misskey.test", { token: "TOKEN" });
|
2023-04-07 16:25:22 +02:00
|
|
|
const mainChannelReceived: any[] = [];
|
2023-04-08 01:47:04 +02:00
|
|
|
const main = stream.useChannel("main");
|
|
|
|
main.on("meUpdated", (payload) => {
|
2023-04-07 16:25:22 +02:00
|
|
|
mainChannelReceived.push(payload);
|
|
|
|
});
|
|
|
|
|
|
|
|
const ws = await server.connected;
|
2023-04-08 01:47:04 +02:00
|
|
|
expect(new URLSearchParams(new URL(ws.url).search).get("i")).toEqual(
|
|
|
|
"TOKEN",
|
|
|
|
);
|
2023-04-07 16:25:22 +02:00
|
|
|
|
2023-04-08 01:47:04 +02:00
|
|
|
const msg = JSON.parse((await server.nextMessage) as string);
|
2023-04-07 16:25:22 +02:00
|
|
|
const mainChannelId = msg.body.id;
|
2023-04-08 01:47:04 +02:00
|
|
|
expect(msg.type).toEqual("connect");
|
|
|
|
expect(msg.body.channel).toEqual("main");
|
2023-04-07 16:25:22 +02:00
|
|
|
expect(mainChannelId != null).toEqual(true);
|
|
|
|
|
2023-04-08 01:47:04 +02:00
|
|
|
server.send(
|
|
|
|
JSON.stringify({
|
|
|
|
type: "channel",
|
2023-04-07 16:25:22 +02:00
|
|
|
body: {
|
2023-04-08 01:47:04 +02:00
|
|
|
id: mainChannelId,
|
|
|
|
type: "meUpdated",
|
|
|
|
body: {
|
|
|
|
id: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
2023-04-07 16:25:22 +02:00
|
|
|
|
|
|
|
expect(mainChannelReceived[0]).toEqual({
|
2023-04-08 01:47:04 +02:00
|
|
|
id: "foo",
|
2023-04-07 16:25:22 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
stream.close();
|
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
|
2023-04-08 01:47:04 +02:00
|
|
|
test("useChannel with parameters", async () => {
|
|
|
|
const server = new WS("wss://misskey.test/streaming");
|
|
|
|
const stream = new Stream("https://misskey.test", { token: "TOKEN" });
|
2023-04-07 16:25:22 +02:00
|
|
|
const messagingChannelReceived: any[] = [];
|
2023-04-08 01:47:04 +02:00
|
|
|
const messaging = stream.useChannel("messaging", { otherparty: "aaa" });
|
|
|
|
messaging.on("message", (payload) => {
|
2023-04-07 16:25:22 +02:00
|
|
|
messagingChannelReceived.push(payload);
|
|
|
|
});
|
|
|
|
|
|
|
|
const ws = await server.connected;
|
2023-04-08 01:47:04 +02:00
|
|
|
expect(new URLSearchParams(new URL(ws.url).search).get("i")).toEqual(
|
|
|
|
"TOKEN",
|
|
|
|
);
|
2023-04-07 16:25:22 +02:00
|
|
|
|
2023-04-08 01:47:04 +02:00
|
|
|
const msg = JSON.parse((await server.nextMessage) as string);
|
2023-04-07 16:25:22 +02:00
|
|
|
const messagingChannelId = msg.body.id;
|
2023-04-08 01:47:04 +02:00
|
|
|
expect(msg.type).toEqual("connect");
|
|
|
|
expect(msg.body.channel).toEqual("messaging");
|
|
|
|
expect(msg.body.params).toEqual({ otherparty: "aaa" });
|
2023-04-07 16:25:22 +02:00
|
|
|
expect(messagingChannelId != null).toEqual(true);
|
|
|
|
|
2023-04-08 01:47:04 +02:00
|
|
|
server.send(
|
|
|
|
JSON.stringify({
|
|
|
|
type: "channel",
|
2023-04-07 16:25:22 +02:00
|
|
|
body: {
|
2023-04-08 01:47:04 +02:00
|
|
|
id: messagingChannelId,
|
|
|
|
type: "message",
|
|
|
|
body: {
|
|
|
|
id: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
2023-04-07 16:25:22 +02:00
|
|
|
|
|
|
|
expect(messagingChannelReceived[0]).toEqual({
|
2023-04-08 01:47:04 +02:00
|
|
|
id: "foo",
|
2023-04-07 16:25:22 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
stream.close();
|
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
|
2023-04-08 01:47:04 +02:00
|
|
|
test("ちゃんとチャンネルごとにidが異なる", async () => {
|
|
|
|
const server = new WS("wss://misskey.test/streaming");
|
|
|
|
const stream = new Stream("https://misskey.test", { token: "TOKEN" });
|
2023-04-07 16:25:22 +02:00
|
|
|
|
2023-04-08 01:47:04 +02:00
|
|
|
stream.useChannel("messaging", { otherparty: "aaa" });
|
|
|
|
stream.useChannel("messaging", { otherparty: "bbb" });
|
2023-04-07 16:25:22 +02:00
|
|
|
|
|
|
|
const ws = await server.connected;
|
2023-04-08 01:47:04 +02:00
|
|
|
expect(new URLSearchParams(new URL(ws.url).search).get("i")).toEqual(
|
|
|
|
"TOKEN",
|
|
|
|
);
|
2023-04-07 16:25:22 +02:00
|
|
|
|
2023-04-08 01:47:04 +02:00
|
|
|
const msg = JSON.parse((await server.nextMessage) as string);
|
2023-04-07 16:25:22 +02:00
|
|
|
const messagingChannelId = msg.body.id;
|
2023-04-08 01:47:04 +02:00
|
|
|
const msg2 = JSON.parse((await server.nextMessage) as string);
|
2023-04-07 16:25:22 +02:00
|
|
|
const messagingChannelId2 = msg2.body.id;
|
|
|
|
|
|
|
|
expect(messagingChannelId != null).toEqual(true);
|
|
|
|
expect(messagingChannelId2 != null).toEqual(true);
|
|
|
|
expect(messagingChannelId).not.toEqual(messagingChannelId2);
|
|
|
|
|
|
|
|
stream.close();
|
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
|
2023-04-08 01:47:04 +02:00
|
|
|
test("Connection#send", async () => {
|
|
|
|
const server = new WS("wss://misskey.test/streaming");
|
|
|
|
const stream = new Stream("https://misskey.test", { token: "TOKEN" });
|
2023-04-07 16:25:22 +02:00
|
|
|
|
2023-04-08 01:47:04 +02:00
|
|
|
const messaging = stream.useChannel("messaging", { otherparty: "aaa" });
|
|
|
|
messaging.send("read", { id: "aaa" });
|
2023-04-07 16:25:22 +02:00
|
|
|
|
|
|
|
const ws = await server.connected;
|
2023-04-08 01:47:04 +02:00
|
|
|
expect(new URLSearchParams(new URL(ws.url).search).get("i")).toEqual(
|
|
|
|
"TOKEN",
|
|
|
|
);
|
2023-04-07 16:25:22 +02:00
|
|
|
|
2023-04-08 01:47:04 +02:00
|
|
|
const connectMsg = JSON.parse((await server.nextMessage) as string);
|
2023-04-07 16:25:22 +02:00
|
|
|
const channelId = connectMsg.body.id;
|
2023-04-08 01:47:04 +02:00
|
|
|
const msg = JSON.parse((await server.nextMessage) as string);
|
2023-04-07 16:25:22 +02:00
|
|
|
|
2023-04-08 01:47:04 +02:00
|
|
|
expect(msg.type).toEqual("ch");
|
2023-04-07 16:25:22 +02:00
|
|
|
expect(msg.body.id).toEqual(channelId);
|
2023-04-08 01:47:04 +02:00
|
|
|
expect(msg.body.type).toEqual("read");
|
|
|
|
expect(msg.body.body).toEqual({ id: "aaa" });
|
2023-04-07 16:25:22 +02:00
|
|
|
|
|
|
|
stream.close();
|
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
|
2023-04-08 01:47:04 +02:00
|
|
|
test("Connection#dispose", async () => {
|
|
|
|
const server = new WS("wss://misskey.test/streaming");
|
|
|
|
const stream = new Stream("https://misskey.test", { token: "TOKEN" });
|
2023-04-07 16:25:22 +02:00
|
|
|
const mainChannelReceived: any[] = [];
|
2023-04-08 01:47:04 +02:00
|
|
|
const main = stream.useChannel("main");
|
|
|
|
main.on("meUpdated", (payload) => {
|
2023-04-07 16:25:22 +02:00
|
|
|
mainChannelReceived.push(payload);
|
|
|
|
});
|
2023-04-08 01:47:04 +02:00
|
|
|
|
2023-04-07 16:25:22 +02:00
|
|
|
const ws = await server.connected;
|
2023-04-08 01:47:04 +02:00
|
|
|
expect(new URLSearchParams(new URL(ws.url).search).get("i")).toEqual(
|
|
|
|
"TOKEN",
|
|
|
|
);
|
2023-04-07 16:25:22 +02:00
|
|
|
|
2023-04-08 01:47:04 +02:00
|
|
|
const msg = JSON.parse((await server.nextMessage) as string);
|
2023-04-07 16:25:22 +02:00
|
|
|
const mainChannelId = msg.body.id;
|
2023-04-08 01:47:04 +02:00
|
|
|
expect(msg.type).toEqual("connect");
|
|
|
|
expect(msg.body.channel).toEqual("main");
|
2023-04-07 16:25:22 +02:00
|
|
|
expect(mainChannelId != null).toEqual(true);
|
|
|
|
main.dispose();
|
|
|
|
|
2023-04-08 01:47:04 +02:00
|
|
|
server.send(
|
|
|
|
JSON.stringify({
|
|
|
|
type: "channel",
|
2023-04-07 16:25:22 +02:00
|
|
|
body: {
|
2023-04-08 01:47:04 +02:00
|
|
|
id: mainChannelId,
|
|
|
|
type: "meUpdated",
|
|
|
|
body: {
|
|
|
|
id: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
2023-04-07 16:25:22 +02:00
|
|
|
|
|
|
|
expect(mainChannelReceived.length).toEqual(0);
|
|
|
|
|
|
|
|
stream.close();
|
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO: SharedConnection#dispose して一定時間経ったら disconnect メッセージがサーバーに送られてくるかのテスト
|
|
|
|
|
|
|
|
// TODO: チャンネル接続が使いまわされるかのテスト
|
|
|
|
});
|