diff --git a/plugins/player_sync/test/controller.js b/plugins/player_sync/test/controller.js index d87c437e..ec2771d6 100644 --- a/plugins/player_sync/test/controller.js +++ b/plugins/player_sync/test/controller.js @@ -6,7 +6,7 @@ import * as lib from "@clusterio/lib"; import * as mock from "../../../test/mock.js"; import controllerEntrypoint from "../dist/node/controller.js"; import { plugin as info } from "../dist/node/index.js"; -import { DatabaseStatsRequest } from "../dist/node/messages.js"; +import { DatabaseStatsRequest, DownloadRequest, UploadRequest } from "../dist/node/messages.js"; describe("player_sync controller", function() { const databaseDir = path.join("temp", "test", "player_sync"); @@ -50,4 +50,41 @@ describe("player_sync controller", function() { assert.equal(stats.databaseEntries, 1); assert.equal(stats.largestEntry.name, "new"); }); + + describe("UploadRequest", function() { + let controller; + let disabled; + beforeEach(async function() { + disabled = []; + const stored = { generation: 1, name: "test", force: "player", tag: "stored" }; + await fs.writeFile(path.join(databaseDir, "player_sync.json"), JSON.stringify([["test", stored]])); + controller = await load(); + controller.instances = new Map([[1, { + config: { + get: name => { + if (name === "instance.name") { return "test instance"; } + return !disabled.includes(name.replace("player_sync.sync_", "")); + }, + }, + }]]); + }); + + async function upload(playerData) { + await mock.getHandler(controller, UploadRequest)({ instanceId: 1, playerName: "test", playerData }); + return (await mock.getHandler(controller, DownloadRequest)({ instanceId: 1, playerName: "test" })) + .playerData; + } + + it("should store the upload as is when everything is synced", async function() { + const playerData = { generation: 2, name: "test", force: "enemy", tag: "uploaded" }; + assert.deepEqual(await upload(playerData), playerData); + }); + it("should keep stored fields the uploading instance does not sync", async function() { + disabled = ["force"]; + assert.deepEqual( + await upload({ generation: 2, name: "test", tag: "uploaded" }), + { generation: 2, name: "test", force: "player", tag: "uploaded" }, + ); + }); + }); });