Add test for servers loading

This commit is contained in:
Mia
2021-10-19 21:35:25 -05:00
parent 94ebbcd1a4
commit ea4e31dc53
5 changed files with 39 additions and 4 deletions

View File

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

View File

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

18
src/test/fixtures/servers.php vendored Normal file
View File

@@ -0,0 +1,18 @@
<?php
/* if ((substr($_SERVER['REMOTE_ADDR'],0,11) === '69.164.163.') ||
(@substr($_SERVER['HTTP_X_FORWARDED_FOR'],0,11) === '69.164.163.')) {
file_put_contents(dirname(__FILE__).'/log', "blocked: ".var_export($_SERVER, TRUE)."\n\n", FILE_APPEND);
die('website disabled');
} */
$PokemonServers = array (
'showdown' =>
array (
'name' => 'Smogon University',
'id' => 'showdown',
'server' => 'sim.psim.us',
'port' => 8000,
'owner' => 'mia',
),
);

View File

@@ -95,4 +95,4 @@ export async function randomBytes(size = 128) {
before(async () => {
await setupDB();
});
});

View File

@@ -12,6 +12,6 @@
},
"types": ["node"],
"include": [
"./src/*",
"./src/*", "./src/test/*",
],
}