33 lines
2.0 KiB
PHP
33 lines
2.0 KiB
PHP
<?php
|
|
namespace Tests\Unit\Freenode;
|
|
use App\Services\Freenode\{Builder, Codec, NativeValidator};
|
|
use PHPUnit\Framework\TestCase;
|
|
use Symfony\Component\Yaml\Yaml;
|
|
|
|
final class BuilderTest extends TestCase
|
|
{
|
|
public function testNativeValidatedBuildPreservesSourcesAndExcludesMalformedNodes(): void
|
|
{
|
|
$sb = getenv('TEST_SINGBOX'); $mh = getenv('TEST_MIHOMO');
|
|
if (!$sb || !$mh) self::markTestSkipped('Set TEST_SINGBOX and TEST_MIHOMO for native validation');
|
|
$uri = 'vless://00000000-0000-0000-0000-000000000001@[2001:db8::1]:443?type=ws&path=%2Fa%26b%2Bc&security=tls&sni=example.invalid#name';
|
|
$clash = Yaml::dump(['proxies' => [
|
|
['name' => 'valid', 'type' => 'ss', 'server' => 'example.invalid', 'port' => 443, 'cipher' => 'aes-128-gcm', 'password' => 'synthetic'],
|
|
['name' => 'bad', 'type' => 'ss', 'server' => 'example.invalid', 'port' => 443, 'cipher' => 'invalid-test-cipher', 'password' => 'synthetic'],
|
|
]]);
|
|
$sources = [
|
|
['name' => 'a', 'v2ray' => "\xEF\xBB\xBF".$uri."\nsocks://example.invalid:0#bad", 'clash' => $clash],
|
|
['name' => 'b', 'v2ray' => $uri, 'clash' => $clash],
|
|
];
|
|
$validator = new NativeValidator($sb, $mh);
|
|
$result = (new Builder())->build($sources, [['code' => 'test', 'clients' => ['v2ray', 'singbox', 'clash']]], $validator);
|
|
self::assertSame(['v2ray' => 1, 'clash' => 1, 'singbox' => 1], $result['report']['counts']);
|
|
self::assertSame(['a', 'b'], array_values($result['report']['nodes']['uri'])[0]);
|
|
self::assertCount(1, $result['report']['native_rejected']['clash']);
|
|
$out = json_decode($result['files']['test/singbox'], true);
|
|
self::assertSame('/a&b+c', $out['outbounds'][2]['transport']['path']);
|
|
self::assertSame($result['files']['base/clash'], $result['files']['test/clash']);
|
|
self::assertSame(Codec::parse($uri)['id'], Codec::parse(Codec::decode64($result['files']['test/v2ray']))['id']);
|
|
}
|
|
}
|