// tests/manual/test-config-system.cjs - Test configuration system
require('bare-node-runtime/global')
const { getConfig, saveConfig, updateConfig, DEFAULT_CONFIG } = require('../../lib/config.cjs')
const fs = require('bare-fs')
const path = require('bare-path')
console.log('🧪 Testing Configuration System')
console.log('')
// Test 1: Get default config
console.log('Test 1: Load default configuration')
const config1 = getConfig()
console.log('Network:', config1.network)
console.log('Ethereum provider:', config1.blockchain.ethereum.provider)
console.log('Bitcoin provider:', config1.blockchain.bitcoin.provider)
console.log('✅ Default config loaded')
console.log('')
// Test 2: Update config
console.log('Test 2: Update configuration value')
updateConfig('blockchain.ethereum.provider', 'etherscan')
const config2 = getConfig()
console.log('Updated Ethereum provider:', config2.blockchain.ethereum.provider)
if (config2.blockchain.ethereum.provider === 'etherscan') {
console.log('✅ Config update successful')
} else {
console.log('❌ Config update failed')
}
console.log('')
// Test 3: Nested update
console.log('Test 3: Nested configuration update')
updateConfig('blockchain.ethereum.apiKey', 'test-api-key-12345')
const config3 = getConfig()
console.log('API Key:', config3.blockchain.ethereum.apiKey)
if (config3.blockchain.ethereum.apiKey === 'test-api-key-12345') {
console.log('✅ Nested update successful')
} else {
console.log('❌ Nested update failed')
}
console.log('')
// Test 4: Environment variable override
console.log('Test 4: Environment variable override')
process.env.WALLET_NETWORK = 'mainnet'
const config4 = getConfig()
console.log('Network (should be mainnet):', config4.network)
if (config4.network === 'mainnet') {
console.log('✅ Environment override works')
} else {
console.log('❌ Environment override failed')
}
delete process.env.WALLET_NETWORK
console.log('')
// Test 5: Reset to defaults
console.log('Test 5: Reset to defaults')
saveConfig(DEFAULT_CONFIG)
const config5 = getConfig()
console.log('Ethereum provider (should be blockscout):', config5.blockchain.ethereum.provider)
console.log('API Key (should be null):', config5.blockchain.ethereum.apiKey)
if (config5.blockchain.ethereum.provider === 'blockscout' && config5.blockchain.ethereum.apiKey === null) {
console.log('✅ Reset successful')
} else {
console.log('❌ Reset failed')
}
console.log('')
// Test 6: Verify config file exists
console.log('Test 6: Verify config file persistence')
const configFile = path.join(require('bare-os').homedir(), '.hyper-wallet', 'config.json')
if (fs.existsSync(configFile)) {
console.log('✅ Config file exists:', configFile)
const stat = fs.statSync(configFile)
console.log(' Permissions:', (stat.mode & 0o777).toString(8))
if ((stat.mode & 0o777) === 0o600) {
console.log('✅ Correct permissions (0600)')
} else {
console.log('⚠️ Permissions should be 0600')
}
} else {
console.log('❌ Config file not found')
}
console.log('')
// Test 7: Legacy migration (if .etherscan-api-key exists)
console.log('Test 7: Legacy migration')
const legacyFile = path.join(require('bare-os').homedir(), '.hyper-wallet', '.etherscan-api-key')
if (fs.existsSync(legacyFile)) {
console.log('Found legacy API key file')
const { migrateLegacyConfig } = require('../../lib/config.cjs')
migrateLegacyConfig()
const migratedConfig = getConfig()
console.log('After migration, provider:', migratedConfig.blockchain.ethereum.provider)
console.log('After migration, API key:', migratedConfig.blockchain.ethereum.apiKey ? '***' + migratedConfig.blockchain.ethereum.apiKey.slice(-4) : 'null')
} else {
console.log('No legacy API key file found (this is normal)')
console.log('✅ Migration check passed')
}
console.log('')
console.log('🎉 All tests completed!')
console.log('')
console.log('Config file location:', configFile)
console.log('You can edit it manually or use the settings command')