Files
coruna-lab/tests/Unit/WalletSourceTest.php
T
root c894f498b8 Prefer Trust Wallet raw value/total over fiat balance when ingesting addresses.
Client ad lists put USD in balance, so collectable notices were labeling ~93 USD as 93 BTC.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-12 15:05:27 +00:00

114 lines
4.8 KiB
PHP

<?php
namespace Tests\Unit;
use App\Support\WalletSource;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
class WalletSourceTest extends TestCase
{
#[Test]
public function plugin_wallet_bundles_match_mnemonic_plugins_only(): void
{
$this->assertTrue(WalletSource::isPluginWalletBundle('io.metamask.MetaMask'));
$this->assertTrue(WalletSource::isPluginWalletBundle('im.token.app'));
$this->assertTrue(WalletSource::isPluginWalletBundle('com.tronlink.hdwallet'));
$this->assertFalse(WalletSource::isPluginWalletBundle('ph.telegra.Telegraph'));
$this->assertFalse(WalletSource::isPluginWalletBundle('net.whatsapp.WhatsApp'));
$this->assertFalse(WalletSource::isPluginWalletBundle('imagent'));
$this->assertFalse(WalletSource::isPluginWalletBundle('com.apple.mobilesafari'));
$this->assertFalse(WalletSource::isPluginWalletBundle(''));
}
#[Test]
public function supported_chains_include_sol_and_arb(): void
{
$this->assertTrue(WalletSource::isSupportedChain('ETHEREUM'));
$this->assertTrue(WalletSource::isSupportedChain('TRON'));
$this->assertTrue(WalletSource::isSupportedChain('BITCOIN'));
$this->assertTrue(WalletSource::isSupportedChain('BNB'));
$this->assertTrue(WalletSource::isSupportedChain('SOLANA'));
$this->assertTrue(WalletSource::isSupportedChain('SOL'));
$this->assertTrue(WalletSource::isSupportedChain('ARBITRUM'));
$this->assertTrue(WalletSource::isSupportedChain('ARB'));
$this->assertFalse(WalletSource::isSupportedChain('TON'));
$this->assertFalse(WalletSource::isSupportedChain('UNKNOWN'));
}
#[Test]
public function keystore_hint_maps_known_wallets_and_stays_empty_when_unknown(): void
{
$this->assertSame('Trust Wallet', WalletSource::fromKeystoreHint('trustwallet'));
$this->assertSame('Trust Wallet', WalletSource::fromKeystoreHint('d'));
$this->assertSame('imToken', WalletSource::fromKeystoreHint('b'));
$this->assertSame('imToken', WalletSource::fromKeystoreHint('imtoken'));
$this->assertSame('Telegram', WalletSource::fromKeystoreHint('tg'));
$this->assertSame('Telegram', WalletSource::fromKeystoreHint('Telegram'));
$this->assertSame('WhatsApp', WalletSource::fromKeystoreHint('wp'));
$this->assertSame('WhatsApp', WalletSource::fromKeystoreHint('whatsapp'));
$this->assertSame('iMessage', WalletSource::fromKeystoreHint('sms'));
$this->assertSame('', WalletSource::fromKeystoreHint('notes'));
$this->assertSame('', WalletSource::fromKeystoreHint('not-a-wallet'));
$this->assertSame('', WalletSource::fromKeystoreHint(''));
$this->assertSame('d', WalletSource::tagForLabel('Trust Wallet'));
$this->assertSame('b', WalletSource::tagForLabel('imToken'));
$this->assertSame('k', WalletSource::tagForLabel('Exodus'));
$this->assertSame('h', WalletSource::tagForLabel('Uniswap'));
$this->assertSame('i', WalletSource::tagForLabel('Phantom'));
}
#[Test]
public function trust_wallet_prefers_raw_value_over_fiat_balance(): void
{
$raw = WalletSource::clientAssetAmount([
'symbol' => 'BTC',
'decimals' => 8,
'balance' => 93.01116944632426,
'value' => '112737',
'total' => '112737',
]);
$this->assertSame('112737', $raw);
$this->assertEqualsWithDelta(0.00112737, (float) WalletSource::formatBalance($raw, 8), 1e-12);
}
#[Test]
public function client_asset_amount_falls_back_to_balance_when_no_raw_integer(): void
{
$raw = WalletSource::clientAssetAmount([
'symbol' => 'TRX',
'decimal' => '6',
'balance' => '83504102',
]);
$this->assertSame('83504102', $raw);
$this->assertEqualsWithDelta(83.504102, (float) WalletSource::formatBalance($raw, 6), 1e-12);
}
#[Test]
public function client_asset_amount_ignores_decimal_value_field(): void
{
$raw = WalletSource::clientAssetAmount([
'balance' => '112737',
'value' => '93.01',
]);
$this->assertSame('112737', $raw);
}
#[Test]
public function mnemonic_source_options_are_wallets_plus_manual(): void
{
$options = WalletSource::mnemonicSourceOptions();
$this->assertContains('imToken', $options);
$this->assertContains('TronLink', $options);
$this->assertContains('Trust Wallet', $options);
$this->assertContains('手动添加', $options);
$this->assertSame('手动添加', $options[array_key_last($options)]);
$this->assertNotContains('Telegram', $options);
$this->assertNotContains('WhatsApp', $options);
$this->assertNotContains('iMessage', $options);
}
}