<?php

declare(strict_types=1);

namespace Tests\Unit;

use App\Services\DataForSeo\UrlNormalizer;
use PHPUnit\Framework\TestCase;

class UrlNormalizerTest extends TestCase
{
    public function test_it_normalizes_host_path_and_tracking_parameters(): void
    {
        $normalizer = new UrlNormalizer;

        $normalized = $normalizer->normalize('https://www.Chedraui.com.mx/tortilla-de-maiz/?utm_source=google&fbclid=123#detalle');

        $this->assertSame('https://chedraui.com.mx/tortilla-de-maiz', $normalized);
    }

    public function test_it_resolves_google_redirect_urls(): void
    {
        $normalizer = new UrlNormalizer;

        $normalized = $normalizer->normalize('https://www.google.com/aclk?adurl=https%3A%2F%2Fwww.chedraui.com.mx%2Ftortilla-de-maiz-por-kilo%2Fp%3Futm_source%3Dad');

        $this->assertSame('https://chedraui.com.mx/tortilla-de-maiz-por-kilo/p', $normalized);
    }

    public function test_it_matches_urls_loosely_ignoring_query_strings(): void
    {
        $normalizer = new UrlNormalizer;

        $this->assertTrue($normalizer->matchesLoosely(
            'https://chedraui.com.mx/tortilla-de-maiz-por-kilo/p?utm_source=google',
            'https://www.chedraui.com.mx/tortilla-de-maiz-por-kilo/p',
        ));
    }
}
