Files
dpl/tests/Feature/Server/ServerDisplayOrderTest.php
T
Codex 6e29d4fdf7
Docker Build and Publish / build (push) Waiting to run
Sort user-facing nodes by region across XB instances
2026-11-07 03:24:00 +00:00

97 lines
3.6 KiB
PHP

<?php
namespace Tests\Feature\Server;
use App\Models\Server;
use App\Models\User;
use App\Protocols\ClashMeta;
use App\Services\ServerService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Symfony\Component\Yaml\Yaml;
use Tests\TestCase;
class ServerDisplayOrderTest extends TestCase
{
use RefreshDatabase;
protected function beforeRefreshingDatabase(): void
{
config([
'database.default' => 'sqlite',
'database.connections.sqlite.url' => null,
'database.connections.sqlite.database' => ':memory:',
'cache.default' => 'array',
'cache.stores.redis' => ['driver' => 'array'],
]);
DB::purge('sqlite');
Cache::forgetDriver('redis');
}
public function test_user_list_and_clash_subscription_group_regions_and_keep_aliases_with_their_parent(): void
{
$this->node('🇨🇦 加拿大 - 日本中转', 1);
$this->node('🇺🇸 美国 - 金穗', 2);
$this->node('SG01', 3);
$this->node('日本 JP - 02', 4, 'jp01.example.test');
$this->node('🇭🇰 香港 - 太平', 5, 'hk02.example.test');
$hongKong = $this->node('🇭🇰 香港 - 维港', 6, 'hk01.example.test');
$this->node('🇭🇰 香港 - 扬帆', 16, 'hk01.example.test', ['parent_id' => $hongKong->id]);
$japan = $this->node('🇯🇵 日本 JP - 01', 7, 'jp03.example.test');
$this->node('🇯🇵 日本 - 樱雪', 0, 'jp03.example.test', ['parent_id' => $japan->id]);
$this->node('🇭🇰 香港 - 福来', 8, 'vn01.example.test');
$hidden = $this->node('HK03', 9, 'hk03.example.test', ['group_ids' => ['2']]);
$this->node('HK03 分身', 17, 'hk03.example.test', ['parent_id' => $hidden->id]);
$this->node('其他节点', 10);
$user = new User([
'group_id' => 1,
'uuid' => '123e4567-e89b-12d3-a456-426614174000',
'u' => 0,
'd' => 0,
'transfer_enable' => 1073741824,
'expired_at' => 2000000000,
]);
$expected = [
'🇭🇰 香港 - 维港',
'🇭🇰 香港 - 扬帆',
'🇭🇰 香港 - 太平',
'HK03 分身',
'🇭🇰 香港 - 福来',
'🇯🇵 日本 JP - 01',
'🇯🇵 日本 - 樱雪',
'日本 JP - 02',
'SG01',
'🇺🇸 美国 - 金穗',
'🇨🇦 加拿大 - 日本中转',
'其他节点',
];
$servers = ServerService::getAvailableServers($user);
$this->assertSame($expected, array_column($servers, 'name'));
$response = (new ClashMeta($user, $servers))->handle();
$subscription = Yaml::parse($response->getContent());
$this->assertSame($expected, array_column($subscription['proxies'], 'name'));
$this->assertSame($expected, collect($subscription['proxy-groups'])
->firstWhere('name', '自动选择')['proxies']);
}
private function node(string $name, int $sort, ?string $sni = null, array $overrides = []): Server
{
return Server::create(array_replace([
'name' => $name,
'type' => Server::TYPE_HYSTERIA,
'host' => 'relay.example.test',
'port' => '443',
'server_port' => 443,
'group_ids' => ['1'],
'rate' => 1,
'sort' => $sort,
'show' => true,
'protocol_settings' => ['version' => 2, 'tls' => ['server_name' => $sni]],
], $overrides));
}
}