mirror of
https://github.com/logos-co/logos-protocol.git
synced 2026-08-31 05:51:08 +00:00
* Extract the Logos protocol layer from logos-cpp-sdk
Transports (plain TCP/TLS, qt_local, qt_remote/QRO, mock), token manager,
consumer core (LogosAPIClient/LogosAPIConsumer incl. the capability
auto-requestModule flow), ModuleProxy, the abstract LogosProviderObject
interface, and the canonical QVariant<->JSON conversion — now behind the
language-neutral lp_* C ABI (logos_protocol.h) carrying the protocol
semver (LOGOS_PROTOCOL_VERSION_*, lp_protocol_version()).
Bytes crossing the ABI use the lossless {"_bytes": base64url} tagging
(NUL-safe), matching the plain wire encoding.
Provider lp_* surface is compiled groundwork; serving lands with module
authoring.
* consumer: typed requestModule for the capability flow
Port of logos-cpp-sdk master f5a127dd ('use updated capability module',
cpp-sdk#85, Iuri Matias) — the touched files (logos_api_client.cpp,
logos_api_consumer.{h,cpp}) moved into this repo in the P1 extraction.
The capability auto-requestModule path now calls a typed std::string
helper on the consumer (which acquires the capability object directly)
instead of a stringly invokeRemoteMethod round-trip. 111/111 tests.
107 lines
3.2 KiB
C++
107 lines
3.2 KiB
C++
#ifndef LOGOS_MOCK_H
|
|
#define LOGOS_MOCK_H
|
|
|
|
/**
|
|
* @file logos_mock.h
|
|
* @brief Convenience header for unit tests that use the mock transport.
|
|
*
|
|
* Include this file in test source files. It pulls in everything needed to
|
|
* set up mock expectations and verify calls:
|
|
*
|
|
* #include "logos_mock.h"
|
|
*
|
|
* LOGOS_TEST(my_test) {
|
|
* LogosMockSetup mock;
|
|
* mock.when("other_module", "someMethod").thenReturn(QVariant(42));
|
|
*
|
|
* // ... exercise code under test ...
|
|
*
|
|
* LOGOS_ASSERT(mock.wasCalled("other_module", "someMethod"));
|
|
* }
|
|
*/
|
|
|
|
#include "../../logos_mode.h"
|
|
#include "../../token_manager.h"
|
|
#include "mock_store.h"
|
|
#include <QString>
|
|
#include <QVariant>
|
|
#include <QVariantList>
|
|
|
|
/**
|
|
* @brief RAII guard that activates mock mode for the duration of a test.
|
|
*
|
|
* Construction:
|
|
* - Switches the SDK to LogosMode::Mock.
|
|
* - Resets MockStore (clears all expectations and call records).
|
|
* - Clears the TokenManager singleton so that no stale tokens from
|
|
* previous tests influence token lookup in LogosAPIClient.
|
|
*
|
|
* Destruction:
|
|
* - Restores the previous LogosMode.
|
|
* - Resets MockStore again (belt-and-suspenders cleanup).
|
|
*
|
|
* Token pre-seeding:
|
|
* when() automatically registers a non-empty dummy token for the target
|
|
* module so that LogosAPIClient::invokeRemoteMethod() does not attempt to
|
|
* call capability_module.requestModule() before invoking the mock.
|
|
*/
|
|
class LogosMockSetup {
|
|
public:
|
|
LogosMockSetup()
|
|
: m_previousMode(LogosModeConfig::getMode())
|
|
{
|
|
LogosModeConfig::setMode(LogosMode::Mock);
|
|
MockStore::instance().reset();
|
|
TokenManager::instance().clearAllTokens();
|
|
}
|
|
|
|
~LogosMockSetup()
|
|
{
|
|
MockStore::instance().reset();
|
|
LogosModeConfig::setMode(m_previousMode);
|
|
}
|
|
|
|
/**
|
|
* @brief Register a mock expectation for module::method.
|
|
*
|
|
* Also seeds a dummy token for the module so LogosAPIClient does not
|
|
* try to call capability_module.requestModule().
|
|
*
|
|
* @return A fluent builder to configure arguments and return value.
|
|
*/
|
|
MockStore::ExpectationBuilder when(const QString& module, const QString& method)
|
|
{
|
|
// Pre-seed a token so LogosAPIClient skips the capability_module lookup
|
|
TokenManager::instance().saveToken(module, "mock-token-" + module);
|
|
return MockStore::instance().when(module, method);
|
|
}
|
|
|
|
// ── Verification helpers (delegates to MockStore) ────────────────────────
|
|
|
|
bool wasCalled(const QString& module, const QString& method) const
|
|
{
|
|
return MockStore::instance().wasCalled(module, method);
|
|
}
|
|
|
|
bool wasCalledWith(const QString& module, const QString& method,
|
|
const QVariantList& args) const
|
|
{
|
|
return MockStore::instance().wasCalledWith(module, method, args);
|
|
}
|
|
|
|
int callCount(const QString& module, const QString& method) const
|
|
{
|
|
return MockStore::instance().callCount(module, method);
|
|
}
|
|
|
|
QVariantList lastArgs(const QString& module, const QString& method) const
|
|
{
|
|
return MockStore::instance().lastArgs(module, method);
|
|
}
|
|
|
|
private:
|
|
LogosMode m_previousMode;
|
|
};
|
|
|
|
#endif // LOGOS_MOCK_H
|