20#ifndef QA_API_E2E_UTILS_H
21#define QA_API_E2E_UTILS_H
28#include <google/protobuf/any.pb.h>
29#include <magic_enum.hpp>
30#include <wx/filename.h>
31#include <wx/process.h>
42#include <nng/protocol/reqrep0/req.h>
46#include <api/common/envelope.pb.h>
47#include <api/common/commands/base_commands.pb.h>
48#include <api/common/commands/editor_commands.pb.h>
49#include <api/common/commands/project_commands.pb.h>
50#include <api/common/types/base_types.pb.h>
51#include <api/common/types/enums.pb.h>
52#include <api/common/types/jobs.pb.h>
59#ifndef QA_KICAD_CLI_PATH
60#define QA_KICAD_CLI_PATH "kicad-cli"
85 static void drainStream( wxInputStream* aStream, wxString& aDest )
87 if( !aStream || !aStream->CanRead() )
90 constexpr size_t chunkSize = 1024;
91 char buffer[chunkSize + 1] = { 0 };
93 while( aStream->CanRead() )
95 aStream->Read( buffer, chunkSize );
96 size_t bytesRead = aStream->LastRead();
101 buffer[bytesRead] =
'\0';
102 aDest += wxString::FromUTF8( buffer );
107 std::function<void(
int,
const wxString&,
const wxString& )>
m_callback;
116 int ret = nng_req0_open( &
m_socket );
121 nng_socket_set_ms(
m_socket, NNG_OPT_RECVTIMEO, 10000 );
122 nng_socket_set_ms(
m_socket, NNG_OPT_SENDTIMEO, 10000 );
127 wxString::Format( wxS(
"nng_req0_open failed: %s" ), wxString::FromUTF8( nng_strerror( ret ) ) );
145 int ret = nng_dial(
m_socket, aSocketUrl.ToStdString().c_str(),
nullptr, 0 );
149 m_lastError = wxString::Format( wxS(
"nng_dial failed: %s" ), wxString::FromUTF8( nng_strerror( ret ) ) );
165 int ret = nng_req0_open( &
m_socket );
170 nng_socket_set_ms(
m_socket, NNG_OPT_RECVTIMEO, 10000 );
171 nng_socket_set_ms(
m_socket, NNG_OPT_SENDTIMEO, 10000 );
178 bool Ping( kiapi::common::ApiStatusCode* aStatusOut =
nullptr )
180 kiapi::common::commands::Ping ping;
181 kiapi::common::ApiResponse response;
187 *aStatusOut = response.status().status();
194 kiapi::common::commands::GetVersion request;
195 kiapi::common::ApiResponse response;
200 if( response.status().status() != kiapi::common::AS_OK )
206 kiapi::common::commands::GetVersionResponse version;
208 if( !response.message().UnpackTo( &version ) )
210 m_lastError = wxS(
"Failed to unpack GetVersionResponse" );
217 bool OpenDocument(
const wxString& aPath, kiapi::common::types::DocumentType aType,
218 kiapi::common::types::DocumentSpecifier* aDocument =
nullptr )
220 kiapi::common::commands::OpenDocument request;
221 request.set_type( aType );
222 request.set_path( aPath.ToStdString() );
224 kiapi::common::ApiResponse response;
229 if( response.status().status() != kiapi::common::AS_OK )
235 kiapi::common::commands::OpenDocumentResponse openResponse;
237 if( !response.message().UnpackTo( &openResponse ) )
239 m_lastError = wxS(
"Failed to unpack OpenDocumentResponse" );
244 *aDocument = openResponse.document();
250 bool OpenDocument(
const wxString& aPath, kiapi::common::types::DocumentSpecifier* aDocument =
nullptr )
252 return OpenDocument( aPath, kiapi::common::types::DOCTYPE_PCB, aDocument );
255 bool GetItemsCount(
const kiapi::common::types::DocumentSpecifier& aDocument,
256 kiapi::common::types::KiCadObjectType aType,
int* aCount )
258 kiapi::common::commands::GetItems request;
259 *request.mutable_header()->mutable_document() = aDocument;
260 request.add_types( aType );
262 kiapi::common::ApiResponse response;
267 if( response.status().status() != kiapi::common::AS_OK )
273 kiapi::common::commands::GetItemsResponse itemsResponse;
275 if( !response.message().UnpackTo( &itemsResponse ) )
277 m_lastError = wxS(
"Failed to unpack GetItemsResponse" );
281 if( itemsResponse.status() != kiapi::common::types::IRS_OK )
283 m_lastError = wxString::Format( wxS(
"GetItems returned non-OK status: %d" ),
284 static_cast<int>( itemsResponse.status() ) );
288 *aCount = itemsResponse.items_size();
294 wxCHECK( aFootprint,
false );
296 kiapi::common::commands::GetItems request;
297 *request.mutable_header()->mutable_document() = aDocument;
298 request.add_types( kiapi::common::types::KOT_PCB_FOOTPRINT );
300 kiapi::common::ApiResponse response;
308 if( response.status().status() != kiapi::common::AS_OK )
314 kiapi::common::commands::GetItemsResponse itemsResponse;
316 if( !response.message().UnpackTo( &itemsResponse ) )
318 m_lastError = wxS(
"Failed to unpack GetItemsResponse" );
322 if( itemsResponse.status() != kiapi::common::types::IRS_OK )
324 m_lastError = wxString::Format( wxS(
"GetItems returned non-OK status: %s" ),
325 magic_enum::enum_name( itemsResponse.status() ) );
329 if( itemsResponse.items_size() == 0 )
331 m_lastError = wxS(
"GetItems returned no footprints" );
335 if( !aFootprint->
Deserialize( itemsResponse.items( 0 ) ) )
337 m_lastError = wxS(
"Failed to deserialize first footprint from GetItems response" );
344 bool CloseDocument(
const kiapi::common::types::DocumentSpecifier* aDocument,
345 kiapi::common::ApiStatusCode* aStatus =
nullptr )
347 kiapi::common::commands::CloseDocument request;
350 *request.mutable_document() = *aDocument;
352 kiapi::common::ApiResponse response;
361 *aStatus = response.status().status();
363 if( response.status().status() != kiapi::common::AS_OK )
372 template <
typename T>
373 bool RunJob(
const T& aJobRequest, kiapi::common::types::RunJobResponse* aResponse )
375 kiapi::common::ApiResponse apiResponse;
380 if( apiResponse.status().status() != kiapi::common::AS_OK )
382 m_lastError = apiResponse.status().error_message();
386 if( !apiResponse.message().UnpackTo( aResponse ) )
388 m_lastError = wxS(
"Failed to unpack RunJobResponse" );
395 template <
typename T>
396 bool SendCommand(
const T& aMessage, kiapi::common::ApiResponse* aResponse )
402 template <
typename T>
403 bool sendCommand(
const T& aMessage, kiapi::common::ApiResponse* aResponse )
407 m_lastError = wxS(
"API client is not connected" );
411 kiapi::common::ApiRequest request;
412 request.mutable_header()->set_client_name(
"kicad.qa" );
414 if( !request.mutable_message()->PackFrom( aMessage ) )
416 m_lastError = wxS(
"Failed to pack command into ApiRequest" );
420 std::string requestStr = request.SerializeAsString();
421 void* sendBuf = nng_alloc( requestStr.size() );
429 memcpy( sendBuf, requestStr.data(), requestStr.size() );
431 int ret = nng_send(
m_socket, sendBuf, requestStr.size(), NNG_FLAG_ALLOC );
435 m_lastError = wxString::Format( wxS(
"nng_send failed: %s" ), wxString::FromUTF8( nng_strerror( ret ) ) );
439 char* reply =
nullptr;
440 size_t replySize = 0;
442 ret = nng_recv(
m_socket, &reply, &replySize, NNG_FLAG_ALLOC );
446 m_lastError = wxString::Format( wxS(
"nng_recv failed: %s" ), wxString::FromUTF8( nng_strerror( ret ) ) );
450 std::string responseStr( reply, replySize );
451 nng_free( reply, replySize );
453 if( !aResponse->ParseFromString( responseStr ) )
455 m_lastError = wxS(
"Failed to parse reply from KiCad" );
525 wxProcess::Kill(
m_pid, wxSIGKILL );
527 wxProcess::Kill(
m_pid, wxSIGKILL, wxKILL_CHILDREN );
542 m_socketPath = wxString::Format( wxS(
"/tmp/kicad-api-e2e-%ld.sock" ), wxGetProcessId() );
544 wxString tempPath = wxFileName::CreateTempFileName( wxS(
"kicad-api-e2e" ) );
546 if( wxFileExists( tempPath ) )
547 wxRemoveFile( tempPath );
565 kill(
static_cast<pid_t
>(
m_pid ), SIGTERM );
567 kill(
static_cast<pid_t
>(
m_pid ), SIGKILL );
574 wxProcess::Kill(
m_pid, wxSIGKILL, wxKILL_CHILDREN );
589 if( !wxFileExists( aCliPath ) )
592 *aError = wxS(
"kicad-cli path does not exist: " ) + aCliPath;
602 [
this](
int aStatus,
const wxString& aOutput,
const wxString& aErrOutput )
614 std::vector<const wchar_t*> args = { aCliPath.wc_str(), wxS(
"api-server" ), wxS(
"--socket" ),
625 *aError = wxS(
"Failed to launch kicad-cli api-server" );
635 auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds( 15 );
637 while( std::chrono::steady_clock::now() < deadline )
645 *aError = wxS(
"kicad-cli process exited before ready" );
652 wxLogTrace(
traceApi,
"kicad-cli connect attempt failed, will retry" );
656 kiapi::common::ApiStatusCode pingStatus = kiapi::common::AS_UNKNOWN;
660 if( pingStatus == kiapi::common::AS_OK )
663 if( pingStatus != kiapi::common::AS_NOT_READY )
667 *aError = wxString::Format( wxS(
"Ping returned unexpected status: %s" ),
668 magic_enum::enum_name( pingStatus ) );
674 wxLogTrace(
traceApi,
"kicad-cli connected but returned AS_NOT_READY, will retry" );
678 wxLogTrace(
traceApi,
"kicad-cli ping attempt failed, will retry" );
683 *aError = wxS(
"Timed out waiting for kicad-cli to start up" );
704 pid_t
result = waitpid(
static_cast<pid_t
>(
m_pid ), &status, WNOHANG );
711 if( WIFSIGNALED( status ) )
715 strsignal( WTERMSIG( status ) ) ) );
717 else if( WIFEXITED( status ) )
720 WEXITSTATUS( status ) ) );
727 return wxProcess::Exists(
m_pid );
761 bool Start(
const wxString& aCliPathOverride = wxString() )
763 wxString cliPath = aCliPathOverride.IsEmpty() ?
m_cliPath : aCliPathOverride;
#define QA_KICAD_CLI_PATH
General utilities for PCB file IO for QA programs.
bool Start(const wxString &aCliPathOverride=wxString())
~API_SERVER_E2E_FIXTURE()=default
const wxString & LastError() const
API_TEST_CLIENT & Client()
Manages a single shared kicad-cli api-server process and NNG client across all E2E tests.
API_TEST_CLIENT & Client()
API_SERVER_PROCESS * m_process
bool connectAndWaitReady(wxString *aError)
API_SERVER_MANAGER & operator=(const API_SERVER_MANAGER &)=delete
static API_SERVER_MANAGER & Instance()
bool isProcessAlive() const
API_SERVER_MANAGER(const API_SERVER_MANAGER &)=delete
void collectServerOutput()
bool startServerProcess(const wxString &aCliPath, wxString *aError)
void Reset()
Kill the current server and reset state so EnsureReady() will start fresh.
bool EnsureReady(const wxString &aCliPath, wxString *aError)
Ensure the api-server is running and the shared client is connected and responsive.
void OnTerminate(int aPid, int aStatus) override
API_SERVER_PROCESS(std::function< void(int, const wxString &, const wxString &)> aCallback)
std::function< void(int, const wxString &, const wxString &)> m_callback
static void drainStream(wxInputStream *aStream, wxString &aDest)
bool GetItemsCount(const kiapi::common::types::DocumentSpecifier &aDocument, kiapi::common::types::KiCadObjectType aType, int *aCount)
bool OpenDocument(const wxString &aPath, kiapi::common::types::DocumentType aType, kiapi::common::types::DocumentSpecifier *aDocument=nullptr)
bool RunJob(const T &aJobRequest, kiapi::common::types::RunJobResponse *aResponse)
Send an arbitrary job request and receive a RunJobResponse.
bool sendCommand(const T &aMessage, kiapi::common::ApiResponse *aResponse)
bool CloseDocument(const kiapi::common::types::DocumentSpecifier *aDocument, kiapi::common::ApiStatusCode *aStatus=nullptr)
bool Connect(const wxString &aSocketUrl)
const wxString & LastError() const
bool OpenDocument(const wxString &aPath, kiapi::common::types::DocumentSpecifier *aDocument=nullptr)
bool SendCommand(const T &aMessage, kiapi::common::ApiResponse *aResponse)
bool Ping(kiapi::common::ApiStatusCode *aStatusOut=nullptr)
bool GetFirstFootprint(const kiapi::common::types::DocumentSpecifier &aDocument, FOOTPRINT *aFootprint)
const wxChar *const traceApi
Flag to enable debug output related to the IPC API and its plugin system.
BOOST_TEST_MESSAGE("\n=== Real-World Polygon PIP Benchmark ===\n"<< formatTable(table))
wxString result
Test unit parsing edge cases and error handling.