#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 7;
use IPC::Open3;

my $SCRIPT_PATH = '/Users/einand/Code/test/perl/print_hej.pl';

sub run_script {
    my ($args) = @_;
    my @cmd = ($SCRIPT_PATH);
    push @cmd, $args if defined $args;
    
    my $pid = open3(my $in, my $out, my $err, @cmd);
    close($in);
    my $result = <$out>;
    close($out);
    close($err);
    waitpid($pid, 0);
    
    chomp $result if defined $result;
    return $result // '';
}

# Test 10 decimals
{
    my $result = run_script('10');
    my $expected = '3.1415926535';
    is($result, $expected, '10 decimals');
}

# Test 5 decimals
{
    my $result = run_script('5');
    my $expected = '3.14159';
    is($result, $expected, '5 decimals');
}

# Test 1 decimal
{
    my $result = run_script('1');
    my $expected = '3.1';
    is($result, $expected, '1 decimal');
}

# Test 100 decimals
{
    my $result = run_script('100');
    my $expected = '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679';
    is($result, $expected, '100 decimals');
}

# Test default 100 decimals
{
    my $result = run_script();
    my $expected = '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679';
    is($result, $expected, 'default 100 decimals');
}

# Test invalid input
{
    my $result = run_script('invalid');
    my $expected = '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679';
    is($result, $expected, 'invalid input uses default');
}

# Test 10000 decimals
{
    my $result = run_script('10000');
    # Check length: "3." + 10000 digits = 10002 characters
    is(length($result), 10002, '10000 decimals length');
    ok(index($result, '3.14159') == 0, '10000 decimals starts with 3.14159');
}