Init files

This commit is contained in:
2020-05-31 15:06:55 +03:00
commit 3cd6a07fbf
29 changed files with 947 additions and 0 deletions

View File

@ -0,0 +1,59 @@
const { readFileSync } = require('fs');
const _ = require('lodash');
const { ScreepsServer, stdHooks } = require('screeps-server-mockup');
const DIST_MAIN_JS = 'dist/main.js';
/*
* Helper class for creating a ScreepsServer and resetting it between tests.
* See https://github.com/Hiryus/screeps-server-mockup for instructions on
* manipulating the terrain and game state.
*/
class IntegrationTestHelper {
private _server: any;
private _player: any;
get server() {
return this._server;
}
get player() {
return this._player;
}
async beforeEach() {
this._server = new ScreepsServer();
// reset world but add invaders and source keepers bots
await this._server.world.reset();
// create a stub world composed of 9 rooms with sources and controller
await this._server.world.stubWorld();
// add a player with the built dist/main.js file
const modules = {
main: readFileSync(DIST_MAIN_JS).toString(),
};
this._player = await this._server.world.addBot({ username: 'player', room: 'W0N1', x: 15, y: 15, modules });
// Start server
await this._server.start();
}
async afterEach() {
await this._server.stop();
}
}
beforeEach(async () => {
await helper.beforeEach();
});
afterEach(async () => {
await helper.afterEach();
});
before(() => {
stdHooks.hookWrite();
});
export const helper = new IntegrationTestHelper();

View File

@ -0,0 +1,18 @@
import {assert} from "chai";
import {helper} from "./helper";
describe("main", () => {
it("runs a server and matches the game tick", async function () {
for (let i = 1; i < 10; i += 1) {
assert.equal(await helper.server.world.gameTime, i);
await helper.server.tick();
}
});
it("writes and reads to memory", async function () {
await helper.player.console(`Memory.foo = 'bar'`);
await helper.server.tick();
const memory = JSON.parse(await helper.player.memory);
assert.equal(memory.foo, 'bar');
});
});

13
test/mocha.opts Normal file
View File

@ -0,0 +1,13 @@
--require test/setup-node.js
--require ts-node/register
--ui bdd
--reporter spec
--bail
--full-trace
--watch-extensions tsx,ts
--colors
--recursive
--timeout 5000
--exit

6
test/setup-node.js Normal file
View File

@ -0,0 +1,6 @@
//inject mocha globally to allow custom interface refer without direct import - bypass bundle issue
global._ = require('lodash');
global.mocha = require('mocha');
global.chai = require('chai');
global.sinon = require('sinon');
global.chai.use(require('sinon-chai'));

25
test/unit/main.test.ts Normal file
View File

@ -0,0 +1,25 @@
import {assert} from "chai";
import {loop} from "../../src/main";
import {Game, Memory} from "./mock"
describe("main", () => {
before(() => {
// runs before all test in this block
});
beforeEach(() => {
// runs before each test in this block
// @ts-ignore : allow adding Game to global
global.Game = _.clone(Game);
// @ts-ignore : allow adding Memory to global
global.Memory = _.clone(Memory);
});
it("should export a loop function", () => {
assert.isTrue(typeof loop === "function");
});
it("should return void when called with no context", () => {
assert.isUndefined(loop());
});
});

10
test/unit/mock.ts Normal file
View File

@ -0,0 +1,10 @@
export const Game = {
creeps: [],
rooms: [],
spawns: {},
time: 12345
};
export const Memory = {
creeps: []
};