///
import { assertEquals } from "jsr:@std/assert";
import { close, connect, flush, href, setup } from "../testSetup.ts";
import { getHandler } from "./deno.ts";
Deno.test(
"Welcome all clients on connect.",
setup(async () => {
const [s1, m1] = await connect(href);
const [s2, m2] = await connect(href);
await flush();
assertEquals(m1, ["Welcome 1, type /join ."]);
assertEquals(m2, ["Welcome 2, type /join ."]);
await close(s2);
await close(s1);
}, getHandler()),
);
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();
m1.length = 0;
m2.length = 0;
s1.send("/join meh");
s2.send("/join meh");
await flush();
assertEquals(m1, ["1 has joined!", "2 has joined!"]);
assertEquals(m2, ["2 has joined!"]);
await close(s2);
await close(s1);
}, getHandler()),
);
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);
}, getHandler()),
);