Files
OrcaSlicer/tests/slic3rutils/test_mcp_core.cpp
T
SoftFever fece418ae0 Make the automation runtime its own subsystem, with MCP as one front end
Everything that drives OrcaSlicer by command (the dispatch of the command
catalog, jobs, the journal, replays, Python scripts, recordings) lived
inside the MCP server's service class. So Action Replay, the Run Script
panel, a plugin's tool call, a person's recording and a run from the
command line each had to start "the MCP service", with all of its GUI
hooks, although none of them needs a server or an agent.

The automation runtime is now a subsystem of its own. It starts the first
time a front end asks for it and knows none of them. The MCP endpoint
(HTTP, the protocol, the token, the discovery file, the takeover of a port
another window holds) is one front end beside the others, opened and
closed without stopping what the others run. The widget inspector, menu
capture and ImGui item recording are turned on only by a front end that
drives the GUI, so a person's recording or long command does not carry
them.

The part of the runtime that needs no GUI is the library
libslic3r_automation, built without wxWidgets and tested by its own suite,
automation_tests. A wx dependency in it fails to build, and its tests no
longer link the GUI library or the embedded Python.

The command line has one option set for this: --run FILE runs a replay or
a Python script once the GUI is ready, with --outputdir for its files, and
--unrestricted-scripts and --cad turn their settings on for one run
without saving them. --mcp-run, --mcp-run-out and --mcp-token are gone;
the scripts setting is python_scripts_unrestricted and a session's output
folder is <datadir>/automation/out. get_app_state reports ready and
version at the top level, and its mcp block describes the endpoint only.

No change to slicing output.
2026-11-14 23:41:01 +08:00

85 lines
2.9 KiB
C++

#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers.hpp>
#include "automation/automation_test_utils.hpp"
#include "slic3r/GUI/CommandCatalog.hpp"
#include "slic3r/GUI/CommandTypes.hpp"
#include "slic3r/GUI/Mcp/AutomationRuntime.hpp"
#include "slic3r/GUI/Mcp/McpJournal.hpp"
#include "slic3r/GUI/Mcp/McpPersonContext.hpp"
#include <nlohmann/json.hpp>
#include <string>
#include <utility>
// The dispatch core's cases that name a type of the GUI library: the automation runtime and a tile's
// context. The others are in tests/automation/test_mcp_core.cpp.
using namespace Slic3r::GUI;
using namespace Slic3r::GUI::Mcp;
using nlohmann::json;
using Slic3r::GUI::CommandFn;
using Slic3r::GUI::NativeCommand;
using Slic3r::GUI::Mcp::Test::Harness;
namespace {
NativeCommand tool(const std::string& name, GuardClass guard, CommandFn handler)
{
NativeCommand d;
d.key = name;
d.guard = guard;
d.run = std::move(handler);
return d;
}
} // namespace
TEST_CASE("a context with no runtime refuses", "[Mcp]")
{
Harness h;
const CommandFn needs_runtime = [](const json&, ToolContext& ctx) {
[[maybe_unused]] AutomationRuntime& runtime = ctx.runtime();
return ToolResult::ok();
};
// Inline, so the handler runs on this thread, where it may assert.
NativeCommand look = tool("look", GuardClass::Inspect, [needs_runtime](const json& args, ToolContext& ctx) {
REQUIRE_THROWS_WITH(ctx.runtime(), "This call runs without the automation runtime");
return needs_runtime(args, ctx);
});
look.inline_call = true;
h.registry.add(look);
h.registry.add(tool("act", GuardClass::Mutate, needs_runtime));
h.start();
// A core no AutomationRuntime owns, as a test builds one: the call fails, inline or as a job, and nothing crashes.
CHECK(h.call("look").is_error);
const ToolResult acted = h.call("act");
CHECK(acted.is_error);
CHECK_FALSE(acted.data.contains("status"));
}
TEST_CASE("a tile's journal update stays inside run_action's envelope", "[Mcp]")
{
Harness h;
h.registry.add(tool("run_action", GuardClass::Mutate, [](const json&, ToolContext& ctx) {
// The command's function, as run_action runs a tile with it.
const auto command = [](const json&, ToolContext& own) {
own.set_journal_args({{"a", 1}});
return ToolResult::ok();
};
TileContext tile(ctx, "orca_command:k:orca");
return command(json::object(), tile);
}));
h.start();
const ToolResult ran = h.call("run_action", {{"id", "k"}});
REQUIRE_FALSE(ran.is_error);
REQUIRE_FALSE(ran.data.contains("status"));
const json steps = h.journal.to_replay("s")["steps"];
REQUIRE(steps.size() == 1);
CHECK(steps[0]["tool"] == "run_action");
CHECK(steps[0]["args"] == json{{"id", "orca_command:k:orca"}, {"args", {{"a", 1}}}});
}