const std = @import("std");
const process = std.process;
const mem = std.mem;

const SCRIPT_PATH = "/Users/einand/Code/test/zig/print_hej";

fn runScript(allocator: mem.Allocator, args: ?[]const u8) ![]u8 {
    var argv = std.ArrayList([]const u8).initCapacity(allocator, 2) catch unreachable;
    defer argv.deinit();
    
    argv.appendAssumeCapacity(SCRIPT_PATH);
    if (args) |a| {
        argv.appendAssumeCapacity(a);
    }
    
    var child = process.Child.run(.{
        .allocator = allocator,
        .argv = argv.items,
    }) catch unreachable;
    
    // Remove trailing newline
    if (child.stdout.len > 0 and child.stdout[child.stdout.len - 1] == '\n') {
        return child.stdout[0 .. child.stdout.len - 1];
    }
    
    return child.stdout;
}

test "10 decimals" {
    const allocator = std.testing.allocator;
    const result = try runScript(allocator, "10");
    defer allocator.free(result);
    
    const expected = "3.1415926535";
    try std.testing.expectEqualStrings(expected, result);
}

test "5 decimals" {
    const allocator = std.testing.allocator;
    const result = try runScript(allocator, "5");
    defer allocator.free(result);
    
    const expected = "3.14159";
    try std.testing.expectEqualStrings(expected, result);
}

test "1 decimal" {
    const allocator = std.testing.allocator;
    const result = try runScript(allocator, "1");
    defer allocator.free(result);
    
    const expected = "3.1";
    try std.testing.expectEqualStrings(expected, result);
}

test "100 decimals" {
    const allocator = std.testing.allocator;
    const result = try runScript(allocator, "100");
    defer allocator.free(result);
    
    const expected = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679";
    try std.testing.expectEqualStrings(expected, result);
}

test "default 100 decimals" {
    const allocator = std.testing.allocator;
    const result = try runScript(allocator, null);
    defer allocator.free(result);
    
    const expected = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679";
    try std.testing.expectEqualStrings(expected, result);
}

test "10000 decimals" {
    const allocator = std.testing.allocator;
    const result = try runScript(allocator, "10000");
    defer allocator.free(result);
    
    // Check length: "3." + 10000 digits = 10002 characters
    try std.testing.expectEqual(@as(usize, 10002), result.len);
    try std.testing.expect(mem.startsWith(u8, result, "3.14159"));
}