// DragonX Wallet - ImGui Edition // Copyright 2024-2026 The Hush Developers // Released under the GPLv3 // // Deterministic in-process fake SDXL backend for lite-wallet tests. // // Provides a LiteClientBridgeApi whose function pointers return canned JSON and // track owned-string allocation/free counts, so tests can drive the lite bridge // (and, from M1 on, the lite services) without a real litelib_* library, network, // or the DRAGONX_ENABLE_LITE_BACKEND build flag. Build a bridge with: // // auto bridge = dragonx::wallet::LiteClientBridge::fromApi(dragonx::test::makeFakeLiteApi()); // // Invariant for leak/double-free checks: after a test, g_liteFakeAlloc == g_liteFakeFreed. #pragma once #include "wallet/lite_client_bridge.h" #include #include namespace dragonx { namespace test { // Owned-string accounting (C++17 inline vars: single definition across TUs). inline long g_liteFakeAlloc = 0; // owned strings handed to the bridge inline long g_liteFakeFreed = 0; // owned strings released via freeString inline bool g_liteFakeWalletExists = true; inline bool g_liteFakeServerOnline = true; inline bool g_liteFakeShutdownCalled = false; inline void resetLiteFakeCounters() { g_liteFakeAlloc = 0; g_liteFakeFreed = 0; g_liteFakeShutdownCalled = false; } inline char* liteFakeDup(const char* s) { char* p = static_cast(std::malloc(std::strlen(s) + 1)); std::strcpy(p, s); ++g_liteFakeAlloc; return p; } inline bool liteFakeWalletExists(const char*) { return g_liteFakeWalletExists; } inline char* liteFakeInitNew(bool, const char*) { return liteFakeDup("{\"result\":\"created\"}"); } inline char* liteFakeInitFromPhrase(bool, const char*, const char*, unsigned long long, unsigned long long, bool) { return liteFakeDup("{\"result\":\"restored\"}"); } inline char* liteFakeInitExisting(bool, const char*) { return liteFakeDup("{\"result\":\"opened\"}"); } inline char* liteFakeExecute(const char* command, const char*) { // A command named "boom" yields an "Error:"-prefixed response (the bridge's // looksLikeError contract maps that to ok=false). if (command && std::strcmp(command, "boom") == 0) { return liteFakeDup("Error: simulated lite backend failure"); } return liteFakeDup("{\"version\":\"sdxl-fake\"}"); } inline void liteFakeFree(char* v) { if (v) { ++g_liteFakeFreed; std::free(v); } } inline bool liteFakeServerOnline(const char*) { return g_liteFakeServerOnline; } inline void liteFakeShutdown() { g_liteFakeShutdownCalled = true; } inline dragonx::wallet::LiteClientBridgeApi makeFakeLiteApi() { dragonx::wallet::LiteClientBridgeApi api; api.walletExists = &liteFakeWalletExists; api.initializeNew = &liteFakeInitNew; api.initializeNewFromPhrase = &liteFakeInitFromPhrase; api.initializeExisting = &liteFakeInitExisting; api.execute = &liteFakeExecute; api.freeString = &liteFakeFree; api.checkServerOnline = &liteFakeServerOnline; api.shutdown = &liteFakeShutdown; return api; } } // namespace test } // namespace dragonx