Add tests

This commit is contained in:
Cooldude2606 committed 2025-07-16 01:25:27 +01:00
1 parent 449bd68fab
commit 88ca4563a8
3 files changed
+46 -7

No files matched your search

+3
View File
@@ -1302,6 +1302,9 @@ end`.replace(/\r?\n/g, " ");
} }
async handleInstanceRestartRequest(request: lib.InstanceRestartRequest) { async handleInstanceRestartRequest(request: lib.InstanceRestartRequest) {
if (this._status !== "running") {
throw new Error("Instance is not running");
}
await this.stop(); await this.stop();
// Stopping invalidates this instance object, a new one must be created by the host // Stopping invalidates this instance object, a new one must be created by the host
// To do this we simulate a waking an instance and sending a start request // To do this we simulate a waking an instance and sending a start request
+42 -6
View File
@@ -529,12 +529,26 @@ describe("Integration of Clusterio", function() {
}); });
}); });
describe("instance start", function() { for (let cmd of ["start", "restart"]) {
describe(`instance ${cmd}`, function() {
async function prepareToStart() {
if (cmd === "restart") {
await execCtl("instance start test");
}
}
after(async function() {
// It is expected that after these tests the instance is running, I dislike this dependency
// So this is here to make sure the other tests can continue any of these ones fail
const instances = await getInstances();
if (instances.get(44).status !== "running") {
await execCtl("instance start test");
}
});
it("should not hang if factorio version does not exist", async function() { it("should not hang if factorio version does not exist", async function() {
slowTest(this); slowTest(this);
try { try {
await execCtl("instance config set 44 factorio.version 0.1.2"); await execCtl("instance config set 44 factorio.version 0.1.2");
await assert.rejects(execCtl("instance start test")); await assert.rejects(execCtl(`instance ${cmd} test`));
} finally { } finally {
await execCtl("instance config set 44 factorio.version latest"); await execCtl("instance config set 44 factorio.version latest");
@@ -544,7 +558,7 @@ describe("Integration of Clusterio", function() {
slowTest(this); slowTest(this);
try { try {
await execCtl("instance config set 44 factorio.game_port 100000"); await execCtl("instance config set 44 factorio.game_port 100000");
await assert.rejects(execCtl("instance start test")); await assert.rejects(execCtl(`instance ${cmd} test`));
await checkInstanceStatus(44, "stopped"); await checkInstanceStatus(44, "stopped");
} finally { } finally {
@@ -553,14 +567,16 @@ describe("Integration of Clusterio", function() {
}); });
it("starts the given save", async function() { it("starts the given save", async function() {
slowTest(this); slowTest(this);
await execCtl("instance start test --save world.zip"); await prepareToStart();
await execCtl(`instance ${cmd} test --save world.zip`);
await checkInstanceStatus(44, "running"); await checkInstanceStatus(44, "running");
}); });
it("allows having a separate console log", async function() { it("allows having a separate console log", async function() {
slowTest(this); slowTest(this);
await execCtl("instance stop 44"); await execCtl("instance stop 44");
await execCtl("instance config set 44 factorio.console_logging true"); await execCtl("instance config set 44 factorio.console_logging true");
await execCtl("instance start test"); await prepareToStart();
await execCtl(`instance ${cmd} test`);
await checkInstanceStatus(44, "running"); await checkInstanceStatus(44, "running");
const checkMessage = `check${Date.now()}`; const checkMessage = `check${Date.now()}`;
await sendRcon(44, checkMessage); await sendRcon(44, checkMessage);
@@ -573,12 +589,32 @@ describe("Integration of Clusterio", function() {
await execCtl("instance stop 44"); await execCtl("instance stop 44");
let savesDir = path.join("temp", "test", "instances", "test", "saves"); let savesDir = path.join("temp", "test", "instances", "test", "saves");
await fs.copy(path.join(savesDir, "world.zip"), path.join(savesDir, "_autosave1.zip")); await fs.copy(path.join(savesDir, "world.zip"), path.join(savesDir, "_autosave1.zip"));
await execCtl("instance start test"); await prepareToStart();
await execCtl(`instance ${cmd} test`);
let saves = await getControl().sendTo({ instanceId: 44 }, new lib.InstanceSaveDetailsListRequest()); let saves = await getControl().sendTo({ instanceId: 44 }, new lib.InstanceSaveDetailsListRequest());
let running = saves.find(s => s.loaded); let running = saves.find(s => s.loaded);
assert(running.name !== "_autosave1.zip"); assert(running.name !== "_autosave1.zip");
}); });
if (cmd === "restart") {
it("fails if the server is not running", async function() {
await execCtl("instance stop test");
await checkInstanceStatus(44, "stopped");
await assert.rejects(
execCtl("instance restart test"),
/Instance is not running/,
);
await execCtl("instance start test");
}); });
} else {
it("fails if the server is running", async function() {
await assert.rejects(
execCtl("instance start test"),
/Instance is already running./,
);
});
}
});
}
describe("instance send-rcon", function() { describe("instance send-rcon", function() {
this.afterAll(async function() { this.afterAll(async function() {
+1 -1
View File
@@ -4,7 +4,7 @@ const lib = require("@clusterio/lib");
const { Host } = require("@clusterio/host"); const { Host } = require("@clusterio/host");
describe("messages/controller", function() { describe("messages/host", function() {
/** @type {Host} */ /** @type {Host} */
let host; let host;