fix: make integration tests headless

This commit is contained in:
Ian Chua
2026-09-23 14:29:57 +08:00
parent ccaafb3003
commit 33179c1902
5 changed files with 64 additions and 44 deletions
+45 -19
View File
@@ -1,8 +1,9 @@
#include "DevManager.h"
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <exception> #include <exception>
#include "DevManager.h" #include <libslic3r/AppConfig.hpp>
#include "CloudProvider.hpp" #include "CloudProvider.hpp"
#include "DevUtil.h" #include "DevUtil.h"
@@ -48,9 +49,11 @@ namespace {
namespace Slic3r namespace Slic3r
{ {
DeviceManager::DeviceManager(NetworkAgent* agent) DeviceManager::DeviceManager(NetworkAgent* agent, bool enable_refresher, AppConfig* app_config)
{ {
m_agent = agent; m_agent = agent;
m_app_config = app_config;
if (enable_refresher)
m_refresher = new DeviceManagerRefresher(this); m_refresher = new DeviceManagerRefresher(this);
DevPrinterConfigUtil::InitFilePath(resources_dir()); DevPrinterConfigUtil::InitFilePath(resources_dir());
@@ -62,9 +65,14 @@ namespace Slic3r
} }
} }
AppConfig* DeviceManager::get_app_config() const
{
return m_app_config ? m_app_config : GUI::wxGetApp().app_config;
}
void DeviceManager::load_local_machines_from_config() void DeviceManager::load_local_machines_from_config()
{ {
AppConfig* config = GUI::wxGetApp().app_config; AppConfig* config = get_app_config();
if (!config) if (!config)
return; return;
const auto local_machines = config->get_local_machines(); const auto local_machines = config->get_local_machines();
@@ -90,9 +98,8 @@ namespace Slic3r
} }
} }
void DeviceManager::update_local_machine(const MachineObject& m) void DeviceManager::update_local_machine(const MachineObject& m, AppConfig* config)
{ {
AppConfig* config = GUI::wxGetApp().app_config;
if (config) { if (config) {
if (m.is_lan_mode_printer()) { if (m.is_lan_mode_printer()) {
if (m.has_access_right()) { if (m.has_access_right()) {
@@ -113,6 +120,7 @@ namespace Slic3r
DeviceManager::~DeviceManager() DeviceManager::~DeviceManager()
{ {
if (m_refresher)
delete m_refresher; delete m_refresher;
for (auto it = localMachineList.begin(); it != localMachineList.end(); it++) for (auto it = localMachineList.begin(); it != localMachineList.end(); it++)
@@ -173,14 +181,22 @@ namespace Slic3r
return printer_agent ? printer_agent->get_agent_info().id : ""; return printer_agent ? printer_agent->get_agent_info().id : "";
} }
std::string DeviceManager::get_current_cloud_provider() const
{
const std::string agent_id = get_current_printer_agent_id();
if (!agent_id.empty())
return agent_id == BBL_PRINTER_AGENT_ID ? BBL_CLOUD_PROVIDER : ORCA_CLOUD_PROVIDER;
return GUI::wxGetApp().get_printer_cloud_provider();
}
void DeviceManager::EnableMultiMachine(bool enable) void DeviceManager::EnableMultiMachine(bool enable)
{ {
m_agent->enable_multi_machine(enable); m_agent->enable_multi_machine(enable);
m_enable_mutil_machine = enable; m_enable_mutil_machine = enable;
} }
void DeviceManager::start_refresher() { m_refresher->Start(); } void DeviceManager::start_refresher() { if (m_refresher) m_refresher->Start(); }
void DeviceManager::stop_refresher() { m_refresher->Stop(); } void DeviceManager::stop_refresher() { if (m_refresher) m_refresher->Stop(); }
void DeviceManager::keep_alive() void DeviceManager::keep_alive()
@@ -297,6 +313,8 @@ namespace Slic3r
/* update localMachineList */ /* update localMachineList */
it = localMachineList.find(dev_id); it = localMachineList.find(dev_id);
AppConfig* config = get_app_config();
if (it != localMachineList.end()) { if (it != localMachineList.end()) {
// update properties // update properties
/* ip changed */ /* ip changed */
@@ -386,7 +404,6 @@ namespace Slic3r
obj->m_is_online = true; obj->m_is_online = true;
//load access code //load access code
AppConfig* config = Slic3r::GUI::wxGetApp().app_config;
if (config) { if (config) {
obj->set_access_code(get_access_code_with_legacy_fallback(config, dev_id, obj->printer_agent_id), false); obj->set_access_code(get_access_code_with_legacy_fallback(config, dev_id, obj->printer_agent_id), false);
} }
@@ -400,7 +417,7 @@ namespace Slic3r
<< ", ip = " << dev_ip <<", printer_name = " << dev_name << ", ip = " << dev_ip <<", printer_name = " << dev_name
<< ", con_type= " << connect_type <<", signal= " << printer_signal << ", bind_state= " << bind_state; << ", con_type= " << connect_type <<", signal= " << printer_signal << ", bind_state= " << bind_state;
} }
update_local_machine(*obj); update_local_machine(*obj, config);
} }
catch (...) { catch (...) {
; ;
@@ -435,11 +452,16 @@ namespace Slic3r
obj->last_alive = Slic3r::Utils::get_current_time_utc(); obj->last_alive = Slic3r::Utils::get_current_time_utc();
obj->set_access_code(access_code, false); obj->set_access_code(access_code, false);
update_local_machine(*obj); update_local_machine(*obj, get_app_config());
return obj; return obj;
} }
void DeviceManager::update_local_machine(const MachineObject& m)
{
update_local_machine(m, GUI::wxGetApp().app_config);
}
int DeviceManager::query_bind_status(std::string& msg, const std::string& provider) int DeviceManager::query_bind_status(std::string& msg, const std::string& provider)
{ {
if (!m_agent) if (!m_agent)
@@ -635,7 +657,8 @@ namespace Slic3r
it->second->reset(); it->second->reset();
#if !BBL_RELEASE_TO_PUBLIC #if !BBL_RELEASE_TO_PUBLIC
it->second->connect(Slic3r::GUI::wxGetApp().app_config->get("enable_ssl_for_mqtt") == "true" ? true : false); AppConfig* config = get_app_config();
it->second->connect(config && config->get("enable_ssl_for_mqtt") == "true");
#else #else
it->second->connect(it->second->local_use_ssl); it->second->connect(it->second->local_use_ssl);
#endif #endif
@@ -661,7 +684,8 @@ namespace Slic3r
BOOST_LOG_TRIVIAL(info) << "set_selected_machine: select new lan machine, dev_id =" << dev_id; BOOST_LOG_TRIVIAL(info) << "set_selected_machine: select new lan machine, dev_id =" << dev_id;
it->second->reset(); it->second->reset();
#if !BBL_RELEASE_TO_PUBLIC #if !BBL_RELEASE_TO_PUBLIC
it->second->connect(Slic3r::GUI::wxGetApp().app_config->get("enable_ssl_for_mqtt") == "true" ? true : false); AppConfig* config = get_app_config();
it->second->connect(config && config->get("enable_ssl_for_mqtt") == "true");
#else #else
it->second->connect(it->second->local_use_ssl); it->second->connect(it->second->local_use_ssl);
#endif #endif
@@ -851,14 +875,15 @@ namespace Slic3r
json j = json::parse(body); json j = json::parse(body);
const bool has_request_context = j.contains("provider") && j.contains("agent_id") && j.contains("generation"); const bool has_request_context = j.contains("provider") && j.contains("agent_id") && j.contains("generation");
const std::string current_provider = get_current_cloud_provider();
const std::string provider = j.contains("provider") ? j["provider"].get<std::string>() const std::string provider = j.contains("provider") ? j["provider"].get<std::string>()
: GUI::wxGetApp().get_printer_cloud_provider(); : current_provider;
const std::string agent_id = j.contains("agent_id") ? j["agent_id"].get<std::string>() const std::string agent_id = j.contains("agent_id") ? j["agent_id"].get<std::string>()
: get_current_printer_agent_id(); : get_current_printer_agent_id();
const std::uint64_t generation = j.value("generation", std::uint64_t(0)); const std::uint64_t generation = j.value("generation", std::uint64_t(0));
if (has_request_context && if (has_request_context &&
(provider != GUI::wxGetApp().get_printer_cloud_provider() || (provider != current_provider ||
agent_id != get_current_printer_agent_id() || agent_id != get_current_printer_agent_id() ||
generation != (m_agent ? m_agent->get_user_machine_list_generation() : 0))) { generation != (m_agent ? m_agent->get_user_machine_list_generation() : 0))) {
BOOST_LOG_TRIVIAL(debug) << __FUNCTION__ << ": ignoring stale response provider=" BOOST_LOG_TRIVIAL(debug) << __FUNCTION__ << ": ignoring stale response provider="
@@ -903,7 +928,8 @@ namespace Slic3r
if (obj->get_dev_ip().empty()) if (obj->get_dev_ip().empty())
{ {
obj->get_dev_ip() = Slic3r::GUI::wxGetApp().app_config->get("ip_address", dev_id); if (AppConfig* config = get_app_config())
obj->get_dev_ip() = config->get("ip_address", dev_id);
} }
userMachineList.insert(std::make_pair(dev_id, obj)); userMachineList.insert(std::make_pair(dev_id, obj));
} }
@@ -1001,15 +1027,15 @@ namespace Slic3r
void DeviceManager::record_user_last_machine(const std::string& dev_id) void DeviceManager::record_user_last_machine(const std::string& dev_id)
{ {
if (Slic3r::GUI::wxGetApp().app_config) { if (AppConfig* config = get_app_config()) {
Slic3r::GUI::wxGetApp().app_config->set("user_last_selected_machine", dev_id); config->set("user_last_selected_machine", dev_id);
} }
} }
std::string DeviceManager::get_user_last_machine() const std::string DeviceManager::get_user_last_machine() const
{ {
if (Slic3r::GUI::wxGetApp().app_config) { if (AppConfig* config = get_app_config()) {
const auto& user_last_machine = Slic3r::GUI::wxGetApp().app_config->get("user_last_selected_machine"); const auto& user_last_machine = config->get("user_last_selected_machine");
if (!user_last_machine.empty()) { if (!user_last_machine.empty()) {
return user_last_machine; return user_last_machine;
} else if (m_agent) { } else if (m_agent) {
+9 -3
View File
@@ -12,6 +12,7 @@ namespace Slic3r
struct BBLocalMachine; struct BBLocalMachine;
class MachineObject; class MachineObject;
class NetworkAgent; class NetworkAgent;
class AppConfig;
namespace GUI { namespace GUI {
class GUI_App; class GUI_App;
@@ -24,6 +25,7 @@ class DeviceManager
friend class DeviceManagerRefresher; friend class DeviceManagerRefresher;
private: private:
NetworkAgent* m_agent{ nullptr }; NetworkAgent* m_agent{ nullptr };
AppConfig* m_app_config{ nullptr };
DeviceManagerRefresher* m_refresher{ nullptr }; DeviceManagerRefresher* m_refresher{ nullptr };
bool m_enable_mutil_machine = false; bool m_enable_mutil_machine = false;
@@ -35,11 +37,13 @@ private:
std::map<std::string, MachineObject*> userMachineList; /* dev_id -> MachineObject* cloudMachine of User */ std::map<std::string, MachineObject*> userMachineList; /* dev_id -> MachineObject* cloudMachine of User */
public: public:
DeviceManager(NetworkAgent* agent = nullptr); DeviceManager(NetworkAgent* agent = nullptr, bool enable_refresher = true,
AppConfig* app_config = nullptr);
~DeviceManager(); ~DeviceManager();
public: public:
NetworkAgent* get_agent() const { return m_agent; } NetworkAgent* get_agent() const { return m_agent; }
AppConfig* get_app_config() const;
void set_agent(NetworkAgent* agent); void set_agent(NetworkAgent* agent);
void start_refresher(); void start_refresher();
@@ -121,6 +125,7 @@ private:
void keep_alive(); void keep_alive();
void check_pushing(); void check_pushing();
std::string get_current_cloud_provider() const;
void OnMachineBindStateChanged(MachineObject* obj, const std::string& new_state); void OnMachineBindStateChanged(MachineObject* obj, const std::string& new_state);
void OnSelectedMachineChanged(const std::string& pre_dev_id, const std::string& new_dev_id); void OnSelectedMachineChanged(const std::string& pre_dev_id, const std::string& new_dev_id);
@@ -133,14 +138,15 @@ public:
std::string connection_type, std::string bind_state, std::string version, std::string connection_type, std::string bind_state, std::string version,
std::string access_code); std::string access_code);
static void update_local_machine(const MachineObject& m); static void update_local_machine(const MachineObject& m);
static void update_local_machine(const MachineObject& m, AppConfig* config);
}; };
class DeviceManagerRefresher : public wxObject class DeviceManagerRefresher : public wxObject
{ {
wxTimer* m_timer{ nullptr }; wxTimer* m_timer{nullptr};
int m_timer_interval_msec = 5000; int m_timer_interval_msec = 5000;
DeviceManager* m_manager{ nullptr }; DeviceManager* m_manager{nullptr};
public: public:
DeviceManagerRefresher(DeviceManager* manger); DeviceManagerRefresher(DeviceManager* manger);
+3 -3
View File
@@ -477,7 +477,7 @@ void MachineObject::set_access_code(std::string code, bool only_refresh)
{ {
this->access_code = code; this->access_code = code;
if (only_refresh) { if (only_refresh) {
AppConfig* config = GUI::wxGetApp().app_config; AppConfig* config = m_manager ? m_manager->get_app_config() : GUI::wxGetApp().app_config;
if (config) { if (config) {
if (is_lan_mode_printer()) { if (is_lan_mode_printer()) {
// why: LAN codes are scoped via BBLocalMachine::access_code, keyed by dev_id and // why: LAN codes are scoped via BBLocalMachine::access_code, keyed by dev_id and
@@ -490,7 +490,7 @@ void MachineObject::set_access_code(std::string code, bool only_refresh)
// fresh from the cloud API's current response, so there's no cross-agent leakage // fresh from the cloud API's current response, so there's no cross-agent leakage
// risk to guard against there. // risk to guard against there.
if (!code.empty()) { if (!code.empty()) {
DeviceManager::update_local_machine(*this); DeviceManager::update_local_machine(*this, config);
} else { } else {
// Only patch an existing record's code - don't persist a brand-new // Only patch an existing record's code - don't persist a brand-new
// never-bound entry just because set_access_code("") was called on it. // never-bound entry just because set_access_code("") was called on it.
@@ -4632,7 +4632,7 @@ int MachineObject::parse_json(std::string tunnel, std::string payload, bool key_
if (diff.count() > 10.0f) { if (diff.count() > 10.0f) {
BOOST_LOG_TRIVIAL(trace) << "parse_json timeout = " << diff.count(); BOOST_LOG_TRIVIAL(trace) << "parse_json timeout = " << diff.count();
} }
DeviceManager::update_local_machine(*this); DeviceManager::update_local_machine(*this, m_manager ? m_manager->get_app_config() : GUI::wxGetApp().app_config);
return 0; return 0;
} }
@@ -2,14 +2,12 @@
#include <slic3r/GUI/DeviceCore/DevManager.h> #include <slic3r/GUI/DeviceCore/DevManager.h>
#include <slic3r/GUI/DeviceManager.hpp> #include <slic3r/GUI/DeviceManager.hpp>
#include <slic3r/GUI/GUI_App.hpp>
#include <libslic3r/AppConfig.hpp> #include <libslic3r/AppConfig.hpp>
#include <slic3r/Utils/NetworkAgent.hpp> #include <slic3r/Utils/NetworkAgent.hpp>
#include <slic3r/Utils/OrcaCloudServiceAgent.hpp> #include <slic3r/Utils/OrcaCloudServiceAgent.hpp>
#include <slic3r/Utils/OrcaPrinterAgent.hpp> #include <slic3r/Utils/OrcaPrinterAgent.hpp>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <wx/init.h>
#include <memory> #include <memory>
#include <cstdint> #include <cstdint>
@@ -55,9 +53,6 @@ private:
struct ScopedAppConfig struct ScopedAppConfig
{ {
AppConfig config; AppConfig config;
ScopedAppConfig() { GUI::wxGetApp().app_config = &config; }
~ScopedAppConfig() { GUI::wxGetApp().app_config = nullptr; }
}; };
std::string machine_list_response(const std::string& provider, const std::string& agent_id, std::string machine_list_response(const std::string& provider, const std::string& agent_id,
@@ -99,12 +94,10 @@ TEST_CASE("Network agent stamps user-machine responses with request context", "[
TEST_CASE("Device manager ignores stale cloud machine responses", "[DeviceManager][integration]") TEST_CASE("Device manager ignores stale cloud machine responses", "[DeviceManager][integration]")
{ {
wxInitializer wx_init;
REQUIRE(wx_init.IsOk());
ScopedAppConfig app_config; ScopedAppConfig app_config;
NetworkAgent network(nullptr, std::make_shared<TestPrinterAgent>("integration-agent")); NetworkAgent network(nullptr, std::make_shared<TestPrinterAgent>("integration-agent"));
network.set_printer_agent(std::make_shared<TestPrinterAgent>("integration-agent")); network.set_printer_agent(std::make_shared<TestPrinterAgent>("integration-agent"));
DeviceManager manager(&network); DeviceManager manager(&network, false, &app_config.config);
const std::uint64_t current_generation = network.get_user_machine_list_generation(); const std::uint64_t current_generation = network.get_user_machine_list_generation();
manager.parse_user_print_info(machine_list_response(ORCA_CLOUD_PROVIDER, "integration-agent", manager.parse_user_print_info(machine_list_response(ORCA_CLOUD_PROVIDER, "integration-agent",
@@ -129,12 +122,11 @@ TEST_CASE("Device manager ignores stale cloud machine responses", "[DeviceManage
TEST_CASE("Device manager filters and rehomes devices by printer-agent ownership", "[DeviceManager][integration]") TEST_CASE("Device manager filters and rehomes devices by printer-agent ownership", "[DeviceManager][integration]")
{ {
wxInitializer wx_init; ScopedAppConfig app_config;
REQUIRE(wx_init.IsOk());
auto agent_a = std::make_shared<TestPrinterAgent>("integration-agent-a"); auto agent_a = std::make_shared<TestPrinterAgent>("integration-agent-a");
auto agent_b = std::make_shared<TestPrinterAgent>("integration-agent-b"); auto agent_b = std::make_shared<TestPrinterAgent>("integration-agent-b");
NetworkAgent network(nullptr, agent_a); NetworkAgent network(nullptr, agent_a);
DeviceManager manager(&network); DeviceManager manager(&network, false, &app_config.config);
BBLocalMachine machine; BBLocalMachine machine;
machine.dev_id = "integration-lan-device"; machine.dev_id = "integration-lan-device";
@@ -2,7 +2,6 @@
#include <slic3r/GUI/WebMediaController.hpp> #include <slic3r/GUI/WebMediaController.hpp>
#include <wx/init.h>
#include <wx/webview.h> #include <wx/webview.h>
#include <string> #include <string>
@@ -84,9 +83,6 @@ public:
TEST_CASE("Web media controller tears down a snapshot lifecycle", "[WebMediaController][integration]") TEST_CASE("Web media controller tears down a snapshot lifecycle", "[WebMediaController][integration]")
{ {
wxInitializer wx_init;
REQUIRE(wx_init.IsOk());
StubWebView view; StubWebView view;
WebMediaController controller(&view); WebMediaController controller(&view);