64 lines
1.6 KiB
TypeScript
64 lines
1.6 KiB
TypeScript
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
|
|
import type { Server } from "bun";
|
|
import type { ServerMessage } from "./protocol";
|
|
|
|
describe("awareness routing", () => {
|
|
let server: Server<unknown>;
|
|
const PORT = 4042;
|
|
|
|
beforeAll(async () => {
|
|
// Import and start server on test port
|
|
process.env.PORT = String(PORT);
|
|
const mod = await import("./index");
|
|
server = mod.server;
|
|
});
|
|
|
|
afterAll(() => {
|
|
server?.stop();
|
|
});
|
|
|
|
test("awareness message routes to other peers in same room", async () => {
|
|
const ws1 = new WebSocket(`ws://localhost:${PORT}/ws`);
|
|
const ws2 = new WebSocket(`ws://localhost:${PORT}/ws`);
|
|
|
|
const received: ServerMessage[] = [];
|
|
|
|
await Promise.all([
|
|
new Promise((r) => {
|
|
ws1.onopen = r;
|
|
}),
|
|
new Promise((r) => {
|
|
ws2.onopen = r;
|
|
}),
|
|
]);
|
|
|
|
ws2.onmessage = (e) => {
|
|
received.push(JSON.parse(e.data));
|
|
};
|
|
|
|
// Both join same room
|
|
ws1.send(JSON.stringify({ type: "join", room: "test" }));
|
|
ws2.send(JSON.stringify({ type: "join", room: "test" }));
|
|
|
|
await Bun.sleep(50);
|
|
|
|
// ws1 sends awareness
|
|
ws1.send(
|
|
JSON.stringify({
|
|
type: "awareness",
|
|
data: { clientId: 1, cursor: { line: 10, col: 5 } },
|
|
}),
|
|
);
|
|
|
|
await Bun.sleep(50);
|
|
|
|
const awareness = received.find((m) => m.type === "awareness");
|
|
expect(awareness).toBeDefined();
|
|
if (awareness?.type === "awareness") {
|
|
expect(awareness.data.cursor).toEqual({ line: 10, col: 5 });
|
|
}
|
|
|
|
ws1.close();
|
|
ws2.close();
|
|
});
|
|
});
|