Files

87 lines
4.0 KiB
PHP
Raw Permalink Normal View History

2026-10-05 18:10:18 -04:00
<?php
declare(strict_types=1);
namespace Pterodactyl\Tests\Pest\Unit\Services\Extensions\ExtensionRuntimeTest;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\File;
use Pterodactyl\Exceptions\Extensions\InvalidExtensionException;
use Pterodactyl\Services\Extensions\ExtensionLock;
use Pterodactyl\Services\Extensions\ExtensionRuntimeRefresher;
use Pterodactyl\Tests\TestCase;
uses(TestCase::class);
test('package operations exclude competing writers and nested activation retains the lock', function (): void {
$directory = sys_get_temp_dir().'/ptero-lock-'.uniqid();
config(['extensions.directory' => $directory]);
$first = new ExtensionLock;
$second = new ExtensionLock;
try {
$first->acquire('first');
$first->acquire('nested');
expect(fn () => $second->acquire('second'))->toThrow(InvalidExtensionException::class, 'Another operation');
$first->release();
expect(fn () => $second->acquire('second'))->toThrow(InvalidExtensionException::class, 'Another operation');
$first->release();
$second->acquire('second');
$second->release();
} finally {
File::deleteDirectory($directory);
}
});
test('activation invalidates cached routes and signals existing workers to restart', function (): void {
2026-10-07 16:32:56 -04:00
$this->freezeTime();
$bootstrap = $this->app->bootstrapPath();
$directory = sys_get_temp_dir().'/ptero-routes-'.uniqid();
File::ensureDirectoryExists($directory.'/cache');
$this->app->useBootstrapPath($directory);
File::put($this->app->getCachedRoutesPath(), '<?php return [];');
2026-10-05 18:10:18 -04:00
try {
2026-10-07 16:32:56 -04:00
resolve(ExtensionRuntimeRefresher::class)->refresh();
expect($this->app->getCachedRoutesPath())->not->toBeFile();
2026-10-05 18:10:18 -04:00
expect(Cache::get('illuminate:queue:restart'))->toBe(now()->getTimestamp());
} finally {
2026-10-07 16:32:56 -04:00
$this->app->useBootstrapPath($bootstrap);
File::deleteDirectory($directory);
2026-10-05 18:10:18 -04:00
}
});
2026-10-12 07:47:42 -04:00
test('development reload follows asset activation and rollback, tells stylesheet-only builds apart and stays absent outside debug mode', function (): void {
2026-10-05 18:10:18 -04:00
$directory = sys_get_temp_dir().'/ptero-development-'.uniqid();
config(['extensions.assets_directory' => $directory, 'app.debug' => true]);
$assets = resolve(\Pterodactyl\Services\Extensions\ExtensionAssetPublisher::class);
2026-10-12 07:47:42 -04:00
[$first, $second, $restyled] = [str_repeat('a', 64), str_repeat('b', 64), str_repeat('c', 64)];
foreach ([$first => ['one', 'a{}'], $second => ['two', 'a{}'], $restyled => ['one', 'b{}']] as $version => [$script, $style]) {
File::ensureDirectoryExists("{$directory}/probe/{$version}");
File::put("{$directory}/probe/{$version}/client.js", $script);
File::put("{$directory}/probe/{$version}/theme.css", $style);
}
$marker = fn (): array => File::json($directory.'/probe/_development');
2026-10-05 18:10:18 -04:00
try {
$assets->activate('probe', $first);
expect($assets->developmentPayload('probe'))->toBeNull();
$assets->watchDevelopment('probe');
2026-10-12 07:47:42 -04:00
$scripts = $marker()['scripts'];
expect($scripts)->toMatch('/^[a-f0-9]{64}$/');
// Only the stylesheet changed, so the page can swap it in.
$assets->activate('probe', $restyled);
expect($marker())->toBe(['version' => $restyled, 'scripts' => $scripts]);
2026-10-05 18:10:18 -04:00
$assets->activate('probe', $second);
2026-10-12 07:47:42 -04:00
expect($marker()['version'])->toBe($second)->and($marker()['scripts'])->not->toBe($scripts);
2026-10-05 18:10:18 -04:00
$assets->activate('probe', $first);
2026-10-12 07:47:42 -04:00
expect($marker())->toBe(['version' => $first, 'scripts' => $scripts]);
expect($assets->developmentPayload('probe'))->toBe(['url' => '/assets/extensions/probe/_development', 'version' => $first, 'scripts' => $scripts]);
2026-10-05 18:10:18 -04:00
config(['app.debug' => false]);
expect($assets->developmentPayload('probe'))->toBeNull();
$assets->stopDevelopment('probe');
expect($directory.'/probe/_development')->not->toBeFile();
} finally {
File::deleteDirectory($directory);
}
});