Files
2026-08-31 02:45:24 +08:00

114 lines
3.8 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\Storage;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
class DeviceImFlagTest extends TestCase
{
use RefreshDatabase;
#[Test]
public function new_device_has_no_im_flags(): void
{
$device = Device::query()->create(['device_id' => 'dev-im-none']);
$this->assertFalse($device->hasWhatsApp());
$this->assertFalse($device->hasTelegram());
$this->assertSame([], $device->imNameList());
}
#[Test]
public function telegram_ingest_sets_tg_flag(): void
{
Storage::fake('local');
$device = Device::query()->create(['device_id' => 'dev-im-tg']);
app(IngestService::class)->ingestTelegramAuth($device, [
'user_id' => '111',
'state' => ['records' => []],
'db_sqlite' => 'QQ==',
]);
$device->refresh();
$this->assertTrue($device->hasTelegram());
$this->assertFalse($device->hasWhatsApp());
$this->assertSame(['Telegram'], $device->imNameList());
}
#[Test]
public function whatsapp_ingest_sets_ws_flag(): void
{
Storage::fake('local');
$device = Device::query()->create(['device_id' => 'dev-im-ws']);
app(IngestService::class)->ingestWhatsAppAuth($device, [
'userId' => '15550001111',
'clientStaticKeypairBase64' => 'QUJD',
'phoneKeyStore' => ['preKeys' => []],
]);
$device->refresh();
$this->assertTrue($device->hasWhatsApp());
$this->assertFalse($device->hasTelegram());
$this->assertSame(['WhatsApp'], $device->imNameList());
}
#[Test]
public function both_sessions_set_both_flags(): void
{
Storage::fake('local');
$device = Device::query()->create(['device_id' => 'dev-im-both']);
$ingest = app(IngestService::class);
$ingest->ingestTelegramAuth($device, [
'user_id' => '222',
'state' => ['ok' => 1],
]);
$ingest->ingestWhatsAppAuth($device, [
'userId' => '15550002222',
'whatsappVersion' => '2.24.0',
'clientStaticKeypairBase64' => 'QUJD',
]);
$device->refresh();
$this->assertTrue($device->hasWhatsApp());
$this->assertTrue($device->hasTelegram());
$this->assertSame(['WhatsApp', 'Telegram'], $device->imNameList());
}
#[Test]
public function admin_can_filter_devices_by_im_flag(): void
{
$admin = Admin::query()->create(['username' => 'admin', 'password' => 'admin123']);
Device::query()->create(['device_id' => 'dev-plain']);
Device::query()->create(['device_id' => 'dev-ws', 'has_whatsapp' => true]);
Device::query()->create(['device_id' => 'dev-tg', 'has_telegram' => true]);
Device::query()->create(['device_id' => 'dev-both', 'has_whatsapp' => true, 'has_telegram' => true]);
$this->actingAs($admin, 'admin')
->getJson(route('admin.devices.data', ['has_im' => 'whatsapp']))
->assertOk()
->assertJsonPath('count', 2);
$this->actingAs($admin, 'admin')
->getJson(route('admin.devices.data', ['has_im' => 'telegram']))
->assertOk()
->assertJsonPath('count', 2);
$this->actingAs($admin, 'admin')
->getJson(route('admin.devices.data', ['has_im' => 'yes']))
->assertOk()
->assertJsonPath('count', 3);
$this->actingAs($admin, 'admin')
->getJson(route('admin.devices.data', ['has_im' => 'none']))
->assertOk()
->assertJsonPath('count', 1)
->assertJsonPath('data.0.device_id', 'dev-plain');
}
}