From e76eb91ac932ed2029ebf718dcc1fa898048d832 Mon Sep 17 00:00:00 2001 From: Daniel Scalzi Date: Thu, 27 Aug 2020 21:55:24 -0400 Subject: [PATCH] Test improvements. Clean mojang test directory. Added test for server status. Disable winston in test mode. --- package-lock.json | 18 +++++++ package.json | 4 +- src/common/logging/loggerutil.ts | 2 +- src/common/mojang/net/ServerStatusAPI.ts | 2 +- test/mojang/net/ServerStatusAPITest.ts | 52 +++++++++++++++++++ .../MojangRestAPITest.ts} | 6 +-- 6 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 test/mojang/net/ServerStatusAPITest.ts rename test/mojang/{mojangTest.ts => rest/MojangRestAPITest.ts} (97%) diff --git a/package-lock.json b/package-lock.json index 4c3d91e7..eee27432 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1418,6 +1418,15 @@ "integrity": "sha512-aN5IAC8QNtSUdQzxu7lGBgYAOuU1tmRU4c9dIq5OKGf/SBVjXo+ffM2wEjudAWbgpOhy60nLoAGH1xm8fpCKFQ==", "dev": true }, + "@types/chai-as-promised": { + "version": "7.1.3", + "resolved": "https://registry.npmjs.org/@types/chai-as-promised/-/chai-as-promised-7.1.3.tgz", + "integrity": "sha512-FQnh1ohPXJELpKhzjuDkPLR2BZCAqed+a6xV4MI/T3XzHfd2FlarfUGUdZYgqYe8oxkYn0fchHEeHfHqdZ96sg==", + "dev": true, + "requires": { + "@types/chai": "*" + } + }, "@types/color-name": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/@types/color-name/-/color-name-1.1.1.tgz", @@ -3078,6 +3087,15 @@ "type-detect": "^4.0.5" } }, + "chai-as-promised": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/chai-as-promised/-/chai-as-promised-7.1.1.tgz", + "integrity": "sha512-azL6xMoi+uxu6z4rhWQ1jbdUhOMhis2PvscD/xjLqNMkv3BPPp2JyyuTHOrf9BOosGpNQ11v6BKv/g57RXbiaA==", + "dev": true, + "requires": { + "check-error": "^1.0.2" + } + }, "chalk": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.0.tgz", diff --git a/package.json b/package.json index 21cfd182..f01ef528 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "lint": "eslint --ext=jsx,js,tsx,ts src", "dev": "electron-webpack dev", "compile": "electron-webpack", - "test": "cross-env TS_NODE_PROJECT='./tsconfig.test.json' mocha -r ts-node/register -r tsconfig-paths/register test/**/*.ts" + "test": "cross-env TS_NODE_PROJECT='./tsconfig.test.json' NODE_ENV=test mocha -r ts-node/register -r tsconfig-paths/register test/**/*.ts" }, "engines": { "node": "12.x.x" @@ -51,6 +51,7 @@ "@types/adm-zip": "^0.4.33", "@types/async": "^3.2.3", "@types/chai": "^4.2.12", + "@types/chai-as-promised": "^7.1.3", "@types/discord-rpc": "^3.0.4", "@types/fs-extra": "^9.0.1", "@types/jquery": "^3.5.1", @@ -68,6 +69,7 @@ "@typescript-eslint/eslint-plugin": "^3.10.1", "@typescript-eslint/parser": "^3.10.1", "chai": "^4.2.0", + "chai-as-promised": "^7.1.1", "cross-env": "^7.0.2", "electron": "^9.2.1", "electron-builder": "^22.8.0", diff --git a/src/common/logging/loggerutil.ts b/src/common/logging/loggerutil.ts index 295bfbf0..2c1dd0b7 100644 --- a/src/common/logging/loggerutil.ts +++ b/src/common/logging/loggerutil.ts @@ -35,7 +35,7 @@ export class LoggerUtil { return `[${moment().format('YYYY-MM-DD hh:mm:ss').trim()}] [${info.level}] [${info.label}]: ${info.message}${info.stack ? `\n${info.stack}` : ''}` }) ), - level: 'debug', + level: process.env.NODE_ENV === 'test' ? 'emerg' : 'debug', transports: [ new transports.Console() ] diff --git a/src/common/mojang/net/ServerStatusAPI.ts b/src/common/mojang/net/ServerStatusAPI.ts index 063b2500..98895d98 100644 --- a/src/common/mojang/net/ServerStatusAPI.ts +++ b/src/common/mojang/net/ServerStatusAPI.ts @@ -89,7 +89,7 @@ export function getServerStatus(protocol: number, address: string, port = 25565) socket.write(getRequestPacket()) }) - socket.setTimeout(10000, () => { + socket.setTimeout(5000, () => { socket.destroy() logger.error(`Server Status Socket timed out (${address}:${port})`) reject(new Error(`Server Status Socket timed out (${address}:${port})`)) diff --git a/test/mojang/net/ServerStatusAPITest.ts b/test/mojang/net/ServerStatusAPITest.ts new file mode 100644 index 00000000..4e53ae89 --- /dev/null +++ b/test/mojang/net/ServerStatusAPITest.ts @@ -0,0 +1,52 @@ +import chai, { expect } from 'chai' +import chaiAsPromised from 'chai-as-promised' +import { getServerStatus, ServerStatus } from 'common/mojang/net/ServerStatusAPI' + +chai.use(chaiAsPromised) + +describe('[Server Status API] Errors', () => { + + it('Server Status (Not Found)', async () => { + + await expect(getServerStatus(47, 'a', 25565)).to.eventually.be.null + + }).timeout(5000) + + it('Server Status (Wrong Port)', async () => { + + await expect(getServerStatus(47, 'play.hypixel.net', 34454)).to.eventually.be.null + + }).timeout(5000) + +}) + +function verifyResult(res: ServerStatus): void { + expect(res).to.not.be.null + expect(res).to.be.an('object') + expect(res).to.have.property('version') + expect(res).to.have.property('players') + expect(res).to.have.property('description') + expect(res.players).to.be.an('object') + expect(res.players).to.have.property('max') + expect(res.players).to.have.property('online') + expect(res.description).to.be.an('object') + expect(res.description).to.have.property('text') +} + +const serversToCheck = [ + 'play.hypixel.net', + 'play.hivemc.com', + 'us.mineplex.com' +] + +describe('[Server Status API] Server Status', () => { + + for(const server of serversToCheck) { + it(`Server Status (${server})`, async () => { + + verifyResult((await getServerStatus(47, server, 25565))!) + + }) + } + +}) \ No newline at end of file diff --git a/test/mojang/mojangTest.ts b/test/mojang/rest/MojangRestAPITest.ts similarity index 97% rename from test/mojang/mojangTest.ts rename to test/mojang/rest/MojangRestAPITest.ts index dc389639..c4d8a3b3 100644 --- a/test/mojang/mojangTest.ts +++ b/test/mojang/rest/MojangRestAPITest.ts @@ -31,7 +31,7 @@ function expectMojangResponse(res: MojangResponse, responseCode: Mojang } } -describe('Mojang Errors', () => { +describe('[Mojang Rest API] Errors', () => { after(() => { nock.cleanAll() @@ -72,7 +72,7 @@ describe('Mojang Errors', () => { }) }) -describe('Mojang Status', () => { +describe('[Mojang Rest API] Status', () => { it('Status (Online)', async () => { @@ -91,7 +91,7 @@ describe('Mojang Status', () => { }) -describe('Mojang Auth', () => { +describe('[Mojang Rest API] Auth', () => { it('Authenticate', async () => {