clusev/tests/Feature/ServerContextSyncTest.php

68 lines
2.2 KiB
PHP

<?php
namespace Tests\Feature;
use App\Livewire\Servers\Show;
use App\Livewire\ServerSwitcher;
use App\Models\Server;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;
class ServerContextSyncTest extends TestCase
{
use RefreshDatabase;
private function server(string $name): Server
{
return Server::create(['name' => $name, 'ip' => '10.0.0.'.rand(2, 250), 'ssh_port' => 22, 'status' => 'online']);
}
public function test_viewing_server_details_sets_it_active(): void
{
$this->actingAs(User::factory()->create());
$b = $this->server('B');
Livewire::test(Show::class, ['server' => $b]);
$this->assertSame($b->id, session('active_server_id'));
}
public function test_interacting_with_a_details_page_resyncs_active_server(): void
{
// Simulates a bfcache restore where mount() did not run and the session points
// elsewhere: the next request to the component (poll/action → hydrate) re-syncs.
$this->actingAs(User::factory()->create());
$a = $this->server('A');
$b = $this->server('B');
$component = Livewire::test(Show::class, ['server' => $a]);
session(['active_server_id' => $b->id]); // drift (as after Back/Forward)
$component->call('pollMetrics');
$this->assertSame($a->id, session('active_server_id'));
}
public function test_switching_on_a_details_page_targets_the_new_server(): void
{
$a = $this->server('A');
$b = $this->server('B');
$switcher = new ServerSwitcher;
// On A's details page, selecting B targets B's details (not a reload of A).
$this->assertSame(route('servers.show', $b), $switcher->targetFor($b, '/servers/'.$a->uuid));
}
public function test_switching_elsewhere_keeps_the_current_page(): void
{
$b = $this->server('B');
$switcher = new ServerSwitcher;
// Off a server-details page, the current page is reloaded as-is.
$this->assertSame('/files', $switcher->targetFor($b, '/files'));
$this->assertSame('/servers', $switcher->targetFor($b, '/servers')); // fleet list, not a details page
}
}