laravel-webrtc/tests/BinaryManagerTest.php

118 lines
3.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Blax\WebRtc\Tests;
use Blax\WebRtc\Media\Rust\BinaryManager;
use Blax\WebRtc\Media\Rust\SidecarException;
use PHPUnit\Framework\TestCase;
final class BinaryManagerTest extends TestCase
{
private string $dir;
protected function setUp(): void
{
$this->dir = sys_get_temp_dir().'/blax-webrtc-binmgr-'.getmypid();
@mkdir($this->dir, 0775, true);
}
protected function tearDown(): void
{
foreach (glob($this->dir.'/*') ?: [] as $file) {
@unlink($file);
}
@rmdir($this->dir);
}
public function test_platform_is_os_dash_arch(): void
{
$platform = (new BinaryManager)->platform();
$this->assertMatchesRegularExpression('/^(linux|darwin|windows|bsd|solaris)-[a-z0-9_]+$/', $platform);
$this->assertStringNotContainsString('amd64', $platform, 'arch aliases are normalized');
$this->assertStringNotContainsString('arm64', $platform);
}
public function test_an_explicit_binary_override_wins(): void
{
$binary = $this->dir.'/custom-sfu';
file_put_contents($binary, '#!/bin/sh');
chmod($binary, 0755);
$manager = new BinaryManager(['binary' => $binary]);
$this->assertSame($binary, $manager->find());
}
public function test_install_downloads_verifies_sha256_and_marks_executable(): void
{
$payload = 'fake-sfu-binary-payload';
file_put_contents($this->dir.'/source', $payload);
// sha256sum format: "<hash> <filename>"
file_put_contents($this->dir.'/source.sha256', hash('sha256', $payload)." blax-webrtc-sfu\n");
$manager = new BinaryManager([
'dev_builds' => false,
'install_path' => $this->dir,
'download_url' => 'file://'.$this->dir.'/source',
'checksum_url' => 'file://'.$this->dir.'/source.sha256',
]);
$installed = $manager->ensureInstalled();
$this->assertSame($manager->installPath(), $installed);
$this->assertSame($payload, file_get_contents($installed));
$this->assertTrue(is_executable($installed));
// Second call finds it without downloading (source removed to prove it).
unlink($this->dir.'/source');
$this->assertSame($installed, $manager->ensureInstalled());
}
public function test_a_checksum_mismatch_aborts_the_install(): void
{
file_put_contents($this->dir.'/source', 'tampered payload');
file_put_contents($this->dir.'/source.sha256', hash('sha256', 'the real payload')."\n");
$manager = new BinaryManager([
'dev_builds' => false,
'install_path' => $this->dir,
'download_url' => 'file://'.$this->dir.'/source',
'checksum_url' => 'file://'.$this->dir.'/source.sha256',
]);
$this->expectException(SidecarException::class);
$this->expectExceptionMessage('checksum mismatch');
$manager->install();
$this->assertFileDoesNotExist($manager->installPath());
}
public function test_auto_install_false_fails_with_actionable_guidance(): void
{
$manager = new BinaryManager([
'dev_builds' => false,
'install_path' => $this->dir,
'auto_install' => false,
]);
$this->expectException(SidecarException::class);
$this->expectExceptionMessage('webrtc:install');
$manager->ensureInstalled();
}
public function test_install_path_is_versioned_and_per_platform(): void
{
$manager = new BinaryManager(['install_path' => '/opt/bin', 'version' => 'v9.9.9']);
$this->assertSame(
'/opt/bin/blax-webrtc-sfu-v9.9.9-'.$manager->platform(),
$manager->installPath(),
);
}
}