import XCTest
@testable import print_hej

class PiTests: XCTestCase {
    let scriptPath = "/Users/einand/Code/test/swift/print_hej"
    
    func runScript(_ args: String? = nil) -> String {
        let process = Process()
        process.executableURL = URL(fileURLWithPath: scriptPath)
        
        if let args = args {
            process.arguments = [args]
        }
        
        let pipe = Pipe()
        process.standardOutput = pipe
        process.standardError = pipe
        
        do {
            try process.run()
            process.waitUntilExit()
            
            let data = pipe.fileHandleForReading.readDataToEndOfFile()
            return String(data: data, encoding: .utf8)?.trimmingCharacters(in: .newlines) ?? ""
        } catch {
            return ""
        }
    }
    
    func test10Decimals() {
        let result = runScript("10")
        let expected = "3.1415926535"
        XCTAssertEqual(result, expected)
    }
    
    func test5Decimals() {
        let result = runScript("5")
        let expected = "3.14159"
        XCTAssertEqual(result, expected)
    }
    
    func test1Decimal() {
        let result = runScript("1")
        let expected = "3.1"
        XCTAssertEqual(result, expected)
    }
    
    func test100Decimals() {
        let result = runScript("100")
        let expected = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679"
        XCTAssertEqual(result, expected)
    }
    
    func testDefault100Decimals() {
        let result = runScript()
        let expected = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679"
        XCTAssertEqual(result, expected)
    }
    
    func test10000Decimals() {
        let result = runScript("10000")
        // Check length: "3." + 10000 digits = 10002 characters
        XCTAssertEqual(result.count, 10002)
        XCTAssertTrue(result.hasPrefix("3.14159"))
    }
    
    static var allTests = [
        ("test10Decimals", test10Decimals),
        ("test5Decimals", test5Decimals),
        ("test1Decimal", test1Decimal),
        ("test100Decimals", test100Decimals),
        ("testDefault100Decimals", testDefault100Decimals),
        ("test10000Decimals", test10000Decimals),
    ]
}