mirror of
https://github.com/tiennm99/coolify.git
synced 2026-04-17 15:20:40 +00:00
Fix duplicate proxy restart notifications
- Remove redundant ProxyStatusChangedUI dispatch from RestartProxyJob (ProxyStatusChanged event already triggers the listener that dispatches it) - Remove redundant Traefik version check from RestartProxyJob (already handled by ProxyStatusChangedNotification listener) - Add lastNotifiedStatus tracking to prevent duplicate toasts - Remove notifications for unknown/default statuses (too noisy) - Simplify RestartProxyJob to only handle stop/start logic 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -4,23 +4,17 @@ namespace Tests\Unit\Jobs;
|
||||
|
||||
use App\Actions\Proxy\StartProxy;
|
||||
use App\Actions\Proxy\StopProxy;
|
||||
use App\Enums\ProxyTypes;
|
||||
use App\Events\ProxyStatusChangedUI;
|
||||
use App\Jobs\CheckTraefikVersionForServerJob;
|
||||
use App\Jobs\RestartProxyJob;
|
||||
use App\Models\Server;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Queue\Middleware\WithoutOverlapping;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
use Mockery;
|
||||
use Spatie\Activitylog\Models\Activity;
|
||||
use Tests\TestCase;
|
||||
|
||||
class RestartProxyJobTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Mockery::close();
|
||||
@@ -43,12 +37,8 @@ class RestartProxyJobTest extends TestCase
|
||||
{
|
||||
// Mock Server
|
||||
$server = Mockery::mock(Server::class);
|
||||
$server->shouldReceive('getAttribute')->with('team_id')->andReturn(1);
|
||||
$server->shouldReceive('getAttribute')->with('proxy')->andReturn((object) ['force_stop' => true]);
|
||||
$server->shouldReceive('save')->once();
|
||||
$server->shouldReceive('proxyType')->andReturn(ProxyTypes::TRAEFIK->value);
|
||||
$server->shouldReceive('getAttribute')->with('id')->andReturn(1);
|
||||
$server->shouldReceive('getAttribute')->with('name')->andReturn('test-server');
|
||||
|
||||
// Mock Activity
|
||||
$activity = Mockery::mock(Activity::class);
|
||||
@@ -66,27 +56,12 @@ class RestartProxyJobTest extends TestCase
|
||||
->with($server, force: true, restarting: true)
|
||||
->andReturn($activity);
|
||||
|
||||
// Mock Events
|
||||
Event::fake();
|
||||
Queue::fake();
|
||||
|
||||
// Mock get_traefik_versions helper
|
||||
$this->app->instance('traefik_versions', ['latest' => '2.10']);
|
||||
|
||||
// Execute job
|
||||
$job = new RestartProxyJob($server);
|
||||
$job->handle();
|
||||
|
||||
// Assert activity ID was set
|
||||
$this->assertEquals(123, $job->activity_id);
|
||||
|
||||
// Assert event was dispatched
|
||||
Event::assertDispatched(ProxyStatusChangedUI::class, function ($event) {
|
||||
return $event->teamId === 1 && $event->activityId === 123;
|
||||
});
|
||||
|
||||
// Assert Traefik version check was dispatched
|
||||
Queue::assertPushed(CheckTraefikVersionForServerJob::class);
|
||||
}
|
||||
|
||||
public function test_job_handles_errors_gracefully()
|
||||
@@ -111,50 +86,17 @@ class RestartProxyJobTest extends TestCase
|
||||
|
||||
// Assert error event was dispatched
|
||||
Event::assertDispatched(ProxyStatusChangedUI::class, function ($event) {
|
||||
return $event->teamId === 1 && $event->activityId === null;
|
||||
return $event->teamId === 1;
|
||||
});
|
||||
}
|
||||
|
||||
public function test_job_skips_traefik_version_check_for_non_traefik_proxies()
|
||||
{
|
||||
// Mock Server with non-Traefik proxy
|
||||
$server = Mockery::mock(Server::class);
|
||||
$server->shouldReceive('getAttribute')->with('team_id')->andReturn(1);
|
||||
$server->shouldReceive('getAttribute')->with('proxy')->andReturn((object) ['force_stop' => true]);
|
||||
$server->shouldReceive('save')->once();
|
||||
$server->shouldReceive('proxyType')->andReturn(ProxyTypes::CADDY->value);
|
||||
|
||||
// Mock Activity
|
||||
$activity = Mockery::mock(Activity::class);
|
||||
$activity->id = 123;
|
||||
|
||||
// Mock Actions
|
||||
$stopProxyMock = Mockery::mock('alias:'.StopProxy::class);
|
||||
$stopProxyMock->shouldReceive('run')->once();
|
||||
|
||||
$startProxyMock = Mockery::mock('alias:'.StartProxy::class);
|
||||
$startProxyMock->shouldReceive('run')->once()->andReturn($activity);
|
||||
|
||||
Event::fake();
|
||||
Queue::fake();
|
||||
|
||||
// Execute job
|
||||
$job = new RestartProxyJob($server);
|
||||
$job->handle();
|
||||
|
||||
// Assert Traefik version check was NOT dispatched
|
||||
Queue::assertNotPushed(CheckTraefikVersionForServerJob::class);
|
||||
}
|
||||
|
||||
public function test_job_clears_force_stop_flag()
|
||||
{
|
||||
// Mock Server
|
||||
$proxy = (object) ['force_stop' => true];
|
||||
$server = Mockery::mock(Server::class);
|
||||
$server->shouldReceive('getAttribute')->with('team_id')->andReturn(1);
|
||||
$server->shouldReceive('getAttribute')->with('proxy')->andReturn($proxy);
|
||||
$server->shouldReceive('save')->once();
|
||||
$server->shouldReceive('proxyType')->andReturn('NONE');
|
||||
|
||||
// Mock Activity
|
||||
$activity = Mockery::mock(Activity::class);
|
||||
@@ -167,8 +109,6 @@ class RestartProxyJobTest extends TestCase
|
||||
Mockery::mock('alias:'.StartProxy::class)
|
||||
->shouldReceive('run')->once()->andReturn($activity);
|
||||
|
||||
Event::fake();
|
||||
|
||||
// Execute job
|
||||
$job = new RestartProxyJob($server);
|
||||
$job->handle();
|
||||
@@ -176,4 +116,52 @@ class RestartProxyJobTest extends TestCase
|
||||
// Assert force_stop was set to false
|
||||
$this->assertFalse($proxy->force_stop);
|
||||
}
|
||||
|
||||
public function test_job_stores_activity_id_when_activity_returned()
|
||||
{
|
||||
// Mock Server
|
||||
$server = Mockery::mock(Server::class);
|
||||
$server->shouldReceive('getAttribute')->with('proxy')->andReturn((object) ['force_stop' => true]);
|
||||
$server->shouldReceive('save')->once();
|
||||
|
||||
// Mock Activity
|
||||
$activity = Mockery::mock(Activity::class);
|
||||
$activity->id = 456;
|
||||
|
||||
// Mock Actions
|
||||
Mockery::mock('alias:'.StopProxy::class)
|
||||
->shouldReceive('run')->once();
|
||||
|
||||
Mockery::mock('alias:'.StartProxy::class)
|
||||
->shouldReceive('run')->once()->andReturn($activity);
|
||||
|
||||
// Execute job
|
||||
$job = new RestartProxyJob($server);
|
||||
$job->handle();
|
||||
|
||||
// Assert activity ID was stored
|
||||
$this->assertEquals(456, $job->activity_id);
|
||||
}
|
||||
|
||||
public function test_job_handles_string_return_from_start_proxy()
|
||||
{
|
||||
// Mock Server
|
||||
$server = Mockery::mock(Server::class);
|
||||
$server->shouldReceive('getAttribute')->with('proxy')->andReturn((object) ['force_stop' => true]);
|
||||
$server->shouldReceive('save')->once();
|
||||
|
||||
// Mock Actions - StartProxy returns 'OK' string when proxy is disabled
|
||||
Mockery::mock('alias:'.StopProxy::class)
|
||||
->shouldReceive('run')->once();
|
||||
|
||||
Mockery::mock('alias:'.StartProxy::class)
|
||||
->shouldReceive('run')->once()->andReturn('OK');
|
||||
|
||||
// Execute job
|
||||
$job = new RestartProxyJob($server);
|
||||
$job->handle();
|
||||
|
||||
// Assert activity ID remains null when string returned
|
||||
$this->assertNull($job->activity_id);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user