Add Factorio to Clusterio communication API

Add send_json to the Clusterio module's API which allows sending
arbitrary tables serialized as JSON to plugins that listen on the
channel specified.
This commit is contained in:
Hornwitser committed 2020-01-04 17:34:16 +01:00
1 parent 6cf16cc09e
commit b8fb73f863
5 files changed
+203 -10

No files matched your search

+49
View File
@@ -1,4 +1,5 @@
const assert = require("assert").strict;
const events = require("events");
const fs = require("fs-extra");
const path = require("path");
@@ -122,5 +123,53 @@ describe("lib/factorio/server", function() {
assert.equal(server.version, "0.1.1");
});
});
describe("._handleIpc()", function() {
it("should emit the correct ipc event", async function() {
let waiter = events.once(server, 'ipc-channel');
await server._handleIpc(Buffer.from('\f$ipc:channel?j"value"'));
let result = await waiter;
assert.equal(result[0], "value");
});
it("should handle special characters in channel name", async function() {
let waiter = events.once(server, 'ipc-$ ?\x00\x0a:');
await server._handleIpc(Buffer.from('\f$ipc:$ \\x3f\\x00\\x0a:?j"value"'));
let result = await waiter;
assert.equal(result[0], "value");
});
it("should throw on malformed ipc line", async function() {
await assert.rejects(
server._handleIpc(Buffer.from('\f$ipc:blah')),
new Error('Malformed IPC line "\f$ipc:blah"')
);
})
it("should throw on unknown type", async function() {
await assert.rejects(
server._handleIpc(Buffer.from('\f$ipc:channel??')),
new Error("Unknown IPC type '?'")
);
})
it("should throw on unknown file type", async function() {
await assert.rejects(
server._handleIpc(Buffer.from('\f$ipc:channel?ffoo.invalid')),
new Error("Unknown IPC file format 'invalid'")
);
})
it("should throw on file name with slash", async function() {
await assert.rejects(
server._handleIpc(Buffer.from('\f$ipc:channel?fa/b')),
new Error("Invalid IPC file name 'a/b'")
);
})
it("should load and delete json file", async function() {
let filePath = server.writePath('script-output', 'data.json');
await fs.outputFile(filePath, '{"data":"spam"}');
let waiter = events.once(server, 'ipc-channel');
await server._handleIpc(Buffer.from('\f$ipc:channel?fdata.json'));
let result = await waiter;
assert.deepEqual(result[0], { "data": "spam" });
assert(!await fs.pathExists(filePath), "File was not deleted");
})
});
});
});