Files
coolify/tests/Feature/ServerResourcesPaginationTest.php
T
Andras BacsaiandClaude Opus 5.5 a70e856882 test: remove tests that do not test behavior and strengthen weak ones
- Remove tests that only check markup, CSS, static text, wire:key strings,
  framework behavior, copied app logic, or expect(true)
- Replace source-grep and Reflection checks of security fixes with tests
  that run the real code (shell quoting, locked properties, 403 checks,
  webhook fork trust, log redaction, Sentinel token cleanup)
- Assert response bodies and unchanged data on API error paths
- Run the Traefik version job through handle() instead of private methods
- Update stale tests for scheduled task props, the default Postgres image,
  and the disabled transfer link of unvalidated servers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 08:47:56 +01:00

111 lines
4.8 KiB
PHP

<?php
use App\Livewire\Server\Resources;
use App\Models\Server;
beforeEach(function () {
$this->component = new Resources;
$this->component->server = Mockery::mock(Server::class)->makePartial();
});
it('paginates sorted resources on both tabs', function (string $tab, string $nameKey) {
$rows = collect(range(23, 1))->map(fn (int $id) => [$nameKey => "Resource $id"]);
$this->component->activeTab = $tab;
if ($tab === 'managed') {
$this->component->server->shouldReceive('definedResources')->andReturn($rows);
} else {
$this->component->unmanagedContainers = $rows->all();
}
$page = $this->component->render()->getData()['resources'];
expect($page->total())->toBe(23)
->and($page->count())->toBe(10)
->and($page->pluck($nameKey)->all())->toBe(array_map(fn ($id) => "Resource $id", range(1, 10)));
$this->component->nextPage();
$page = $this->component->render()->getData()['resources'];
expect($page->currentPage())->toBe(2)->and($page->count())->toBe(10)
->and($page->first()[$nameKey])->toBe('Resource 11');
$this->component->nextPage();
$page = $this->component->render()->getData()['resources'];
expect($page->count())->toBe(3)->and($page->first()[$nameKey])->toBe('Resource 21');
$this->component->previousPage();
expect($this->component->render()->getData()['resources']->currentPage())->toBe(2);
})->with([['managed', 'name'], ['unmanaged', 'Names']]);
it('resets pagination and search when switching tabs but preserves them on refresh', function () {
$this->component->server->shouldReceive('refresh')->andReturnSelf();
$this->component->server->shouldReceive('loadUnmanagedContainers')->andReturn(collect());
$this->component->setPage(3);
$this->component->search = 'managed resource';
$this->component->loadUnmanagedContainers();
expect($this->component->getPage())->toBe(1)
->and($this->component->search)->toBe('');
$this->component->setPage(2);
$this->component->search = 'container';
$this->component->loadUnmanagedContainers();
expect($this->component->getPage())->toBe(2)
->and($this->component->search)->toBe('container');
$this->component->loadManagedContainers();
expect($this->component->getPage())->toBe(1)
->and($this->component->search)->toBe('');
$this->component->setPage(2);
$this->component->search = 'application';
$this->component->loadManagedContainers();
expect($this->component->getPage())->toBe(2)
->and($this->component->search)->toBe('application');
});
it('clamps page size and resets the page', function (int $size, int $expected) {
$this->component->setPage(3);
$this->component->perPage = $size;
$this->component->updatedPerPage();
expect($this->component->perPage)->toBe($expected)
->and($this->component->getPage())->toBe(1);
})->with([[25, 25], [0, 1], [200, 100]]);
it('clamps stale pages after resources disappear including an empty list', function (int $count, int $expectedPage) {
$this->component->activeTab = 'unmanaged';
$this->component->unmanagedContainers = array_fill(0, $count, ['Names' => 'Container']);
$this->component->setPage(9);
$page = $this->component->render()->getData()['resources'];
expect($page->currentPage())->toBe($expectedPage)
->and($this->component->getPage())->toBe($expectedPage)
->and($page->total())->toBe($count);
})->with([[12, 2], [0, 1]]);
it('searches names across all pages before pagination on both tabs', function (string $tab, string $nameKey) {
$rows = collect(range(1, 25))->map(fn (int $id) => [$nameKey => "Resource $id"]);
$this->component->activeTab = $tab;
if ($tab === 'managed') {
$this->component->server->shouldReceive('definedResources')->andReturn($rows);
} else {
$this->component->unmanagedContainers = $rows->all();
}
$this->component->setPage(3);
$this->component->search = ' RESOURCE 2 ';
$this->component->updatedSearch();
$page = $this->component->render()->getData()['resources'];
expect($page->currentPage())->toBe(1)
->and($page->total())->toBe(7)
->and($page->pluck($nameKey)->all())->toBe([
'Resource 2', 'Resource 20', 'Resource 21', 'Resource 22',
'Resource 23', 'Resource 24', 'Resource 25',
]);
$this->component->search = 'missing';
$this->component->updatedSearch();
expect($this->component->render()->getData()['resources']->total())->toBe(0);
$this->component->search = '';
$this->component->updatedSearch();
$page = $this->component->render()->getData()['resources'];
expect($page->total())->toBe(25)->and($page->currentPage())->toBe(1);
$this->component->search = ' ';
expect($this->component->render()->getData()['resources']->total())->toBe(25);
})->with([['managed', 'name'], ['unmanaged', 'Names']]);