Files
pterodactyl-panel/tests/Unit/Services/Extensions/ExtensionCompatibilityTest.php
T
6c0413e39b Start 2.0 development
Co-authored-by: Anthony <anthony@infraly.co>
Co-authored-by: Sky Mulley <daniel.mulley@outlook.com>
2026-10-05 18:10:18 -04:00

146 lines
8.0 KiB
PHP

<?php
declare(strict_types=1);
namespace Pterodactyl\Tests\Pest\Unit\Services\Extensions\ExtensionCompatibilityTest;
use Pterodactyl\Exceptions\Extensions\InvalidExtensionException;
use Pterodactyl\Services\Extensions\ExtensionCompatibility;
use Pterodactyl\Services\Extensions\ExtensionManifest;
use Pterodactyl\Tests\TestCase;
uses(TestCase::class);
beforeEach(function (): void {
config(['extensions.panel_version' => '2.0.0', 'extensions.sdk_version' => '2.0.0-beta.1']);
});
/** @param array{panel?: string, sdk?: string, php?: string, extensions?: array<string, string>} $requires */
function manifest(string $id, array $requires = [], string $version = '1.0.0'): ExtensionManifest
{
return ExtensionManifest::fromValidatedData('', ['id' => $id, 'name' => $id, 'version' => $version, 'requires' => $requires], [], null, 'native', null);
}
test('orders enabled extensions after their dependencies', function (): void {
$child = manifest('child', ['panel' => '^2.0', 'sdk' => '>=2.0.0-beta.1 <3.0', 'extensions' => ['base' => '^1.2']]);
$base = manifest('base', version: '1.3.0');
$enabled = collect(['child' => $child, 'base' => $base]);
$result = resolve(ExtensionCompatibility::class)->resolve($enabled);
expect($result['manifests']->keys()->all())->toBe(['base', 'child']);
expect($result['errors'])->toBe([]);
expect($enabled->keys()->all())->toBe(['child', 'base']);
});
test('isolates invalid dependencies while preserving independent extensions', function (): void {
$enabled = collect([
'healthy' => manifest('healthy'),
'future' => manifest('future', ['panel' => '^3.0']),
'dependent' => manifest('dependent', ['extensions' => ['future' => '*']]),
'missing' => manifest('missing', ['extensions' => ['absent' => '*']]),
'wrong-sdk' => manifest('wrong-sdk', ['sdk' => '^3.0']),
'wrong-php' => manifest('wrong-php', ['php' => '>=99.0']),
'wrong-version' => manifest('wrong-version', ['extensions' => ['healthy' => '^2.0']]),
'cycle-a' => manifest('cycle-a', ['extensions' => ['cycle-b' => '*']]),
'cycle-b' => manifest('cycle-b', ['extensions' => ['cycle-a' => '*']]),
]);
$result = resolve(ExtensionCompatibility::class)->resolve($enabled);
expect($result['manifests']->keys()->all())->toBe(['healthy']);
expect($result['errors'])->toHaveKeys(['future', 'dependent', 'missing', 'wrong-sdk', 'wrong-php', 'wrong-version', 'cycle-a', 'cycle-b']);
expect($result['errors']['dependent'])->toContain('unavailable extension "future"');
expect($result['errors']['cycle-a'])->toContain('circular');
});
test('rejects upgrades and disabling that would break an enabled dependent', function (): void {
$base = manifest('base');
$child = manifest('child', ['extensions' => ['base' => '^1.0']]);
$enabled = collect(['base' => $base, 'child' => $child]);
$compatibility = resolve(ExtensionCompatibility::class);
expect(fn () => $compatibility->assertCompatible(manifest('base', version: '2.0.0'), $enabled))->toThrow(InvalidExtensionException::class, 'child');
expect($enabled->get('base'))->toBe($base);
expect(fn () => $compatibility->assertCanDisable('base', $enabled))->toThrow(InvalidExtensionException::class, 'child');
$compatibility->assertCanDisable('child', $enabled);
});
/** @param list<string> $components */
function presentation(string $id, array $components): ExtensionManifest
{
return ExtensionManifest::fromValidatedData('', ['id' => $id, 'name' => $id, 'version' => '1.0.0', 'ui' => ['entry' => 'dist/client.js', 'components' => $components]], [], 'dist/client.js', 'native', null);
}
test('rejects conflicting component ownership while preserving unrelated extensions', function (): void {
$enabled = collect([
'first' => presentation('first', ['dashboard.serverCard']),
'second' => presentation('second', ['dashboard.serverCard']),
'healthy' => presentation('healthy', ['server.files.details']),
]);
$compatibility = resolve(ExtensionCompatibility::class);
$result = $compatibility->resolve($enabled);
expect($result['manifests']->keys()->all())->toBe(['healthy']);
expect($result['errors']['first'])->toContain('dashboard.serverCard', 'second');
expect($result['errors']['second'])->toContain('dashboard.serverCard', 'first');
expect(fn () => $compatibility->assertCompatible(presentation('third', ['dashboard.serverCard']), $enabled))->toThrow(InvalidExtensionException::class, 'dashboard.serverCard');
});
test('allows an owner to update itself and ownership to transfer after disabling', function (): void {
$compatibility = resolve(ExtensionCompatibility::class);
$first = presentation('first', ['dashboard.serverCard']);
$enabled = collect(['first' => $first]);
$compatibility->assertCompatible(presentation('first', ['dashboard.serverCard']), $enabled);
expect($enabled->get('first'))->toBe($first);
$compatibility->assertCompatible(presentation('second', ['dashboard.serverCard']), collect());
});
/** @param list<string> $prefixes */
function rooted(string $id, array $prefixes): ExtensionManifest
{
return ExtensionManifest::fromValidatedData('', ['id' => $id, 'name' => $id, 'version' => '1.0.0', 'routes' => ['root' => $prefixes]], [], null, 'native', null);
}
test('rejects root path prefixes another enabled extension already claims', function (): void {
$enabled = collect([
'first' => rooted('first', ['go', 'links']),
'second' => rooted('second', ['go']),
'healthy' => rooted('healthy', ['status']),
]);
$compatibility = resolve(ExtensionCompatibility::class);
$result = $compatibility->resolve($enabled);
expect($result['manifests']->keys()->all())->toBe(['healthy']);
expect($result['errors']['first'])->toContain('root path "/go"', 'second');
expect($result['errors']['second'])->toContain('root path "/go"', 'first');
expect(fn () => $compatibility->assertCompatible(rooted('third', ['status']), collect(['healthy' => $enabled['healthy']])))->toThrow(InvalidExtensionException::class, 'root path "/status"');
$compatibility->assertCompatible(rooted('healthy', ['status', 'uptime']), collect(['healthy' => $enabled['healthy']]));
$compatibility->assertCompatible(rooted('third', ['status']), collect());
});
function styled(string $id, ?string $prefix): ExtensionManifest
{
return ExtensionManifest::fromValidatedData('', ['id' => $id, 'name' => $id, 'version' => '1.0.0', 'ui' => array_filter(['entry' => 'dist/client.js', 'prefix' => $prefix])], [], 'dist/client.js', 'native', null);
}
test('rejects a tailwind prefix another enabled extension already uses', function (): void {
$enabled = collect([
'first' => styled('first', 'shared'),
'second' => styled('second', 'shared'),
'healthy' => styled('healthy', 'hl'),
'plain' => styled('plain', null),
'bare' => styled('bare', null),
]);
$compatibility = resolve(ExtensionCompatibility::class);
$result = $compatibility->resolve($enabled);
expect($result['manifests']->keys()->all())->toBe(['healthy', 'plain', 'bare']);
expect($result['errors']['first'])->toBe('Extension "first" cannot use Tailwind prefix "shared": enabled extension "second" also declares it.');
expect($result['errors']['second'])->toBe('Extension "second" cannot use Tailwind prefix "shared": enabled extension "first" also declares it.');
expect(fn () => $compatibility->assertCompatible(styled('third', 'hl'), collect(['healthy' => $enabled['healthy']])))->toThrow(InvalidExtensionException::class, 'Extension "third" cannot use Tailwind prefix "hl": enabled extension "healthy" also declares it.');
// An upgrade keeps its own prefix, and a prefix is free again once its owner is disabled.
$compatibility->assertCompatible(styled('healthy', 'hl'), collect(['healthy' => $enabled['healthy']]));
$compatibility->assertCompatible(styled('third', 'hl'), collect());
$compatibility->assertCompatible(styled('third', null), collect(['plain' => $enabled['plain']]));
});