#!/usr/bin/env nu
#
# runtest-debug — interactively debug one CLI test file.
#
# Sources the given suite (e.g. `runtest-debug vm_test`) and drops you into
# an interactive nushell session with it loaded, instead of running it
# through nutest. Call any @test-tagged command by its full name to run
# just that test on its own and inspect what happens — nutest's runner
# captures output and runs tests concurrently, which gets in the way of
# stepping through a failure by hand.
#
# Test files don't need NU_LIB_DIRS/nutest themselves (only runtest's own
# runner does), so this works without the devShell's nutest wiring too.

def main [suite: string] {
    let repo_root = ($env.FILE_PWD | path dirname)
    let file = ($repo_root | path join "nu" "tests" $"($suite).nu")
    if not ($file | path exists) {
        print -e $"no such test file: ($file)"
        exit 1
    }
    let child_script = (
        "source '" + $file + "'\n"
        + "print 'Loaded. Custom/test commands now in scope:'\n"
        + "scope commands | where type == 'custom' | get name | each {|n| print ('  ' + $n)}\n"
        + "print 'Call one by its full name to run just that test.'"
    )
    ^nu --execute $child_script
}
