mirror of
https://github.com/smogon/pokemon-showdown.git
synced 2026-05-21 23:15:44 -05:00
This enables battles in tests to reset their RNG to what it originally
was when they were created. A number of tests do this already by
breaking encapsulating and modifying the prng variable directly. This
should fix that.
We also remove createBattleWithPRNG and min/maxSeed properties.
Firstly, the tests that were still using the maxSeed property were
setting it to Battle.seed which has no effect since the PRNG class does
not look at this property any more - so these were no-ops.
After removing this property from tests, maxRollSeed was never used, so
I removed it and renamed minRollSeed to DEFAULT_ROLL_SEED (since it's
constant).
The places that were still using minRollSeed also were no-ops or were
using createBattleWithPRNG. Since every single instance was actually
just using the same code, I removed createBattleWithPRNG and made
createBattle default to giving you a seed with DEFAULT_ROLL_SEED and
removed the minRollSeed property too.
This makes tests much simpler and reduces the usage surface of
TestTools; now, you must define your *own* seed if you're making a PRNG
for a test, or you use one that TestTools gives you; you may not use a
public property that TestTools gives you.
We also remove the code that removes a listener in the Battle Engine.
This was rendered obsolete by f6a7c4b (see more info [in the discussion
on github][disc]
[disc]:
https://github.com/Zarel/Pokemon-Showdown/pull/3272#discussion_r102414795
115 lines
3.1 KiB
JavaScript
115 lines
3.1 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('assert');
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
const Module = require('module');
|
|
const mock = require('mock-fs-require-fix');
|
|
|
|
const noop = () => {};
|
|
|
|
function getDirTypedContentsSync(dir, forceType) {
|
|
// Return value can be fed to mock-fs
|
|
if (forceType !== 'dir' && forceType !== 'file') throw new Error("Not implemented");
|
|
return fs.readdirSync(dir).reduce(function (dict, elem) {
|
|
dict[elem] = forceType === 'dir' ? {} : '';
|
|
return dict;
|
|
}, {});
|
|
}
|
|
|
|
function init(callback) {
|
|
require('./../app');
|
|
|
|
Rooms.RoomBattle.prototype.send = noop;
|
|
Rooms.RoomBattle.prototype.receive = noop;
|
|
for (let process of Rooms.SimulatorProcess.processes) {
|
|
// Don't crash -we don't care of battle child processes.
|
|
process.process.on('error', noop);
|
|
}
|
|
|
|
LoginServer.disabled = true;
|
|
|
|
// Disable writing to modlog
|
|
Rooms.Room.prototype.modlog = noop;
|
|
|
|
callback();
|
|
}
|
|
|
|
before('initialization', function (done) {
|
|
this.timeout(0); // Remove timeout limitation
|
|
|
|
// Load and override configuration before starting the server
|
|
let config;
|
|
try {
|
|
require.resolve('./../config/config');
|
|
} catch (err) {
|
|
if (err.code !== 'MODULE_NOT_FOUND') throw err; // Should never happen
|
|
|
|
console.log("config.js doesn't exist - creating one with default settings...");
|
|
fs.writeFileSync(path.resolve(__dirname, '../config/config.js'),
|
|
fs.readFileSync(path.resolve(__dirname, '../config/config-example.js'))
|
|
);
|
|
} finally {
|
|
config = require('./../config/config');
|
|
}
|
|
|
|
try {
|
|
let chatRoomsPath = require.resolve('./../config/chatrooms.json');
|
|
let chatRoomsData = require.cache[chatRoomsPath] = new Module(chatRoomsPath, module);
|
|
chatRoomsData.filename = chatRoomsData.id;
|
|
chatRoomsData.exports = []; // empty chatrooms list
|
|
chatRoomsData.loaded = true;
|
|
} catch (e) {}
|
|
|
|
// Actually crash if we crash
|
|
config.crashguard = false;
|
|
|
|
// Don't try to write to file system
|
|
config.logladderip = false;
|
|
config.logchallenges = false;
|
|
config.logchat = false;
|
|
|
|
// Don't create a REPL
|
|
require('./../repl').start = noop;
|
|
|
|
// Sandbox file system: it's possible for a production server to be running in the same directory.
|
|
// And using a sandbox is safer anyway.
|
|
const fsSandbox = {
|
|
'config': {},
|
|
'chat-plugins': getDirTypedContentsSync('chat-plugins', 'file'),
|
|
'mods': getDirTypedContentsSync('mods', 'dir'),
|
|
'logs': {
|
|
'chat': {}, 'ladderip': {}, 'modlog': {}, 'repl': {},
|
|
'lastbattle.txt': '0',
|
|
},
|
|
};
|
|
|
|
// `watchFile` is unsupported and throws with mock-fs
|
|
Object.defineProperty(fs, 'watchFile', {
|
|
get: function () {return noop;},
|
|
set: noop,
|
|
});
|
|
mock(fsSandbox);
|
|
|
|
init(done);
|
|
});
|
|
|
|
describe('Native timer/event loop globals', function () {
|
|
let globalList = ['setTimeout', 'clearTimeout', 'setImmediate', 'clearImmediate'];
|
|
for (let elem of globalList) {
|
|
describe('`' + elem + '`', function () {
|
|
it('should be a global function', function () {
|
|
assert.strictEqual(typeof global[elem], 'function');
|
|
});
|
|
});
|
|
}
|
|
});
|
|
|
|
describe('Battle simulation', function () {
|
|
require('./simulator');
|
|
});
|
|
|
|
describe('mocks', function () {
|
|
require('./mocks/Battle.spec');
|
|
});
|