Files
factorio-clusterio/plugins/global_chat/test/plugin.js
T
Hornwitser 7f8a700716 Refactor message registration and validation
Split up the action of registring an event or request data class to be
decoded by the Link class and the action of registring a handler for
those requests and events.  This moves the validation work from Link
instantiation time to startup initialisation which makes logic errors
fail on startup instead of failing instantiation of the link when a
connection attempt is made.

Additionally implement a partial validation logic for messages received
using the newly refactored message registration.  This unfortunately
ended up merged with the registration refactor.
2023-06-03 16:47:47 +02:00

72 lines
2.4 KiB
JavaScript

"use strict";
const assert = require("assert").strict;
const libLink = require("@clusterio/lib/link");
const mock = require("../../../test/mock");
const lines = require("../../../test/lib/factorio/lines");
const instance = require("../instance");
const info = require("../info");
const { ChatEvent } = require("../messages");
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("class InstancePlugin", function() {
let instancePlugin;
before(async function() {
libLink.Link.register(ChatEvent);
instancePlugin = new instance.InstancePlugin(info, new mock.MockInstance(), new mock.MockHost());
await instancePlugin.init();
});
describe(".handleChatEvent()", function() {
it("should send received chat as command", async function() {
instancePlugin.instance.server.rconCommands = [];
await instancePlugin.handleChatEvent(new ChatEvent("test", "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 += 1;
}
}
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 += 1;
}
}
assert(count > 0, "no lines were tested");
});
});
});
});