Init unit tests

This commit is contained in:
Hri7566 2024-01-26 05:59:39 -05:00
parent 2b8ab0ddca
commit 6b1e979e7d
10 changed files with 96 additions and 49 deletions

View File

@ -1,6 +1,5 @@
import EventEmitter from "events"; import EventEmitter from "events";
import { Logger } from "../util/Logger"; import { Logger } from "../util/Logger";
import { loadConfig } from "../util/config";
import { import {
ChannelSettingValue, ChannelSettingValue,
IChannelSettings, IChannelSettings,
@ -14,37 +13,7 @@ import { validateChannelSettings } from "./settings";
import { findSocketByPartID, socketsBySocketID } from "../ws/Socket"; import { findSocketByPartID, socketsBySocketID } from "../ws/Socket";
import Crown from "./Crown"; import Crown from "./Crown";
import { ChannelList } from "./ChannelList"; import { ChannelList } from "./ChannelList";
import { config } from "./config";
interface ChannelConfig {
forceLoad: string[];
lobbySettings: Partial<IChannelSettings>;
defaultSettings: Partial<IChannelSettings>;
lobbyRegexes: string[];
lobbyBackdoor: string;
fullChannel: string;
}
export const config = loadConfig<ChannelConfig>("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"
});
export class Channel extends EventEmitter { export class Channel extends EventEmitter {
private settings: Partial<IChannelSettings> = config.defaultSettings; private settings: Partial<IChannelSettings> = config.defaultSettings;
@ -96,7 +65,7 @@ export class Channel extends EventEmitter {
this.bindEventListeners(); this.bindEventListeners();
ChannelList.add(this); ChannelList.add(this);
// TODO channel closing // TODO implement owner_id
this.logger.info("Created"); this.logger.info("Created");
} }
@ -160,7 +129,7 @@ export class Channel extends EventEmitter {
}); });
this.on("command", (msg, socket) => { this.on("command", (msg, socket) => {
console.log(); // TODO commands
}); });
} }
@ -565,15 +534,3 @@ export class Channel extends EventEmitter {
} }
export default Channel; 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);
}

33
src/channel/config.ts Normal file
View File

@ -0,0 +1,33 @@
import { loadConfig } from "../util/config";
import { IChannelSettings } from "../util/types";
interface ChannelConfig {
forceLoad: string[];
lobbySettings: Partial<IChannelSettings>;
defaultSettings: Partial<IChannelSettings>;
lobbyRegexes: string[];
lobbyBackdoor: string;
fullChannel: string;
}
export const config = loadConfig<ChannelConfig>("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"
});

14
src/channel/forceLoad.ts Normal file
View File

@ -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);
}

View File

@ -5,6 +5,7 @@
*/ */
import "./ws/server"; import "./ws/server";
import "./channel/forceLoad";
import { Logger } from "./util/Logger"; import { Logger } from "./util/Logger";
const logger = new Logger("Main"); const logger = new Logger("Main");

View File

@ -350,7 +350,7 @@ export class Socket extends EventEmitter {
await updateUser(this._id, { await updateUser(this._id, {
name: typeof name == "string" ? name : undefined, name: typeof name == "string" ? name : undefined,
color: color ? (isColor ? color : undefined) : undefined color: color && isColor ? color : undefined
}); });
await this.loadUser(); await this.loadUser();

View File

@ -1,7 +1,6 @@
import { readUser, updateUser } from "../../../../data/user"; import { readUser, updateUser } from "../../../../data/user";
import { ServerEventListener } from "../../../../util/types"; import { ServerEventListener } from "../../../../util/types";
import { findSocketsByUserID } from "../../../Socket"; import { findSocketsByUserID } from "../../../Socket";
import { eventGroups } from "../../../events";
export const color: ServerEventListener<"color"> = { export const color: ServerEventListener<"color"> = {
id: "color", id: "color",
@ -21,7 +20,7 @@ export const color: ServerEventListener<"color"> = {
const toUpdate = findSocketsByUserID(id); const toUpdate = findSocketsByUserID(id);
toUpdate.forEach(s => { toUpdate.forEach(s => {
s.userset(undefined, msg.color); s.userset(undefined, msg.color, true);
}); });
} }
}; };

View File

@ -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);
});
}
};

View File

@ -3,9 +3,11 @@ import { EventGroup, eventGroups } from "../../events";
export const EVENT_GROUP_ADMIN = new EventGroup("admin"); export const EVENT_GROUP_ADMIN = new EventGroup("admin");
import { color } from "./handlers/color"; import { color } from "./handlers/color";
import { name } from "./handlers/name";
import { user_flag } from "./handlers/user_flag"; import { user_flag } from "./handlers/user_flag";
EVENT_GROUP_ADMIN.add(color); EVENT_GROUP_ADMIN.add(color);
EVENT_GROUP_ADMIN.add(name);
EVENT_GROUP_ADMIN.add(user_flag); EVENT_GROUP_ADMIN.add(user_flag);
eventGroups.push(EVENT_GROUP_ADMIN); eventGroups.push(EVENT_GROUP_ADMIN);

View File

@ -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();
});

1
test/ws/FakeSocket.ts Normal file
View File

@ -0,0 +1 @@
export class FakeSocket {}