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
+87 -48

No files matched your search

+3
View File
@@ -1302,6 +1302,9 @@ end`.replace(/\r?\n/g, " ");
}
async handleInstanceRestartRequest(request: lib.InstanceRestartRequest) {
if (this._status !== "running") {
throw new Error("Instance is not running");
}
await this.stop();
// 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
+83 -47
View File
@@ -529,56 +529,92 @@ describe("Integration of Clusterio", function() {
});
});
describe("instance start", function() {
it("should not hang if factorio version does not exist", async function() {
slowTest(this);
try {
await execCtl("instance config set 44 factorio.version 0.1.2");
await assert.rejects(execCtl("instance start test"));
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() {
slowTest(this);
try {
await execCtl("instance config set 44 factorio.version 0.1.2");
await assert.rejects(execCtl(`instance ${cmd} test`));
} finally {
await execCtl("instance config set 44 factorio.version latest");
} finally {
await execCtl("instance config set 44 factorio.version latest");
}
});
it("should not leave the instance in the stopping state if it fails", async function() {
slowTest(this);
try {
await execCtl("instance config set 44 factorio.game_port 100000");
await assert.rejects(execCtl(`instance ${cmd} test`));
await checkInstanceStatus(44, "stopped");
} finally {
await execCtl("instance config set 44 factorio.game_port");
}
});
it("starts the given save", async function() {
slowTest(this);
await prepareToStart();
await execCtl(`instance ${cmd} test --save world.zip`);
await checkInstanceStatus(44, "running");
});
it("allows having a separate console log", async function() {
slowTest(this);
await execCtl("instance stop 44");
await execCtl("instance config set 44 factorio.console_logging true");
await prepareToStart();
await execCtl(`instance ${cmd} test`);
await checkInstanceStatus(44, "running");
const checkMessage = `check${Date.now()}`;
await sendRcon(44, checkMessage);
const consoleLog = await fs.readFile(path.join("temp", "test", "instances", "test", "console.log"));
assert(consoleLog.includes(checkMessage));
await execCtl("instance config set 44 factorio.console_logging false");
});
it("copies the save if an autosave is the target", async function() {
slowTest(this);
await execCtl("instance stop 44");
let savesDir = path.join("temp", "test", "instances", "test", "saves");
await fs.copy(path.join(savesDir, "world.zip"), path.join(savesDir, "_autosave1.zip"));
await prepareToStart();
await execCtl(`instance ${cmd} test`);
let saves = await getControl().sendTo({ instanceId: 44 }, new lib.InstanceSaveDetailsListRequest());
let running = saves.find(s => s.loaded);
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./,
);
});
}
});
it("should not leave the instance in the stopping state if it fails", async function() {
slowTest(this);
try {
await execCtl("instance config set 44 factorio.game_port 100000");
await assert.rejects(execCtl("instance start test"));
await checkInstanceStatus(44, "stopped");
} finally {
await execCtl("instance config set 44 factorio.game_port");
}
});
it("starts the given save", async function() {
slowTest(this);
await execCtl("instance start test --save world.zip");
await checkInstanceStatus(44, "running");
});
it("allows having a separate console log", async function() {
slowTest(this);
await execCtl("instance stop 44");
await execCtl("instance config set 44 factorio.console_logging true");
await execCtl("instance start test");
await checkInstanceStatus(44, "running");
const checkMessage = `check${Date.now()}`;
await sendRcon(44, checkMessage);
const consoleLog = await fs.readFile(path.join("temp", "test", "instances", "test", "console.log"));
assert(consoleLog.includes(checkMessage));
await execCtl("instance config set 44 factorio.console_logging false");
});
it("copies the save if an autosave is the target", async function() {
slowTest(this);
await execCtl("instance stop 44");
let savesDir = path.join("temp", "test", "instances", "test", "saves");
await fs.copy(path.join(savesDir, "world.zip"), path.join(savesDir, "_autosave1.zip"));
await execCtl("instance start test");
let saves = await getControl().sendTo({ instanceId: 44 }, new lib.InstanceSaveDetailsListRequest());
let running = saves.find(s => s.loaded);
assert(running.name !== "_autosave1.zip");
});
});
}
describe("instance send-rcon", function() {
this.afterAll(async function() {
+1 -1
View File
@@ -4,7 +4,7 @@ const lib = require("@clusterio/lib");
const { Host } = require("@clusterio/host");
describe("messages/controller", function() {
describe("messages/host", function() {
/** @type {Host} */
let host;