Add tests

This commit is contained in:
Cooldude2606 committed 2026-06-01 00:31:29 +01:00
1 parent 22b16a7ac2
commit 2c199028ea
1 file changed
+66 -13
+66 -13
View File
@@ -7,9 +7,11 @@ const mock = require("../../../test/mock");
const controller = require("../dist/node/controller");
const instance = require("../dist/node/instance");
const info = require("../dist/node/index").plugin;
const { FetchPlayerCodeRequest, SetVerifyCodeRequest } = require("../dist/node/messages");
const { FetchPlayerCodeRequest, SetVerifyCodeRequest, PlayerAuthServer } = require("../dist/node/messages");
const { testRoundTripJsonSerialisable, testMatrix } = require("../../../test/common");
const lib = require("@clusterio/lib");
function postJSON(url, body) {
return fetch(url, {
method: "POST",
@@ -19,6 +21,44 @@ function postJSON(url, body) {
}
describe("player_auth", function() {
describe("messages.js", function() {
describe("PlayerAuthServer", function() {
it("should be round trip json serialisable", function() {
testRoundTripJsonSerialisable(PlayerAuthServer, testMatrix(
["server1", "server2"], // name
["127.0.0.1", "10.0.0.1:1234"], // address
[undefined, "1.1.0", "2.0.0"], // version
));
});
});
describe("FetchPlayerCodeResponse", function() {
it("should be round trip json serialisable", function() {
testRoundTripJsonSerialisable(FetchPlayerCodeRequest.Response, testMatrix(
["abc123", "player-code"], // playerCode
["http://localhost", "https://example.com"], // controllerUrl
));
});
});
describe("FetchPlayerCodeRequest", function() {
it("should be round trip json serialisable", function() {
testRoundTripJsonSerialisable(FetchPlayerCodeRequest, testMatrix(
["player1", "player2"], // player
));
});
});
describe("SetVerifyCodeRequest", function() {
it("should be round trip json serialisable", function() {
testRoundTripJsonSerialisable(SetVerifyCodeRequest, testMatrix(
["player1", "player2"], // player
["code123", "verify456"], // verifyCode
));
});
});
});
describe("controller.js", function() {
describe("generateCode()", function() {
it("should generate a code of the given length", async function() {
@@ -60,33 +100,46 @@ describe("player_auth", function() {
describe("/api/player_auth/servers", function() {
it("should return a list of running servers with player_auth loaded", async function() {
function addInstance(id, status, load, name) {
function addInstance(id, status, load, name, version) {
controllerPlugin.controller.instances.records.set(id, {
config: {
get(field) {
if (field === "player_auth.load_plugin") {
return load;
} else if (field === "factorio.settings") {
}
if (field === "factorio.settings") {
return { name };
} else if (field === "instance.id") {
}
if (field === "instance.id") {
return id;
}
if (field === "instance.assigned_host") {
return 1;
}
throw new Error(`field ${field} not implemented`);
},
},
status,
gamePort: 34197,
factorioVersion: version,
});
}
addInstance(1, "running", true, "running loaded");
addInstance(2, "stopped", true, "stopped loaded");
addInstance(3, "running", false, "running unloaded");
addInstance(4, "stopped", false, "stopped unloaded");
addInstance(5, "running", true, undefined);
addInstance(1, "running", true, "running loaded", "1.1.0");
addInstance(2, "stopped", true, "stopped loaded", "1.1.1");
addInstance(3, "running", false, "running unloaded", "1.1.2");
addInstance(4, "stopped", false, "stopped unloaded", "1.1.3");
addInstance(5, "running", true, undefined, undefined);
controllerPlugin.controller.hosts.set(1, {
publicAddress: "127.0.0.1",
});
const result = await fetch(`${controllerUrl}/api/player_auth/servers`);
assert.deepEqual(await result.json(), ["running loaded", "unnamed server"]);
for (let id of [1, 2, 3, 4, 5]) {
controllerPlugin.controller.instances.records.delete(id);
}
assert.deepEqual(await result.json(), [
{ name: "running loaded", address: "127.0.0.1:34197", factorioVersion: "1.1.0" },
{ name: "unnamed server", address: "127.0.0.1:34197" },
]);
});
});