-module(pi_test).
-include_lib("eunit/include/eunit.hrl").

-define(SCRIPT_PATH, "/Users/einand/Code/test/erlang/print_hej").

run_script(Args) ->
    Cmd = ?SCRIPT_PATH ++ " " ++ Args,
    Port = open_port({spawn, Cmd}, [stream, exit_status]),
    receive
        {Port, {data, Output}} ->
            port_close(Port),
            string:trim(Output, trailing, "\n");
        {Port, {exit_status, _Status}} ->
            port_close(Port),
            ""
    after 5000 ->
        port_close(Port),
        ""
    end.

run_script_no_args() ->
    Cmd = ?SCRIPT_PATH,
    Port = open_port({spawn, Cmd}, [stream, exit_status]),
    receive
        {Port, {data, Output}} ->
            port_close(Port),
            string:trim(Output, trailing, "\n");
        {Port, {exit_status, _Status}} ->
            port_close(Port),
            ""
    after 5000 ->
        port_close(Port),
        ""
    end.

test_10_decimals_test() ->
    Result = run_script("10"),
    Expected = "3.1415926535",
    ?assertEqual(Expected, Result).

test_5_decimals_test() ->
    Result = run_script("5"),
    Expected = "3.14159",
    ?assertEqual(Expected, Result).

test_1_decimal_test() ->
    Result = run_script("1"),
    Expected = "3.1",
    ?assertEqual(Expected, Result).

test_100_decimals_test() ->
    Result = run_script("100"),
    Expected = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679",
    ?assertEqual(Expected, Result).

test_default_100_decimals_test() ->
    Result = run_script_no_args(),
    Expected = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679",
    ?assertEqual(Expected, Result).

test_10000_decimals_test() ->
    Result = run_script("10000"),
    % Check length: "3." + 10000 digits = 10002 characters
    ?assertEqual(10002, length(Result)),
    ?assert(lists:prefix("3.14159", Result)).