import sys, os
sys.path.insert(0, os.path.dirname(__file__))
from crlfpatch import patch

patch('crates/spt/tests/serving_in_brain_e2e.rs', [
('''    let docs_url = {
        let mut cmd = Command::new(&spt_bin);
        cmd.no_window().args(["docs", "url"]).env("SPT_HOME", home.path());
        common::output_bounded(cmd, Duration::from_secs(20))
    };
''',
'''    let docs_url_asked = Instant::now();
    let docs_url = {
        let mut cmd = Command::new(&spt_bin);
        cmd.no_window().args(["docs", "url"]).env("SPT_HOME", home.path());
        common::output_bounded(cmd, Duration::from_secs(40))
    };
    let docs_url_took = docs_url_asked.elapsed();
'''),
('''         test_pid={} bind_fails={bind_fails} docs_url_exit={:?} ===\\n\\''',
'''         test_pid={} bind_fails={bind_fails} docs_url_exit={:?} docs_url_ms={} ===\\n\\'''),
('''        std::process::id(),
        docs_url.status.code(),
    );
''',
'''        std::process::id(),
        docs_url.status.code(),
        docs_url_took.as_millis(),
    );
'''),
('''    // (4) the foreign listener (the old broker's stand-in) still owns the port.''',
'''    // (3b) settled, not pending: the brain announced its failed bind as port 0,
    // so the URL verb answers at once instead of waiting out the 15 s bound.
    assert!(
        docs_url_took < spt_daemon::servehost::DOCS_PORT_PENDING_BOUND / 3,
        "FIELD TRANSITION (3b): UNAVAILABLE must be a settled answer, not a pending one waited \\
         out (took {} ms).\\n{daemon_panel}",
        docs_url_took.as_millis()
    );
    // (4) the foreign listener (the old broker's stand-in) still owns the port.'''),
])
