Files
ObsidianDragon/tests/fake_lite_backend.h
DanS 5586f334a4 feat(lite): real backend integration — controller, M0-M2a wiring, smoke tool, tests
- LiteWalletController (src/wallet/lite_wallet_controller.*): App-owned; runs real
  create/open/restore via the linked SDXL bridge with allowBridgeCalls=true; wipes
  seed/passphrase with sodium_memzero; persists on a ready wallet. M2a:
  applyLiteRefreshModelToWalletState maps a parsed refresh bundle into WalletState
  (zatoshi->DRGX, z/t split, tx typing + confirmations, sync progress).
- App wiring: liteWallet() accessor + init() construction when supportsLiteBackend();
  persist -> settings save.
- settings_page: "Validate" reroutes to the controller for real execution (validation-
  only fallback otherwise); wipes UI secret buffers after submit.
- chain name default -> "main" with load-time migration of legacy "DRAGONX"
  (settings.cpp), preventing the backend "Unknown chain" panic.
- M0: build.sh --lite-backend flag; lite_smoke real-backend tool + CMake targets;
  tests/fake_lite_backend.h deterministic harness.
- Tests (test_phase4): injectable-fake bridge, controller lifecycle, chain-name
  migration, refresh->WalletState mapping; plus the lite test-suite churn-cleanup rewrite.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-04 21:15:44 -05:00

91 lines
3.1 KiB
C++

// 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 <cstdlib>
#include <cstring>
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<char*>(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