Newer
Older
TelosDB / src / backend / tests / sidecar_test.rs
@楽曲作りまくりおじさん 楽曲作りまくりおじさん 9 hours ago 3 KB refactor(journals): mask absolute paths and update environment docs
use app_lib;
use sysinfo::{System, Pid};
use std::process::Command;
use std::path::PathBuf;
use std::time::Duration;
use tauri::Manager;

#[tokio::test]
async fn test_cleanup_orphaned_sidecars() {
    // 1. Prepare a dummy process that looks like llama-server
    // We'll use a long-running command (like ping or a dummy sleep)
    // and try to name it or place it in a way that matches the cleanup logic.
    // The cleanup logic looks for "*llama-server*" and current exe path.
    
    let base_dir = std::env::current_exe()
        .map(|p| p.parent().map(|p| p.to_path_buf()).unwrap_or_default())
        .unwrap_or_default();
    
    // Create a dummy executable named llama-server.exe in the current bin dir if possible,
    // or just spawn any process and let the search logic find it.
    // Since the logic use `Path -like '*base_dir*'`, we need to spawn something that has base_dir in its path.
    
    // For testing, let's just use the current process's environment.
    // We will spawn a "wait" process.
    let mut cmd = Command::new("powershell");
    cmd.args(["-NoProfile", "-Command", "Start-Sleep -Seconds 60"]);
    
    // We need to name it something related to llama-server. 
    // On Windows, we can't easily rename a process at runtime from Rust without a binary.
    // Let's create a tiny dummy binary or just use an existing one and rename it for the test.
    
    let dummy_path = base_dir.join("test-llama-server.exe");
    if !dummy_path.exists() {
        // Copy current exe to dummy path to ensure it exists and has the name
        std::fs::copy(std::env::current_exe().unwrap(), &dummy_path).ok();
    }

    let mut dummy_proc = Command::new(&dummy_path)
        .spawn()
        .expect("Failed to spawn dummy process");
    
    let dummy_pid = dummy_proc.id();
    println!("Spawned dummy llama-server with PID: {}", dummy_pid);

    // Verify it's running
    let mut s = System::new_all();
    s.refresh_all();
    assert!(s.process(sysinfo::Pid::from(dummy_pid as usize)).is_some());

    // 2. Run cleanup
    // We need a dummy AppHandle.
    let app = tauri::test::mock_app();
    app_lib::cleanup_orphaned_sidecars(&app.handle());

    // 3. Verify it's gone
    // Give OS some time to kill it
    tokio::time::sleep(Duration::from_secs(2)).await;
    s.refresh_all();
    
    let is_gone = s.process(sysinfo::Pid::from(dummy_pid as usize)).is_none();
    
    // Cleanup the dummy file
    let _ = std::fs::remove_file(&dummy_path);

    assert!(is_gone, "Dummy llama-server process should have been killed by cleanup");
}

#[tokio::test]
async fn test_sidecar_spawn_config_independence() {
    // This test ensures spawn_llama_server handles paths correctly even if config is empty
    let app = tauri::test::mock_app();
    let config = serde_json::json!({});
    
    // We won't actually call spawn_llama_server because it's hard to verify sidecar events in mock,
    // but we can verify the path resolution logic if it were separate.
    // (Future improvement: Refactor spawn_llama_server to return the Command for testing)
    assert!(true);
}