mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-12-02 11:21:56 +00:00
* Fix six defects in the Orca printer agent's command path The requests the agent sends for print.gcode_file and files.* took their sequence_id from a counter seeded with system_clock nanoseconds, which is 19 digits. The Cloud relay accepts 1 to 16 digits and drops the rest without an error, so a print started from the Storage tab on a cloud-reached printer simply did nothing. They now draw from PrinterSequenceIds, the process-wide counter that stays inside the 20000-29999 band the protocol reserves for this app. OrcaFilesPanel hardcoded connection_type = "lan", so a file printed from the printer's own storage went over the LAN even when the printer was reached through the Cloud, and start_sdcard_print would publish into whichever LAN session happened to be open. The panel now passes the printer's real transport. route_send also refuses a LAN send whose dev_id is not the live session's, instead of publishing it into another printer's request topic, and holds the connection across the send so a racing disconnect cannot free it. command_set_nozzle always sent extruder_index 0. The call carries no tool index and cannot infer one, so it is now refused when the capability manifest reports more than one toolhead. An unknown count keeps the old behaviour, which is right for the single-tool case it was written for. command_ams_refresh_rfid discarded ams_id. The wire field tray_id is the flat index 4 * ams_id + slot_id; it is now encoded, and a negative slot is refused even when the caller passes an already-flat index, since the printer only checks the result for being non-negative. command_set_bed ignored whether the printer accepts set_bed_temp, and always sent it. It now follows the advertised capability and falls back to M140 when a manifest was received that omits it. The Bambu fun-flags bit that used to be the only signal is never set by an Orca printer, so honouring it would have broken bed heating. bind() and request_bind_ticket() sat on the interface defaults, which return success with an empty ticket, so the sign-in URL went out ending in &ticket=. OrcaSonar is paired on the printer itself, so both now report failure and the caller uses its plain URL. Tests: six cases in tests/slic3rutils/test_orca_printer_agent.cpp, one per fix, covering the request number's band and length, the LAN refusal, the flat RFID index and its negative-slot guard, both bed paths, the multi-tool nozzle refusal, and the binding failures. Not built or run in this environment. * Drop the Orca agent's nozzle override instead of guarding it The previous commit guarded command_set_nozzle by refusing when the capability manifest reported more than one toolhead. That was treating the symptom: the call carries no tool index, but it also does not need one. The interface default already builds print.gcode_line with M104, and OrcaSonar runs that line verbatim (adapter.go, print.gcode_line), so it works with no override at all. From the GUI the call is reachable only on a single-tool printer: StatusPanel.cpp:4903 sends it only for a field typed TEMP_OF_NORMAL_TYPE, which update_temp_ctrl sets in its nozzle_num == 1 branch alone. Two-tool and N-tool printers go through command_set_nozzle_new with an explicit tool. So the override never fired, and the guard protected nothing. Removing it also removes what existed only for it: the tool_count field, the topology loop that counted tools, and the test that covered the refusal. The topology tools array is still parsed for the nozzle-diameter cache. Supersedes the third fix of the previous commit.
906 lines
45 KiB
C++
906 lines
45 KiB
C++
#include <catch2/catch_test_macros.hpp>
|
|
#include <catch2/matchers/catch_matchers_floating_point.hpp>
|
|
#include <cstddef>
|
|
#include <libslic3r/ProjectTask.hpp>
|
|
#include <slic3r/GUI/FilamentMappingUtils.hpp>
|
|
#include <slic3r/Utils/IPrinterAgent.hpp>
|
|
#include <slic3r/Utils/OrcaCloudServiceAgent.hpp>
|
|
#include <slic3r/Utils/OrcaPrinterAgent.hpp>
|
|
|
|
#include "catch2/matchers/catch_matchers.hpp"
|
|
#include "orca_mqtt_mock_broker.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <chrono>
|
|
#include <condition_variable>
|
|
#include <cstdint>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <slic3r/Utils/bambu_networking.hpp>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
using Slic3r::OrcaPrinterAgent;
|
|
|
|
namespace {
|
|
// Probe exposes the protected internals the tests drive.
|
|
struct Probe : OrcaPrinterAgent {
|
|
using OrcaPrinterAgent::OrcaPrinterAgent;
|
|
using OrcaPrinterAgent::deliver_to_sink;
|
|
using OrcaPrinterAgent::parse_lan_endpoint;
|
|
using OrcaPrinterAgent::make_lan_client_id;
|
|
using OrcaPrinterAgent::lan_connection_target;
|
|
using OrcaPrinterAgent::resolve_upload_origin;
|
|
using OrcaPrinterAgent::build_filament_mapping;
|
|
using OrcaPrinterAgent::build_gcode_file_payload;
|
|
using OrcaPrinterAgent::prepare_outgoing_request;
|
|
using OrcaPrinterAgent::parse_files_list_reply;
|
|
using OrcaPrinterAgent::parse_files_space_reply;
|
|
using OrcaPrinterAgent::parse_files_metadata_reply;
|
|
using OrcaPrinterAgent::parse_files_thumbnail_reply;
|
|
using OrcaPrinterAgent::try_consume_files_reply;
|
|
};
|
|
}
|
|
|
|
TEST_CASE("OrcaPrinterAgent forwards a status payload to on_message_fn", "[OrcaPrinterAgent]") {
|
|
Probe agent("/tmp");
|
|
std::string got_id, got_payload;
|
|
agent.set_on_message_fn([&](std::string id, std::string p){ got_id = std::move(id); got_payload = std::move(p); });
|
|
agent.deliver_to_sink("dev-1", R"({"print":{"command":"push_status"}})", /*local=*/false);
|
|
CHECK(got_id == "dev-1");
|
|
CHECK(got_payload.find("push_status") != std::string::npos);
|
|
}
|
|
|
|
TEST_CASE("OrcaPrinterAgent stamps the get_capabilities nozzle diameter onto push_status frames", "[OrcaPrinterAgent]") {
|
|
Probe agent("/tmp");
|
|
std::string last_payload;
|
|
agent.set_on_message_fn([&](std::string, std::string p){ last_payload = std::move(p); });
|
|
|
|
// Before any capabilities reply, a push_status frame is forwarded untouched.
|
|
agent.deliver_to_sink("dev-1", R"({"print":{"command":"push_status","mc_percent":10}})", /*local=*/false);
|
|
CHECK(last_payload.find("nozzle_diameter") == std::string::npos);
|
|
|
|
// The get_capabilities reply is forwarded verbatim; its topology nozzle diameter
|
|
// is cached for the device.
|
|
agent.deliver_to_sink(
|
|
"dev-1",
|
|
R"({"info":{"command":"get_capabilities","capabilities":{"topology":{"tools":[{"id":"T0","nozzle":{"diameter_mm":0.4}}]}}}})",
|
|
/*local=*/false);
|
|
CHECK(last_payload.find("\"command\":\"get_capabilities\"") != std::string::npos);
|
|
CHECK(last_payload.find("\"print\"") == std::string::npos);
|
|
|
|
// Later push_status frames for that device get the cached diameter plus a neutral
|
|
// nozzle_type, so MachineObject::parse_json's legacy nozzle parser can run.
|
|
agent.deliver_to_sink("dev-1", R"({"print":{"command":"push_status","mc_percent":20}})", /*local=*/false);
|
|
CHECK(last_payload.find("\"nozzle_diameter\":0.4") != std::string::npos);
|
|
CHECK(last_payload.find("\"nozzle_type\":\"N/A\"") != std::string::npos);
|
|
|
|
// A different device is unaffected.
|
|
agent.deliver_to_sink("dev-2", R"({"print":{"command":"push_status"}})", /*local=*/false);
|
|
CHECK(last_payload.find("nozzle_diameter") == std::string::npos);
|
|
|
|
// A frame that already carries real nozzle data is not overridden.
|
|
agent.deliver_to_sink("dev-1", R"({"print":{"command":"push_status","nozzle_diameter":0.6}})", /*local=*/false);
|
|
CHECK(last_payload.find("\"nozzle_diameter\":0.6") != std::string::npos);
|
|
CHECK(last_payload.find("N/A") == std::string::npos);
|
|
}
|
|
|
|
TEST_CASE("OrcaPrinterAgent owns connector capabilities and fails closed for AMS commands", "[OrcaPrinterAgent]") {
|
|
Probe agent("/tmp");
|
|
agent.deliver_to_sink("orca-caps", R"({
|
|
"info": {
|
|
"command": "get_capabilities",
|
|
"supported_features": {"fms": true, "filament_slots": true, "filament_mapping": true},
|
|
"supported_commands": ["print.ams_get_rfid"],
|
|
"capabilities": {
|
|
"protocol": {
|
|
"features": {"filament_mapping": true},
|
|
"supported_commands": ["print.ams_change_filament"]
|
|
}
|
|
}
|
|
}
|
|
})", false);
|
|
|
|
CHECK(agent.supports_feature("orca-caps", "filament_mapping"));
|
|
CHECK(agent.supports_command("orca-caps", "print.ams_filament_setting"));
|
|
CHECK(agent.supports_command("orca-caps", "print.ams_change_filament"));
|
|
CHECK(agent.supports_command("orca-caps", "print.ams_get_rfid"));
|
|
CHECK_FALSE(agent.supports_command("orca-caps", "print.ams_control"));
|
|
|
|
agent.deliver_to_sink("orca-caps", R"({
|
|
"info": {
|
|
"command": "get_capabilities",
|
|
"supported_features": {"fms": false, "filament_slots": false, "filament_mapping": false},
|
|
"supported_commands": ["print.ams_control"],
|
|
"capabilities": {"protocol": {"features": {}, "supported_commands": []}}
|
|
}
|
|
})", false);
|
|
|
|
CHECK_FALSE(agent.supports_feature("orca-caps", "filament_mapping"));
|
|
CHECK_FALSE(agent.supports_command("orca-caps", "print.ams_filament_setting"));
|
|
CHECK_FALSE(agent.supports_command("orca-caps", "print.ams_change_filament"));
|
|
CHECK_FALSE(agent.supports_command("orca-caps", "print.ams_control"));
|
|
CHECK_FALSE(agent.supports_command("orca-caps", "print.ams_calibrate"));
|
|
CHECK(agent.supports_command("orca-caps", "print.gcode_file"));
|
|
}
|
|
|
|
TEST_CASE("OrcaPrinterAgent clears cached capabilities when a device is unbound", "[OrcaPrinterAgent]") {
|
|
Probe agent("/tmp");
|
|
agent.deliver_to_sink("orca-forget", R"({
|
|
"info": {
|
|
"command": "get_capabilities",
|
|
"supported_features": {"filament_slots": true},
|
|
"supported_commands": [],
|
|
"capabilities": {"protocol": {"features": {}, "supported_commands": []}}
|
|
}
|
|
})", false);
|
|
REQUIRE(agent.supports_command("orca-forget", "print.ams_filament_setting"));
|
|
|
|
agent.unbind("orca-forget");
|
|
CHECK_FALSE(agent.supports_command("orca-forget", "print.ams_filament_setting"));
|
|
}
|
|
|
|
TEST_CASE("OrcaPrinterAgent removes setting_id from AMS metadata and gates the request", "[OrcaPrinterAgent]") {
|
|
Probe agent("/tmp");
|
|
agent.deliver_to_sink("orca-ams-write", R"({
|
|
"info": {
|
|
"command": "get_capabilities",
|
|
"supported_features": {"fms": false, "filament_slots": true},
|
|
"supported_commands": [],
|
|
"capabilities": {"protocol": {"features": {"filament_slots": true}, "supported_commands": []}}
|
|
}
|
|
})", false);
|
|
|
|
const std::string request = R"({"print":{"command":"ams_filament_setting","sequence_id":"9","tray_info_idx":"GFL99","setting_id":"preset-setting"}})";
|
|
std::string command;
|
|
std::string prepared;
|
|
CHECK(agent.prepare_outgoing_request("orca-ams-write", request, command, prepared) == BAMBU_NETWORK_SUCCESS);
|
|
CHECK(command == "print.ams_filament_setting");
|
|
const nlohmann::json parsed = nlohmann::json::parse(prepared);
|
|
CHECK_FALSE(parsed["print"].contains("setting_id"));
|
|
CHECK(parsed["print"]["tray_info_idx"] == "GFL99");
|
|
|
|
agent.deliver_to_sink("orca-ams-write", R"({
|
|
"info": {
|
|
"command": "get_capabilities",
|
|
"supported_features": {"filament_slots": false},
|
|
"capabilities": {"protocol": {"features": {"filament_slots": false}}}
|
|
}
|
|
})", false);
|
|
CHECK(agent.prepare_outgoing_request("orca-ams-write", request, command, prepared) == ORCA_NETWORK_ERR_CMD_NOT_SUPPORTED);
|
|
}
|
|
|
|
TEST_CASE("OrcaPrinterAgent::parse_lan_endpoint", "[OrcaPrinterAgent]") {
|
|
std::string h, p;
|
|
REQUIRE(Probe::parse_lan_endpoint("192.168.1.9", h, p));
|
|
CHECK(h == "192.168.1.9"); CHECK(p == "8280");
|
|
REQUIRE(Probe::parse_lan_endpoint("http://host.local:9000/x", h, p));
|
|
CHECK(h == "host.local"); CHECK(p == "9000");
|
|
CHECK_FALSE(Probe::parse_lan_endpoint("", h, p));
|
|
}
|
|
|
|
TEST_CASE("OrcaPrinterAgent::parse_lan_endpoint distinguishes a default port from an explicit one", "[OrcaPrinterAgent]") {
|
|
std::string h, p;
|
|
bool explicit_port = false;
|
|
REQUIRE(Probe::parse_lan_endpoint("192.168.1.9", h, p, &explicit_port));
|
|
CHECK_FALSE(explicit_port);
|
|
CHECK(p == "8280");
|
|
|
|
explicit_port = false;
|
|
REQUIRE(Probe::parse_lan_endpoint("host.local:9000", h, p, &explicit_port));
|
|
CHECK(explicit_port);
|
|
CHECK(p == "9000");
|
|
}
|
|
|
|
TEST_CASE("OrcaPrinterAgent::parse_lan_endpoint handles bracketed IPv6 literals", "[OrcaPrinterAgent]") {
|
|
std::string h, p;
|
|
bool explicit_port = false;
|
|
|
|
REQUIRE(Probe::parse_lan_endpoint("[::1]", h, p, &explicit_port));
|
|
CHECK(h == "[::1]");
|
|
CHECK(p == "8280");
|
|
CHECK_FALSE(explicit_port);
|
|
|
|
explicit_port = false;
|
|
REQUIRE(Probe::parse_lan_endpoint("[::1]:9000", h, p, &explicit_port));
|
|
CHECK(h == "[::1]");
|
|
CHECK(p == "9000");
|
|
CHECK(explicit_port);
|
|
|
|
explicit_port = false;
|
|
REQUIRE(Probe::parse_lan_endpoint("https://[2001:db8::1]:8123/mqtt", h, p, &explicit_port));
|
|
CHECK(h == "[2001:db8::1]");
|
|
CHECK(p == "8123");
|
|
CHECK(explicit_port);
|
|
|
|
CHECK_FALSE(Probe::parse_lan_endpoint("[::1", h, p));
|
|
CHECK_FALSE(Probe::parse_lan_endpoint("[::1]garbage", h, p));
|
|
}
|
|
|
|
TEST_CASE("OrcaPrinterAgent::resolve_upload_origin targets the requested printer", "[OrcaPrinterAgent]") {
|
|
std::string origin;
|
|
|
|
// A job with its own dev_ip uses that host and the default 8280 when no port is given.
|
|
REQUIRE(Probe::resolve_upload_origin("192.168.1.20", false, "", "", "dev-1", origin));
|
|
CHECK(origin == "http://192.168.1.20:8280");
|
|
|
|
// An embedded port and an explicit scheme are preserved.
|
|
REQUIRE(Probe::resolve_upload_origin("printer.local:9000", false, "", "", "dev-1", origin));
|
|
CHECK(origin == "http://printer.local:9000");
|
|
REQUIRE(Probe::resolve_upload_origin("https://printer.local", false, "", "", "dev-1", origin));
|
|
CHECK(origin == "https://printer.local:8280");
|
|
REQUIRE(Probe::resolve_upload_origin("printer.local", true, "", "", "dev-1", origin));
|
|
CHECK(origin == "https://printer.local:8280");
|
|
REQUIRE(Probe::resolve_upload_origin("http://printer.local", true, "", "", "dev-1", origin));
|
|
CHECK(origin == "http://printer.local:8280");
|
|
|
|
// A bracketed IPv6 host keeps its brackets and never gains a second port.
|
|
REQUIRE(Probe::resolve_upload_origin("[::1]:9000", false, "", "", "dev-1", origin));
|
|
CHECK(origin == "http://[::1]:9000");
|
|
REQUIRE(Probe::resolve_upload_origin("[::1]", false, "dev-1", "wss://[::1]:8123/mqtt", "dev-1", origin));
|
|
CHECK(origin == "https://[::1]:8123");
|
|
|
|
// A bare host takes the live session's actual port when that session is the
|
|
// requested printer.
|
|
REQUIRE(Probe::resolve_upload_origin("printer.local", false, "dev-1", "ws://printer.local:8123/mqtt", "dev-1", origin));
|
|
CHECK(origin == "http://printer.local:8123");
|
|
|
|
// A matching session supplies the scheme too: TLS stays TLS even when the
|
|
// pre-connect/flag value is false, and plain stays plain when it is true.
|
|
REQUIRE(Probe::resolve_upload_origin("printer.local", false, "dev-1", "wss://printer.local:8123/mqtt", "dev-1", origin));
|
|
CHECK(origin == "https://printer.local:8123");
|
|
REQUIRE(Probe::resolve_upload_origin("printer.local", true, "dev-1", "ws://printer.local:8123/mqtt", "dev-1", origin));
|
|
CHECK(origin == "http://printer.local:8123");
|
|
|
|
// An explicit target may upgrade a plain session, but must not downgrade a
|
|
// matching TLS session: that would send the access code in clear.
|
|
CHECK_FALSE(Probe::resolve_upload_origin("http://printer.local", false, "dev-1", "wss://printer.local:8123/mqtt", "dev-1", origin));
|
|
CHECK_FALSE(Probe::resolve_upload_origin("http://printer.local:9000", false, "dev-1", "wss://printer.local:8123/mqtt", "dev-1", origin));
|
|
REQUIRE(Probe::resolve_upload_origin("https://printer.local", false, "dev-1", "ws://printer.local:8123/mqtt", "dev-1", origin));
|
|
CHECK(origin == "https://printer.local:8123");
|
|
REQUIRE(Probe::resolve_upload_origin("http://printer.local", false, "dev-1", "ws://printer.local:8123/mqtt", "dev-1", origin));
|
|
CHECK(origin == "http://printer.local:8123");
|
|
|
|
// A different printer's TLS session does not constrain an explicit target.
|
|
REQUIRE(Probe::resolve_upload_origin("http://printer.local", false, "dev-2", "wss://printer.local:8123/mqtt", "dev-1", origin));
|
|
CHECK(origin == "http://printer.local:8280");
|
|
|
|
// A session that belongs to another printer must not supply its port, host or
|
|
// scheme: the pre-connect MQTT flag is the only TLS source left.
|
|
REQUIRE(Probe::resolve_upload_origin("printer.local", false, "dev-2", "wss://printer.local:8123/mqtt", "dev-1", origin));
|
|
CHECK(origin == "http://printer.local:8280");
|
|
REQUIRE(Probe::resolve_upload_origin("printer.local", true, "dev-2", "ws://printer.local:8123/mqtt", "dev-1", origin));
|
|
CHECK(origin == "https://printer.local:8280");
|
|
|
|
// Without a dev_ip, only a session for the requested printer is usable.
|
|
REQUIRE(Probe::resolve_upload_origin("", false, "dev-1", "wss://printer.local:8123/mqtt", "dev-1", origin));
|
|
CHECK(origin == "https://printer.local:8123");
|
|
CHECK_FALSE(Probe::resolve_upload_origin("", false, "dev-2", "ws://printer.local:8123/mqtt", "dev-1", origin));
|
|
CHECK_FALSE(Probe::resolve_upload_origin("", false, "", "", "dev-1", origin));
|
|
}
|
|
|
|
TEST_CASE("OrcaPrinterAgent::parse_files_space_reply reads a 64-bit free_bytes", "[OrcaPrinterAgent]") {
|
|
std::uint64_t free_bytes = 0;
|
|
|
|
REQUIRE(Probe::parse_files_space_reply(R"({"free_bytes": 123456})", free_bytes));
|
|
CHECK(free_bytes == 123456u);
|
|
REQUIRE(Probe::parse_files_space_reply(R"({"free_bytes": 5000000000})", free_bytes));
|
|
CHECK(free_bytes == 5000000000ull);
|
|
|
|
CHECK_FALSE(Probe::parse_files_space_reply("", free_bytes));
|
|
CHECK_FALSE(Probe::parse_files_space_reply("not json", free_bytes));
|
|
CHECK_FALSE(Probe::parse_files_space_reply("{}", free_bytes));
|
|
CHECK_FALSE(Probe::parse_files_space_reply(R"({"free_bytes": -1})", free_bytes));
|
|
CHECK_FALSE(Probe::parse_files_space_reply(R"({"free_bytes": "123"})", free_bytes));
|
|
CHECK_FALSE(Probe::parse_files_space_reply(R"({"free_bytes": 1.5})", free_bytes));
|
|
CHECK_FALSE(Probe::parse_files_space_reply("[]", free_bytes));
|
|
}
|
|
|
|
TEST_CASE("OrcaPrinterAgent::make_lan_client_id is stable and prefixed", "[OrcaPrinterAgent]") {
|
|
const auto a = Probe::make_lan_client_id("dev-1");
|
|
const auto b = Probe::make_lan_client_id("dev-1");
|
|
CHECK(a == b); // drawn once per process
|
|
CHECK(a.rfind("orcaslicer-lan-dev-1-", 0) == 0);
|
|
}
|
|
|
|
TEST_CASE("OrcaPrinterAgent::parse_files_list_reply normalizes the MQTT entry fields", "[OrcaPrinterAgent]") {
|
|
const std::vector<Slic3r::PrinterFileEntry> files = Probe::parse_files_list_reply(R"({
|
|
"files": {
|
|
"command": "list",
|
|
"sequence_id": "70001",
|
|
"result": "success",
|
|
"entries": [
|
|
{"name": "foo.gcode", "path": "sub/foo.gcode", "is_dir": false, "size": 1234, "modified": 1700000000.75},
|
|
{"name": "bar.gcode", "path": "bar.gcode", "is_dir": false, "size": 7, "modified": 42}
|
|
]
|
|
}
|
|
})");
|
|
REQUIRE(files.size() == 2);
|
|
CHECK(files[0].path == "sub/foo.gcode");
|
|
CHECK(files[0].name == "foo.gcode");
|
|
CHECK(files[0].size == 1234);
|
|
CHECK(files[0].modified == 1700000000); // fractional seconds truncate
|
|
CHECK(files[1].path == "bar.gcode");
|
|
CHECK(files[1].name == "bar.gcode");
|
|
CHECK(files[1].size == 7);
|
|
CHECK(files[1].modified == 42);
|
|
}
|
|
|
|
TEST_CASE("OrcaPrinterAgent::parse_files_list_reply falls back to the name when path is absent", "[OrcaPrinterAgent]") {
|
|
const std::vector<Slic3r::PrinterFileEntry> files =
|
|
Probe::parse_files_list_reply(R"({"files": {"result": "success", "entries": [{"name": "solo.gcode"}]}})");
|
|
REQUIRE(files.size() == 1);
|
|
CHECK(files[0].path == "solo.gcode");
|
|
CHECK(files[0].name == "solo.gcode");
|
|
CHECK(files[0].size == 0);
|
|
CHECK(files[0].modified == 0);
|
|
}
|
|
|
|
TEST_CASE("OrcaPrinterAgent::parse_files_list_reply skips directories and unusable entries", "[OrcaPrinterAgent]") {
|
|
const std::vector<Slic3r::PrinterFileEntry> files = Probe::parse_files_list_reply(R"({
|
|
"files": {
|
|
"result": "success",
|
|
"entries": [
|
|
{"name": "sub", "path": "sub", "is_dir": true},
|
|
{"path": "no-name.gcode"},
|
|
{"name": ""},
|
|
"not-an-object",
|
|
{"name": "kept.gcode", "path": "kept.gcode"}
|
|
]
|
|
}
|
|
})");
|
|
REQUIRE(files.size() == 1);
|
|
CHECK(files[0].name == "kept.gcode");
|
|
CHECK(files[0].path == "kept.gcode");
|
|
}
|
|
|
|
TEST_CASE("OrcaPrinterAgent::parse_files_list_reply rejects a malformed or empty reply", "[OrcaPrinterAgent]") {
|
|
CHECK(Probe::parse_files_list_reply("not json").empty());
|
|
CHECK(Probe::parse_files_list_reply(R"({"result": []})").empty());
|
|
CHECK(Probe::parse_files_list_reply(R"({"files": {"entries": "nope"}})").empty());
|
|
}
|
|
|
|
TEST_CASE("OrcaPrinterAgent::parse_files_list_reply tolerates a non-boolean is_dir", "[OrcaPrinterAgent]") {
|
|
const std::vector<Slic3r::PrinterFileEntry> files =
|
|
Probe::parse_files_list_reply(R"({"files": {"result": "success", "entries": [{"name": "x.gcode", "is_dir": "yes"}]}})");
|
|
REQUIRE(files.size() == 1);
|
|
CHECK(files[0].name == "x.gcode");
|
|
}
|
|
|
|
TEST_CASE("OrcaPrinterAgent::parse_files_thumbnail_reply decodes the requested path's image and ignores others", "[OrcaPrinterAgent]") {
|
|
const std::string payload = R"({
|
|
"files": {
|
|
"command": "thumbnails",
|
|
"sequence_id": "1",
|
|
"result": "success",
|
|
"root": "gcodes",
|
|
"items": [
|
|
{"path": "a.gcode", "state": "none"},
|
|
{"path": "b.gcode", "content_type": "image/png", "data": "aGk="}
|
|
]
|
|
}
|
|
})";
|
|
CHECK(Probe::parse_files_thumbnail_reply(payload, "b.gcode") == "hi"); // aGk= is base64 "hi"
|
|
CHECK(Probe::parse_files_thumbnail_reply(payload, "a.gcode").empty()); // state item carries no image
|
|
CHECK(Probe::parse_files_thumbnail_reply(payload, "missing.gcode").empty());
|
|
}
|
|
|
|
TEST_CASE("OrcaPrinterAgent::parse_files_thumbnail_reply is empty for malformed or dataless replies", "[OrcaPrinterAgent]") {
|
|
CHECK(Probe::parse_files_thumbnail_reply("not json", "x.gcode").empty());
|
|
CHECK(Probe::parse_files_thumbnail_reply(R"({"files":{"items":"nope"}})", "x.gcode").empty());
|
|
CHECK(Probe::parse_files_thumbnail_reply(
|
|
R"({"files":{"items":[{"path": "x.gcode", "state": "too_large"}]}})", "x.gcode").empty());
|
|
}
|
|
|
|
TEST_CASE("OrcaPrinterAgent::try_consume_files_reply ignores reports it did not request", "[OrcaPrinterAgent]") {
|
|
Probe agent("/tmp");
|
|
CHECK_FALSE(agent.try_consume_files_reply("dev-1", R"({"print": {"command": "push_status"}})"));
|
|
CHECK_FALSE(agent.try_consume_files_reply("dev-1", R"({"files": {"command": "list"}})")); // no sequence_id
|
|
CHECK_FALSE(agent.try_consume_files_reply("dev-1", R"({"files": {"command": "list", "sequence_id": "999"}})")); // not pending
|
|
}
|
|
|
|
TEST_CASE("OrcaPrinterAgent::parse_files_metadata_reply parses the metadata fields", "[OrcaPrinterAgent]") {
|
|
using Catch::Matchers::WithinAbs;
|
|
const Slic3r::PrinterFileMetadata meta = Probe::parse_files_metadata_reply(R"({
|
|
"files": {
|
|
"command": "metadata",
|
|
"sequence_id": "70002",
|
|
"result": "success",
|
|
"path": "sub/foo.gcode",
|
|
"size": 1234,
|
|
"modified": 1700000000.5,
|
|
"estimated_time": 3725,
|
|
"filament_total": 10500.5,
|
|
"filament_weight_total": 31.6
|
|
}
|
|
})");
|
|
CHECK(meta.estimated_time == 3725);
|
|
CHECK_THAT(meta.filament_total, WithinAbs(10500.5, 1e-9));
|
|
CHECK_THAT(meta.filament_weight, WithinAbs(31.6, 1e-9));
|
|
}
|
|
|
|
TEST_CASE("OrcaPrinterAgent::parse_files_metadata_reply defaults missing fields to zero", "[OrcaPrinterAgent]") {
|
|
using Catch::Matchers::WithinAbs;
|
|
const Slic3r::PrinterFileMetadata meta = Probe::parse_files_metadata_reply(R"({"files": {"path": "foo.gcode"}})");
|
|
CHECK(meta.estimated_time == 0);
|
|
CHECK_THAT(meta.filament_total, WithinAbs(0.0, 1e-12));
|
|
CHECK_THAT(meta.filament_weight, WithinAbs(0.0, 1e-12));
|
|
}
|
|
|
|
TEST_CASE("OrcaPrinterAgent::parse_files_metadata_reply yields zeros for a malformed reply", "[OrcaPrinterAgent]") {
|
|
using Catch::Matchers::WithinAbs;
|
|
const Slic3r::PrinterFileMetadata malformed = Probe::parse_files_metadata_reply("not json");
|
|
CHECK(malformed.estimated_time == 0);
|
|
CHECK_THAT(malformed.filament_total, WithinAbs(0.0, 1e-12));
|
|
CHECK_THAT(malformed.filament_weight, WithinAbs(0.0, 1e-12));
|
|
|
|
const Slic3r::PrinterFileMetadata wrong_type = Probe::parse_files_metadata_reply(R"({"files": "nope"})");
|
|
CHECK(wrong_type.estimated_time == 0);
|
|
CHECK_THAT(wrong_type.filament_total, WithinAbs(0.0, 1e-12));
|
|
}
|
|
|
|
TEST_CASE("filament mapping is keyed by the ams_mapping2 array position", "[OrcaPrinterAgent]") {
|
|
const nlohmann::json mapping = Probe::build_filament_mapping(
|
|
R"([{"ams_id":1,"slot_id":5},{"ams_id":255,"slot_id":255},{"ams_id":255,"slot_id":0}])");
|
|
REQUIRE(mapping.is_array());
|
|
REQUIRE(mapping.size() == 2);
|
|
CHECK(mapping[0]["filament_index"] == 0);
|
|
CHECK(mapping[0]["ams_id"] == 1);
|
|
CHECK(mapping[0]["slot_id"] == 5);
|
|
// The unmatched middle entry is dropped; the third entry keeps index 2.
|
|
CHECK(mapping[1]["filament_index"] == 2);
|
|
CHECK(mapping[1]["ams_id"] == 255);
|
|
CHECK(mapping[1]["slot_id"] == 0);
|
|
}
|
|
|
|
// Index-correlation merge gate (plan PR 3). `ams_mapping2` is built one entry
|
|
// per logical filament, so its array position is the identifier the generated
|
|
// G-code toolchange passes to the Klipper macro (`next_filament_id`). The
|
|
// serializer must key `filament_index` by that position and must never
|
|
// re-densify after dropping unused/sentinel entries, or a used filament would
|
|
// be aimed at the wrong lane. This test covers the plan's matrix: preset order
|
|
// differing from used order, a middle filament unused, and external slots.
|
|
TEST_CASE("filament mapping index correlates with the ams_mapping2 position", "[OrcaPrinterAgent]") {
|
|
// Positions 0..4. Used filaments are 0, 2 and 4; 1 and 3 are unused.
|
|
const nlohmann::json mapping = Probe::build_filament_mapping(
|
|
R"([{"ams_id":0,"slot_id":1},{"ams_id":255,"slot_id":255},{"ams_id":2,"slot_id":3},{"ams_id":255,"slot_id":255},{"ams_id":255,"slot_id":0}])");
|
|
REQUIRE(mapping.size() == 3);
|
|
CHECK(mapping[0]["filament_index"] == 0);
|
|
CHECK(mapping[1]["filament_index"] == 2);
|
|
CHECK(mapping[2]["filament_index"] == 4); // external slot keeps its position
|
|
CHECK(mapping[2]["ams_id"] == 255);
|
|
CHECK(mapping[2]["slot_id"] == 0);
|
|
// No re-densification: a used filament after a dropped sentinel keeps its
|
|
// original logical index.
|
|
for (const auto& entry : mapping)
|
|
CHECK(entry.contains("filament_index"));
|
|
}
|
|
|
|
TEST_CASE("filament mapping is omitted when nothing remains", "[OrcaPrinterAgent]") {
|
|
CHECK(Probe::build_filament_mapping("").empty());
|
|
CHECK(Probe::build_filament_mapping(R"([{"ams_id":255,"slot_id":255}])").empty());
|
|
CHECK(Probe::build_filament_mapping("not json").empty());
|
|
CHECK(Probe::build_filament_mapping(R"({"ams_id":1,"slot_id":0})").empty()); // not an array
|
|
}
|
|
|
|
// The GUI capability gate and the agent serializer must classify the same
|
|
// entries as engaged. External slots ({255,0}/{254,0}) are normalized as-is, so
|
|
// they engage; only the {255,255} unmatched sentinel is dropped. A mismatch lets
|
|
// an entry past the GUI and refused late with a generic publish error.
|
|
TEST_CASE("the GUI mapping gate engages exactly the entries the serializer sends", "[OrcaPrinterAgent]") {
|
|
using Slic3r::GUI::has_engaged_filament_mapping;
|
|
CHECK_FALSE(has_engaged_filament_mapping(""));
|
|
CHECK_FALSE(has_engaged_filament_mapping("[]"));
|
|
CHECK_FALSE(has_engaged_filament_mapping("not json"));
|
|
CHECK_FALSE(has_engaged_filament_mapping(R"([{"ams_id":255,"slot_id":255}])"));
|
|
CHECK(has_engaged_filament_mapping(R"([{"ams_id":255,"slot_id":0}])")); // external main
|
|
CHECK(has_engaged_filament_mapping(R"([{"ams_id":254,"slot_id":0}])")); // external deputy
|
|
CHECK(has_engaged_filament_mapping(R"([{"ams_id":0,"slot_id":0}])")); // box slot
|
|
CHECK(has_engaged_filament_mapping(R"([{"ams_id":255,"slot_id":255},{"ams_id":1,"slot_id":2}])"));
|
|
|
|
for (const char* s : {"", "[]", "not json", R"([{"ams_id":255,"slot_id":255}])",
|
|
R"([{"ams_id":255,"slot_id":0}])", R"([{"ams_id":254,"slot_id":0}])",
|
|
R"([{"ams_id":0,"slot_id":0}])",
|
|
R"([{"ams_id":255,"slot_id":255},{"ams_id":1,"slot_id":2}])"}) {
|
|
CHECK(has_engaged_filament_mapping(s) == !Probe::build_filament_mapping(s).empty());
|
|
}
|
|
}
|
|
|
|
// The used-unmapped refusal reads m_ams_mapping_result: an entry with no target carries
|
|
// empty ams_id/slot_id, while an external-spool assignment (ams_id 255/254) is a real
|
|
// target. A wholly unmapped print reports no target and falls to the existing send flow.
|
|
TEST_CASE("used-filament targets split mapped from unmapped", "[OrcaPrinterAgent]") {
|
|
using Slic3r::GUI::has_any_mapped_target;
|
|
using Slic3r::GUI::has_used_filament_without_target;
|
|
|
|
Slic3r::FilamentInfo box; box.ams_id = "0"; box.slot_id = "1"; // box slot
|
|
Slic3r::FilamentInfo external; external.ams_id = "255"; external.slot_id = "0"; // external spool
|
|
Slic3r::FilamentInfo unmapped; // no target
|
|
|
|
CHECK_FALSE(has_used_filament_without_target({box, external}));
|
|
CHECK(has_used_filament_without_target({box, unmapped}));
|
|
CHECK(has_used_filament_without_target({external, unmapped}));
|
|
CHECK(has_used_filament_without_target({unmapped})); // wholly unmapped: all-invalid flow, not this refusal
|
|
CHECK(has_any_mapped_target({box, unmapped}));
|
|
CHECK(has_any_mapped_target({external, unmapped}));
|
|
CHECK_FALSE(has_any_mapped_target({unmapped}));
|
|
}
|
|
|
|
// A device with no AMS units has one source, the external spool. OrcaSlicer's
|
|
// auto-mapping force-selects it for every filament; that is not a lane choice, so it
|
|
// must not gate the print or reach print.gcode_file.
|
|
TEST_CASE("a no-AMS device drops the forced external-spool selection", "[OrcaPrinterAgent]") {
|
|
using Slic3r::GUI::drop_forced_external_selection;
|
|
using Slic3r::GUI::has_engaged_filament_mapping;
|
|
|
|
std::string external_only = R"([{"ams_id":255,"slot_id":0}])";
|
|
drop_forced_external_selection(/*device_has_ams=*/false, external_only);
|
|
CHECK(external_only.empty());
|
|
CHECK_FALSE(has_engaged_filament_mapping(external_only));
|
|
CHECK(Probe::build_filament_mapping(external_only).empty());
|
|
|
|
std::string box_mapping = R"([{"ams_id":0,"slot_id":2}])";
|
|
drop_forced_external_selection(/*device_has_ams=*/true, box_mapping);
|
|
CHECK(box_mapping == R"([{"ams_id":0,"slot_id":2}])");
|
|
CHECK(has_engaged_filament_mapping(box_mapping));
|
|
}
|
|
|
|
// An empty mapping must leave the gcode_file payload byte-identical to today:
|
|
// exactly command, sequence_id and param, with no filament_mapping key.
|
|
TEST_CASE("gcode_file payload omits filament_mapping when the map is empty", "[OrcaPrinterAgent]") {
|
|
const nlohmann::json empty = Probe::build_gcode_file_payload("7", "job.gcode", nlohmann::json::array());
|
|
REQUIRE(empty.contains("print"));
|
|
CHECK(empty["print"].size() == 3);
|
|
CHECK(empty["print"]["command"] == "gcode_file");
|
|
CHECK(empty["print"]["sequence_id"] == "7");
|
|
CHECK(empty["print"]["param"] == "job.gcode");
|
|
CHECK_FALSE(empty["print"].contains("filament_mapping"));
|
|
|
|
const nlohmann::json mapping = Probe::build_filament_mapping(R"([{"ams_id":1,"slot_id":0},{"ams_id":255,"slot_id":255}])");
|
|
const nlohmann::json with = Probe::build_gcode_file_payload("8", "job.gcode", mapping);
|
|
REQUIRE(with["print"].contains("filament_mapping"));
|
|
REQUIRE(with["print"]["filament_mapping"].size() == 1);
|
|
CHECK(with["print"]["filament_mapping"][0]["filament_index"] == 0);
|
|
}
|
|
|
|
// The defensive gate: a mapped print is refused before anything is published when the
|
|
// connector never advertised filament_mapping. The GUI send gates make this visible first;
|
|
// this covers callers that bypass them (calibration, plugin). A sentinel-only mapping does
|
|
// not engage the gate and falls through to the normal publish path.
|
|
TEST_CASE("an engaged mapping is refused when the connector never advertised filament_mapping", "[OrcaPrinterAgent]") {
|
|
OrcaPrinterAgent agent("/tmp");
|
|
Slic3r::PrintParams params;
|
|
params.dev_id = "dev-no-mapping-cap";
|
|
params.dst_file = "/tmp/job.gcode";
|
|
params.ams_mapping2 = R"([{"ams_id":0,"slot_id":2}])";
|
|
|
|
CHECK(agent.start_sdcard_print(params, {}, {}) == ORCA_NETWORK_ERR_CMD_NOT_SUPPORTED);
|
|
|
|
params.ams_mapping2 = R"([{"ams_id":255,"slot_id":255}])";
|
|
CHECK(agent.start_sdcard_print(params, {}, {}) == BAMBU_NETWORK_ERR_PRINT_LP_PUBLISH_MSG_FAILED);
|
|
}
|
|
|
|
// The FTP "send with record" transport does not exist on OrcaSonar. It must
|
|
// report a non-success result so PrintJob falls back to start_print() rather
|
|
// than treating a print that was never sent as successful.
|
|
TEST_CASE("start_local_print_with_record never reports silent success", "[OrcaPrinterAgent]") {
|
|
Probe agent("/tmp");
|
|
Slic3r::PrintParams params;
|
|
const int rc = agent.start_local_print_with_record(params, {}, {}, {});
|
|
CHECK(rc < 0);
|
|
}
|
|
|
|
TEST_CASE("connect_printer wires up a LAN Config", "[OrcaPrinterAgent][.integration]") {
|
|
Probe agent("/tmp");
|
|
Slic3r::PrinterConnectionParams params{
|
|
"dev-1", "10.255.255.1", "", "orcasonar", "code", false, ""
|
|
};
|
|
const int rc = agent.connect_printer(params);
|
|
CHECK(rc == BAMBU_NETWORK_SUCCESS);
|
|
CHECK(agent.lan_connection_target() == "ws://10.255.255.1:8280/mqtt");
|
|
CHECK(agent.get_user_selected_machine().empty()); // LAN path must not touch the cloud selection
|
|
agent.disconnect_printer();
|
|
}
|
|
|
|
TEST_CASE("connect_printer uses the separately supplied port", "[OrcaPrinterAgent][.integration]") {
|
|
Probe agent("/tmp");
|
|
// DeviceManager strips the port from dev_ip and passes it in params.port.
|
|
Slic3r::PrinterConnectionParams params{
|
|
"dev-1", "10.255.255.1", "9999", "orcasonar", "code", false, ""
|
|
};
|
|
const int rc = agent.connect_printer(params);
|
|
CHECK(rc == BAMBU_NETWORK_SUCCESS);
|
|
CHECK(agent.lan_connection_target() == "ws://10.255.255.1:9999/mqtt");
|
|
agent.disconnect_printer();
|
|
}
|
|
|
|
TEST_CASE("post-connect sequence is subscribe then 3 requests in order", "[OrcaPrinterAgent]") {
|
|
struct SeqProbe : OrcaPrinterAgent {
|
|
using OrcaPrinterAgent::OrcaPrinterAgent;
|
|
std::vector<std::string> calls;
|
|
void emit_connect_sequence(const std::string& dev_id,
|
|
std::function<void(const std::string&)> /*sub*/,
|
|
std::function<void(const std::string&)> /*req*/) override {
|
|
OrcaPrinterAgent::emit_connect_sequence(dev_id,
|
|
[&](const std::string& id){ calls.push_back("sub:" + id); },
|
|
[&](const std::string& body){ calls.push_back(body); });
|
|
}
|
|
} probe("/tmp");
|
|
probe.run_connect_sequence_for_test("dev-1");
|
|
REQUIRE(probe.calls.size() == 4);
|
|
CHECK(probe.calls[0] == "sub:dev-1");
|
|
// The ids OPCP's handshake gives the three requests.
|
|
CHECK(probe.calls[1] == R"({"pushing":{"command":"pushall","sequence_id":"20002","version":1,"push_target":1}})");
|
|
CHECK(probe.calls[2] == R"({"info":{"command":"get_version","sequence_id":"20003"}})");
|
|
CHECK(probe.calls[3] == R"({"info":{"command":"get_capabilities","sequence_id":"20004"}})");
|
|
}
|
|
|
|
// pushing.start and pushing.stop are not this client's to send: a printer reports to every
|
|
// subscriber by itself, and through the Cloud the two are reserved for the Cloud.
|
|
TEST_CASE("a LAN session sends neither pushing.start nor pushing.stop", "[OrcaPrinterAgent]") {
|
|
orca_mqtt_test::MockBroker broker;
|
|
OrcaPrinterAgent agent("/tmp");
|
|
agent.connect_printer(Slic3r::PrinterConnectionParams{"dev-1", "127.0.0.1:" + std::to_string(broker.port()), "", "orcasonar", "code", false, ""});
|
|
|
|
REQUIRE(broker.wait_subscribed("device/dev-1/report"));
|
|
REQUIRE(broker.wait_publishes(3)); // the connect requests
|
|
|
|
// Disconnecting flushes what is queued and ends with the UNSUBSCRIBE, so every
|
|
// request of the session has reached the broker once the topic is gone.
|
|
agent.disconnect_printer();
|
|
REQUIRE(broker.wait_unsubscribed("device/dev-1/report"));
|
|
|
|
const std::vector<std::string> requests = broker.received_requests();
|
|
REQUIRE(requests.size() == 3);
|
|
CHECK(requests[0].find("\"pushall\"") != std::string::npos);
|
|
CHECK(requests[1].find("\"get_version\"") != std::string::npos);
|
|
CHECK(requests[2].find("\"get_capabilities\"") != std::string::npos);
|
|
for (const std::string& request : requests) {
|
|
CHECK(request.find("\"start\"") == std::string::npos);
|
|
CHECK(request.find("\"stop\"") == std::string::npos);
|
|
}
|
|
}
|
|
|
|
// Hidden: spawns the connect worker and attempts a real (failing) connect.
|
|
TEST_CASE("selecting a cloud printer configures the fleet socket", "[OrcaPrinterAgent][.integration]") {
|
|
auto cloud = std::make_shared<Slic3r::OrcaCloudServiceAgent>("/tmp");
|
|
cloud->set_api_base_url("api.example.com");
|
|
OrcaPrinterAgent agent("/tmp");
|
|
agent.set_cloud_agent(cloud);
|
|
|
|
agent.set_user_selected_machine("printer-uuid-1");
|
|
// The configure runs on the connect worker; poll rather than racing it.
|
|
std::string url;
|
|
for (int i = 0; i < 300; ++i) {
|
|
url = cloud->selected_printer_mqtt_url();
|
|
if (!url.empty()) break;
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
}
|
|
CHECK(url == "wss://api.example.com/api/v1/printers/mqtt");
|
|
|
|
agent.set_user_selected_machine(""); // selection changes do not tear down the fleet socket
|
|
CHECK(cloud->selected_printer_mqtt_url() == "wss://api.example.com/api/v1/printers/mqtt");
|
|
}
|
|
|
|
TEST_CASE("a stale-generation inbound message is dropped", "[OrcaPrinterAgent]") {
|
|
struct GenProbe : OrcaPrinterAgent {
|
|
using OrcaPrinterAgent::OrcaPrinterAgent;
|
|
using OrcaPrinterAgent::make_lan_message_handler; // expose for the test
|
|
};
|
|
GenProbe agent("/tmp");
|
|
int hits = 0;
|
|
agent.set_on_message_fn([&](std::string, std::string){ ++hits; });
|
|
auto handler_gen1 = agent.make_lan_message_handler(/*generation=*/1);
|
|
// m_lan_generation starts at 0; two bumps -> 2, so the epoch-1 handler is stale.
|
|
agent.bump_lan_generation_for_test();
|
|
agent.bump_lan_generation_for_test();
|
|
handler_gen1("dev-1", "{}"); // late callback from gen 1
|
|
CHECK(hits == 0);
|
|
}
|
|
|
|
TEST_CASE("connect_server does not start an MQTT socket", "[OrcaCloud]") {
|
|
auto cloud = std::make_shared<Slic3r::OrcaCloudServiceAgent>("/tmp");
|
|
cloud->set_api_base_url("127.0.0.1:1"); // no session -> connect_server short-circuits before any probe
|
|
cloud->connect_server();
|
|
REQUIRE(cloud->get_mqtt_connection() != nullptr); // created in the ctor
|
|
CHECK_FALSE(cloud->get_mqtt_connection()->is_running()); // never started
|
|
CHECK(cloud->selected_printer_mqtt_url().empty());
|
|
}
|
|
|
|
TEST_CASE("send_message* reject when there is no connection", "[OrcaPrinterAgent]") {
|
|
OrcaPrinterAgent agent("/tmp"); // no cloud agent, no LAN connection
|
|
CHECK(agent.send_message("d", "{}", 0, 0) == BAMBU_NETWORK_ERR_INVALID_HANDLE);
|
|
CHECK(agent.send_message_to_printer("d", "{}", 0, 0) == BAMBU_NETWORK_ERR_INVALID_HANDLE);
|
|
CHECK(agent.send_message("", "{}", 0, 0) == BAMBU_NETWORK_ERR_INVALID_HANDLE); // empty dev_id
|
|
}
|
|
|
|
TEST_CASE("send_message_to_printer publishes on the LAN connection", "[OrcaPrinterAgent]") {
|
|
orca_mqtt_test::MockBroker broker;
|
|
OrcaPrinterAgent agent("/tmp");
|
|
agent.connect_printer(Slic3r::PrinterConnectionParams{"dev-1", "127.0.0.1:" + std::to_string(broker.port()), "", "orcasonar", "code", false, ""});
|
|
|
|
for (int i = 0; i < 150 && broker.connect_count() == 0; ++i)
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(20));
|
|
REQUIRE(broker.connect_count() >= 1);
|
|
|
|
CHECK(agent.send_message_to_printer("dev-1", R"({"print":{"command":"pause","sequence_id":"20007"}})", 0, 0)
|
|
== BAMBU_NETWORK_SUCCESS);
|
|
|
|
// on_connected also publishes 3 requests; poll until "pause" specifically shows up.
|
|
bool saw_pause = false;
|
|
for (int i = 0; i < 150 && !saw_pause; ++i) {
|
|
for (const auto& r : broker.received_requests())
|
|
if (r.find("pause") != std::string::npos) { saw_pause = true; break; }
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(20));
|
|
}
|
|
CHECK(saw_pause);
|
|
agent.disconnect_printer();
|
|
}
|
|
|
|
TEST_CASE("a refusal for bad credentials after a drop is reported as a failed connection", "[OrcaPrinterAgent]") {
|
|
orca_mqtt_test::MockBroker broker;
|
|
// Declared before the agent, so they outlive any callback it makes while it is destroyed.
|
|
std::mutex mutex;
|
|
std::condition_variable changed;
|
|
std::vector<std::pair<int, std::string>> statuses;
|
|
OrcaPrinterAgent agent("/tmp");
|
|
agent.set_on_local_connect_fn([&](int status, std::string /*dev_id*/, std::string msg) {
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
statuses.emplace_back(status, std::move(msg));
|
|
changed.notify_all();
|
|
});
|
|
const auto wait_for_status = [&](int status) {
|
|
std::unique_lock<std::mutex> lock(mutex);
|
|
return changed.wait_for(lock, std::chrono::seconds(30), [&] {
|
|
return std::any_of(statuses.begin(), statuses.end(), [&](const auto& entry) { return entry.first == status; });
|
|
});
|
|
};
|
|
|
|
agent.connect_printer(Slic3r::PrinterConnectionParams{"dev-1", "127.0.0.1:" + std::to_string(broker.port()), "", "orcasonar", "code", false, ""});
|
|
REQUIRE(wait_for_status(Slic3r::ConnectStatusOk));
|
|
|
|
broker.set_connack_rc(5); // the next attempt is refused as not authorised
|
|
broker.drop_client();
|
|
REQUIRE(wait_for_status(Slic3r::ConnectStatusFailed));
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
const auto failed = std::find_if(statuses.begin(), statuses.end(), [](const auto& entry) { return entry.first == Slic3r::ConnectStatusFailed; });
|
|
CHECK(failed->second == "5");
|
|
}
|
|
agent.disconnect_printer();
|
|
}
|
|
|
|
TEST_CASE("a command is numbered inside the app's own band", "[OrcaPrinterAgent]") {
|
|
orca_mqtt_test::MockBroker broker;
|
|
OrcaPrinterAgent agent("/tmp");
|
|
agent.connect_printer(Slic3r::PrinterConnectionParams{"dev-1", "127.0.0.1:" + std::to_string(broker.port()), "", "orcasonar", "code", false, ""});
|
|
REQUIRE(broker.wait_subscribed("device/dev-1/report"));
|
|
|
|
Slic3r::PrintParams params;
|
|
params.dev_id = "dev-1";
|
|
params.dst_file = "job.gcode";
|
|
params.connection_type = "lan";
|
|
REQUIRE(agent.start_sdcard_print(params, {}, {}) == BAMBU_NETWORK_SUCCESS);
|
|
|
|
REQUIRE(broker.wait_publishes(4));
|
|
std::string id;
|
|
for (const auto& payload : broker.received_requests()) {
|
|
const size_t at = payload.find("\"sequence_id\":\"");
|
|
const size_t end = payload.find('"', at + 15);
|
|
if (payload.find("\"command\":\"gcode_file\"") != std::string::npos && at != std::string::npos)
|
|
id = payload.substr(at + 15, end - (at + 15));
|
|
}
|
|
// The Cloud relay drops a sequence_id that is not 1-16 digits, so an id outside this
|
|
// band does not fail loudly: the request is simply lost.
|
|
REQUIRE_FALSE(id.empty());
|
|
CHECK(id.size() <= 16);
|
|
const long value = std::stol(id);
|
|
CHECK(value >= 20000);
|
|
CHECK(value < 30000);
|
|
|
|
agent.disconnect_printer();
|
|
}
|
|
|
|
TEST_CASE("a LAN session refuses a command for another printer", "[OrcaPrinterAgent]") {
|
|
orca_mqtt_test::MockBroker broker;
|
|
OrcaPrinterAgent agent("/tmp");
|
|
agent.connect_printer(Slic3r::PrinterConnectionParams{"dev-1", "127.0.0.1:" + std::to_string(broker.port()), "", "orcasonar", "code", false, ""});
|
|
REQUIRE(broker.wait_subscribed("device/dev-1/report"));
|
|
|
|
// The session belongs to dev-1. Publishing dev-2's command through it would land in
|
|
// dev-1's request topic, so it must be refused instead of queued.
|
|
CHECK(agent.send_message_to_printer("dev-2", R"({"print":{"command":"pause","sequence_id":"20007"}})", 0, 0)
|
|
== BAMBU_NETWORK_ERR_INVALID_HANDLE);
|
|
CHECK(agent.send_message_to_printer("dev-1", R"({"print":{"command":"pause","sequence_id":"20008"}})", 0, 0)
|
|
== BAMBU_NETWORK_SUCCESS);
|
|
|
|
agent.disconnect_printer();
|
|
}
|
|
|
|
TEST_CASE("an RFID refresh addresses a slot by its flat index", "[OrcaPrinterAgent]") {
|
|
orca_mqtt_test::MockBroker broker;
|
|
Probe agent("/tmp");
|
|
agent.set_on_message_fn([](std::string, std::string) {});
|
|
agent.connect_printer(Slic3r::PrinterConnectionParams{"dev-1", "127.0.0.1:" + std::to_string(broker.port()), "", "orcasonar", "code", false, ""});
|
|
REQUIRE(broker.wait_subscribed("device/dev-1/report"));
|
|
|
|
// ams_get_rfid is macro-backed, so the shim only forwards it when the manifest advertises
|
|
// both the AMS feature and the command itself.
|
|
agent.deliver_to_sink("dev-1",
|
|
R"({"info":{"command":"get_capabilities","supported_features":{"fms":true},"supported_commands":["print.ams_get_rfid"]}})",
|
|
/*local=*/false);
|
|
|
|
// Box 1, slot 2 on the wire is 4 * 1 + 2.
|
|
CHECK(agent.command_ams_refresh_rfid("dev-1", 1, 2, 20031, /*lan_mode=*/true) == BAMBU_NETWORK_SUCCESS);
|
|
REQUIRE(broker.wait_publishes(4));
|
|
bool saw_flat_six = false;
|
|
for (const auto& payload : broker.received_requests())
|
|
if (payload.find("\"tray_id\":6") != std::string::npos)
|
|
saw_flat_six = true;
|
|
CHECK(saw_flat_six);
|
|
|
|
// A negative slot cannot be a tray, whichever form the caller passes it in.
|
|
CHECK(agent.command_ams_refresh_rfid("dev-1", -1, -4, 20032, /*lan_mode=*/true) == BAMBU_NETWORK_ERR_INVALID_HANDLE);
|
|
CHECK(agent.command_ams_refresh_rfid("dev-1", 1, -2, 20033, /*lan_mode=*/true) == BAMBU_NETWORK_ERR_INVALID_HANDLE);
|
|
|
|
agent.disconnect_printer();
|
|
}
|
|
|
|
TEST_CASE("the bed uses the command the connector advertises", "[OrcaPrinterAgent]") {
|
|
orca_mqtt_test::MockBroker broker;
|
|
Probe agent("/tmp");
|
|
agent.set_on_message_fn([](std::string, std::string) {});
|
|
agent.connect_printer(Slic3r::PrinterConnectionParams{"dev-1", "127.0.0.1:" + std::to_string(broker.port()), "", "orcasonar", "code", false, ""});
|
|
REQUIRE(broker.wait_subscribed("device/dev-1/report"));
|
|
REQUIRE(broker.wait_publishes(3));
|
|
|
|
// No set_bed_temp in the manifest: fall back to M140 rather than a command the
|
|
// printer would refuse. The Bambu fun-flags bit never decides this for Orca.
|
|
agent.deliver_to_sink("dev-1",
|
|
R"({"info":{"command":"get_capabilities","supported_commands":["print.set_nozzle_temp","print.gcode_line"]}})",
|
|
/*local=*/false);
|
|
CHECK(agent.command_set_bed("dev-1", 60, /*supports_mqtt_bed_ctrl=*/true, 20041, /*lan_mode=*/true) == BAMBU_NETWORK_SUCCESS);
|
|
REQUIRE(broker.wait_publishes(4));
|
|
bool saw_m140 = false;
|
|
for (const auto& payload : broker.received_requests())
|
|
if (payload.find("M140 S60") != std::string::npos)
|
|
saw_m140 = true;
|
|
CHECK(saw_m140);
|
|
|
|
// A manifest that lists it keeps the native command.
|
|
agent.deliver_to_sink("dev-1",
|
|
R"({"info":{"command":"get_capabilities","supported_commands":["print.set_bed_temp"]}})",
|
|
/*local=*/false);
|
|
CHECK(agent.command_set_bed("dev-1", 70, /*supports_mqtt_bed_ctrl=*/false, 20042, /*lan_mode=*/true) == BAMBU_NETWORK_SUCCESS);
|
|
REQUIRE(broker.wait_publishes(5));
|
|
bool saw_command = false;
|
|
for (const auto& payload : broker.received_requests())
|
|
if (payload.find("\"command\":\"set_bed_temp\"") != std::string::npos)
|
|
saw_command = true;
|
|
CHECK(saw_command);
|
|
|
|
agent.disconnect_printer();
|
|
}
|
|
|
|
TEST_CASE("binding and its ticket report failure instead of a silent success", "[OrcaPrinterAgent]") {
|
|
OrcaPrinterAgent agent("/tmp");
|
|
// OrcaSonar is paired on the printer. The interface default answered success with an
|
|
// empty ticket, and the caller built a sign-in URL ending in "&ticket=".
|
|
std::string ticket = "untouched";
|
|
CHECK(agent.request_bind_ticket(&ticket) < 0);
|
|
CHECK(ticket.empty());
|
|
CHECK(agent.bind("10.0.0.9", "dev-1", "Orca", "secure", "+08:00", false, {}) < 0);
|
|
}
|
|
|
|
TEST_CASE("destroying an agent mid-connect does not hang or crash", "[OrcaPrinterAgent]") {
|
|
for (int i = 0; i < 20; ++i) {
|
|
auto agent = std::make_unique<OrcaPrinterAgent>("/tmp");
|
|
agent->connect_printer(Slic3r::PrinterConnectionParams{"dev-1", "127.0.0.1:1", "", "orcasonar", "code", false, ""}); // nothing listening: instant ECONNREFUSED
|
|
agent.reset(); // ~OrcaPrinterAgent must stop the conn, join the thread, and not hang/crash
|
|
}
|
|
SUCCEED();
|
|
}
|