@@ -15,6 +15,7 @@ import { expect } from 'chai'
1515import { describe , it } from 'mocha'
1616
1717import { walletCacheSaverConfig } from '../../../src/core/cache/cache-wallet-saver'
18+ import { EdgeToken } from '../../../src/index'
1819import { makeFakeEdgeWorld } from '../../../src/index'
1920import { snooze } from '../../../src/util/snooze'
2021import {
@@ -1347,4 +1348,123 @@ describe('wallet cache', function () {
13471348
13481349 await account2 . logout ( )
13491350 } )
1351+
1352+ // ===========================================================================
1353+ // Config caching tests
1354+ // ===========================================================================
1355+
1356+ it ( 'caches custom tokens on config' , async function ( ) {
1357+ this . timeout ( 10000 )
1358+
1359+ const world = await makeFakeEdgeWorld ( [ fakeUser ] , quiet )
1360+ const context = await world . makeEdgeContext ( {
1361+ ...contextOptions ,
1362+ plugins : { fakecoin : true }
1363+ } )
1364+
1365+ // First login - add a custom token
1366+ const account1 = await context . loginWithPIN ( fakeUser . username , fakeUser . pin )
1367+ const walletInfo = account1 . getFirstWalletInfo ( 'wallet:fakecoin' )
1368+ if ( walletInfo == null ) throw new Error ( 'No wallet' )
1369+
1370+ await account1 . waitForCurrencyWallet ( walletInfo . id )
1371+
1372+ const customToken : EdgeToken = {
1373+ currencyCode : 'CUSTOM' ,
1374+ displayName : 'Custom Token' ,
1375+ denominations : [ { multiplier : '1000' , name : 'CUSTOM' } ] ,
1376+ networkLocation : {
1377+ contractAddress :
1378+ '0X7CD5885327FD60E825D67D32F9D22B018227A208AA3C4819DA15B36B5D5869D3'
1379+ }
1380+ }
1381+ await account1 . currencyConfig . fakecoin . addCustomToken ( customToken )
1382+
1383+ const customTokenId =
1384+ '7cd5885327fd60e825d67d32f9d22b018227a208aa3c4819da15b36b5d5869d3'
1385+ expect ( account1 . currencyConfig . fakecoin . customTokens ) . to . have . property (
1386+ customTokenId
1387+ )
1388+
1389+ // Wait for cache saver to write (throttled to 50ms in tests):
1390+ await snooze ( CACHE_SAVE_WAIT_MS )
1391+ await account1 . logout ( )
1392+
1393+ // Second login - use gate to block engine, verify custom tokens from cache
1394+ const { gate } = createEngineGate ( )
1395+ fakePluginTestConfig . engineGate = gate
1396+
1397+ const account2 = await context . loginWithPIN ( fakeUser . username , fakeUser . pin )
1398+ const cachedConfig = account2 . currencyConfig . fakecoin
1399+
1400+ expect ( cachedConfig . customTokens ) . to . have . property ( customTokenId )
1401+ expect ( cachedConfig . customTokens [ customTokenId ] . currencyCode ) . equals (
1402+ 'CUSTOM'
1403+ )
1404+
1405+ // Custom tokens should also appear in allTokens
1406+ expect ( cachedConfig . allTokens ) . to . have . property ( customTokenId )
1407+
1408+ await account2 . logout ( )
1409+ } )
1410+
1411+ it ( 'config getters delegate to real config after engine loads' , async function ( ) {
1412+ this . timeout ( 10000 )
1413+
1414+ const world = await makeFakeEdgeWorld ( [ fakeUser ] , quiet )
1415+ const context = await world . makeEdgeContext ( {
1416+ ...contextOptions ,
1417+ plugins : { fakecoin : true }
1418+ } )
1419+
1420+ // First login - add a custom token to populate the cache
1421+ const account1 = await context . loginWithPIN ( fakeUser . username , fakeUser . pin )
1422+ const walletInfo = account1 . getFirstWalletInfo ( 'wallet:fakecoin' )
1423+ if ( walletInfo == null ) throw new Error ( 'No wallet' )
1424+
1425+ await account1 . waitForCurrencyWallet ( walletInfo . id )
1426+
1427+ const customToken : EdgeToken = {
1428+ currencyCode : 'CUSTOM' ,
1429+ displayName : 'Custom Token' ,
1430+ denominations : [ { multiplier : '1000' , name : 'CUSTOM' } ] ,
1431+ networkLocation : {
1432+ contractAddress :
1433+ '0X7CD5885327FD60E825D67D32F9D22B018227A208AA3C4819DA15B36B5D5869D3'
1434+ }
1435+ }
1436+ await account1 . currencyConfig . fakecoin . addCustomToken ( customToken )
1437+
1438+ // Wait for cache saver to write (throttled to 50ms in tests):
1439+ await snooze ( CACHE_SAVE_WAIT_MS )
1440+ await account1 . logout ( )
1441+
1442+ // Second login - gate blocks engine
1443+ const { gate, release } = createEngineGate ( )
1444+ fakePluginTestConfig . engineGate = gate
1445+
1446+ const account2 = await context . loginWithPIN ( fakeUser . username , fakeUser . pin )
1447+ const cachedConfig = account2 . currencyConfig . fakecoin
1448+
1449+ // In cache mode: builtinTokens should have cached data
1450+ expect ( cachedConfig . builtinTokens ) . to . have . property ( 'badf00d5' )
1451+
1452+ // In cache mode: customTokens should have cached data
1453+ const customTokenId =
1454+ '7cd5885327fd60e825d67d32f9d22b018227a208aa3c4819da15b36b5d5869d3'
1455+ expect ( cachedConfig . customTokens ) . to . have . property ( customTokenId )
1456+
1457+ // Release the gate to allow real config to load
1458+ release ( )
1459+
1460+ // Use a delegating write method to confirm the real config is loaded,
1461+ // then verify getters delegate to the real config:
1462+ await cachedConfig . changeAlwaysEnabledTokenIds ( [ 'badf00d5' ] )
1463+
1464+ expect ( cachedConfig . alwaysEnabledTokenIds ) . deep . equals ( [ 'badf00d5' ] )
1465+ expect ( cachedConfig . builtinTokens ) . to . have . property ( 'badf00d5' )
1466+ expect ( cachedConfig . customTokens ) . to . have . property ( customTokenId )
1467+
1468+ await account2 . logout ( )
1469+ } )
13501470} )
0 commit comments