mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-12-02 17:58:22 -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.
490 lines
22 KiB
C++
490 lines
22 KiB
C++
#include <catch2/catch_message.hpp>
|
|
#include <catch2/catch_test_macros.hpp>
|
|
#include <catch2/generators/catch_generators.hpp>
|
|
|
|
#include "slic3r/GUI/CommandTypes.hpp"
|
|
#include "slic3r/GUI/Mcp/McpReplay.hpp"
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
#include <cstddef>
|
|
#include <cstdlib>
|
|
#include <functional>
|
|
#include <map>
|
|
#include <optional>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
#ifndef _WIN32
|
|
#include <stdlib.h> // setenv
|
|
#endif
|
|
|
|
using namespace Slic3r::GUI;
|
|
using namespace Slic3r::GUI::Mcp;
|
|
using nlohmann::json;
|
|
|
|
namespace {
|
|
|
|
Replay parse_ok(const std::string& text)
|
|
{
|
|
std::string err;
|
|
auto s = parse_replay(text, err);
|
|
INFO(err);
|
|
REQUIRE(s.has_value());
|
|
return *s;
|
|
}
|
|
|
|
// A replay host whose tools are lambdas; records calls and rule cleanup.
|
|
struct FakeHost
|
|
{
|
|
std::map<std::string, std::function<ToolResult(const json&)>> tools;
|
|
std::map<std::string, ToolResult> job_results;
|
|
std::vector<std::pair<std::string, json>> calls;
|
|
bool ended = false;
|
|
bool cancel = false;
|
|
|
|
ReplayHost host()
|
|
{
|
|
ReplayHost h;
|
|
h.call = [this](const std::string& tool, const json& args) {
|
|
calls.emplace_back(tool, args);
|
|
auto it = tools.find(tool);
|
|
return it == tools.end() ? ToolResult::error("unknown tool " + tool) : it->second(args);
|
|
};
|
|
h.wait_job = [this](const std::string& job, int) -> std::optional<ToolResult> {
|
|
auto it = job_results.find(job);
|
|
if (it == job_results.end())
|
|
return std::nullopt;
|
|
return it->second;
|
|
};
|
|
h.end_replay = [this] { ended = true; };
|
|
h.cancelled = [this] { return cancel; };
|
|
return h;
|
|
}
|
|
};
|
|
|
|
} // namespace
|
|
|
|
TEST_CASE("replays allow comments and read their fields", "[Mcp]")
|
|
{
|
|
const Replay s = parse_ok(R"({
|
|
// a comment
|
|
"orca_replay": 1, "name": "repro", /* block */ "vars": {"n": 3},
|
|
"steps": [{"tool": "slice", "expect": {"/ok": {"eq": true}}, "optional": true, "timeout_s": 5},
|
|
{"tool": "run_action", "await": false, "save_as": "prefs"}]
|
|
})");
|
|
CHECK(s.name == "repro");
|
|
REQUIRE(s.steps.size() == 2);
|
|
CHECK(s.steps[0].optional);
|
|
CHECK(s.steps[0].timeout_s == 5);
|
|
CHECK_FALSE(s.steps[1].await);
|
|
CHECK(s.steps[1].save_as == "prefs");
|
|
}
|
|
|
|
TEST_CASE("invalid replays are rejected with a reason", "[Mcp]")
|
|
{
|
|
const auto [text, reason] = GENERATE(table<std::string, std::string>({
|
|
{"{", "not valid JSON"},
|
|
{R"({"steps": []})", "orca_replay"},
|
|
{R"({"orca_replay": 2, "steps": []})", "orca_replay"},
|
|
{R"({"orca_replay": 1})", "steps"},
|
|
{R"({"orca_replay": 1, "steps": [{"args": {}}]})", "step 1"},
|
|
{R"({"orca_replay": 1, "steps": [{"tool": "x", "await": false}]})", "save_as"},
|
|
{R"({"orca_replay": 1, "steps": [{"tool": "x", "args": []}]})", "args"},
|
|
}));
|
|
std::string err;
|
|
CHECK_FALSE(parse_replay(text, err).has_value());
|
|
CHECK(err.find(reason) != std::string::npos);
|
|
}
|
|
|
|
TEST_CASE("vars, env and jobs substitute recursively", "[Mcp]")
|
|
{
|
|
const Replay s = parse_ok(R"({"orca_replay": 1, "vars": {"model": "${replay_dir}/part.stl", "count": 3}, "steps": []})");
|
|
ReplayContext ctx;
|
|
ctx.replay_dir = "/replays";
|
|
ctx.out_dir = "/out";
|
|
ctx.jobs = {{"prefs", "j9"}};
|
|
#ifdef _WIN32
|
|
_putenv_s("MCP_REPLAY_TEST", "hello");
|
|
#else
|
|
setenv("MCP_REPLAY_TEST", "hello", 1);
|
|
#endif
|
|
std::string err;
|
|
const json out = substitute({{"paths", {"${vars.model}"}},
|
|
{"n", "${vars.count}"},
|
|
{"shot", "${out_dir}/a.png"},
|
|
{"job", "${jobs.prefs}"},
|
|
{"greeting", "say ${env:MCP_REPLAY_TEST}"}},
|
|
s, ctx, err);
|
|
CHECK(err.empty());
|
|
CHECK(out["paths"][0] == "/replays/part.stl");
|
|
CHECK(out["n"] == 3);
|
|
CHECK(out["shot"] == "/out/a.png");
|
|
CHECK(out["job"] == "j9");
|
|
CHECK(out["greeting"] == "say hello");
|
|
|
|
substitute("${vars.missing}", s, ctx, err);
|
|
CHECK(err.find("unknown placeholder") != std::string::npos);
|
|
}
|
|
|
|
TEST_CASE("each matcher passes and fails with a message naming the pointer", "[Mcp]")
|
|
{
|
|
const json result = {{"n", 5}, {"s", "Slicing done"}, {"a", {1, 2}}, {"e", json::array()}, {"o", {{"k", 1}}}};
|
|
struct Case
|
|
{
|
|
json expect;
|
|
bool pass;
|
|
};
|
|
const Case c = GENERATE_COPY(values<Case>({
|
|
{{{"/n", {{"eq", 5}}}}, true}, {{{"/n", {{"eq", 6}}}}, false},
|
|
{{{"/n", {{"ne", 6}}}}, true}, {{{"/n", {{"gt", 4}}}}, true},
|
|
{{{"/n", {{"gt", 5}}}}, false}, {{{"/n", {{"ge", 5}}}}, true},
|
|
{{{"/n", {{"lt", 6}}}}, true}, {{{"/n", {{"le", 4}}}}, false},
|
|
{{{"/s", {{"contains", "done"}}}}, true}, {{{"/a", {{"contains", 2}}}}, true},
|
|
{{{"/o", {{"contains", "k"}}}}, true}, {{{"/a", {{"contains", 3}}}}, false},
|
|
{{{"/s", {{"regex", "^Slic"}}}}, true}, {{{"/s", {{"regex", "^x"}}}}, false},
|
|
{{{"/n", {{"exists", true}}}}, true}, {{{"/zz", {{"exists", false}}}}, true},
|
|
{{{"/zz", {{"eq", 1}}}}, false}, {{{"/e", {{"empty", true}}}}, true},
|
|
{{{"/a", {{"empty", true}}}}, false},
|
|
}));
|
|
const auto failure = check_expect(result, c.expect);
|
|
CHECK(failure.has_value() != c.pass);
|
|
if (failure)
|
|
CHECK(failure->find(c.expect.begin().key()) == 0);
|
|
}
|
|
|
|
TEST_CASE("a job-returning step is awaited before its expect is checked", "[Mcp]")
|
|
{
|
|
FakeHost f;
|
|
f.tools["slice"] = [](const json&) { return ToolResult::ok({{"status", "running"}, {"job", "j3"}}); };
|
|
f.job_results["j3"] = ToolResult::ok({{"plates", {{{"outcome", "ok"}}}}});
|
|
const Replay s = parse_ok(R"({"orca_replay": 1, "steps": [{"tool": "slice", "expect": {"/plates/0/outcome": {"eq": "ok"}}}]})");
|
|
ReplayContext ctx;
|
|
const json out = run_replay(s, ctx, f.host());
|
|
CHECK(out["passed"] == true);
|
|
CHECK(f.ended);
|
|
}
|
|
|
|
TEST_CASE("await false stores the job under save_as for later steps", "[Mcp]")
|
|
{
|
|
FakeHost f;
|
|
f.tools["run_action"] = [](const json&) { return ToolResult::ok({{"status", "blocked_by_dialog"}, {"job", "j5"}}); };
|
|
f.tools["answer_dialog"] = [](const json&) { return ToolResult::ok(); };
|
|
f.tools["wait"] = [](const json& a) { return ToolResult::ok({{"job", a["job"]}}); };
|
|
const Replay s = parse_ok(R"({"orca_replay": 1, "steps": [
|
|
{"tool": "run_action", "await": false, "save_as": "prefs"},
|
|
{"tool": "answer_dialog", "args": {"button": "close"}},
|
|
{"tool": "wait", "args": {"for": "job", "job": "${jobs.prefs}"}, "expect": {"/job": {"eq": "j5"}}}]})");
|
|
ReplayContext ctx;
|
|
const json out = run_replay(s, ctx, f.host());
|
|
CHECK(out["passed"] == true);
|
|
REQUIRE(f.calls.size() == 3);
|
|
CHECK(f.calls[2].second["job"] == "j5");
|
|
}
|
|
|
|
TEST_CASE("a run stops at the first failed step unless it is optional", "[Mcp]")
|
|
{
|
|
FakeHost f;
|
|
f.tools["ok"] = [](const json&) { return ToolResult::ok({{"v", 1}}); };
|
|
f.tools["fail"] = [](const json&) { return ToolResult::error("nope"); };
|
|
const Replay s = parse_ok(R"({"orca_replay": 1, "steps": [
|
|
{"tool": "fail", "optional": true}, {"tool": "ok", "expect": {"/v": {"eq": 2}}}, {"tool": "ok"}]})");
|
|
ReplayContext ctx;
|
|
const json out = run_replay(s, ctx, f.host());
|
|
CHECK(out["passed"] == false);
|
|
CHECK(out["failed_step"] == 2);
|
|
CHECK(out["steps"].size() == 2);
|
|
CHECK(out["steps"][1]["expect_failure"].get<std::string>().find("/v") == 0);
|
|
CHECK(f.ended); // replay-scoped rules are dropped even when it fails
|
|
}
|
|
|
|
TEST_CASE("a run reports each step as it starts and ends", "[Mcp]")
|
|
{
|
|
FakeHost f;
|
|
f.tools["a"] = [](const json&) { return ToolResult::ok(); };
|
|
f.tools["b"] = [](const json&) { return ToolResult::ok(); };
|
|
const Replay s = parse_ok(R"({"orca_replay": 1, "steps": [{"tool": "a"}, {"tool": "b"}]})");
|
|
std::vector<std::string> events;
|
|
ReplayHost h = f.host();
|
|
h.step_started = [&](std::size_t index) { events.push_back("started " + std::to_string(index)); };
|
|
h.step_finished = [&](const json& entry) {
|
|
events.push_back("finished " + std::to_string(entry["index"].get<int>()));
|
|
CHECK(entry["ok"] == true);
|
|
CHECK(entry["duration_ms"].get<long long>() >= 0);
|
|
};
|
|
ReplayContext ctx;
|
|
run_replay(s, ctx, h);
|
|
CHECK(events == std::vector<std::string>{"started 1", "finished 1", "started 2", "finished 2"});
|
|
}
|
|
|
|
TEST_CASE("only_step runs one step and reports its own index", "[Mcp]")
|
|
{
|
|
FakeHost f;
|
|
for (const char* name : {"a", "b", "c"})
|
|
f.tools[name] = [](const json&) { return ToolResult::ok(); };
|
|
const Replay s = parse_ok(R"({"orca_replay": 1, "steps": [{"tool": "a"}, {"tool": "b"}, {"tool": "c"}]})");
|
|
ReplayContext ctx;
|
|
const json out = run_replay(s, ctx, f.host(), 2);
|
|
REQUIRE(f.calls.size() == 1);
|
|
CHECK(f.calls[0].first == "b");
|
|
REQUIRE(out["steps"].size() == 1);
|
|
CHECK(out["steps"][0]["index"] == 2);
|
|
CHECK(out["passed"] == true);
|
|
CHECK_THROWS_AS(run_replay(s, ctx, f.host(), 4), std::out_of_range);
|
|
}
|
|
|
|
TEST_CASE("jobs saved by an earlier run resolve in a later single step", "[Mcp]")
|
|
{
|
|
FakeHost f;
|
|
f.tools["x"] = [](const json&) { return ToolResult::ok({{"status", "running"}, {"job", "j7"}}); };
|
|
f.tools["wait"] = [](const json& a) { return ToolResult::ok({{"job", a["job"]}}); };
|
|
const Replay s = parse_ok(R"({"orca_replay": 1, "steps": [
|
|
{"tool": "x", "await": false, "save_as": "prefs"},
|
|
{"tool": "wait", "args": {"job": "${jobs.prefs}"}},
|
|
{"tool": "wait", "args": {"job": "${jobs.none}"}}]})");
|
|
|
|
ReplayContext fresh;
|
|
const json alone = run_replay(s, fresh, f.host(), 2);
|
|
CHECK(alone["steps"][0]["ok"] == false);
|
|
CHECK(alone["steps"][0]["error"].get<std::string>().find("no job is saved as prefs yet; step 1 saves it") != std::string::npos);
|
|
CHECK(alone["steps"][0]["reason"] == json{{"kind", "missing_job"}, {"name", "prefs"}, {"saved_by", 1}});
|
|
|
|
ReplayContext shared;
|
|
run_replay(s, shared, f.host(), 1);
|
|
run_replay(s, shared, f.host(), 2);
|
|
CHECK(f.calls.back().second["job"] == "j7");
|
|
|
|
const json unsaved = run_replay(s, shared, f.host(), 3);
|
|
CHECK(unsaved["steps"][0]["reason"]["kind"] == "missing_job");
|
|
CHECK(unsaved["steps"][0]["reason"]["saved_by"].is_null());
|
|
}
|
|
|
|
TEST_CASE("a stop during a step marks that step, even the last one", "[Mcp]")
|
|
{
|
|
const std::string text = GENERATE(as<std::string>{}, R"({"orca_replay": 1, "steps": [{"tool": "stopper"}]})",
|
|
R"({"orca_replay": 1, "steps": [{"tool": "stopper", "optional": true}]})");
|
|
const bool single = GENERATE(false, true);
|
|
FakeHost f;
|
|
f.tools["stopper"] = [&f](const json&) {
|
|
f.cancel = true;
|
|
return ToolResult::ok({{"plates", json::array()}});
|
|
};
|
|
json reported;
|
|
ReplayHost h = f.host();
|
|
h.step_finished = [&](const json& entry) { reported = entry; };
|
|
const Replay s = parse_ok(text);
|
|
ReplayContext ctx;
|
|
const json out = single ? run_replay(s, ctx, h, 1) : run_replay(s, ctx, h);
|
|
for (const json& entry : {reported, out["steps"][0]}) {
|
|
CHECK(entry["cancelled"] == true);
|
|
CHECK(entry["ok"] == false);
|
|
CHECK(entry["reason"]["kind"] == "cancelled");
|
|
}
|
|
CHECK(out["passed"] == false);
|
|
CHECK(out["failed_step"] == 1);
|
|
}
|
|
|
|
TEST_CASE("a stop during a job wait is a cancel, not a timeout", "[Mcp]")
|
|
{
|
|
FakeHost f;
|
|
f.tools["slow"] = [](const json&) { return ToolResult::ok({{"status", "running"}, {"job", "j1"}}); };
|
|
const Replay s = parse_ok(R"({"orca_replay": 1, "steps": [{"tool": "slow"}]})");
|
|
|
|
ReplayHost stopping = f.host();
|
|
stopping.wait_job = [&f](const std::string&, int) -> std::optional<ToolResult> {
|
|
f.cancel = true;
|
|
return std::nullopt;
|
|
};
|
|
ReplayContext ctx;
|
|
const json stopped = run_replay(s, ctx, stopping);
|
|
CHECK(stopped["steps"][0]["cancelled"] == true);
|
|
CHECK(stopped["steps"][0]["reason"]["kind"] == "cancelled");
|
|
|
|
FakeHost g;
|
|
g.tools = f.tools;
|
|
const json out = run_replay(s, ctx, g.host());
|
|
CHECK(out["steps"][0]["reason"] == json{{"kind", "timeout"}, {"seconds", 120}});
|
|
}
|
|
|
|
TEST_CASE("a job wait that times out names the dialog left open", "[Mcp]")
|
|
{
|
|
FakeHost f;
|
|
f.tools["opens"] = [](const json&) { return ToolResult::ok({{"status", "blocked_by_dialog"}, {"job", "j1"}}); };
|
|
const Replay s = parse_ok(R"({"orca_replay": 1, "steps": [{"tool": "opens"}]})");
|
|
|
|
const json dialogs = json::array({{{"title", "Uniform scale"}, {"native", false}}, {{"title", "Preferences"}, {"native", false}}});
|
|
ReplayHost host = f.host();
|
|
host.open_dialogs = [&dialogs] { return dialogs; };
|
|
ReplayContext ctx;
|
|
const json out = run_replay(s, ctx, host);
|
|
const json& step = out["steps"][0];
|
|
const std::string error = step["error"].get<std::string>();
|
|
CHECK(step["reason"]["kind"] == "timeout");
|
|
// The topmost dialog, the last one listed, is the one named.
|
|
CHECK(error.find("\"Preferences\"") != std::string::npos);
|
|
CHECK(error.find("auto_answer_dialogs") != std::string::npos);
|
|
CHECK(step["details"]["dialogs"] == dialogs);
|
|
}
|
|
|
|
TEST_CASE("a failed expectation carries its parts", "[Mcp]")
|
|
{
|
|
FakeHost f;
|
|
f.tools["slice"] = [](const json&) { return ToolResult::ok({{"plates", {{{"outcome", "error"}}}}}); };
|
|
const Replay s = parse_ok(R"({"orca_replay": 1, "steps": [{"tool": "slice", "expect": {"/plates/0/outcome": {"eq": "ok"}}}]})");
|
|
ReplayContext ctx;
|
|
const json entry = run_replay(s, ctx, f.host())["steps"][0];
|
|
CHECK(entry["reason"] == json{{"kind", "expect"}, {"pointer", "/plates/0/outcome"}, {"op", "eq"},
|
|
{"expected", "ok"}, {"actual", "error"}, {"exists", true}});
|
|
CHECK(entry["expect_failure"].get<std::string>().find("/plates/0/outcome: expected eq \"ok\"") == 0);
|
|
}
|
|
|
|
TEST_CASE("a matcher that is not an object is a malformed expectation", "[Mcp]")
|
|
{
|
|
FakeHost f;
|
|
f.tools["slice"] = [](const json&) { return ToolResult::ok({{"outcome", "ok"}}); };
|
|
const Replay s = parse_ok(R"({"orca_replay": 1, "steps": [{"tool": "slice", "expect": {"/outcome": "ok"}}]})");
|
|
ReplayContext ctx;
|
|
const json entry = run_replay(s, ctx, f.host())["steps"][0];
|
|
CHECK(entry["ok"] == false);
|
|
CHECK(entry["reason"] == json{{"kind", "expect_invalid"}});
|
|
CHECK(entry["expect_failure"].get<std::string>().find("must be an object") != std::string::npos);
|
|
}
|
|
|
|
// Action Replay's step descriptions mirror this: an inline nested replay reads its placeholders
|
|
// from the outer replay, whose substitution runs before the nested run starts.
|
|
TEST_CASE("an inline nested replay's placeholders are the outer replay's", "[Mcp]")
|
|
{
|
|
FakeHost f;
|
|
f.tools["run_replay"] = [](const json&) { return ToolResult::ok({{"passed", true}}); };
|
|
const Replay with_outer = parse_ok(R"({"orca_replay": 1, "vars": {"p": "outer.stl"}, "steps": [{"tool": "run_replay",
|
|
"args": {"vars": {"p": "step.stl"}, "replay": {"orca_replay": 1, "steps": [{"tool": "open_files", "args": {"paths": ["${vars.p}"]}}]}}}]})");
|
|
ReplayContext ctx;
|
|
CHECK(run_replay(with_outer, ctx, f.host())["passed"] == true);
|
|
REQUIRE(f.calls.size() == 1);
|
|
CHECK(f.calls[0].second["replay"]["steps"][0]["args"]["paths"][0] == "outer.stl");
|
|
|
|
FakeHost g;
|
|
const Replay step_only = parse_ok(R"({"orca_replay": 1, "steps": [{"tool": "run_replay",
|
|
"args": {"vars": {"p": "step.stl"}, "replay": {"orca_replay": 1, "steps": [{"tool": "open_files", "args": {"paths": ["${vars.p}"]}}]}}}]})");
|
|
const json out = run_replay(step_only, ctx, g.host());
|
|
CHECK(out["passed"] == false);
|
|
CHECK(out["steps"][0]["reason"] == json{{"kind", "placeholder"}, {"text", "${vars.p}"}});
|
|
CHECK(g.calls.empty());
|
|
}
|
|
|
|
TEST_CASE("lenient substitution keeps what it cannot resolve", "[Mcp]")
|
|
{
|
|
const Replay s = parse_ok(R"({"orca_replay": 1, "vars": {"model": "C:/m/part.stl"}, "steps": []})");
|
|
ReplayContext ctx;
|
|
CHECK(substitute_lenient("${vars.model}", s, ctx) == "C:/m/part.stl");
|
|
CHECK(substitute_lenient("${jobs.none}/x", s, ctx) == "${jobs.none}/x");
|
|
CHECK(substitute_lenient(json{{"p", "${vars.nope}"}}, s, ctx) == json{{"p", "${vars.nope}"}});
|
|
}
|
|
|
|
TEST_CASE("malformed replays are errors, not exceptions", "[Mcp]")
|
|
{
|
|
const auto [text, field] = GENERATE(table<std::string, std::string>({
|
|
{R"({"orca_replay": "1", "steps": []})", "orca_replay"},
|
|
{R"({"orca_replay": 1, "name": 5, "steps": []})", "name"},
|
|
{R"({"orca_replay": 1, "steps": [{"tool": 3}]})", "step 1: \"tool\""},
|
|
{R"({"orca_replay": 1, "steps": [{"tool": "a", "timeout_s": "9"}]})", "step 1: \"timeout_s\""},
|
|
{R"({"orca_replay": 1, "steps": [{"tool": "a", "optional": "yes"}]})", "step 1: \"optional\""},
|
|
{R"([])", "orca_replay"},
|
|
{R"({})", "orca_replay"},
|
|
}));
|
|
std::string err;
|
|
std::optional<Replay> s;
|
|
CHECK_NOTHROW(s = parse_replay(text, err));
|
|
CHECK_FALSE(s.has_value());
|
|
CHECK(err.find(field) != std::string::npos);
|
|
}
|
|
|
|
TEST_CASE("broken JSON says where", "[Mcp]")
|
|
{
|
|
std::string err;
|
|
CHECK_FALSE(parse_replay("{\n \"orca_replay\": 1,\n \"steps\": [ {\"tool\": \"a\",, } ]\n}", err).has_value());
|
|
CHECK(err.find("line 3, column") != std::string::npos);
|
|
}
|
|
|
|
TEST_CASE("a Python step's code runs as written while its other arguments substitute", "[Mcp]")
|
|
{
|
|
const Replay s = parse_ok(R"({"orca_replay": 1, "vars": {"a": "seven"}, "steps": [
|
|
{"tool": "run_script", "args": {"code": "label = '${vars.a}'", "args": {"label": "${vars.a}"}}}]})");
|
|
FakeHost fake;
|
|
fake.tools["run_script"] = [](const json&) { return ToolResult::ok({{"status", "finished"}, {"result", nullptr}, {"output", ""}}); };
|
|
ReplayContext ctx;
|
|
const json out = run_replay(s, ctx, fake.host(), std::nullopt);
|
|
CHECK(out["passed"] == true);
|
|
REQUIRE(fake.calls.size() == 1);
|
|
CHECK(fake.calls[0].second["code"] == "label = '${vars.a}'");
|
|
CHECK(fake.calls[0].second["args"]["label"] == "seven");
|
|
}
|
|
|
|
TEST_CASE("a Python step that did not finish fails its replay", "[Mcp]")
|
|
{
|
|
const Replay s = parse_ok(R"S({"orca_replay": 1, "steps": [
|
|
{"tool": "run_script", "args": {"code": "raise ValueError('x')"}},
|
|
{"tool": "get_app_state"}]})S");
|
|
FakeHost fake;
|
|
fake.tools["run_script"] = [](const json&) {
|
|
return ToolResult::ok({{"status", "raised"}, {"message", "ValueError: x"}, {"traceback", "..."}, {"output", ""}});
|
|
};
|
|
fake.tools["get_app_state"] = [](const json&) { return ToolResult::ok(); };
|
|
ReplayContext ctx;
|
|
const json out = run_replay(s, ctx, fake.host(), std::nullopt);
|
|
CHECK(out["passed"] == false);
|
|
CHECK(out["failed_step"] == 1);
|
|
CHECK(out["steps"][0]["error"] == "ValueError: x");
|
|
CHECK(out["steps"][0]["reason"]["kind"] == "python");
|
|
CHECK(out["steps"][0]["reason"]["status"] == "raised");
|
|
CHECK(fake.calls.size() == 1);
|
|
}
|
|
|
|
TEST_CASE("a Python step left running with await false keeps its job for later steps", "[Mcp]")
|
|
{
|
|
const Replay s = parse_ok(R"({"orca_replay": 1, "steps": [
|
|
{"tool": "run_script", "args": {"code": "x = 1"}, "await": false, "save_as": "py"},
|
|
{"tool": "get_app_state"}]})");
|
|
FakeHost fake;
|
|
fake.tools["run_script"] = [](const json&) { return ToolResult::ok({{"status", "running"}, {"job", "j5"}}); };
|
|
fake.tools["get_app_state"] = [](const json&) { return ToolResult::ok(); };
|
|
ReplayContext ctx;
|
|
const json out = run_replay(s, ctx, fake.host(), std::nullopt);
|
|
CHECK(out["passed"] == true);
|
|
CHECK(ctx.jobs["py"] == "j5");
|
|
}
|
|
|
|
TEST_CASE("Python reached through call_tool or a nested replay follows the Python step rules", "[Mcp]")
|
|
{
|
|
// Its code runs as written however the step reaches it.
|
|
const Replay s = parse_ok(R"S({"orca_replay": 1, "vars": {"a": "seven"}, "steps": [
|
|
{"tool": "call_tool", "args": {"tool": "run_script", "args": {"code": "x = '${vars.a}'", "args": {"v": "${vars.a}"}}}},
|
|
{"tool": "run_replay", "args": {"replay": {"orca_replay": 1, "steps": [
|
|
{"tool": "run_script", "args": {"code": "y = '${vars.a}'"}},
|
|
{"tool": "set_settings", "args": {"note": "${vars.a}"}}]}}}]})S");
|
|
FakeHost fake;
|
|
fake.tools["call_tool"] = [](const json&) { return ToolResult::ok({{"status", "finished"}, {"result", nullptr}, {"output", ""}}); };
|
|
fake.tools["run_replay"] = [](const json&) { return ToolResult::ok({{"passed", true}}); };
|
|
ReplayContext ctx;
|
|
const json out = run_replay(s, ctx, fake.host(), std::nullopt);
|
|
CHECK(out["passed"] == true);
|
|
REQUIRE(fake.calls.size() == 2);
|
|
CHECK(fake.calls[0].second["args"]["code"] == "x = '${vars.a}'");
|
|
CHECK(fake.calls[0].second["args"]["args"]["v"] == "seven");
|
|
const json nested = fake.calls[1].second["replay"]["steps"];
|
|
CHECK(nested[0]["args"]["code"] == "y = '${vars.a}'");
|
|
CHECK(nested[1]["args"]["note"] == "seven");
|
|
|
|
// A run that raised through call_tool fails its step.
|
|
const Replay failing = parse_ok(R"({"orca_replay": 1, "steps": [
|
|
{"tool": "call_tool", "args": {"tool": "run_script", "args": {"code": "x"}}}]})");
|
|
FakeHost raising;
|
|
raising.tools["call_tool"] = [](const json&) { return ToolResult::ok({{"status", "raised"}, {"message", "NameError: x"}}); };
|
|
ReplayContext ctx2;
|
|
const json failed = run_replay(failing, ctx2, raising.host(), std::nullopt);
|
|
CHECK(failed["passed"] == false);
|
|
CHECK(failed["steps"][0]["reason"]["kind"] == "python");
|
|
}
|