Files
OrcaSlicer/tests/slic3rutils/test_mcp_replay_summary.cpp
T
SoftFever 0616068e26 Run commands, tools and the plugin API through one operations layer
A Speed Dial command, the MCP tool that does the same thing and the plugin
API's preset calls now run one shared operation, so every route accepts and
refuses the same states, with one wording and a code for each refusal. An
import by a command or a tool is one undo step, and import_file adds geometry
only.

The cases where two routes behaved differently now behave the same: a project
is not saved under a .gcode.3mf name, an empty project exports as .3mf but not
as STL, arrange runs from any view and with a gizmo open, a mixed-colour
filament slot is refused as one, a hidden preset is shown before it is
selected, and a layer is refused while the Preview has none.
2026-11-12 22:06:10 +08:00

340 lines
18 KiB
C++

#include <catch2/catch_message.hpp>
#include <catch2/catch_test_macros.hpp>
#include <catch2/generators/catch_generators.hpp>
#include "slic3r/GUI/Mcp/McpReplay.hpp"
#include "slic3r/GUI/Mcp/McpReplaySummary.hpp"
#include <nlohmann/json.hpp>
#include <optional>
#include <string>
#include <vector>
using namespace Slic3r::GUI::Mcp;
using nlohmann::json;
namespace {
using Flags = std::vector<std::string>;
const Flags kEveryFlag{"writes_file", "changes_presets", "replaces_project", "sends_to_printer"};
// Describes the one step of a replay built around `step`, run from C:/replays with output in C:/out.
StepDescription describe(const std::string& step, const std::string& vars = "{}")
{
std::string err;
const auto replay = parse_replay(R"({"orca_replay": 1, "vars": )" + vars + R"(, "steps": [)" + step + "]}", err);
INFO(err);
REQUIRE(replay.has_value());
ReplayContext ctx;
ctx.replay_dir = "C:/replays";
ctx.out_dir = "C:/out";
return describe_step(replay->steps[0], *replay, ctx);
}
Flags flags_of(const StepDescription& d)
{
Flags out;
for (StepFlag f : d.flags)
out.push_back(flag_id(f));
return out;
}
// An absolute path on this platform: Speed Dial params name files only by absolute paths, and
// "C:/a" is relative outside Windows.
std::string abs_path(const std::string& name)
{
#ifdef _WIN32
return "C:/a/" + name;
#else
return "/a/" + name;
#endif
}
std::string action(const std::string& id, const std::string& param = {})
{
json args{{"id", id}};
if (!param.empty())
args["param"] = param;
return json{{"tool", "run_action"}, {"args", args}}.dump();
}
} // namespace
TEST_CASE("open_files is flagged by what it opens", "[Mcp]")
{
struct Case
{
std::string step;
std::string vars;
Flags flags;
std::string replace;
};
const Case c = GENERATE(values<Case>({
{R"({"tool": "open_files", "args": {"paths": ["C:/a/p.3mf"]}})", "{}", {"replaces_project"}, "checked"},
{R"({"tool": "open_files", "args": {"paths": ["C:/a/p.3mf"], "as": "geometry"}})", "{}", {}, ""},
{R"({"tool": "open_files", "args": {"paths": ["C:/a/p.3mf", "C:/a/b.stl"]}})", "{}", {}, ""},
{R"({"tool": "open_files", "args": {"paths": ["C:/a/b.stl"]}})", "{}", {}, ""},
{R"({"tool": "open_files", "args": {"paths": ["C:/a/m.step"]}})", "{}", {}, ""},
{R"({"tool": "open_files", "args": {"paths": ["C:/a/x.gcode"]}})", "{}", {"replaces_project"}, "app_asked"},
{R"({"tool": "open_files", "args": {"paths": ["${vars.project}"]}})", R"({"project": "C:/a/p.3mf"})",
{"replaces_project"}, "checked"},
}));
INFO(c.step);
const StepDescription d = describe(c.step, c.vars);
CHECK(flags_of(d) == c.flags);
CHECK(d.replace == c.replace);
}
TEST_CASE("project tools replace the project after checking for unsaved changes", "[Mcp]")
{
const StepDescription fresh = describe(R"({"tool": "new_project", "args": {"discard_changes": true}})");
CHECK(flags_of(fresh) == Flags{"replaces_project"});
CHECK(fresh.replace == "checked");
const StepDescription calib = describe(R"({"tool": "generate_calibration_test", "args": {"test": "temperature"}})");
CHECK(flags_of(calib) == Flags{"changes_presets", "replaces_project"});
CHECK(calib.replace == "checked");
}
TEST_CASE("speed dial actions are flagged by their id", "[Mcp]")
{
struct Case
{
std::string id;
std::string param;
Flags flags;
std::string replace;
};
const Case c = GENERATE(values<Case>({
{"orca_command:calib_flow_ratio:orca", "", {"changes_presets", "replaces_project"}, "app_asked"},
{"orca_command:send_gcode:orca", "", {"sends_to_printer"}, ""},
{"orca_command:open_preferences:orca", "", {}, ""},
{"orca_command:slice_all:orca", "", {}, ""},
{"orca_command:export_gcode:orca", "C:/x/a.gcode", {"writes_file"}, ""},
{"orca_command:export_gcode:orca", "", {"writes_file"}, ""},
{"orca_command:export_config:orca", "C:/x", {"writes_file"}, ""},
{"orca_command:obj_set_instances:orca", "3", {}, ""},
{"orca_command:save_project_as:orca", "C:/x/a.3mf", {"writes_file"}, ""},
{"orca_command:new_project:orca", "", {"replaces_project"}, "app_asked"},
{"orca_command:import_file:orca", abs_path("p.3mf"), {}, ""},
{"orca_command:import_file:orca", json{{"path", abs_path("p.3mf")}, {"overwrite", true}}.dump(), {}, ""},
{"orca_command:import_file:orca", json{{"path", abs_path("x.gcode")}}.dump(), {}, ""},
{"orca_command:import_file:orca", abs_path("b.stl"), {}, ""},
{"orca_recent_project:C:/p/a.3mf:orca", "", {"replaces_project"}, "app_asked"},
{"orca_plate_goto:1:orca", "", {}, ""},
{"orca_setting:layer_height:1", "", {}, ""},
{"plugin_replay_action:tool:plugin", "", kEveryFlag, "app_asked"},
{"plugin_page_action:page:plugin", "", kEveryFlag, "app_asked"},
}));
INFO(c.id << " " << c.param);
const StepDescription d = describe(action(c.id, c.param));
CHECK(flags_of(d) == c.flags);
CHECK(d.replace == c.replace);
}
TEST_CASE("plugin actions are taken to answer dialogs", "[Mcp]")
{
CHECK(describe(action("plugin_replay_action:tool:plugin")).answers_dialogs);
CHECK_FALSE(describe(action("orca_command:new_project:orca")).answers_dialogs);
}
TEST_CASE("speed dial actions describe their command and file", "[Mcp]")
{
const auto [id, param, what] = GENERATE(table<std::string, std::string, json>({
{"orca_command:import_file:orca", abs_path("p.3mf"), {{"kind", "command"}, {"key", "import_file"}, {"param", "p.3mf"}}},
{"orca_command:import_file:orca", json{{"path", abs_path("p.3mf")}, {"overwrite", true}}.dump(),
{{"kind", "command"}, {"key", "import_file"}, {"param", "p.3mf"}}},
{"orca_command:import_file:orca", json{{"path", abs_path("x.gcode")}}.dump(), {{"kind", "command"}, {"key", "import_file"}, {"param", "x.gcode"}}},
{"orca_command:go_to_layer:orca", "50", {{"kind", "command"}, {"key", "go_to_layer"}, {"param", "50"}}},
{"orca_command:slice_all:orca", "", {{"kind", "command"}, {"key", "slice_all"}}},
{"orca_recent_project:C:/p/a.3mf:orca", "", {{"kind", "recent_project"}, {"name", "a.3mf"}}},
{"orca_setting:layer_height:1", "", {{"kind", "value"}, {"text", "layer_height"}}},
{"plugin_replay_action:tool:plugin", "", {{"kind", "value"}, {"text", "plugin_replay_action:tool:plugin"}}},
}));
INFO(id << " " << param);
CHECK(describe(action(id, param)).what == what);
}
TEST_CASE("files written inside the output folder are not flagged", "[Mcp]")
{
const auto [step, flags] = GENERATE(table<std::string, Flags>({
{R"({"tool": "screenshot", "args": {"save_path": "C:/out/a.png"}})", {}},
{R"({"tool": "screenshot", "args": {"save_path": "${out_dir}/a.png"}})", {}},
{R"({"tool": "screenshot", "args": {"save_path": "C:/x/a.png"}})", {"writes_file"}},
{R"({"tool": "screenshot", "args": {"target": "canvas"}})", {}},
{R"({"tool": "export_replay", "args": {"path": "${out_dir}/s.json"}})", {}},
{R"({"tool": "export_replay", "args": {"path": "C:/x/s.json"}})", {"writes_file"}},
{R"({"tool": "pick_gizmo_file", "args": {"action": "svg_save_as", "path": "C:/x/a.svg"}})", {"writes_file"}},
{R"({"tool": "pick_gizmo_file", "args": {"action": "svg_change", "path": "C:/x/a.svg"}})", {}},
// A path rule may answer a save picker, so its path is a file the step may write.
{R"({"tool": "auto_answer_dialogs", "args": {"rules": [{"match": "Save", "button": "ok", "path": "C:/x/a.3mf"}]}})", {"writes_file"}},
{R"({"tool": "auto_answer_dialogs", "args": {"rules": [{"match": "Save", "button": "ok", "path": "${out_dir}/a.3mf"}]}})", {}},
{R"({"tool": "auto_answer_dialogs", "args": {"rules": [{"match": "Save", "button": "cancel"}]}})", {}},
{R"({"tool": "auto_answer_dialogs", "args": {"rules": [{"match": "Discard", "button": "no"}, {"match": "Save", "button": "ok", "path": "C:/x/a.3mf", "overwrite": true}]}})",
{"writes_file"}},
{R"({"tool": "export", "args": {"path": "${out_dir}/a.gcode"}})", {"writes_file"}},
{R"({"tool": "save_preset", "args": {"type": "filament", "name": "Mine"}})", {"changes_presets"}},
{R"({"tool": "delete_preset", "args": {"type": "filament", "name": "Mine"}})", {"changes_presets"}},
}));
INFO(step);
CHECK(flags_of(describe(step)) == flags);
}
TEST_CASE("steps that can answer a dialog are told apart", "[Mcp]")
{
const auto [tool, answers] = GENERATE(table<std::string, bool>({
{"auto_answer_dialogs", true},
{"answer_dialog", true},
{"click", true},
{"press_keys", true},
{"set_widget_value", false},
{"mouse_gesture", false},
{"run_javascript", false},
}));
INFO(tool);
const StepDescription d = describe(json{{"tool", tool}}.dump());
CHECK(d.answers_dialogs == answers);
CHECK(flags_of(d).empty());
}
TEST_CASE("nested replays join their steps' descriptions", "[Mcp]")
{
const StepDescription replacing = describe(
R"({"tool": "run_replay", "args": {"replay": {"orca_replay": 1, "steps": [{"tool": "new_project", "args": {"discard_changes": true}}]}}})");
CHECK(flags_of(replacing) == Flags{"replaces_project"});
CHECK(replacing.replace == "checked");
CHECK(replacing.what == json{{"kind", "replay"}, {"steps", 1}});
// An inline replay given as JSON text, comments included, reads as its object does.
const std::string text = "// starts over\n" R"({"orca_replay": 1, "steps": [{"tool": "new_project", "args": {"discard_changes": true}}]})";
const StepDescription as_text = describe(json{{"tool", "run_replay"}, {"args", {{"replay", text}}}}.dump());
CHECK(flags_of(as_text) == Flags{"replaces_project"});
CHECK(as_text.what == json{{"kind", "replay"}, {"steps", 1}});
const StepDescription own_output = describe(
R"({"tool": "run_replay", "args": {"replay": {"orca_replay": 1, "steps": [{"tool": "screenshot", "args": {"save_path": "${out_dir}/n.png"}}]}}})");
CHECK(flags_of(own_output).empty());
// The outer replay substitutes an inline replay's placeholders before the nested run starts, as
// run_replay does: the step's own vars come too late, and its out_dir doesn't move ${out_dir}.
const StepDescription outer_vars = describe(
R"({"tool": "run_replay", "args": {"replay": {"orca_replay": 1, "steps": [{"tool": "open_files", "args": {"paths": ["${vars.p}"]}}]}}})",
R"({"p": "C:/a/p.3mf"})");
CHECK(flags_of(outer_vars) == Flags{"replaces_project"});
CHECK(outer_vars.replace == "checked");
const StepDescription step_vars = describe(
R"({"tool": "run_replay", "args": {"vars": {"p": "C:/a/p.3mf"}, "replay": {"orca_replay": 1, "vars": {"p": "C:/a/q.3mf"}, "steps": [{"tool": "open_files", "args": {"paths": ["${vars.p}"]}}]}}})");
CHECK(flags_of(step_vars).empty());
CHECK(step_vars.replace == "");
const StepDescription own_folder = describe(
R"({"tool": "run_replay", "args": {"out_dir": "C:/elsewhere", "replay": {"orca_replay": 1, "steps": [{"tool": "screenshot", "args": {"save_path": "${out_dir}/n.png"}}]}}})");
CHECK(flags_of(own_folder).empty());
const StepDescription mixed = describe(
R"({"tool": "run_replay", "args": {"replay": {"orca_replay": 1, "steps": [{"tool": "run_action", "args": {"id": "orca_command:new_project:orca"}}, {"tool": "new_project"}]}}})");
CHECK(mixed.replace == "checked");
const StepDescription clicking = describe(
R"({"tool": "run_replay", "args": {"replay": {"orca_replay": 1, "steps": [{"tool": "click", "args": {"target": {"label": "No"}}}]}}})");
CHECK(clicking.answers_dialogs);
// Five levels of nesting: past the inspection depth, so the effect can't be read.
std::string deep = R"({"orca_replay": 1, "steps": [{"tool": "new_project"}]})";
for (int i = 0; i < 5; ++i)
deep = R"({"orca_replay": 1, "steps": [{"tool": "run_replay", "args": {"replay": )" + deep + "}}]}";
const StepDescription too_deep = describe(R"({"tool": "run_replay", "args": {"replay": )" + deep + "}}");
CHECK(flags_of(too_deep) == kEveryFlag);
CHECK(too_deep.replace == "checked");
CHECK(too_deep.answers_dialogs);
CHECK(too_deep.what == json{{"kind", "replay"}, {"steps", 1}});
const StepDescription invalid = describe(R"({"tool": "run_replay", "args": {"replay": {"orca_replay": 1, "steps": [{"tool": 3}]}}})");
CHECK(flags_of(invalid) == kEveryFlag);
REQUIRE(invalid.what.value("kind", "") == "value");
CHECK(invalid.what.value("text", "").find("step 1") != std::string::npos);
const StepDescription broken_text = describe(json{{"tool", "run_replay"}, {"args", {{"replay", "{\"orca_replay\": 1,\n \"steps\": ["}}}}.dump());
CHECK(flags_of(broken_text) == kEveryFlag);
REQUIRE(broken_text.what.value("kind", "") == "value");
CHECK(broken_text.what.value("text", "").find("line 2") != std::string::npos);
const StepDescription missing = describe(R"({"tool": "run_replay", "args": {"path": "C:/no/such/replay.json"}})");
CHECK(flags_of(missing) == kEveryFlag);
CHECK(missing.replace == "checked");
CHECK(missing.answers_dialogs);
CHECK(missing.what == json{{"kind", "files"}, {"names", {"replay.json"}}});
}
// The runner describes steps without the saved jobs a single step runs with: ${jobs.NAME} resolves
// to a job id such as "j12", never a path, so the effects read the same either way.
TEST_CASE("saved jobs don't change a step's flags", "[Mcp]")
{
const std::string step = GENERATE(as<std::string>{}, R"({"tool": "open_files", "args": {"paths": ["${jobs.p}"]}})",
R"({"tool": "screenshot", "args": {"save_path": "${jobs.p}/a.png"}})",
R"({"tool": "run_action", "args": {"id": "orca_command:import_file:orca", "param": "${jobs.p}"}})");
INFO(step);
std::string err;
const auto replay = parse_replay(R"({"orca_replay": 1, "steps": [)" + step + "]}", err);
REQUIRE(replay.has_value());
ReplayContext without;
without.out_dir = "C:/out";
ReplayContext with = without;
with.jobs = json{{"p", "j12"}};
const StepDescription a = describe_step(replay->steps[0], *replay, without);
const StepDescription b = describe_step(replay->steps[0], *replay, with);
CHECK(flags_of(a) == flags_of(b));
CHECK(a.replace == b.replace);
CHECK(a.answers_dialogs == b.answers_dialogs);
}
TEST_CASE("a step's what names what it acts on", "[Mcp]")
{
const auto [step, what] = GENERATE(table<std::string, json>({
{R"({"tool": "set_settings", "args": {"values": {"layer_height": 0.12, "wall_loops": "3"}}})",
{{"kind", "settings"}, {"pairs", {"layer_height = 0.12", "wall_loops = 3"}}}},
{R"({"tool": "open_files", "args": {"paths": ["C:/m/3DBenchy.stl", "C:/m/cube.stl"]}})",
{{"kind", "files"}, {"names", {"3DBenchy.stl", "cube.stl"}}}},
{R"({"tool": "screenshot", "args": {"save_path": "${out_dir}/preview.png"}})", {{"kind", "files"}, {"names", {"preview.png"}}}},
{R"({"tool": "slice"})", {{"kind", "plates"}, {"plates", "current"}}},
{R"({"tool": "slice", "args": {"plates": 2}})", {{"kind", "plates"}, {"plates", 2}}},
{R"({"tool": "arrange"})", {{"kind", "plates"}, {"plates", "all"}}},
{R"({"tool": "select_preset", "args": {"type": "filament", "name": "Generic PLA"}})", {{"kind", "preset"}, {"name", "Generic PLA"}}},
{R"({"tool": "wait", "args": {"for": "idle"}})", {{"kind", "value"}, {"text", "idle"}}},
{R"({"tool": "answer_dialog", "args": {"button": "no"}})", {{"kind", "value"}, {"text", "no"}}},
{R"({"tool": "transform", "args": {"object": 1}})", {{"kind", "value"}, {"text", "object: 1"}}},
{R"({"tool": "get_app_state"})", nullptr},
}));
INFO(step);
CHECK(describe(step).what == what);
}
TEST_CASE("a step with an expect checks its result", "[Mcp]")
{
CHECK(describe(R"({"tool": "slice", "expect": {"/plates/0/outcome": {"eq": "ok"}}})").checks_result);
CHECK_FALSE(describe(R"({"tool": "slice"})").checks_result);
}
TEST_CASE("results are described from the tools' real replies", "[Mcp]")
{
CHECK(describe_result("open_files", {{"objects", 1}, {"names", {"Benchy"}}, {"plates", 1}}) == json{{"kind", "objects"}, {"count", 1}});
CHECK(describe_result("generate_calibration_test", {{"objects", 3}, {"plates", 1}}) == json{{"kind", "objects"}, {"count", 3}});
CHECK(describe_result("open_files", {{"opened", "gcode"}, {"path", "C:/a/x.gcode"}}) == json{{"kind", "file"}, {"name", "x.gcode"}});
CHECK(describe_result("slice", {{"plates", {{{"index", 0}, {"outcome", "ok"}}, {{"index", 1}, {"outcome", "error"}}}}}) ==
json{{"kind", "plates"}, {"ok", 1}, {"other", 1}});
CHECK(describe_result("screenshot", {{"path", "C:/out/a.png"}}) == json{{"kind", "file"}, {"name", "a.png"}});
CHECK(describe_result("pick_gizmo_file", {{"saved", "C:/x/a.svg"}}) == json{{"kind", "file"}, {"name", "a.svg"}});
CHECK(describe_result("run_replay", {{"passed", false}, {"failed_step", 2}, {"steps", json::array()}}) ==
json{{"kind", "replay"}, {"passed", false}, {"failed_step", 2}});
CHECK(describe_result("run_replay", {{"passed", true}, {"steps", json::array()}}) == json{{"kind", "replay"}, {"passed", true}});
CHECK(describe_result("get_app_state", {{"ready", true}}).is_null());
CHECK(result_files({{"saved", "C:/x/a.svg"}}) == std::vector<std::string>{"C:/x/a.svg"});
CHECK(result_files({{"path", "C:/out/a.png"}}) == std::vector<std::string>{"C:/out/a.png"});
CHECK(result_files({{"path", ""}}).empty());
CHECK(result_files(json::object()).empty());
}