From 14b8b5ac61e1784ef6d9512271280c8820ed1e25 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Mon, 4 Aug 2025 18:28:25 +0000 Subject: [PATCH] ctl instance stop-all test --- test/ctl/instance start_all.js | 83 ----------------------- test/ctl/instance.js | 120 +++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 83 deletions(-) delete mode 100644 test/ctl/instance start_all.js create mode 100644 test/ctl/instance.js diff --git a/test/ctl/instance start_all.js b/test/ctl/instance start_all.js deleted file mode 100644 index 02d2c876..00000000 --- a/test/ctl/instance start_all.js +++ /dev/null @@ -1,83 +0,0 @@ -"use strict"; - -const assert = require("assert").strict; -const lib = require("@clusterio/lib"); - -// Import the command registration helper from the compiled Ctl package. This -// gives us access to the full command tree without spinning up a controller or -// host process. -const { registerCommands } = require("@clusterio/ctl/dist/node/src/commands"); - -// yargs instance used only for command registration (it will not parse any -// command-line input in this test). -function createYargsStub() { - const stub = {}; - for (const method of ["command", "option", "options", "positional"]) { - stub[method] = () => stub; - } - return stub; -} -const yargs = createYargsStub(); - -describe("ctl instance start-all command", function () { - let startAllCommand; - - before(async function () { - // Build the root command tree so we can fetch the start-all command. - const rootCommands = await registerCommands(new Map(), yargs); - const instanceTree = rootCommands.subCommands.get("instance"); - startAllCommand = instanceTree.subCommands.get("start-all"); - - if (!startAllCommand) { - throw new Error("start-all command was not found in command tree"); - } - }); - - /** - * Helper that constructs a mock control link implementing the minimal API - * surface required by the start-all command (send and sendTo). - * - * @param {lib.InstanceDetails[]} instances – list returned by the mocked - * InstanceDetailsListRequest. - */ - function createMockControl(instances) { - return { - // Called by the command to fetch the instance list. - async send(_request) { - return instances; - }, - - // Called by the command for each instance that should be started. - started: [], - async sendTo(target, _request) { - this.started.push(target.instanceId); - }, - }; - } - - it("starts only stopped instances that are not excluded by default", async function () { - const instances = [ - new lib.InstanceDetails("one", 1, undefined, undefined, "stopped", "1.1.0", 0, false), - new lib.InstanceDetails("two", 2, undefined, undefined, "running", "1.1.0", 0, false), - new lib.InstanceDetails("three", 3, undefined, undefined, "stopped", "1.1.0", 0, true), - ]; - const control = createMockControl(instances); - - await startAllCommand.run({ force: false }, control); - - assert.deepEqual(control.started, [1], "Should only start non-excluded stopped instances"); - }); - - it("starts excluded instances when --force flag is provided", async function () { - const instances = [ - new lib.InstanceDetails("one", 1, undefined, undefined, "stopped", "1.1.0", 0, false), - new lib.InstanceDetails("three", 3, undefined, undefined, "stopped", "1.1.0", 0, true), - ]; - const control = createMockControl(instances); - - await startAllCommand.run({ force: true }, control); - - // Order is not guaranteed – sort before comparison. - assert.deepEqual(control.started.sort(), [1, 3], "--force should include excluded instances"); - }); -}); diff --git a/test/ctl/instance.js b/test/ctl/instance.js new file mode 100644 index 00000000..b18ad353 --- /dev/null +++ b/test/ctl/instance.js @@ -0,0 +1,120 @@ +"use strict"; + +const assert = require("assert").strict; +const lib = require("@clusterio/lib"); + +// Import the command registration helper from the compiled Ctl package. This +// gives us access to the full command tree without spinning up a controller or +// host process. +const { registerCommands } = require("@clusterio/ctl/dist/node/src/commands"); + +// yargs instance used only for command registration (it will not parse any +// command-line input in these tests). +function createYargsStub() { + const stub = {}; + for (const method of ["command", "option", "options", "positional"]) { + stub[method] = () => stub; + } + return stub; +} +const yargs = createYargsStub(); + +/** + * Fetches the requested sub-command from the command tree under the + * "instance" command. + * + * @param {string} name – The name of the sub-command (e.g. "start-all"). + * @returns {Promise} The command object. + */ +async function getInstanceSubCommand(name) { + const rootCommands = await registerCommands(new Map(), yargs); + const instanceTree = rootCommands.subCommands.get("instance"); + const command = instanceTree.subCommands.get(name); + + if (!command) { + throw new Error(`${name} command was not found in command tree`); + } + return command; +} + +/** + * Constructs a mock control link implementing the minimal API surface + * required by the *-all commands (send and sendTo). + * + * @param {lib.InstanceDetails[]} instances – List returned by the mocked + * InstanceDetailsListRequest. + * @param {string} action – The property name ("started" | "stopped") that + * should collect the instance ids passed to sendTo. + * @returns {object} The mock control link. + */ +function createMockControl(instances, action) { + return { + // Called by the command to fetch the instance list. + async send(_request) { + return instances; + }, + + // Populated by sendTo calls performed by the command. + [action]: [], + async sendTo(target, _request) { + this[action].push(target.instanceId); + }, + }; +} + +describe("ctl instance start-all command", function () { + let startAllCommand; + + before(async function () { + startAllCommand = await getInstanceSubCommand("start-all"); + }); + + it("starts only stopped instances that are not excluded by default", async function () { + const instances = [ + new lib.InstanceDetails("one", 1, undefined, undefined, "stopped", "1.1.0", 0, false), + new lib.InstanceDetails("two", 2, undefined, undefined, "running", "1.1.0", 0, false), + new lib.InstanceDetails("three", 3, undefined, undefined, "stopped", "1.1.0", 0, true), + ]; + const control = createMockControl(instances, "started"); + + await startAllCommand.run({ force: false }, control); + + assert.deepEqual(control.started, [1], "Should only start non-excluded stopped instances"); + }); + + it("starts excluded instances when --force flag is provided", async function () { + const instances = [ + new lib.InstanceDetails("one", 1, undefined, undefined, "stopped", "1.1.0", 0, false), + new lib.InstanceDetails("three", 3, undefined, undefined, "stopped", "1.1.0", 0, true), + ]; + const control = createMockControl(instances, "started"); + + await startAllCommand.run({ force: true }, control); + + // Order is not guaranteed – sort before comparison. + assert.deepEqual(control.started.sort(), [1, 3], "--force should include excluded instances"); + }); +}); + +describe("ctl instance stop-all command", function () { + let stopAllCommand; + + before(async function () { + stopAllCommand = await getInstanceSubCommand("stop-all"); + }); + + it("stops only running or starting instances", async function () { + const instances = [ + new lib.InstanceDetails("one", 1, undefined, undefined, "stopped", "1.1.0", 0, false), + // Does not care about excludeFromStartAll + new lib.InstanceDetails("two", 2, undefined, undefined, "running", "1.1.0", 0, true), + new lib.InstanceDetails("three", 3, undefined, undefined, "starting", "1.1.0", 0, false), + ]; + const control = createMockControl(instances, "stopped"); + + await stopAllCommand.run({}, control); + + // Order is not guaranteed – sort before comparison. + assert.deepEqual(control.stopped.sort(), [2, 3], "Should only stop running or starting instances"); + }); +});