Luigit
repositories / smith

smith

There are many coding harnesses - but this one is fast

owned by admin

smith-cli/tests/e2e_observe.rs

Raw
//! Active writer inspected by a second built binary (`SMH-SPEC-SPEC0001`, Sessions).
use assert_cmd::cargo::cargo_bin;
use serde_json::Value;
use std::io::{BufRead, BufReader, Write};
use std::process::{Command, Stdio};
use std::sync::mpsc;
use std::time::Duration;

#[test]
fn active_rpc_session_is_visible_to_read_only_inspection() {
    let home = std::env::temp_dir().join(format!("smith-observe-e2e-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&home);
    let mut writer = Command::new(cargo_bin("smith"))
        .arg("rpc")
        .env("XDG_DATA_HOME", &home)
        .env_remove("SMITH_PROVIDER")
        .env_remove("SMITH_MODEL")
        .env_remove("SMITH_BASE_URL")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .unwrap();
    let mut input = writer.stdin.take().unwrap();
    input
        .write_all(b"{\"id\":1,\"method\":\"ping\"}\n")
        .unwrap();
    input.flush().unwrap();
    let stdout = writer.stdout.take().unwrap();
    let (tx, rx) = mpsc::sync_channel(1);
    std::thread::spawn(move || {
        let mut line = String::new();
        let result = BufReader::new(stdout).read_line(&mut line).map(|_| line);
        let _ = tx.send(result);
    });
    let ready = rx.recv_timeout(Duration::from_secs(10)).unwrap().unwrap();
    assert_eq!(serde_json::from_str::<Value>(&ready).unwrap()["pong"], true);
    let listed = Command::new(cargo_bin("smith"))
        .args(["sessions", "list", "--json"])
        .env("XDG_DATA_HOME", &home)
        .output()
        .unwrap();
    assert!(
        listed.status.success(),
        "{}",
        String::from_utf8_lossy(&listed.stderr)
    );
    let line: Value = serde_json::from_slice(&listed.stdout).unwrap();
    let id = line["session"].as_str().unwrap();
    let path = home.join("smith/sessions/run").join(format!("{id}.smh"));
    let original = std::fs::read(&path).unwrap();
    let read = Command::new(cargo_bin("smith"))
        .args(["sessions", "get", id, "--json"])
        .env("XDG_DATA_HOME", &home)
        .output()
        .unwrap();
    assert!(
        read.status.success(),
        "{}",
        String::from_utf8_lossy(&read.stderr)
    );
    assert_eq!(std::fs::read(&path).unwrap(), original);
    drop(input);
    assert!(writer.wait().unwrap().success());
    let _ = std::fs::remove_dir_all(home);
}