Files
OrcaSlicer/tests/slic3rutils/test_moonraker_printer_agent.cpp
SoftFever aaa2e5079e Stop requesting a full status from idle printers every 25 seconds
While a selected printer sent nothing but empty keep-alives, OrcaSlicer asked it for its
whole status every 25 seconds, on the LAN and through the cloud, which for a Bambu printer
is about 9 KB each time. Idle printers keep themselves shown as connected on their own, as
Bambu printers and OrcaSonar do, so OrcaSlicer again keeps the connection alive the way
Bambu Studio does.

This reverts commit 45d0c1ed46.
2026-11-28 13:21:42 +08:00

1298 lines
48 KiB
C++

#include <catch2/catch_message.hpp>
#include <catch2/catch_test_macros.hpp>
#include <catch2/generators/catch_generators.hpp>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <cstddef>
#include <cstring>
#include <functional>
#include <future>
#include <ios>
#include <iterator>
#include <memory>
#include <mutex>
#include <optional>
#include <string>
#include <string_view>
#include <thread>
#include <utility>
#include <vector>
#include <boost/asio/buffer.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/ip/address.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ssl/context.hpp>
#include <boost/asio/ssl/stream.hpp>
#include <boost/asio/ssl/stream_base.hpp>
#include <boost/beast/core/error.hpp>
#include <boost/beast/core/flat_buffer.hpp>
#include <boost/beast/core/stream_traits.hpp>
#include <boost/beast/core/string_type.hpp>
#include <boost/beast/http/message.hpp>
#include <boost/beast/http/read.hpp>
#include <boost/beast/http/status.hpp>
#include <boost/beast/http/string_body.hpp>
#include <boost/beast/http/write.hpp>
#include <boost/beast/websocket/rfc6455.hpp>
#include <boost/beast/websocket/ssl.hpp>
#include <boost/beast/websocket/stream.hpp>
#include <boost/filesystem/directory.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/nowide/convert.hpp>
#include <boost/nowide/cstdlib.hpp>
#include <miniz.h>
#include <nlohmann/json.hpp>
#include "libslic3r/miniz_extension.hpp"
#include "slic3r/GUI/DeviceCore/DevStorage.h"
#include "slic3r/Utils/IPrinterAgent.hpp"
#include "slic3r/Utils/MoonrakerPrinterAgent.hpp"
#include "slic3r/Utils/bambu_networking.hpp"
#include "test_utils.hpp"
using namespace Slic3r;
namespace {
namespace beast = boost::beast;
namespace http = beast::http;
namespace websocket = beast::websocket;
namespace net = boost::asio;
namespace ssl = net::ssl;
using tcp = net::ip::tcp;
using namespace std::chrono_literals;
// A self-signed certificate for the IP address 127.0.0.1, valid until 2126. The agent verifies it only against a
// configured CA file.
const char* const TEST_CERTIFICATE = R"(-----BEGIN CERTIFICATE-----
MIIBkDCCATagAwIBAgIUNBsiBs6jFo0EpvlmtkfeQS61SuEwCgYIKoZIzj0EAwIw
FDESMBAGA1UEAwwJMTI3LjAuMC4xMCAXDTI2MTExMDA5NDE1MFoYDzIxMjYxMDE3
MDk0MTUwWjAUMRIwEAYDVQQDDAkxMjcuMC4wLjEwWTATBgcqhkjOPQIBBggqhkjO
PQMBBwNCAATE+xP+W5IMnVnXRLDZ7waSG8K/mrX7C5ag0wK0/RnQtuDI1DrH8BtN
G3pYefP1LyLHvY6eRo+idPJfdYZLRnMMo2QwYjAdBgNVHQ4EFgQUpS54cY4Tts7j
LPybtg/pMEkuT1UwHwYDVR0jBBgwFoAUpS54cY4Tts7jLPybtg/pMEkuT1UwDwYD
VR0RBAgwBocEfwAAATAPBgNVHRMBAf8EBTADAQH/MAoGCCqGSM49BAMCA0gAMEUC
IDBLkpS0DeU/dKzTb4znxv84rGABrQAPJhgalc5gSfgYAiEAzfAubIe5TfCeFrjC
oRWZJcP9UJlO2lVLsBZ1L2KUeC0=
-----END CERTIFICATE-----
)";
const char* const TEST_PRIVATE_KEY = R"(-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgB91ojZEjc9V7/8BD
gdxLN7Eoy/72IpxOwZ2kKY9KCO6hRANCAATE+xP+W5IMnVnXRLDZ7waSG8K/mrX7
C5ag0wK0/RnQtuDI1DrH8BtNG3pYefP1LyLHvY6eRo+idPJfdYZLRnMM
-----END PRIVATE KEY-----
)";
// A CA that did not sign TEST_CERTIFICATE.
const char* const OTHER_CA_CERTIFICATE = R"(-----BEGIN CERTIFICATE-----
MIIBfjCCASOgAwIBAgIUSYKsTDY8WgVvylcwcdjL+ihbvaswCgYIKoZIzj0EAwIw
EzERMA8GA1UEAwwIT3RoZXIgQ0EwIBcNMjYxMTEwMDk0MTUwWhgPMjEyNjEwMTcw
OTQxNTBaMBMxETAPBgNVBAMMCE90aGVyIENBMFkwEwYHKoZIzj0CAQYIKoZIzj0D
AQcDQgAEjJAsmZ+N5ToGh+h0OcFHx2x4UEyMh83D3H4UY/0ppnLYYHQula5BuUAM
mVFbWioCcEMBFruXdzfGiL0BZMeIhqNTMFEwHQYDVR0OBBYEFCgsL2wl3BU0BAFQ
Kgp7dtBHLMtpMB8GA1UdIwQYMBaAFCgsL2wl3BU0BAFQKgp7dtBHLMtpMA8GA1Ud
EwEB/wQFMAMBAf8wCgYIKoZIzj0EAwIDSQAwRgIhAMM1lNuHC8twhyBOU4t8yw8y
XHC1sk0CwzYizVRAABJqAiEA15w9kqqGc6jeTOkkukrizM/krNOaR0r3OxIGaayQ
shg=
-----END CERTIFICATE-----
)";
class DiscoveryAgent : public MoonrakerPrinterAgent
{
public:
using MoonrakerPrinterAgent::MoonrakerPrinterAgent;
using MoonrakerPrinterAgent::device_info;
mutable unsigned fetch_count = 0;
protected:
bool fetch_device_info(const ConnectionSettings&, MoonrakerDeviceInfo& info, std::string&, const std::function<bool()>&) const override
{
++fetch_count;
info.dev_name = "Test printer";
return true;
}
};
// Exposes the protected device-info query and the advertised model it returns.
class ServerInfoAgent : public MoonrakerPrinterAgent
{
public:
using MoonrakerPrinterAgent::MoonrakerPrinterAgent;
struct Info
{
std::string advertised_model;
bool identity_fetched{false};
};
bool fetch_info(const std::string& base_url, Info& out)
{
ConnectionSettings connection;
connection.base_url = base_url;
MoonrakerDeviceInfo info;
std::string error;
if (!MoonrakerPrinterAgent::fetch_device_info(connection, info, error, {}))
return false;
out.advertised_model = info.advertised_model;
out.identity_fetched = info.identity_fetched;
return true;
}
};
// Connects without the app's printer preset, and counts the device info queries that have returned.
class ConnectAgent : public MoonrakerPrinterAgent
{
public:
using MoonrakerPrinterAgent::MoonrakerPrinterAgent;
std::shared_ptr<std::atomic<int>> fetches_returned;
protected:
bool init_device_info(const PrinterConnectionParams& params) override
{
device_info = MoonrakerDeviceInfo{};
device_info.dev_id = params.dev_id;
device_info.dev_ip = params.host;
device_info.base_url = std::string(params.use_ssl ? "https://" : "http://") + params.host + ":" + params.port;
return true;
}
bool fetch_device_info(const ConnectionSettings& connection,
MoonrakerDeviceInfo& info,
std::string& error,
const std::function<bool()>& cancelled) const override
{
// Copied first, since the base destructor that waits for this query runs after this class's members are gone
const std::shared_ptr<std::atomic<int>> counter = fetches_returned;
const bool fetched = MoonrakerPrinterAgent::fetch_device_info(connection, info, error, cancelled);
if (counter)
++*counter;
return fetched;
}
};
class StreamAgent : public ConnectAgent
{
public:
using ConnectAgent::ConnectAgent;
using MoonrakerPrinterAgent::ws_timings;
// Starts the stream the way a connection does, from a snapshot of the connection settings.
void start_stream(const std::string& base_url, bool use_ssl = false, const std::string& ca_file = {})
{
const std::string dev_id = "printer";
ConnectionSettings connection;
connection.dev_id = dev_id;
connection.base_url = base_url;
connection.use_ssl = use_ssl;
connection.ca_file = ca_file;
start_status_stream(dev_id, std::move(connection));
}
void stop_stream() { stop_status_stream(); }
protected:
bool fetch_device_info(const ConnectionSettings&, MoonrakerDeviceInfo& info, std::string&, const std::function<bool()>&) const override
{
info.dev_name = "Test printer";
return true;
}
};
// Fails its first `refusals` device info queries, as when macOS refuses the app's first local network connections.
class RefusedAgent : public StreamAgent
{
public:
using StreamAgent::StreamAgent;
int refusals = 0;
mutable std::atomic<int> fetches{0};
protected:
bool fetch_device_info(const ConnectionSettings& connection,
MoonrakerDeviceInfo& info,
std::string& error,
const std::function<bool()>& cancelled) const override
{
if (++fetches <= refusals) {
error = "Couldn't connect to server";
return false;
}
return StreamAgent::fetch_device_info(connection, info, error, cancelled);
}
};
enum class Behavior {
SilentBeforeHandshake, // Accepts the connection and never answers
Silent, // Completes the upgrade, then never reads or sends
AnswersPings, // Reports a status, then answers pings and sends nothing else
AnswersPingsWithoutStatus, // Answers pings but never reports a status
Chatters, // Reports a status, then answers pings and sends notifications that carry no status
HangsUp, // Reports a status, then closes the websocket
HangsUpWithoutAnswer, // Closes the websocket without sending anything
IgnoresClose, // Reports Klippy disconnected, then never answers the close
StallsObjectList, // Never answers the stream's object list request
StallsHttp, // Never answers HTTP requests until released, then hangs up on them
RejectsTls, // Records the first byte of the stream, which a TLS client sends as a handshake
};
class FakePrinter
{
public:
explicit FakePrinter(Behavior behavior, bool tls = false) : m_behavior(behavior), m_tls(tls)
{
m_tls_context.use_certificate_chain(net::buffer(std::string_view(TEST_CERTIFICATE)));
m_tls_context.use_private_key(net::buffer(std::string_view(TEST_PRIVATE_KEY)), ssl::context::pem);
m_accept_thread = std::thread([this] { accept_loop(); });
}
~FakePrinter()
{
release();
m_stopping = true;
// Wakes the blocking accept.
tcp::socket wake{m_accept_ioc};
beast::error_code ec;
wake.connect(m_acceptor.local_endpoint(), ec);
m_accept_thread.join();
for (std::thread& connection : m_connections)
connection.join();
}
std::string host() const { return "127.0.0.1"; }
std::string port() const { return std::to_string(m_acceptor.local_endpoint().port()); }
std::string base_url() const { return (m_tls ? "https://" : "http://") + host() + ":" + port(); }
// A connection is ready once it reaches the state its behavior waits in.
bool wait_for_ready_connections(int count, std::chrono::milliseconds timeout = 10s)
{
std::unique_lock<std::mutex> lock(m_mutex);
return m_changed.wait_for(lock, timeout, [&] { return m_ready >= count; });
}
bool wait_for_http_requests(int count)
{
std::unique_lock<std::mutex> lock(m_mutex);
return m_changed.wait_for(lock, 10s, [&] { return m_http_requests >= count; });
}
// Waits until the printer, once released, has hung up on `count` stalled HTTP requests.
bool wait_for_http_hang_ups(int count)
{
std::unique_lock<std::mutex> lock(m_mutex);
return m_changed.wait_for(lock, 10s, [&] { return m_http_hung_up >= count; });
}
int close_frames()
{
std::lock_guard<std::mutex> lock(m_mutex);
return m_close_frames;
}
// First byte the stream sent, or -1 until it has sent one.
bool wait_for_first_byte()
{
std::unique_lock<std::mutex> lock(m_mutex);
return m_changed.wait_for(lock, 10s, [&] { return m_first_byte >= 0; });
}
int first_byte()
{
std::lock_guard<std::mutex> lock(m_mutex);
return m_first_byte;
}
// The answer to the gcodes directory listing, which reports the free space in disk_usage. Null leaves disk_usage out.
void set_gcodes_listing(nlohmann::json disk_usage)
{
std::lock_guard<std::mutex> lock(m_mutex);
m_disk_usage = std::move(disk_usage);
}
// The model /server/info advertises; empty means the printer reports none.
void set_server_model(std::string model)
{
std::lock_guard<std::mutex> lock(m_mutex);
m_server_model = std::move(model);
}
// The multipart body of the last file upload.
std::string upload()
{
std::lock_guard<std::mutex> lock(m_mutex);
return m_upload;
}
// The last G-code script sent to the printer.
std::string script()
{
std::lock_guard<std::mutex> lock(m_mutex);
return m_script;
}
// Hangs up every connection that is waiting.
void release()
{
std::lock_guard<std::mutex> lock(m_mutex);
m_released = true;
m_changed.notify_all();
}
private:
void accept_loop()
{
for (;;) {
// Each connection gets its own io_context, so a connection that runs one never runs another's handlers.
auto ioc = std::make_unique<net::io_context>();
tcp::socket socket{*ioc};
beast::error_code ec;
m_acceptor.accept(socket, ec);
if (ec || m_stopping)
return;
m_connections.emplace_back(
[this, ioc = std::move(ioc), socket = std::move(socket)]() mutable { serve(*ioc, std::move(socket)); });
}
}
void serve(net::io_context& ioc, tcp::socket socket)
{
if (m_behavior == Behavior::SilentBeforeHandshake) {
mark_ready();
wait_for_release();
return;
}
if (m_behavior == Behavior::RejectsTls) {
char first = 0;
beast::error_code ec;
socket.read_some(net::buffer(&first, 1), ec);
if (!ec) {
std::lock_guard<std::mutex> lock(m_mutex);
m_first_byte = static_cast<unsigned char>(first);
m_changed.notify_all();
}
mark_ready();
wait_for_release();
return;
}
if (!m_tls) {
serve_stream(ioc, std::move(socket));
return;
}
ssl::stream<tcp::socket> stream{std::move(socket), m_tls_context};
beast::error_code ec;
stream.handshake(ssl::stream_base::server, ec);
if (!ec)
serve_stream(ioc, std::move(stream));
}
template<class Stream> void serve_stream(net::io_context& ioc, Stream stream)
{
beast::flat_buffer buffer;
http::request<http::string_body> request;
beast::error_code ec;
http::read(stream, buffer, request, ec);
if (ec)
return;
// The agent also queries the printer's status and object list over HTTP.
if (!websocket::is_upgrade(request)) {
serve_http(stream, request);
return;
}
websocket::stream<Stream> ws{std::move(stream)};
ws.accept(request, ec);
// Without its object list the stream never sends the subscribe request.
const int requests = m_behavior == Behavior::StallsObjectList ? 1 : 2;
for (int i = 0; i < requests && !ec; ++i) {
beast::flat_buffer message;
ws.read(message, ec);
}
if (!ec && (m_behavior == Behavior::AnswersPings || m_behavior == Behavior::Chatters || m_behavior == Behavior::HangsUp)) {
const std::string status =
nlohmann::json{{"jsonrpc", "2.0"}, {"id", 1}, {"result", {{"status", {{"print_stats", {{"state", "standby"}}}}}}}}.dump();
ws.write(net::buffer(status), ec);
}
if (ec)
return;
switch (m_behavior) {
case Behavior::AnswersPings:
case Behavior::AnswersPingsWithoutStatus:
case Behavior::Chatters:
mark_ready();
answer_pings(ioc, ws, m_behavior == Behavior::Chatters);
break;
case Behavior::HangsUp:
case Behavior::HangsUpWithoutAnswer:
ws.close(websocket::close_code::normal, ec);
mark_ready();
break;
case Behavior::IgnoresClose: {
const std::string notification = nlohmann::json{{"jsonrpc", "2.0"}, {"method", "notify_klippy_disconnected"}}.dump();
ws.write(net::buffer(notification), ec);
mark_ready();
wait_for_release();
break;
}
case Behavior::StallsObjectList: answer_pings(ioc, ws, false); break;
default: mark_ready(); wait_for_release();
}
}
template<class Stream> void serve_http(Stream& stream, const http::request<http::string_body>& request)
{
{
std::lock_guard<std::mutex> lock(m_mutex);
++m_http_requests;
m_changed.notify_all();
}
if (m_behavior == Behavior::StallsObjectList) {
mark_ready();
wait_for_release();
return;
}
if (m_behavior == Behavior::StallsHttp) {
wait_for_release();
beast::error_code ec;
beast::get_lowest_layer(stream).close(ec);
std::lock_guard<std::mutex> lock(m_mutex);
++m_http_hung_up;
m_changed.notify_all();
return;
}
http::response<http::string_body> response{http::status::not_found, request.version()};
if (request.target() == "/server/files/directory?path=gcodes") {
nlohmann::json result = {{"dirs", nlohmann::json::array()}, {"files", nlohmann::json::array()}};
{
std::lock_guard<std::mutex> lock(m_mutex);
if (!m_disk_usage.is_null())
result["disk_usage"] = m_disk_usage;
}
response.result(http::status::ok);
response.body() = nlohmann::json{{"result", result}}.dump();
} else if (request.target() == "/server/info") {
nlohmann::json result{{"machine_name", "Test printer"}, {"moonraker_version", "v0.9.0"}, {"klippy_state", "ready"}};
{
std::lock_guard<std::mutex> lock(m_mutex);
if (!m_server_model.empty())
result["model"] = m_server_model;
}
response.result(http::status::ok);
response.body() = nlohmann::json{{"result", result}}.dump();
} else if (request.target() == "/server/files/upload") {
std::lock_guard<std::mutex> lock(m_mutex);
m_upload = request.body();
response.result(http::status::created);
response.body() = "{}";
} else if (request.target() == "/printer/gcode/script") {
std::lock_guard<std::mutex> lock(m_mutex);
const nlohmann::json body = nlohmann::json::parse(request.body(), nullptr, false);
m_script = body.is_object() ? body.value("script", "") : "";
response.result(http::status::ok);
response.body() = R"({"result": "ok"})";
}
response.prepare_payload();
beast::error_code ec;
http::write(stream, response, ec);
}
// Keeps a read pending, which answers pings, until released or closed, and with `chatter` also sends a notification every 50 ms.
template<class WebSocket> void answer_pings(net::io_context& ioc, WebSocket& ws, bool chatter)
{
const std::string notification = nlohmann::json{{"jsonrpc", "2.0"},
{"method", "notify_proc_stat_update"},
{"params", nlohmann::json::array({nlohmann::json::object()})}}
.dump();
beast::flat_buffer message;
bool reading = true;
bool writing = false;
// Counted on arrival: the read completes only after the reply and the teardown, by which time the
// agent's stop may have returned and the test's release closed this socket, failing the read instead.
ws.control_callback([&](websocket::frame_type kind, beast::string_view) {
if (kind == websocket::frame_type::close) {
std::lock_guard<std::mutex> lock(m_mutex);
++m_close_frames;
}
});
std::function<void()> read_next = [&] {
ws.async_read(message, [&](beast::error_code ec, std::size_t) {
message.clear();
if (ec)
reading = false;
else
read_next();
});
};
read_next();
auto next_notification = std::chrono::steady_clock::now();
while (reading && !wait_for_release(10ms)) {
if (chatter && !writing && std::chrono::steady_clock::now() >= next_notification) {
writing = true;
next_notification += 50ms;
ws.async_write(net::buffer(notification), [&](beast::error_code, std::size_t) { writing = false; });
}
ioc.restart();
ioc.run_for(20ms);
}
beast::error_code ignored;
beast::get_lowest_layer(ws).close(ignored);
while (reading || writing) {
ioc.restart();
ioc.run_for(20ms);
}
}
void mark_ready()
{
std::lock_guard<std::mutex> lock(m_mutex);
++m_ready;
m_changed.notify_all();
}
void wait_for_release()
{
std::unique_lock<std::mutex> lock(m_mutex);
m_changed.wait(lock, [this] { return m_released; });
}
bool wait_for_release(std::chrono::milliseconds timeout)
{
std::unique_lock<std::mutex> lock(m_mutex);
return m_changed.wait_for(lock, timeout, [this] { return m_released; });
}
const Behavior m_behavior;
const bool m_tls;
ssl::context m_tls_context{ssl::context::tls_server};
net::io_context m_accept_ioc;
tcp::acceptor m_acceptor{m_accept_ioc, {net::ip::make_address("127.0.0.1"), 0}};
std::atomic<bool> m_stopping{false};
std::thread m_accept_thread;
std::vector<std::thread> m_connections;
std::mutex m_mutex;
std::condition_variable m_changed;
int m_ready = 0;
int m_http_requests = 0;
int m_http_hung_up = 0;
int m_close_frames = 0;
int m_first_byte = -1;
bool m_released = false;
nlohmann::json m_disk_usage = {{"total", 8e9}, {"used", 2e9}, {"free", 6e9}};
std::string m_server_model = "Qidi-Q2";
std::string m_upload;
std::string m_script;
};
// Records when the agent dispatches a status report.
class Reports
{
public:
void attach(StreamAgent& agent)
{
agent.set_on_local_message_fn([this](std::string, std::string) {
std::lock_guard<std::mutex> lock(m_mutex);
m_times.push_back(std::chrono::steady_clock::now());
m_changed.notify_all();
});
}
bool wait_for(std::size_t count)
{
std::unique_lock<std::mutex> lock(m_mutex);
return m_changed.wait_for(lock, 10s, [&] { return m_times.size() >= count; });
}
std::vector<std::chrono::steady_clock::time_point> times()
{
std::lock_guard<std::mutex> lock(m_mutex);
return m_times;
}
private:
std::mutex m_mutex;
std::condition_variable m_changed;
std::vector<std::chrono::steady_clock::time_point> m_times;
};
// A printer and a stream agent connected to it.
struct StreamFixture
{
explicit StreamFixture(Behavior behavior, bool tls = false) : printer(behavior, tls) {}
// Runs before the members are destroyed, so the agent's destructor never joins a stream the printer still holds open.
~StreamFixture() { printer.release(); }
void start() { agent.start_stream(printer.base_url()); }
// Runs the stream as a wss client; the printer must be served on the same port.
void start_secure() { agent.start_stream("https://" + printer.host() + ":" + printer.port(), true); }
// Runs the stream against a TLS printer, verifying its certificate against the CA certificate `ca_pem`.
void start_with_ca(const std::string& ca_pem)
{
const boost::filesystem::path ca_file = ca_dir.path() / "ca.pem";
boost::filesystem::ofstream(ca_file, std::ios::binary) << ca_pem;
agent.start_stream(printer.base_url(), true, ca_file.string());
}
// Stops the stream, giving it `limit` to return before the printer hangs up on it.
bool stops_within(std::chrono::milliseconds limit)
{
auto stopped = std::async(std::launch::async, [this] { agent.stop_stream(); });
const bool in_time = stopped.wait_for(limit) == std::future_status::ready;
printer.release();
stopped.wait();
return in_time;
}
ScopedTemporaryDir ca_dir{"moonraker-ca"};
FakePrinter printer;
StreamAgent agent{""};
};
PrinterConnectionParams connection_to(const FakePrinter& printer)
{
PrinterConnectionParams params;
params.dev_id = "printer";
params.host = printer.host();
params.port = printer.port();
return params;
}
// Destroys the agent, giving it `limit` to return.
template<class Agent> bool destroys_within(std::unique_ptr<Agent>& agent, std::chrono::milliseconds limit)
{
auto destroyed = std::async(std::launch::async, [&] { agent.reset(); });
return destroyed.wait_for(limit) == std::future_status::ready;
}
// Uploads to a printer without connecting to it, and records the file it is told to print.
class UploadAgent : public MoonrakerPrinterAgent
{
public:
explicit UploadAgent(const FakePrinter& printer) : MoonrakerPrinterAgent("") { device_info.base_url = printer.base_url(); }
std::string printed;
bool takes_3mf = false;
protected:
bool accepts_3mf() const override { return takes_3mf; }
bool print_uploaded_file(const PrintParams& params, const std::string& filename) override
{
printed = filename;
return MoonrakerPrinterAgent::print_uploaded_file(params, filename);
}
};
// Writes a .gcode.3mf holding one line of G-code for each of the given plate numbers.
void write_bundle(const std::string& path, const std::vector<std::string>& plates)
{
mz_zip_archive zip;
mz_zip_zero_struct(&zip);
REQUIRE(open_zip_writer(&zip, path));
for (const std::string& plate : plates) {
const std::string gcode = "; plate " + plate + "\n";
REQUIRE(mz_zip_writer_add_mem(&zip, ("Metadata/plate_" + plate + ".gcode").c_str(), gcode.data(), gcode.size(), MZ_DEFAULT_COMPRESSION));
}
REQUIRE(mz_zip_writer_finalize_archive(&zip));
REQUIRE(close_zip_writer(&zip));
}
// Makes a fresh directory the system temp folder for its lifetime, so a test sees only what the agent leaves there.
class ScopedSystemTemp : public ScopedTemporaryDir
{
public:
ScopedSystemTemp() : ScopedTemporaryDir("moonraker-temp")
{
#ifdef _WIN32
const std::string dir = boost::nowide::narrow(path().wstring());
#else
const std::string dir = string();
#endif
for (const char* name : {"TMP", "TEMP", "TMPDIR"}) {
const char* value = boost::nowide::getenv(name);
m_saved.emplace_back(name, value ? std::optional<std::string>(value) : std::nullopt);
boost::nowide::setenv(name, dir.c_str(), 1);
}
}
~ScopedSystemTemp()
{
for (const auto& [name, value] : m_saved) {
if (value)
boost::nowide::setenv(name.c_str(), value->c_str(), 1);
else
boost::nowide::unsetenv(name.c_str());
}
}
private:
std::vector<std::pair<std::string, std::optional<std::string>>> m_saved;
};
} // namespace
TEST_CASE("Moonraker discovery waits for a complete device identity", "[MoonrakerPrinterAgent][Discovery]")
{
DiscoveryAgent agent("");
unsigned announcements = 0;
nlohmann::json announcement;
agent.set_on_ssdp_msg_fn([&](std::string payload) {
++announcements;
announcement = nlohmann::json::parse(payload);
});
agent.start_discovery(true, false);
REQUIRE(announcements == 0);
REQUIRE(agent.fetch_count == 0);
agent.device_info.dev_id = "127.0.0.1:7125";
agent.device_info.dev_ip = "127.0.0.1";
agent.device_info.base_url = "http://127.0.0.1:7125";
agent.device_info.model_name = "Test model";
agent.start_discovery(true, false);
REQUIRE(announcements == 1);
REQUIRE(agent.fetch_count == 0);
REQUIRE(announcement.at("dev_id") == agent.device_info.dev_id);
REQUIRE(announcement.at("dev_ip") == "127.0.0.1:7125");
REQUIRE(announcement.at("dev_name") == "Test model");
}
TEST_CASE("Moonraker reads the model the printer advertises in server info", "[MoonrakerPrinterAgent][Discovery]")
{
FakePrinter printer{Behavior::Silent};
ServerInfoAgent agent("");
ServerInfoAgent::Info info;
REQUIRE(agent.fetch_info(printer.base_url(), info));
CHECK(info.identity_fetched);
CHECK(info.advertised_model == "Qidi-Q2");
// A printer that reports no model marks the identity as queried but unknown.
printer.set_server_model("");
REQUIRE(agent.fetch_info(printer.base_url(), info));
CHECK(info.identity_fetched);
CHECK(info.advertised_model.empty());
}
TEST_CASE("Binding another address does not reuse the connected printer's identity", "[MoonrakerPrinterAgent][Bind]")
{
// The agent was last connected to .5; binding .23 must not return .5's cached identity.
DiscoveryAgent agent("");
agent.device_info.dev_id = "192.168.1.5";
agent.device_info.dev_ip = "192.168.1.5";
agent.device_info.base_url = "http://192.168.1.5";
agent.device_info.dev_name = "Qidi Q2 at .5";
agent.device_info.model_id = "qidi_q2";
agent.device_info.model_name = "Qidi Q2";
agent.device_info.version = "01.02.03";
detectResult detect;
REQUIRE(agent.bind_detect("192.168.1.23", "secure", detect) == BAMBU_NETWORK_SUCCESS);
CHECK(detect.dev_id == "192.168.1.23");
CHECK(detect.dev_name == "192.168.1.23");
CHECK(detect.model_id.empty());
CHECK(detect.version.empty());
CHECK(detect.connect_type == "lan");
CHECK(detect.bind_state == "free");
}
TEST_CASE("A binding address is normalized like a preset's print host", "[MoonrakerPrinterAgent][Bind]")
{
const auto [entered, expected] = GENERATE(table<std::string, std::string>({
{"192.168.1.23", "192.168.1.23"},
{"192.168.1.23:7125", "192.168.1.23:7125"},
{"http://192.168.1.23", "192.168.1.23"},
{"https://192.168.1.23:7126", "192.168.1.23:7126"},
{"http://192.168.1.23:7125/printer", "192.168.1.23:7125"},
}));
CAPTURE(entered);
DiscoveryAgent agent("");
detectResult detect;
REQUIRE(agent.bind_detect(entered, "secure", detect) == BAMBU_NETWORK_SUCCESS);
CHECK(detect.dev_id == expected);
}
TEST_CASE("Binding the connected address does not reuse cached metadata", "[MoonrakerPrinterAgent][Bind]")
{
DiscoveryAgent agent("");
agent.device_info.dev_id = "192.168.1.23:7125";
agent.device_info.dev_ip = "192.168.1.23";
agent.device_info.dev_name = "Qidi Q2";
agent.device_info.model_id = "qidi_q2";
agent.device_info.version = "01.02.03";
detectResult detect;
REQUIRE(agent.bind_detect("http://192.168.1.23:7125/printer", "secure", detect) == BAMBU_NETWORK_SUCCESS);
CHECK(detect.dev_id == "192.168.1.23:7125");
CHECK(detect.dev_name == "192.168.1.23:7125");
CHECK(detect.model_id.empty());
CHECK(detect.version.empty());
CHECK(agent.device_info.dev_name == "Qidi Q2");
CHECK(agent.device_info.model_id == "qidi_q2");
CHECK(agent.device_info.version == "01.02.03");
}
TEST_CASE("Stopping the status stream gives a silent printer no more than its timeouts", "[MoonrakerPrinterAgent][StatusStream]")
{
// An open websocket gets the 200 ms close timeout, and a stalled object list request up to 1 s of curl progress to see the stop.
const auto [behavior, limit] = GENERATE(table<Behavior, std::chrono::milliseconds>({
{Behavior::SilentBeforeHandshake, 500ms},
{Behavior::Silent, 1000ms},
{Behavior::StallsObjectList, 2000ms},
}));
CAPTURE(static_cast<int>(behavior));
StreamFixture f(behavior);
f.agent.ws_timings.close = 200ms;
f.start();
REQUIRE(f.printer.wait_for_ready_connections(1));
CHECK(f.stops_within(limit));
}
TEST_CASE("A secure status stream opens with a TLS handshake", "[MoonrakerPrinterAgent][StatusStream]")
{
StreamFixture f(Behavior::RejectsTls);
f.start_secure();
REQUIRE(f.printer.wait_for_first_byte());
// 0x16 is a TLS handshake record; a plaintext client would send "GET".
CHECK(f.printer.first_byte() == 0x16);
CHECK(f.stops_within(3s));
}
TEST_CASE("Stopping the status stream sends the printer a close frame", "[MoonrakerPrinterAgent][StatusStream]")
{
// A stop while the stream reads, and one while it waits on the object list request.
const Behavior behavior = GENERATE(Behavior::AnswersPings, Behavior::StallsObjectList);
CAPTURE(static_cast<int>(behavior));
StreamFixture f(behavior);
f.start();
REQUIRE(f.printer.wait_for_ready_connections(1));
CHECK(f.stops_within(3s));
CHECK(f.printer.close_frames() == 1);
}
TEST_CASE("Stopping the status stream does not wait out the reconnect delay", "[MoonrakerPrinterAgent][StatusStream]")
{
StreamFixture f(Behavior::HangsUp);
f.agent.ws_timings.reconnect_delay = 10s;
f.start();
REQUIRE(f.printer.wait_for_ready_connections(1));
// Lets the stream reach its reconnect wait.
std::this_thread::sleep_for(200ms);
CHECK(f.stops_within(3s));
}
TEST_CASE("The status stream reconnects when the printer stops answering", "[MoonrakerPrinterAgent][StatusStream]")
{
// An unanswered close ends at whichever of the handshake and close timeouts is shorter.
const auto [behavior, handshake, close] = GENERATE(table<Behavior, std::chrono::milliseconds, std::chrono::milliseconds>({
{Behavior::Silent, 300ms, 1000ms},
{Behavior::IgnoresClose, 300ms, 1000ms},
{Behavior::IgnoresClose, 1000ms, 200ms},
}));
CAPTURE(static_cast<int>(behavior), handshake.count(), close.count());
StreamFixture f(behavior);
f.agent.ws_timings.handshake = handshake;
f.agent.ws_timings.close = close;
f.agent.ws_timings.idle = 400ms;
f.agent.ws_timings.reconnect_delay = 20ms;
f.start();
CHECK(f.printer.wait_for_ready_connections(2));
CHECK(f.stops_within(3s));
}
TEST_CASE("The status stream reconnects at the shortest delay while the printer answers", "[MoonrakerPrinterAgent][StatusStream]")
{
StreamFixture f(Behavior::HangsUp);
f.agent.ws_timings.reconnect_delay = 50ms;
f.start();
// Twelve 50 ms waits; a backoff that kept doubling would need over 10 s.
CHECK(f.printer.wait_for_ready_connections(12, 3s));
CHECK(f.stops_within(3s));
}
TEST_CASE("The status stream keeps retrying a printer that never answers", "[MoonrakerPrinterAgent][StatusStream]")
{
StreamFixture f(Behavior::HangsUpWithoutAnswer);
f.agent.ws_timings.reconnect_delay = 1ms;
f.start();
// The backoff stops doubling at 32 ms, so 20 attempts take well under the wait.
CHECK(f.printer.wait_for_ready_connections(20));
CHECK(f.stops_within(3s));
}
TEST_CASE("A callback on the stream thread can stop the status stream", "[MoonrakerPrinterAgent][StatusStream]")
{
std::promise<void> stopped;
std::once_flag stopped_once;
StreamFixture f(Behavior::AnswersPings);
f.agent.set_on_local_message_fn([&](std::string, std::string) {
std::call_once(stopped_once, [&] {
f.agent.stop_stream();
stopped.set_value();
});
});
f.start();
REQUIRE(stopped.get_future().wait_for(5s) == std::future_status::ready);
CHECK(f.stops_within(3s));
}
TEST_CASE("Status reports tell the send dialog whether the gcodes storage has free space", "[MoonrakerPrinterAgent][StatusStream]")
{
auto [disk_usage, has_storage] = GENERATE(table<nlohmann::json, bool>({
{{{"total", 8e9}, {"used", 2e9}, {"free", 6e9}}, true},
{{{"total", 8e9}, {"used", 8e9}, {"free", 0}}, false},
{nullptr, false},
}));
CAPTURE(disk_usage.dump());
std::promise<std::string> first_report;
std::once_flag reported;
StreamFixture f(Behavior::AnswersPings);
f.printer.set_gcodes_listing(disk_usage);
f.agent.set_on_local_message_fn([&](std::string, std::string payload) {
std::call_once(reported, [&] { first_report.set_value(payload); });
});
f.start();
auto report = first_report.get_future();
REQUIRE(report.wait_for(5s) == std::future_status::ready);
const nlohmann::json print = nlohmann::json::parse(report.get()).at("print");
// MachineObject reads the storage state from "sdcard" and then again from home_flag bits 8-9.
CHECK(print.at("sdcard") == has_storage);
CHECK(((print.at("home_flag").get<int>() >> 8) & 0x3) == (has_storage ? DevStorage::HAS_SDCARD_NORMAL : DevStorage::NO_SDCARD));
CHECK(f.stops_within(3s));
}
TEST_CASE("The status stream keeps re-reading the printer's free space", "[MoonrakerPrinterAgent][StatusStream]")
{
std::mutex mutex;
std::condition_variable changed;
std::vector<bool> storage_reports;
// Chatters sends a message every 50 ms, which the stream re-reads the free space after
StreamFixture f(Behavior::Chatters);
f.agent.ws_timings.storage_check = 100ms;
f.printer.set_gcodes_listing({{"total", 8e9}, {"used", 8e9}, {"free", 0}});
f.agent.set_on_local_message_fn([&](std::string, std::string payload) {
std::lock_guard<std::mutex> lock(mutex);
storage_reports.push_back(nlohmann::json::parse(payload).at("print").at("sdcard").get<bool>());
changed.notify_all();
});
const auto wait_for_storage = [&](bool has_storage) {
std::unique_lock<std::mutex> lock(mutex);
return changed.wait_for(lock, 5s, [&] { return !storage_reports.empty() && storage_reports.back() == has_storage; });
};
f.start();
REQUIRE(wait_for_storage(false));
f.printer.set_gcodes_listing({{"total", 8e9}, {"used", 2e9}, {"free", 6e9}});
REQUIRE(wait_for_storage(true));
// A listing without disk usage cannot be checked, which counts as no storage
f.printer.set_gcodes_listing(nullptr);
CHECK(wait_for_storage(false));
CHECK(f.stops_within(3s));
}
TEST_CASE("A quiet printer that is still up keeps reporting its status", "[MoonrakerPrinterAgent][StatusStream]")
{
Reports reports;
StreamFixture f(Behavior::AnswersPings);
f.agent.ws_timings.idle = 400ms;
f.agent.ws_timings.heartbeat = 300ms;
reports.attach(f.agent);
f.start();
// The status the printer sent, then two heartbeats.
REQUIRE(reports.wait_for(3));
const auto times = reports.times();
// The steady clock is compared in whole milliseconds, so allow 1 ms of rounding.
CHECK(times[1] - times[0] >= 299ms);
CHECK(times[2] - times[1] >= 299ms);
CHECK(f.stops_within(3s));
}
TEST_CASE("The status stream reports a printer's status over TLS", "[MoonrakerPrinterAgent][StatusStream]")
{
Reports reports;
StreamFixture f(Behavior::AnswersPings, true);
reports.attach(f.agent);
f.start();
CHECK(reports.wait_for(1));
CHECK(f.stops_within(3s));
CHECK(f.printer.close_frames() == 1);
}
TEST_CASE("A configured CA file makes the status stream verify the printer's certificate", "[MoonrakerPrinterAgent][StatusStream]")
{
Reports reports;
StreamFixture f(Behavior::AnswersPings, true);
reports.attach(f.agent);
SECTION("The CA signed the printer's certificate")
{
f.start_with_ca(TEST_CERTIFICATE);
CHECK(reports.wait_for(1));
}
SECTION("Another CA did")
{
f.agent.ws_timings.reconnect_delay = 50ms;
f.start_with_ca(OTHER_CA_CERTIFICATE);
// The printer completes no websocket upgrade, however often the stream retries
CHECK_FALSE(f.printer.wait_for_ready_connections(1, 1500ms));
CHECK(reports.times().empty());
}
CHECK(f.stops_within(3s));
}
TEST_CASE("The status stream sends no heartbeat while the printer has nothing new to report", "[MoonrakerPrinterAgent][StatusStream]")
{
const Behavior behavior = GENERATE(Behavior::Chatters, Behavior::AnswersPingsWithoutStatus);
CAPTURE(static_cast<int>(behavior));
Reports reports;
StreamFixture f(behavior);
f.agent.ws_timings.idle = 400ms;
f.agent.ws_timings.heartbeat = 800ms;
reports.attach(f.agent);
f.start();
REQUIRE(f.printer.wait_for_ready_connections(1));
// The chattering printer's status, and nothing from the one that never sent a status.
const std::size_t expected = behavior == Behavior::Chatters ? 1 : 0;
std::this_thread::sleep_for(1500ms);
CHECK(reports.times().size() == expected);
CHECK(f.stops_within(3s));
}
TEST_CASE("A disconnect during the connection queries keeps the status stream from starting", "[MoonrakerPrinterAgent][StatusStream]")
{
StreamFixture f(Behavior::StallsHttp);
f.agent.connect_printer(connection_to(f.printer));
REQUIRE(f.printer.wait_for_http_requests(1));
// disconnect_printer joins the worker running the stalled query, so it is called aside and the
// printer is released to let that query return.
auto disconnecting = std::async(std::launch::async, [&] { f.agent.disconnect_printer(); });
f.printer.release();
// The query has returned once the printer hangs up on it, and a stream would connect right after.
REQUIRE(f.printer.wait_for_http_hang_ups(1));
CHECK_FALSE(f.printer.wait_for_ready_connections(1, 500ms));
disconnecting.wait();
}
TEST_CASE("Destroying the agent waits for the connection threads it superseded", "[MoonrakerPrinterAgent][Connect]")
{
FakePrinter printer(Behavior::StallsHttp);
const auto fetches_returned = std::make_shared<std::atomic<int>>(0);
auto agent = std::make_unique<ConnectAgent>("");
agent->fetches_returned = fetches_returned;
agent->connect_printer(connection_to(printer));
REQUIRE(printer.wait_for_http_requests(1));
agent->connect_printer(connection_to(printer));
REQUIRE(printer.wait_for_http_requests(2));
agent->disconnect_printer();
// The stalled queries see the change within 1 s of curl progress.
CHECK(destroys_within(agent, 2s));
CHECK(*fetches_returned == 2);
}
TEST_CASE("Destroying the agent stops a stalled status query", "[MoonrakerPrinterAgent][Connect]")
{
FakePrinter printer(Behavior::StallsHttp);
auto agent = std::make_unique<StreamAgent>("");
agent->connect_printer(connection_to(printer));
REQUIRE(printer.wait_for_http_requests(1));
// The stalled query sees the change within 1 s of curl progress.
CHECK(destroys_within(agent, 2s));
}
TEST_CASE("A connection the printer refuses at first is retried", "[MoonrakerPrinterAgent][Connect]")
{
FakePrinter printer(Behavior::AnswersPings);
RefusedAgent agent("");
agent.refusals = 2;
agent.ws_timings.reconnect_delay = 10ms;
agent.connect_printer(connection_to(printer));
// The status stream starts only once a device info query succeeds.
CHECK(printer.wait_for_ready_connections(1));
CHECK(agent.fetches == 3);
printer.release();
}
TEST_CASE("Destroying the agent does not wait out a connection retry's delay", "[MoonrakerPrinterAgent][Connect]")
{
FakePrinter printer(Behavior::AnswersPings);
auto agent = std::make_unique<RefusedAgent>("");
agent->refusals = 1;
agent->ws_timings.reconnect_delay = 10s;
agent->connect_printer(connection_to(printer));
const auto deadline = std::chrono::steady_clock::now() + 5s;
while (agent->fetches == 0 && std::chrono::steady_clock::now() < deadline)
std::this_thread::sleep_for(10ms);
REQUIRE(agent->fetches == 1);
CHECK(destroys_within(agent, 1s));
}
TEST_CASE("A .gcode.3mf goes to the printer as the printed plate's G-code unless the printer takes 3MF", "[MoonrakerPrinterAgent][Upload]")
{
const bool takes_3mf = GENERATE(false, true);
CAPTURE(takes_3mf);
const std::string name = takes_3mf ? "Cube.gcode.3mf" : "Cube.gcode";
ScopedTemporaryDir dir("moonraker-upload");
const std::string bundle = (dir.path() / ".1234.0.3mf").string();
write_bundle(bundle, {"1", "2"});
ScopedSystemTemp temp;
REQUIRE(boost::filesystem::equivalent(boost::filesystem::temp_directory_path(), temp.path()));
FakePrinter printer(Behavior::AnswersPings);
UploadAgent agent(printer);
agent.takes_3mf = takes_3mf;
PrintParams params{};
params.filename = bundle;
params.plate_index = 2;
SECTION("Print plate")
{
params.project_name = "Cube";
REQUIRE(agent.start_local_print(params, nullptr, nullptr) == BAMBU_NETWORK_SUCCESS);
CHECK(agent.printed == name);
}
SECTION("Send to printer")
{
params.project_name = "Cube.gcode.3mf";
REQUIRE(agent.start_send_gcode_to_sdcard(params, nullptr, nullptr, nullptr) == BAMBU_NETWORK_SUCCESS);
CHECK(agent.printed.empty());
}
const std::string upload = printer.upload();
CHECK(upload.find("filename=\"" + name + "\"") != std::string::npos);
if (takes_3mf) {
CHECK(upload.find("PK\x03\x04") != std::string::npos);
} else {
CHECK(upload.find("; plate 2") != std::string::npos);
CHECK(upload.find("; plate 1") == std::string::npos);
}
// Neither the source folder nor the temp folder keeps an extracted copy
CHECK(std::distance(boost::filesystem::directory_iterator(dir.path()), boost::filesystem::directory_iterator()) == 1);
CHECK(boost::filesystem::is_empty(temp.path()));
}
TEST_CASE("A bundle without the printed plate's G-code is uploaded only when it holds a single plate", "[MoonrakerPrinterAgent][Upload]")
{
auto [plates, uploaded] = GENERATE(table<std::vector<std::string>, bool>({
{{"1"}, true},
{{"1", "3"}, false},
}));
CAPTURE(plates.size());
ScopedTemporaryDir dir("moonraker-upload");
const std::string bundle = (dir.path() / ".1234.0.3mf").string();
write_bundle(bundle, plates);
FakePrinter printer(Behavior::AnswersPings);
UploadAgent agent(printer);
PrintParams params{};
params.filename = bundle;
params.plate_index = 2;
params.project_name = "Cube";
const int result = agent.start_local_print(params, nullptr, nullptr);
CHECK((result == BAMBU_NETWORK_SUCCESS) == uploaded);
CHECK((printer.upload().find("; plate 1") != std::string::npos) == uploaded);
}
TEST_CASE("The job name reaches the print start as a file name the printer can store and list", "[MoonrakerPrinterAgent][Upload]")
{
// A long name is cut to fit 255 bytes with room for .gcode.3mf, at a whole 2-byte character
std::string long_name;
for (int i = 0; i < 150; ++i)
long_name += "\xC3\xA9";
const std::size_t max_stem = 255 - std::strlen(".gcode.3mf");
const std::string cut_long_name = long_name.substr(0, max_stem - max_stem % 2);
auto [job_name, file_name] = GENERATE_COPY(table<std::string, std::string>({
{"Cable clip #2; v\"3\"*", "Cable clip _2_ v_3__.gcode"},
{"Bracket 1/2", "Bracket 1_2.gcode"},
{"A: frame", "A: frame.gcode"},
{"Bob's", "Bob's.gcode"},
{"Bob's \"best\" frame (v2) [final] {\xC3\xA9} <x> |y| ?!&%$@=+,~`", "Bob's _best_ frame (v2) [final] {\xC3\xA9} <x> |y| ?!&%$@=+,~`.gcode"},
{"x\nM104 S300", "x_M104 S300.gcode"},
{" .Cube", "Cube.gcode"},
{"", "1234.0.gcode"},
{long_name, cut_long_name + ".gcode"},
}));
CAPTURE(job_name);
ScopedTemporaryDir dir("moonraker-upload");
const std::string bundle = (dir.path() / ".1234.0.3mf").string();
write_bundle(bundle, {"1"});
FakePrinter printer(Behavior::AnswersPings);
UploadAgent agent(printer);
PrintParams params{};
params.filename = bundle;
params.plate_index = 1;
params.project_name = job_name;
REQUIRE(agent.start_local_print(params, nullptr, nullptr) == BAMBU_NETWORK_SUCCESS);
CHECK(printer.upload().find("filename=\"" + file_name + "\"") != std::string::npos);
CHECK(printer.script() == "SDCARD_PRINT_FILE FILENAME=\"" + file_name + "\"");
}
TEST_CASE("A file that is not a 3MF is uploaded as it is, named after the job when there is one", "[MoonrakerPrinterAgent][Upload]")
{
auto [job_name, file_name] = GENERATE(table<std::string, std::string>({
{"Cube", "Cube.gcode"},
{"", "part.gcode"},
}));
CAPTURE(job_name);
ScopedTemporaryDir dir("moonraker-upload");
const boost::filesystem::path gcode = dir.path() / "part.gcode";
boost::filesystem::ofstream(gcode, std::ios::binary) << "; plain G-code\n";
FakePrinter printer(Behavior::AnswersPings);
UploadAgent agent(printer);
PrintParams params{};
params.filename = gcode.string();
params.project_name = job_name;
SECTION("Print plate")
{
REQUIRE(agent.start_local_print(params, nullptr, nullptr) == BAMBU_NETWORK_SUCCESS);
CHECK(agent.printed == file_name);
}
SECTION("Send to printer")
{
REQUIRE(agent.start_send_gcode_to_sdcard(params, nullptr, nullptr, nullptr) == BAMBU_NETWORK_SUCCESS);
}
const std::string upload = printer.upload();
CHECK(upload.find("filename=\"" + file_name + "\"") != std::string::npos);
CHECK(upload.find("; plain G-code") != std::string::npos);
CHECK(std::distance(boost::filesystem::directory_iterator(dir.path()), boost::filesystem::directory_iterator()) == 1);
}