mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-11-27 15:49:52 -05:00
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.
176 lines
6.6 KiB
C++
176 lines
6.6 KiB
C++
#include <catch2/catch_test_macros.hpp>
|
|
#include <catch2/matchers/catch_matchers.hpp>
|
|
#include <catch2/matchers/catch_matchers_string.hpp>
|
|
|
|
#include "automation_test_utils.hpp"
|
|
|
|
#include "slic3r/GUI/CommandCatalog.hpp"
|
|
#include "slic3r/GUI/CommandTypes.hpp"
|
|
#include "slic3r/GUI/Mcp/McpArguments.hpp"
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <type_traits>
|
|
#include <vector>
|
|
|
|
using Catch::Matchers::ContainsSubstring;
|
|
using nlohmann::json;
|
|
using Slic3r::GUI::CommandCatalog;
|
|
using Slic3r::GUI::CommandCompletion;
|
|
using Slic3r::GUI::CommandFn;
|
|
using Slic3r::GUI::GuardClass;
|
|
using Slic3r::GUI::NativeCommand;
|
|
using Slic3r::GUI::Mcp::Test::command;
|
|
using Slic3r::GUI::Mcp::Test::OneCommandSource;
|
|
namespace Mcp = Slic3r::GUI::Mcp;
|
|
|
|
namespace {
|
|
|
|
std::vector<std::string> keys_of(const std::vector<NativeCommand>& commands)
|
|
{
|
|
std::vector<std::string> keys;
|
|
for (const NativeCommand& c : commands)
|
|
keys.push_back(c.key);
|
|
return keys;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST_CASE("a key added twice is refused", "[CommandCatalog]")
|
|
{
|
|
CommandCatalog catalog;
|
|
catalog.add(command("a"));
|
|
CHECK_THROWS_AS(catalog.add(command("a")), std::logic_error);
|
|
CHECK(catalog.commands().size() == 1);
|
|
}
|
|
|
|
TEST_CASE("a sealed catalog refuses additions", "[CommandCatalog]")
|
|
{
|
|
CommandCatalog catalog;
|
|
catalog.add(command("a"));
|
|
CHECK_FALSE(catalog.sealed());
|
|
catalog.seal();
|
|
CHECK(catalog.sealed());
|
|
CHECK_THROWS_AS(catalog.add(command("b")), std::logic_error);
|
|
CHECK_THROWS_AS(catalog.add_source(std::make_unique<OneCommandSource>("plugin.x.y")), std::logic_error);
|
|
CHECK(catalog.find("b") == nullptr);
|
|
}
|
|
|
|
TEST_CASE("find sees added commands, lookup also a source's", "[CommandCatalog]")
|
|
{
|
|
CommandCatalog catalog;
|
|
catalog.add(command("slice"));
|
|
catalog.add_source(std::make_unique<OneCommandSource>("plugin.x.y"));
|
|
catalog.seal();
|
|
|
|
CHECK(catalog.find("plugin.x.y") == nullptr);
|
|
const std::shared_ptr<const NativeCommand> from_source = catalog.lookup("plugin.x.y");
|
|
REQUIRE(from_source != nullptr);
|
|
CHECK(from_source->key == "plugin.x.y");
|
|
|
|
const NativeCommand* added = catalog.find("slice");
|
|
REQUIRE(added != nullptr);
|
|
CHECK(catalog.lookup("slice").get() == added);
|
|
|
|
CHECK(catalog.lookup("plugin.x.z") == nullptr);
|
|
REQUIRE(catalog.source_commands().size() == 1);
|
|
CHECK(catalog.source_commands().front()->key == "plugin.x.y");
|
|
}
|
|
|
|
TEST_CASE("commands keep the order they were added in", "[CommandCatalog]")
|
|
{
|
|
CommandCatalog catalog;
|
|
for (const char* key : {"c", "a", "b"})
|
|
catalog.add(command(key));
|
|
catalog.seal();
|
|
CHECK(keys_of(catalog.commands()) == std::vector<std::string>{"c", "a", "b"});
|
|
}
|
|
|
|
TEST_CASE("a Design command's title is translated in the Design context, any other's in none", "[CommandCatalog]")
|
|
{
|
|
NativeCommand design;
|
|
design.key = "shell";
|
|
design.title = "Shell";
|
|
design.group = Slic3r::GUI::kDesignGroup;
|
|
REQUIRE(Slic3r::GUI::title_context(design) != nullptr);
|
|
CHECK(std::string(Slic3r::GUI::title_context(design)) == "Design");
|
|
NativeCommand other;
|
|
other.key = "save_project";
|
|
other.title = "Save project";
|
|
other.group = "Commands";
|
|
CHECK(Slic3r::GUI::title_context(other) == nullptr);
|
|
}
|
|
|
|
TEST_CASE("a command's function is typed in the catalog's own namespace", "[CommandCatalog]")
|
|
{
|
|
STATIC_REQUIRE(std::is_same_v<CommandFn, std::function<Slic3r::GUI::ToolResult(const nlohmann::json&, Slic3r::GUI::ToolContext&)>>);
|
|
}
|
|
|
|
TEST_CASE("the person's form is the call with no arguments", "[CommandCatalog]")
|
|
{
|
|
CHECK(Mcp::person_form(json::object()));
|
|
CHECK(Mcp::person_form(json()));
|
|
CHECK_FALSE(Mcp::person_form({{"silent", true}}));
|
|
CHECK_FALSE(Mcp::person_form({{"path", "a"}}));
|
|
CHECK(Mcp::silent_args({{"silent", true}, {"path", "a"}}) == json{{"path", "a"}});
|
|
}
|
|
|
|
TEST_CASE("a two-form schema requires its arguments only when some are given", "[CommandCatalog]")
|
|
{
|
|
const json s = Mcp::with_silent({{"type", "object"},
|
|
{"properties", {{"path", {{"type", "string"}}}, {"overwrite", {{"type", "boolean"}}}}},
|
|
{"required", {"path"}},
|
|
{"additionalProperties", false}});
|
|
CHECK_FALSE(s.contains("required"));
|
|
CHECK(s["then"]["required"] == json::array({"path"}));
|
|
CHECK(s["properties"]["silent"]["enum"] == json::array({true}));
|
|
CHECK(Mcp::validate_arguments(s, json::object()).empty());
|
|
CHECK(Mcp::validate_arguments(s, {{"path", "a"}}).empty());
|
|
CHECK_THAT(Mcp::validate_arguments(s, {{"overwrite", true}}), ContainsSubstring("path"));
|
|
CHECK_THAT(Mcp::validate_arguments(s, {{"silent", true}}), ContainsSubstring("path"));
|
|
CHECK_THAT(Mcp::validate_arguments(s, {{"silent", false}}), ContainsSubstring("silent"));
|
|
}
|
|
|
|
TEST_CASE("a reference entry has the command's fields, and its examples only when it has some", "[CommandCatalog][Reference]")
|
|
{
|
|
NativeCommand c = command("export_it", true);
|
|
c.title = "Export It";
|
|
c.group = "Commands";
|
|
c.description = "Exports it.";
|
|
c.input_schema = {{"type", "object"}, {"properties", {{"path", {{"type", "string"}}}}}};
|
|
c.guard = GuardClass::Recover;
|
|
c.completion = CommandCompletion::Export;
|
|
c.operations = {"export_gcode"};
|
|
c.open_world = true;
|
|
const json entry = Slic3r::GUI::command_reference(c);
|
|
CHECK(entry == json{{"key", "export_it"},
|
|
{"title", "Export It"},
|
|
{"group", "Commands"},
|
|
{"description", "Exports it."},
|
|
{"input_schema", c.input_schema},
|
|
{"listed", false},
|
|
{"in_palette", true},
|
|
{"read_only", false},
|
|
{"destructive", false},
|
|
{"idempotent", false},
|
|
{"open_world", true},
|
|
{"guard", "recover"},
|
|
{"completion", "export"},
|
|
{"operations", json::array({"export_gcode"})}});
|
|
|
|
const json call{{"tool", "export_it"}, {"args", {{"path", "a"}}}};
|
|
c.guard = GuardClass::Inspect;
|
|
c.completion = CommandCompletion::None;
|
|
c.examples = json::array({json{{"title", "Export"}, {"calls", json::array({call})}}});
|
|
const json other = Slic3r::GUI::command_reference(c);
|
|
CHECK(other["guard"] == "inspect");
|
|
CHECK(other["completion"] == "none");
|
|
CHECK(other["examples"] == c.examples);
|
|
c.guard = GuardClass::Mutate;
|
|
CHECK(Slic3r::GUI::command_reference(c)["guard"] == "mutate");
|
|
}
|