#!perl
use 5.014;
use strict;
use warnings;
use Test::More tests => 9;

# Edge case tests for C utility functions (Phase 5)

BEGIN {
    use_ok( 'Medusa::XS' ) || BAIL_OUT("Cannot load Medusa::XS");
}

# ------------------------------------------------------------------ #
# generate_guid() edge cases                                          #
# ------------------------------------------------------------------ #

subtest 'generate_guid() stress test - uniqueness' => sub {
    plan tests => 1;
    
    my %seen;
    for (1..100) {
        my $guid = Medusa::XS::generate_guid();
        $seen{$guid}++;
    }
    is(scalar keys %seen, 100, '100 GUIDs are all unique');
};

# ------------------------------------------------------------------ #
# format_time() edge cases                                            #
# ------------------------------------------------------------------ #

subtest 'format_time() empty format string' => sub {
    plan tests => 1;
    
    my $time = Medusa::XS::format_time(1, '');
    is($time, '', 'Empty format returns empty string');
};

subtest 'format_time() multiple %ms placeholders' => sub {
    plan tests => 1;
    
    # Note: only first %ms is replaced (single-pass replacement)
    my $time = Medusa::XS::format_time(1, '%ms-%ms');
    like($time, qr/^\d{3}-%ms$/, 'First %ms replaced');
};

# ------------------------------------------------------------------ #
# clean_dumper() edge cases                                           #
# ------------------------------------------------------------------ #

subtest 'clean_dumper() empty input' => sub {
    plan tests => 2;
    
    my $output = Medusa::XS::clean_dumper('');
    is($output, '', 'Empty string returns empty');
    
    $output = Medusa::XS::clean_dumper('$VAR1 = ;');
    is($output, '', 'Just prefix/suffix returns empty');
};

subtest 'clean_dumper() nested structures' => sub {
    plan tests => 1;
    
    my $input = q{$VAR1 = {
        'outer' => {
            'inner' => [
                1,
                2,
                3
            ]
        }
    };};
    my $output = Medusa::XS::clean_dumper($input);
    # Spaces around braces preserved, newlines collapsed to single spaces
    like($output, qr/\{ 'outer' => \{ 'inner' => \[ 1, 2, 3 \] \} \}/, 
         'Nested structure cleaned correctly');
};

subtest 'clean_dumper() escaped quotes' => sub {
    plan tests => 1;
    
    my $input = q{$VAR1 = "hello \"world\"";};
    my $output = Medusa::XS::clean_dumper($input);
    like($output, qr/hello \\"world\\"/, 'Escaped quotes preserved');
};

subtest 'clean_dumper() mixed quotes' => sub {
    plan tests => 1;
    
    my $input = q{$VAR1 = { 'key' => "value with   spaces" };};
    my $output = Medusa::XS::clean_dumper($input);
    like($output, qr/value with   spaces/, 'Spaces in double quotes preserved');
};

subtest 'clean_dumper() no $VAR1 prefix' => sub {
    plan tests => 1;
    
    # Without $VAR1 prefix, minimal whitespace collapsing happens
    my $input = '{ "key" => "value" }';
    my $output = Medusa::XS::clean_dumper($input);
    like($output, qr/"key" => "value"/, 'Input without $VAR1 passed through');
};
