module Main where

import Test.HUnit
import System.Process
import Data.List (stripPrefix)

scriptPath :: String
scriptPath = "/Users/einand/Code/test/haskell/print_hej"

runScript :: Maybe String -> IO String
runScript mArgs = do
    let args = maybe [] (\x -> [x]) mArgs
    output <- readProcess scriptPath args ""
    return $ init output  -- Remove trailing newline

test10Decimals :: Test
test10Decimals = TestCase $ do
    result <- runScript (Just "10")
    let expected = "3.1415926535"
    assertEqual "10 decimals" expected result

test5Decimals :: Test
test5Decimals = TestCase $ do
    result <- runScript (Just "5")
    let expected = "3.14159"
    assertEqual "5 decimals" expected result

test1Decimal :: Test
test1Decimal = TestCase $ do
    result <- runScript (Just "1")
    let expected = "3.1"
    assertEqual "1 decimal" expected result

test100Decimals :: Test
test100Decimals = TestCase $ do
    result <- runScript (Just "100")
    let expected = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679"
    assertEqual "100 decimals" expected result

testDefault100Decimals :: Test
testDefault100Decimals = TestCase $ do
    result <- runScript Nothing
    let expected = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679"
    assertEqual "default 100 decimals" expected result

test10000Decimals :: Test
test10000Decimals = TestCase $ do
    result <- runScript (Just "10000")
    -- Check length: "3." + 10000 digits = 10002 characters
    assertEqual "10000 decimals length" 10002 (length result)
    assertBool "10000 decimals starts with 3.14159" (take 7 result == "3.14159")

tests :: Test
tests = TestList [
    TestLabel "test10Decimals" test10Decimals,
    TestLabel "test5Decimals" test5Decimals,
    TestLabel "test1Decimal" test1Decimal,
    TestLabel "test100Decimals" test100Decimals,
    TestLabel "testDefault100Decimals" testDefault100Decimals,
    TestLabel "test10000Decimals" test10000Decimals
    ]

main :: IO ()
main = do
    counts <- runTestTT tests
    putStrLn $ showCounts counts