<?php

declare(strict_types=1);

namespace Tests\Feature;

use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Support\Facades\Log;
use Livewire\Mechanisms\HandleComponents\CorruptComponentPayloadException;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
use TypeError;

/**
 * #59 — El ruido benigno de Livewire (snapshots viejos/incompatibles que
 * llegan al polling de notificaciones de Filament) no debe ensuciar el log:
 *  - CorruptComponentPayloadException no se reporta (ya devuelve 419 al cliente).
 *  - El TypeError de Filament\Notifications\Collection::fromLivewire se silencia.
 *  - Cualquier otro TypeError real SÍ se sigue reportando.
 */
class NotificationHydrationLoggingTest extends TestCase
{
    #[Test]
    public function corrupt_livewire_payload_is_not_reported(): void
    {
        $handler = app(ExceptionHandler::class);

        $this->assertFalse(
            $handler->shouldReport(new CorruptComponentPayloadException),
            'CorruptComponentPayloadException debería estar en la lista dontReport.',
        );
    }

    #[Test]
    public function filament_notifications_hydration_typeerror_is_silenced(): void
    {
        Log::spy();

        report(new TypeError(
            'Filament\Notifications\Collection::Filament\Notifications\{closure}(): '
            .'Argument #1 ($notification) must be of type array, int given',
        ));

        Log::shouldNotHaveReceived('error');
        Log::shouldNotHaveReceived('log');
    }

    #[Test]
    public function unrelated_typeerror_is_still_reported(): void
    {
        Log::spy();

        report(new TypeError('Algun otro TypeError no relacionado.'));

        Log::shouldHaveReceived('error');
    }
}
