diff --git a/src/dispatcher.ts b/src/dispatcher.ts index e6f767f..9fe18af 100644 --- a/src/dispatcher.ts +++ b/src/dispatcher.ts @@ -162,7 +162,7 @@ export class Dispatcher { } return list; } - static servers: {[k: string]: RegisteredServer} = (() => { + static loadServers(): {[k: string]: RegisteredServer} { try { const stdout = child.execFileSync( `php`, ['-f', 'src/lib/load-servers.php', Config.serverlist] @@ -172,6 +172,7 @@ export class Dispatcher { if (e.code !== 'ENOENT') throw e; } return {}; - })(); + } + static servers: {[k: string]: RegisteredServer} = Dispatcher.loadServers(); static ActionError = ActionError; } diff --git a/src/test/dispatcher.test.ts b/src/test/dispatcher.test.ts index cf8ccb2..f2e062c 100644 --- a/src/test/dispatcher.test.ts +++ b/src/test/dispatcher.test.ts @@ -5,6 +5,8 @@ */ import {strict as assert} from 'assert'; import {Config} from '../config-loader'; +import {Dispatcher} from '../dispatcher'; +import * as path from 'path'; import * as utils from './test-utils'; @@ -65,4 +67,18 @@ describe('Dispatcher features', () => { assert(act === 'mmr'); assert(body?.userid === 'mia'); }); + it("Should load servers properly", () => { + Config.serverlist = path.join(__dirname, '/../../', 'src/test/fixtures/servers.php'); + const servers = Dispatcher.loadServers(); + const expected = { + showdown: { + name: 'Smogon University', + id: 'showdown', + server: 'sim.psim.us', + port: 8000, + owner: 'mia' + }, + }; + assert.deepStrictEqual(expected, servers); + }); }); diff --git a/src/test/fixtures/servers.php b/src/test/fixtures/servers.php new file mode 100644 index 0000000..62e4329 --- /dev/null +++ b/src/test/fixtures/servers.php @@ -0,0 +1,18 @@ + + array ( + 'name' => 'Smogon University', + 'id' => 'showdown', + 'server' => 'sim.psim.us', + 'port' => 8000, + 'owner' => 'mia', + ), +); diff --git a/src/test/test-utils.ts b/src/test/test-utils.ts index 72ba3f6..e38c2b6 100644 --- a/src/test/test-utils.ts +++ b/src/test/test-utils.ts @@ -95,4 +95,4 @@ export async function randomBytes(size = 128) { before(async () => { await setupDB(); -}); +}); \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 86fd2ab..b820a37 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,6 +12,6 @@ }, "types": ["node"], "include": [ - "./src/*", + "./src/*", "./src/test/*", ], }