From f062753f8f2b4e07396d8410d280eb83510a2ead Mon Sep 17 00:00:00 2001 From: Schluffe Date: Thu, 17 Sep 2026 22:21:02 +0200 Subject: [PATCH] Fix stupidity --- server/client.test.ts | 58 +++++++++++++++++++++++++++++-------------- server/deno.ts | 11 +++++--- 2 files changed, 47 insertions(+), 22 deletions(-) diff --git a/server/client.test.ts b/server/client.test.ts index 2914b8e..ae7e99b 100644 --- a/server/client.test.ts +++ b/server/client.test.ts @@ -47,7 +47,7 @@ function flush(): Promise { } Deno.test( - "welcome all clients on open", + "Welcome all clients on connect.", setup(async () => { const [s1, m1] = await connect(href); const [s2, m2] = await connect(href); @@ -62,26 +62,46 @@ Deno.test( }), ); -// Deno.test( -// "removes a client from broadcast after it disconnects", -// setup(async () => { -// const [s1, m1] = await connect(href); -// const [s2, m2] = await connect(href); -// const [s3, m3] = await connect(href); +Deno.test( + "Messages all clients in a room on joining.", + setup(async () => { + const [s1, m1] = await connect(href); + const [s2, m2] = await connect(href); -// await flush(); + await flush(); + m1.length = 0; + m2.length = 0; -// m1.length = 0; -// m2.length = 0; -// m3.length = 0; + s1.send("/join meh"); + s2.send("/join meh"); + await flush(); -// await close(s3); + assertEquals(m1, ["1 has joined!", "2 has joined!"]); + assertEquals(m2, ["2 has joined!"]); -// assertEquals(m1, ["3 has left!"]); -// assertEquals(m2, ["3 has left!"]); -// assertEquals(m3, []); + await close(s2); + await close(s1); + }), +); -// await close(s2); -// await close(s1); -// }), -// ); +Deno.test( + "Messages remaining members if one left the room.", + setup(async () => { + const [s1, m1] = await connect(href); + const [s2, m2] = await connect(href); + + s1.send("/join meh"); + s2.send("/join meh"); + await flush(); + + m1.length = 0; + m2.length = 0; + await close(s2); + await flush(); + + assertEquals(m1, ["2 has left!"]); + assertEquals(m2, []); + + await close(s1); + }), +); diff --git a/server/deno.ts b/server/deno.ts index fb1dc26..38df654 100644 --- a/server/deno.ts +++ b/server/deno.ts @@ -22,10 +22,15 @@ export function startServer( }; const commands = { - join: (id: number, ...args: string[]) => { + join: (id: number, args: string[]) => { const room = args[0]; - roomsMap.getOrInsert(room, [id]); - clientRoomMap.getOrInsert(id, room); + if (roomsMap.has(room)) { + roomsMap.get(room)!.push(id); + } else { + roomsMap.set(room, [id]); + } + + clientRoomMap.getOrInsert(id, room); // not sure about that broadcast(room, `${id} has joined!`); }, } as const;