<?php

namespace Tests\Unit;

use App\Support\BackUrl;
use Illuminate\Support\Arr;
use Tests\TestCase;

class BackUrlWithBackFragmentTest extends TestCase
{
    public function test_with_back_should_preserve_existing_fragment_when_safe(): void
    {
        $back = '/admin/platform-orders/2';
        $backQuery = Arr::query(['back' => $back]);

        $url = BackUrl::withBack('/admin/site-subscriptions/2#syncable-batch', $back);

        $this->assertSame('/admin/site-subscriptions/2?' . $backQuery . '#syncable-batch', $url);
    }

    public function test_with_back_should_drop_existing_fragment_when_not_whitelisted(): void
    {
        $back = '/admin/platform-orders/2';
        $backQuery = Arr::query(['back' => $back]);

        $url = BackUrl::withBack('/admin/site-subscriptions/2#bad#frag', $back);

        $this->assertSame('/admin/site-subscriptions/2?' . $backQuery, $url);
    }

    public function test_with_back_first_should_preserve_existing_fragment_when_safe(): void
    {
        $back = '/admin/platform-orders/2';
        $backQuery = Arr::query(['back' => $back]);

        $url = BackUrl::withBackFirst('/admin/site-subscriptions/2?order_sync_status=syncable#syncable-batch', $back);

        $this->assertSame('/admin/site-subscriptions/2?' . $backQuery . '&order_sync_status=syncable#syncable-batch', $url);
    }
}
