mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-11 23:04:52 +09:00
Port the globalChat plugin to the new plugin system. Since there's no config system yet the config options for it is removed.
92 lines
3.1 KiB
JavaScript
92 lines
3.1 KiB
JavaScript
const assert = require('assert').strict;
|
|
|
|
const mock = require('../../../test/mock');
|
|
const lines = require('../../../test/lib/factorio/lines');
|
|
const instance = require('../instance');
|
|
const info = require('../info');
|
|
|
|
|
|
describe("global_chat plugin", function() {
|
|
describe("removeTags()", function() {
|
|
it("should pass through an ordinary string", function() {
|
|
assert.equal(instance._removeTags("string"), "string");
|
|
});
|
|
it("should strip out gps tag", function() {
|
|
assert.equal(instance._removeTags("Look at [gps=12,-4]"), "Look at ");
|
|
});
|
|
it("should strip out train tag", function() {
|
|
assert.equal(instance._removeTags("Train [train=1235]"), "Train ");
|
|
});
|
|
});
|
|
|
|
describe("escapeLuaSrting", function() {
|
|
it("should escape backslashes", function() {
|
|
assert.equal(instance._escapeLuaString("\\a"), "\\\\a");
|
|
});
|
|
it("should escape double quotes", function() {
|
|
assert.equal(instance._escapeLuaString('"a"'), '\\"a\\"');
|
|
});
|
|
it("should escape single quotes", function() {
|
|
assert.equal(instance._escapeLuaString("'a'"), "\\'a\\'");
|
|
});
|
|
it("should escape nul bytes", function() {
|
|
assert.equal(instance._escapeLuaString("a\0b"), "a\\0b");
|
|
});
|
|
it("should escape newlines", function() {
|
|
assert.equal(instance._escapeLuaString("a\nb"), "a\\nb");
|
|
});
|
|
it("should escape carriage return", function() {
|
|
assert.equal(instance._escapeLuaString("a\rb"), "a\\rb");
|
|
});
|
|
it("should escape all combined", function() {
|
|
assert.equal(instance._escapeLuaString("a\\b\"c'd\0e\nf\rg"), "a\\\\b\\\"c\\'d\\0e\\nf\\rg");
|
|
});
|
|
});
|
|
|
|
describe("class InstancePlugin", function() {
|
|
let instancePlugin;
|
|
|
|
before(async function() {
|
|
instancePlugin = new instance.InstancePlugin(info, new mock.MockInstance());
|
|
await instancePlugin.init();
|
|
});
|
|
|
|
describe(".chatEventHandler()", function() {
|
|
it("should send received chat as command", async function() {
|
|
instancePlugin.instance.server.rconCommands = [];
|
|
await instancePlugin.chatEventHandler({ data: { instance_name: "test", content: "User: message" } });
|
|
assert.deepEqual(
|
|
instancePlugin.instance.server.rconCommands,
|
|
["/sc game.print('[test] User: message')"],
|
|
);
|
|
});
|
|
});
|
|
describe(".onOutput()", function() {
|
|
it("should forward chat", async function() {
|
|
let count = 0;
|
|
for (let [_, output] of lines.testLines) {
|
|
if (output.type === "action" && output.action === "CHAT") {
|
|
instancePlugin.instance.connector.sentMessages = [];
|
|
await instancePlugin.onOutput(output);
|
|
assert(instancePlugin.instance.connector.sentMessages.length, "message was not sent");
|
|
count++;
|
|
}
|
|
}
|
|
assert(count > 0, "no lines were tested");
|
|
});
|
|
it("should ignore regular output", async function() {
|
|
let count = 0;
|
|
for (let [_, output] of lines.testLines) {
|
|
if (output.type !== "action" || output.action !== "CHAT") {
|
|
instancePlugin.instance.connector.sentMessages = [];
|
|
await instancePlugin.onOutput(output);
|
|
assert(!instancePlugin.instance.connector.sentMessages.length, "message was sent");
|
|
count++;
|
|
}
|
|
}
|
|
assert(count > 0, "no lines were tested");
|
|
});
|
|
})
|
|
});
|
|
});
|