forked from Hri7566/mpp-server-dev2
Init unit tests
This commit is contained in:
parent
2b8ab0ddca
commit
6b1e979e7d
|
@ -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<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"
|
||||
});
|
||||
import { config } from "./config";
|
||||
|
||||
export class Channel extends EventEmitter {
|
||||
private settings: Partial<IChannelSettings> = 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);
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
});
|
|
@ -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);
|
||||
}
|
|
@ -5,6 +5,7 @@
|
|||
*/
|
||||
|
||||
import "./ws/server";
|
||||
import "./channel/forceLoad";
|
||||
import { Logger } from "./util/Logger";
|
||||
|
||||
const logger = new Logger("Main");
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
}
|
||||
};
|
|
@ -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);
|
||||
|
|
|
@ -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();
|
||||
});
|
|
@ -0,0 +1 @@
|
|||
export class FakeSocket {}
|
Loading…
Reference in New Issue