diff --git a/test/test_config.ts b/test/test_config.ts new file mode 100644 index 0000000000000000000000000000000000000000..94d7daa838da35759980f09796c24844dee67fbb --- /dev/null +++ b/test/test_config.ts @@ -0,0 +1,44 @@ +import * as Chai from "chai"; +import * as ChaiAsPromised from "chai-as-promised"; +import { DiscordBridgeConfig } from "../src/config"; +Chai.use(ChaiAsPromised); +const expect = Chai.expect; + + +describe("DiscordBridgeConfig.ApplyConfig", () => { + it("should merge configs correctly", () => { + const config = new DiscordBridgeConfig(); + config.ApplyConfig({ + bridge: { + homeserverUrl: "blah", + disableTypingNotifications: true, + disableDiscordMentions: false, + disableDeletionForwarding: true, + enableSelfServiceBridging: false + }, + logging: { + console: "warn", + } + }); + expect(config.bridge.homeserverUrl, "blah"); + expect(config.bridge.disableTypingNotifications).to.be.true; + expect(config.bridge.disableDiscordMentions).to.be.false; + expect(config.bridge.disableDeletionForwarding).to.be.true; + expect(config.bridge.enableSelfServiceBridging).to.be.false; + expect(config.logging.console, "warn"); + }); + it("should merge logging.files correctly", () => { + const config = new DiscordBridgeConfig(); + config.ApplyConfig({ + logging: { + console: "silent", + files: [ + { + file: "./bacon.log", + } + ] + } + }); + expect(config.logging.files[0].file, "./bacon.log"); + }); +}) \ No newline at end of file diff --git a/test/test_log.ts b/test/test_log.ts new file mode 100644 index 0000000000000000000000000000000000000000..5bd6853b426cebcffe5be256042c1e7159835120 --- /dev/null +++ b/test/test_log.ts @@ -0,0 +1,77 @@ +import * as Chai from "chai"; +import * as ChaiAsPromised from "chai-as-promised"; +import * as Proxyquire from "proxyquire"; +import * as RealLog from "../src/log"; +Chai.use(ChaiAsPromised); +const expect = Chai.expect; + +let created_logger = null; +let logger_closed = false; +let logged_messages = []; + +const WinstonMock = { + createLogger: (format, transports) => { + return created_logger = { + format, + transports, + close: () => { + logger_closed = true; + }, + silent: false, + log: (type, ...msg) => { + logged_messages = logged_messages.concat(msg); + } + }; + }, +}; + +const Log = (Proxyquire("../src/log", { + "winston": WinstonMock, +}).Log); + +describe("Log", () => { + + beforeEach(() => { + logger_closed = false; + logged_messages = []; + }) + + describe("ConfigureBridge", () => { + it("should pass if config is empty", () => { + Log.ConfigureBridge({}); + }); + it("should set basic log options", () => { + Log.ConfigureBridge({ + console: "warn", + lineDateFormat: "HH:mm:ss" + }); + expect(Log.config.console).to.equal("warn"); + expect(Log.config.lineDateFormat).to.equal("HH:mm:ss"); + expect(Log.config.files).to.be.empty; + }); + it("should setup file logging", () => { + Log.ConfigureBridge({ + files: [ + { + file: "./logfile.log" + } + ] + }); + expect(Log.config.files).to.not.be.empty; + expect(Log.config.files[0].file).to.equal("./logfile.log"); + }); + }); + describe("ForceSilent", () => { + it("should be silent", () => { + Log.ForceSilent(); + expect(created_logger.silent).to.be.true; + expect(logged_messages).to.contain("Log set to silent"); + }); + }); + describe("instance", () => { + it("should log without configuring", () => { + new Log("test").info("hi"); + expect(logged_messages).to.contain("hi"); + }); + }); +}) \ No newline at end of file