use crate::paths;
pub fn sid_live(sid: &str) -> bool {
use crate::platform::proc_check::ProcCheck;
let content = match std::fs::read_to_string(paths::pid_file(sid)) {
Ok(c) => c,
Err(_) => return false, };
let (pid, _born) = match parse_pid_file(&content) {
Some(p) => p,
None => return false, };
crate::platform::PlatformProcCheck::is_live_claude_or_node(pid)
}
pub(crate) fn parse_pid_file(content: &str) -> Option<(u32, i64)> {
let mut tokens = content.split_whitespace();
let pid: u32 = tokens.next()?.parse().ok()?;
let born: i64 = tokens.next()?.parse().ok()?;
Some((pid, born))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_pid_file_basic() {
let (pid, born) = parse_pid_file("12345 1718000000\n").expect("should parse pid+born");
assert_eq!(pid, 12345);
assert_eq!(born, 1_718_000_000);
}
#[test]
fn parse_pid_file_no_trailing_newline() {
let (pid, born) = parse_pid_file("99 42").expect("no newline is fine");
assert_eq!(pid, 99);
assert_eq!(born, 42);
}
#[test]
fn parse_pid_file_extra_whitespace() {
let (pid, born) =
parse_pid_file(" 1000 9999999999 ").expect("leading/trailing whitespace is fine");
assert_eq!(pid, 1000);
assert_eq!(born, 9_999_999_999);
}
#[test]
fn parse_pid_file_extra_tokens_ignored() {
let (pid, born) =
parse_pid_file("1 2 extra tokens here").expect("extra tokens are ignored");
assert_eq!(pid, 1);
assert_eq!(born, 2);
}
#[test]
fn parse_pid_file_empty_returns_none() {
assert!(parse_pid_file("").is_none());
assert!(parse_pid_file(" ").is_none());
}
#[test]
fn parse_pid_file_only_pid_returns_none() {
assert!(parse_pid_file("12345").is_none());
}
#[test]
fn parse_pid_file_non_numeric_pid_returns_none() {
assert!(parse_pid_file("abc 1718000000").is_none());
}
#[test]
fn parse_pid_file_non_numeric_born_returns_none() {
assert!(parse_pid_file("12345 notanumber").is_none());
}
#[test]
fn parse_pid_file_negative_born_is_valid() {
let (pid, born) = parse_pid_file("1 -1").expect("negative born is valid i64");
assert_eq!(pid, 1);
assert_eq!(born, -1);
}
#[test]
fn parse_pid_file_pid_overflow_returns_none() {
let too_large = "4294967296 1718000000"; assert!(parse_pid_file(too_large).is_none());
}
}