quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

test_wallet_sqlite_lifecycle.js (2989B)


      1 import * as os from "os";
      2 
      3 function assert(condition, message) {
      4   if (!condition) throw new Error(message);
      5 }
      6 
      7 const dbPath = `/tmp/qtart-wallet-lifecycle-${Date.now()}.sqlite3`;
      8 const idbDbPath = `/tmp/qtart-wallet-idb-lifecycle-${Date.now()}.sqlite3`;
      9 const { createNativeWalletHost2 } = globalThis.talerModules.talerWalletCore;
     10 
     11 try {
     12   const { wallet } = await createNativeWalletHost2({
     13     persistentStoragePath: dbPath,
     14   });
     15   const init = await wallet.handleCoreApiRequest("initWallet", "init", {
     16     config: {
     17       lazyTaskLoop: true,
     18       testing: { skipDefaults: true },
     19       features: { migrateNativeDb: false, useNativeDb: true },
     20     },
     21   });
     22   assert(init.type === "response", `init failed: ${JSON.stringify(init)}`);
     23   assert(
     24     init.result.databaseBackend === "sqlite",
     25     `unexpected backend: ${JSON.stringify(init.result)}`,
     26   );
     27 
     28   const balances = await wallet.handleCoreApiRequest(
     29     "getBalances",
     30     "balances",
     31     {},
     32   );
     33   assert(
     34     balances.type === "response",
     35     `balance query failed: ${JSON.stringify(balances)}`,
     36   );
     37 
     38   const shutdown = await wallet.handleCoreApiRequest(
     39     "shutdown",
     40     "shutdown",
     41     {},
     42   );
     43   assert(
     44     shutdown.type === "response",
     45     `shutdown failed: ${JSON.stringify(shutdown)}`,
     46   );
     47   const repeated = await wallet.handleCoreApiRequest(
     48     "shutdown",
     49     "shutdown-again",
     50     {},
     51   );
     52   assert(
     53     repeated.type === "response",
     54     `repeated shutdown failed: ${JSON.stringify(repeated)}`,
     55   );
     56 
     57   try {
     58     await wallet.handleCoreApiRequest("getBalances", "after-shutdown", {});
     59     throw new Error("request after shutdown unexpectedly succeeded");
     60   } catch (error) {
     61     assert(
     62       error?.errorDetail?.code === 7011,
     63       `unexpected post-shutdown error: ${error?.stack ?? error}`,
     64     );
     65   }
     66 
     67   // The legacy IndexedDB emulation owns the same kind of raw SQLite
     68   // connection.  Its terminal close must dispose the backend statement cache
     69   // before releasing that connection too.
     70   const { wallet: idbWallet } = await createNativeWalletHost2({
     71     persistentStoragePath: idbDbPath,
     72   });
     73   const idbInit = await idbWallet.handleCoreApiRequest(
     74     "initWallet",
     75     "idb-init",
     76     {
     77       config: {
     78         lazyTaskLoop: true,
     79         testing: { skipDefaults: true },
     80         features: { migrateNativeDb: false, useNativeDb: false },
     81       },
     82     },
     83   );
     84   assert(
     85     idbInit.type === "response",
     86     `IDB init failed: ${JSON.stringify(idbInit)}`,
     87   );
     88   assert(
     89     idbInit.result.databaseBackend === "indexeddb",
     90     `unexpected IDB backend: ${JSON.stringify(idbInit.result)}`,
     91   );
     92   const idbShutdown = await idbWallet.handleCoreApiRequest(
     93     "shutdown",
     94     "idb-shutdown",
     95     {},
     96   );
     97   assert(
     98     idbShutdown.type === "response",
     99     `IDB shutdown failed: ${JSON.stringify(idbShutdown)}`,
    100   );
    101 } finally {
    102   os.remove(dbPath);
    103   os.remove(`${dbPath}-wal`);
    104   os.remove(`${dbPath}-shm`);
    105   os.remove(idbDbPath);
    106   os.remove(`${idbDbPath}-wal`);
    107   os.remove(`${idbDbPath}-shm`);
    108 }