mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-21 08:03:13 +09:00
Cover ctl output code that only ran in child processes
CI coverage does not measure the ctl processes the integration tests start, so cover the Ctl log and debug event handlers, start-all, stop-all and pickFields with unit tests, and run mod show, user list flags, parse-exchange-string and log follow in process. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f49c633e77
commit
f52821d1cd
4 files changed
+170
-1
No files matched your search
@@ -0,0 +1,56 @@
|
||||
import assert from "node:assert/strict";
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { Ctl } from "@clusterio/ctl/dist/node/ctl.js";
|
||||
import { CommandOutput } from "@clusterio/ctl/dist/node/src/command_ops.js";
|
||||
|
||||
/** Run fn and return the lines it printed with console.log */
|
||||
async function captureLog(fn) {
|
||||
const lines = [];
|
||||
const originalLog = console.log; // eslint-disable-line no-console
|
||||
console.log = (...args) => lines.push(args.join(" ")); // eslint-disable-line no-console
|
||||
try {
|
||||
await fn();
|
||||
} finally {
|
||||
console.log = originalLog; // eslint-disable-line no-console
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
describe("ctl/ctl", function() {
|
||||
describe("class Ctl", function() {
|
||||
let ctl;
|
||||
beforeEach(function() {
|
||||
const connector = new lib.VirtualConnector(
|
||||
lib.Address.fromShorthand({ controlId: 1 }),
|
||||
lib.Address.fromShorthand("controller"),
|
||||
);
|
||||
ctl = new Ctl(connector, new lib.CtlConfig("control"));
|
||||
});
|
||||
|
||||
it("prints log events as JSON lines only when streaming the log", async function() {
|
||||
const info = { level: "info", message: "hello" };
|
||||
ctl.output = new CommandOutput("json");
|
||||
const logged = await captureLog(() => ctl.handleLogMessageEvent(new lib.LogMessageEvent({ ...info })));
|
||||
assert(logged.every(line => !line.startsWith("{")), "log event printed as a result");
|
||||
ctl.output.streamingLog = true;
|
||||
assert.deepEqual(
|
||||
await captureLog(() => ctl.handleLogMessageEvent(new lib.LogMessageEvent({ ...info, timestamp: 1 }))),
|
||||
['{"level":"info","message":"hello","timestamp":1}'],
|
||||
);
|
||||
});
|
||||
it("logs log events in human mode", async function() {
|
||||
ctl.output.streamingLog = true;
|
||||
const info = { level: "verbose", message: "hello" };
|
||||
assert.deepEqual(await captureLog(() => ctl.handleLogMessageEvent(new lib.LogMessageEvent(info))), []);
|
||||
});
|
||||
it("prints debug WebSocket messages", async function() {
|
||||
const event = new lib.DebugWsMessageEvent("in", "content");
|
||||
assert.deepEqual(await captureLog(() => ctl.handleDebugWsMessageEvent(event)), ["WS in content"]);
|
||||
ctl.output = new CommandOutput("json");
|
||||
assert.deepEqual(
|
||||
await captureLog(() => ctl.handleDebugWsMessageEvent(event)),
|
||||
['{"direction":"in","content":"content"}'],
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user