<?php

declare(strict_types=1);

namespace Srp\Tests;

use PHPUnit\Framework\TestCase;

/**
 * Contract test for redirect/sanitize.php — the _meetups click-pipeline
 * sanitizers that take raw $_GET values. Token/country return null on
 * failure; IP returns '' (empty) on failure, never the raw input.
 */
final class SanitizeTest extends TestCase
{
    public function testTokenAcceptsUrlSafeWithinLength(): void
    {
        self::assertSame('Abc-1_2', meetup_sanitize_token('Abc-1_2', 16));
    }

    public function testTokenTrimsThenValidates(): void
    {
        self::assertSame('abc', meetup_sanitize_token('  abc  ', 16));
    }

    public function testTokenRejectsOverLength(): void
    {
        self::assertNull(meetup_sanitize_token('abcdef', 3));
    }

    public function testTokenRejectsIllegalChars(): void
    {
        self::assertNull(meetup_sanitize_token('a.b/c', 16));
    }

    public function testTokenRejectsNonScalar(): void
    {
        self::assertNull(meetup_sanitize_token(['x'], 16));
    }

    public function testCountryCodeLowercasesTwoLetters(): void
    {
        self::assertSame('id', meetup_sanitize_country_code('ID'));
    }

    public function testCountryCodeRejectsNonAlphaOrWrongLength(): void
    {
        self::assertNull(meetup_sanitize_country_code('USA'));
        self::assertNull(meetup_sanitize_country_code('u1'));
    }

    public function testIpPassesValidV4AndV6(): void
    {
        self::assertSame('203.0.113.7', meetup_sanitize_ip('203.0.113.7'));
        self::assertSame('2001:db8::1', meetup_sanitize_ip('2001:db8::1'));
    }

    public function testIpReturnsEmptyOnInvalidOrNonScalar(): void
    {
        self::assertSame('', meetup_sanitize_ip('999.999.999.999'));
        self::assertSame('', meetup_sanitize_ip('not-an-ip'));
        self::assertSame('', meetup_sanitize_ip(['x']));
    }
}
