From 6b1e979e7d7f5587261df0f4d3ba41871e6c7a5b Mon Sep 17 00:00:00 2001 From: Hri7566 Date: Fri, 26 Jan 2024 05:59:39 -0500 Subject: [PATCH] Init unit tests --- src/channel/Channel.ts | 49 ++------------------------- src/channel/config.ts | 33 ++++++++++++++++++ src/channel/forceLoad.ts | 14 ++++++++ src/index.ts | 1 + src/ws/Socket.ts | 2 +- src/ws/events/admin/handlers/color.ts | 3 +- src/ws/events/admin/handlers/name.ts | 25 ++++++++++++++ src/ws/events/admin/index.ts | 2 ++ test/channel/Channel.test.ts | 15 ++++++++ test/ws/FakeSocket.ts | 1 + 10 files changed, 96 insertions(+), 49 deletions(-) create mode 100644 src/channel/config.ts create mode 100644 src/channel/forceLoad.ts create mode 100644 src/ws/events/admin/handlers/name.ts create mode 100644 test/channel/Channel.test.ts create mode 100644 test/ws/FakeSocket.ts diff --git a/src/channel/Channel.ts b/src/channel/Channel.ts index 55908ed..b48c440 100644 --- a/src/channel/Channel.ts +++ b/src/channel/Channel.ts @@ -1,6 +1,5 @@ import EventEmitter from "events"; import { Logger } from "../util/Logger"; -import { loadConfig } from "../util/config"; import { ChannelSettingValue, IChannelSettings, @@ -14,37 +13,7 @@ import { validateChannelSettings } from "./settings"; import { findSocketByPartID, socketsBySocketID } from "../ws/Socket"; import Crown from "./Crown"; import { ChannelList } from "./ChannelList"; - -interface ChannelConfig { - forceLoad: string[]; - lobbySettings: Partial; - defaultSettings: Partial; - lobbyRegexes: string[]; - lobbyBackdoor: string; - fullChannel: string; -} - -export const config = loadConfig("config/channels.yml", { - forceLoad: ["lobby", "test/awkward"], - lobbySettings: { - lobby: true, - chat: true, - crownsolo: false, - visible: true, - color: "#73b3cc", - color2: "#273546" - }, - defaultSettings: { - chat: true, - crownsolo: false, - color: "#3b5054", - color2: "#001014", - visible: true - }, - lobbyRegexes: ["^lobby[0-9][0-9]$", "^lobby[1-9]$", "^test/.+$"], - lobbyBackdoor: "lolwutsecretlobbybackdoor", - fullChannel: "test/awkward" -}); +import { config } from "./config"; export class Channel extends EventEmitter { private settings: Partial = config.defaultSettings; @@ -96,7 +65,7 @@ export class Channel extends EventEmitter { this.bindEventListeners(); ChannelList.add(this); - // TODO channel closing + // TODO implement owner_id this.logger.info("Created"); } @@ -160,7 +129,7 @@ export class Channel extends EventEmitter { }); this.on("command", (msg, socket) => { - console.log(); + // TODO commands }); } @@ -565,15 +534,3 @@ export class Channel extends EventEmitter { } export default Channel; - -// Channel forceloader (cringe) -let hasFullChannel = false; - -for (const id of config.forceLoad) { - new Channel(id, undefined, undefined, undefined, true); - if (id == config.fullChannel) hasFullChannel = true; -} - -if (!hasFullChannel) { - new Channel(config.fullChannel, undefined, undefined, undefined, true); -} diff --git a/src/channel/config.ts b/src/channel/config.ts new file mode 100644 index 0000000..5ed7912 --- /dev/null +++ b/src/channel/config.ts @@ -0,0 +1,33 @@ +import { loadConfig } from "../util/config"; +import { IChannelSettings } from "../util/types"; + +interface ChannelConfig { + forceLoad: string[]; + lobbySettings: Partial; + defaultSettings: Partial; + lobbyRegexes: string[]; + lobbyBackdoor: string; + fullChannel: string; +} + +export const config = loadConfig("config/channels.yml", { + forceLoad: ["lobby", "test/awkward"], + lobbySettings: { + lobby: true, + chat: true, + crownsolo: false, + visible: true, + color: "#73b3cc", + color2: "#273546" + }, + defaultSettings: { + chat: true, + crownsolo: false, + color: "#3b5054", + color2: "#001014", + visible: true + }, + lobbyRegexes: ["^lobby[0-9][0-9]$", "^lobby[1-9]$", "^test/.+$"], + lobbyBackdoor: "lolwutsecretlobbybackdoor", + fullChannel: "test/awkward" +}); diff --git a/src/channel/forceLoad.ts b/src/channel/forceLoad.ts new file mode 100644 index 0000000..606bbd0 --- /dev/null +++ b/src/channel/forceLoad.ts @@ -0,0 +1,14 @@ +import { Channel } from "./Channel"; +import { config } from "./config"; + +// Channel forceloader (cringe) +let hasFullChannel = false; + +for (const id of config.forceLoad) { + new Channel(id, undefined, undefined, undefined, true); + if (id == config.fullChannel) hasFullChannel = true; +} + +if (!hasFullChannel) { + new Channel(config.fullChannel, undefined, undefined, undefined, true); +} diff --git a/src/index.ts b/src/index.ts index 2153ee5..03cfaf2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,6 +5,7 @@ */ import "./ws/server"; +import "./channel/forceLoad"; import { Logger } from "./util/Logger"; const logger = new Logger("Main"); diff --git a/src/ws/Socket.ts b/src/ws/Socket.ts index d0b8584..9169994 100644 --- a/src/ws/Socket.ts +++ b/src/ws/Socket.ts @@ -350,7 +350,7 @@ export class Socket extends EventEmitter { await updateUser(this._id, { name: typeof name == "string" ? name : undefined, - color: color ? (isColor ? color : undefined) : undefined + color: color && isColor ? color : undefined }); await this.loadUser(); diff --git a/src/ws/events/admin/handlers/color.ts b/src/ws/events/admin/handlers/color.ts index 005ca71..2a3773e 100644 --- a/src/ws/events/admin/handlers/color.ts +++ b/src/ws/events/admin/handlers/color.ts @@ -1,7 +1,6 @@ import { readUser, updateUser } from "../../../../data/user"; import { ServerEventListener } from "../../../../util/types"; import { findSocketsByUserID } from "../../../Socket"; -import { eventGroups } from "../../../events"; export const color: ServerEventListener<"color"> = { id: "color", @@ -21,7 +20,7 @@ export const color: ServerEventListener<"color"> = { const toUpdate = findSocketsByUserID(id); toUpdate.forEach(s => { - s.userset(undefined, msg.color); + s.userset(undefined, msg.color, true); }); } }; diff --git a/src/ws/events/admin/handlers/name.ts b/src/ws/events/admin/handlers/name.ts new file mode 100644 index 0000000..94a4f1f --- /dev/null +++ b/src/ws/events/admin/handlers/name.ts @@ -0,0 +1,25 @@ +import { readUser, updateUser } from "../../../../data/user"; +import { ServerEventListener } from "../../../../util/types"; +import { findSocketsByUserID } from "../../../Socket"; + +export const name: ServerEventListener<"name"> = { + id: "name", + callback: async (msg, socket) => { + const id = msg._id; + const name = msg.name; + + if (typeof id !== "string") return; + if (typeof name !== "string") return; + + const user = await readUser(msg._id); + if (!user) return; + + user.name = name; + await updateUser(id, user); + + const toUpdate = findSocketsByUserID(id); + toUpdate.forEach(s => { + s.userset(msg.name, undefined, true); + }); + } +}; diff --git a/src/ws/events/admin/index.ts b/src/ws/events/admin/index.ts index d06d284..38bf1d6 100644 --- a/src/ws/events/admin/index.ts +++ b/src/ws/events/admin/index.ts @@ -3,9 +3,11 @@ import { EventGroup, eventGroups } from "../../events"; export const EVENT_GROUP_ADMIN = new EventGroup("admin"); import { color } from "./handlers/color"; +import { name } from "./handlers/name"; import { user_flag } from "./handlers/user_flag"; EVENT_GROUP_ADMIN.add(color); +EVENT_GROUP_ADMIN.add(name); EVENT_GROUP_ADMIN.add(user_flag); eventGroups.push(EVENT_GROUP_ADMIN); diff --git a/test/channel/Channel.test.ts b/test/channel/Channel.test.ts new file mode 100644 index 0000000..e6509a4 --- /dev/null +++ b/test/channel/Channel.test.ts @@ -0,0 +1,15 @@ +import { test, expect } from "bun:test"; +import { Channel } from "../../src/channel/Channel"; + +test("Channel is created correctly", () => { + const channel = new Channel("my room"); + expect(channel.getID()).toBe("my room"); + + const info = channel.getInfo(); + expect(info.id).toBe("my room"); + expect(info._id).toBe("my room"); + expect(info.count).toBe(0); + + const ppl = channel.getParticipantList(); + expect(ppl).toBeEmpty(); +}); diff --git a/test/ws/FakeSocket.ts b/test/ws/FakeSocket.ts new file mode 100644 index 0000000..949be65 --- /dev/null +++ b/test/ws/FakeSocket.ts @@ -0,0 +1 @@ +export class FakeSocket {}