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

# Test XS utility functions

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

# ------------------------------------------------------------------ #
# Test generate_guid()                                                #
# ------------------------------------------------------------------ #

subtest 'generate_guid() returns valid UUID v4' => sub {
    plan tests => 4;
    
    my $guid = Medusa::XS::generate_guid();
    ok(defined $guid, 'GUID is defined');
    is(length($guid), 36, 'GUID is 36 characters');
    like($guid, qr/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i, 
         'GUID matches UUID v4 format');
    
    my $guid2 = Medusa::XS::generate_guid();
    isnt($guid, $guid2, 'Two GUIDs are different');
};

# ------------------------------------------------------------------ #
# Test format_time()                                                  #
# ------------------------------------------------------------------ #

subtest 'format_time() with default format' => sub {
    plan tests => 3;
    
    my $time = Medusa::XS::format_time();
    ok(defined $time, 'Time is defined');
    ok(length($time) > 0, 'Time is not empty');
    # Default format: "Wed Mar 25 12:34:56 2026"
    like($time, qr/^\w{3}\s+\w{3}\s+\d+\s+\d+:\d+:\d+\s+\d{4}$/, 'Time matches asctime format');
};

subtest 'format_time() with custom format' => sub {
    plan tests => 2;
    
    my $time = Medusa::XS::format_time(1, '%Y-%m-%d');
    ok(defined $time, 'Custom format time is defined');
    like($time, qr/^\d{4}-\d{2}-\d{2}$/, 'Time matches custom format');
};

subtest 'format_time() with milliseconds' => sub {
    plan tests => 2;
    
    my $time = Medusa::XS::format_time(1, '%H:%M:%S.%ms');
    ok(defined $time, 'Time with ms is defined');
    like($time, qr/^\d{2}:\d{2}:\d{2}\.\d{3}$/, 'Time includes milliseconds');
};

subtest 'format_time() gmtime vs localtime' => sub {
    plan tests => 2;
    
    # Both should return valid times (exact comparison is timezone-dependent)
    my $gm = Medusa::XS::format_time(1);  # gmtime
    my $lt = Medusa::XS::format_time(0);  # localtime
    
    ok(defined $gm, 'gmtime format works');
    ok(defined $lt, 'localtime format works');
};

# ------------------------------------------------------------------ #
# Test collect_caller_stack()                                         #
# ------------------------------------------------------------------ #

subtest 'collect_caller_stack() returns string' => sub {
    plan tests => 2;
    
    my $stack = Medusa::XS::collect_caller_stack();
    ok(defined $stack, 'Caller stack is defined');
    # Stack format: "Pkg1:Line1->Pkg2:Line2->..." or "Pkg::Sub:Line1->..."
    # Package names can contain ::
    like($stack, qr/^([\w:]+:\d+)?(->([\w:]+):\d+)*$|^$/, 'Stack matches expected format or is empty');
};

sub helper_for_stack_test {
    return Medusa::XS::collect_caller_stack();
}

subtest 'collect_caller_stack() captures nested calls' => sub {
    plan tests => 2;
    
    my $stack = helper_for_stack_test();
    ok(defined $stack, 'Nested stack is defined');
    like($stack, qr/main:\d+/, 'Stack contains main package');
};

# ------------------------------------------------------------------ #
# Test clean_dumper()                                                 #
# ------------------------------------------------------------------ #

subtest 'clean_dumper() strips $VAR1 prefix' => sub {
    plan tests => 2;
    
    my $input = '$VAR1 = "hello";';
    my $output = Medusa::XS::clean_dumper($input);
    ok(defined $output, 'Output is defined');
    is($output, '"hello"', 'Prefix and semicolon stripped');
};

subtest 'clean_dumper() collapses whitespace' => sub {
    plan tests => 1;
    
    my $input = q{$VAR1 = {
    'key' => 'value',
    'foo' => 'bar'
};};
    my $output = Medusa::XS::clean_dumper($input);
    unlike($output, qr/\n/, 'Newlines collapsed');
};

subtest 'clean_dumper() preserves quoted strings' => sub {
    plan tests => 2;
    
    my $input = '$VAR1 = "hello   world";';
    my $output = Medusa::XS::clean_dumper($input);
    like($output, qr/hello   world/, 'Whitespace inside quotes preserved');
    
    my $input2 = q{$VAR1 = 'single   quotes';};
    my $output2 = Medusa::XS::clean_dumper($input2);
    like($output2, qr/single   quotes/, 'Single quote strings preserved');
};
