Skip to content

A cross-platform desktop All-in-One assistant for Claude Code, Codex, OpenCode,DeepSeek Harness and more. Only official website: ccswitch.io

4.1 KB
Raw Blame
use super::*;
use serde_json::{json, Value};
use std::sync::{Arc, Mutex};

#[test]
fn content_search_accepts_cancellation_before_its_first_poll() {
    let request_id = uuid::Uuid::new_v4().to_string();
    cancel_session_content_search(request_id.clone());
    let error = tauri::async_runtime::block_on(search_session_content(
        "的".into(),
        None,
        None,
        Some(request_id),
    ))
    .unwrap_err();
    assert_eq!(error, "Session content search cancelled");
}

#[test]
#[serial_test::serial]
fn borrowed_session_responses_preserve_the_json_and_channel_contract() {
    let _home = crate::commands::startup_tests::TestHome::new();
    let root = crate::config::get_claude_settings_path()
        .parent()
        .unwrap()
        .join("projects/runtime");
    std::fs::create_dir_all(&root).unwrap();
    let path = root.join("runtime.jsonl");
    let record = json!({"type":"user", "uuid":"u1", "sessionId":"runtime",
        "message":{"role":"user", "content":"你好\n\"quoted\" 😀"}});
    std::fs::write(&path, format!("{record}\n")).unwrap();
    let source = path.to_string_lossy().into_owned();
    let expected =
        serde_json::to_value(session_manager::load_messages("claude", &source).unwrap()).unwrap();
    assert_eq!(expected.as_array().unwrap().len(), 1);

    let response =
        tauri::async_runtime::block_on(get_session_messages("claude".into(), source.clone()))
            .unwrap();
    assert_eq!(response.deserialize::<Value>().unwrap(), expected);

    let chunks = Arc::new(Mutex::new(Vec::<Value>::new()));
    let received = Arc::clone(&chunks);
    let channel = Channel::new(move |body| {
        received
            .lock()
            .unwrap()
            .push(body.deserialize::<Value>().unwrap());
        Ok(())
    });
    tauri::async_runtime::block_on(stream_session_messages("claude".into(), source, channel))
        .unwrap();
    let chunks = chunks.lock().unwrap();
    assert_eq!(chunks.len(), 3);
    assert_eq!(chunks[0]["type"], "header");
    assert_eq!(chunks[0]["total"], 1);
    assert!(chunks[0]["parseMs"].is_number());
    assert_eq!(
        chunks[1],
        json!({"type":"messages", "start":0, "messages":expected})
    );
    assert_eq!(chunks[2]["type"], "done");
    assert!(chunks[2]["payloadBytes"].as_u64().unwrap() > 0);
}

#[test]
fn failed_session_stream_still_sends_an_error_packet() {
    let chunks = Arc::new(Mutex::new(Vec::<Value>::new()));
    let received = Arc::clone(&chunks);
    let channel = Channel::new(move |body| {
        received
            .lock()
            .unwrap()
            .push(body.deserialize::<Value>().unwrap());
        Ok(())
    });
    let error = tauri::async_runtime::block_on(stream_session_messages(
        "unknown".into(),
        "missing".into(),
        channel,
    ))
    .unwrap_err();
    assert_eq!(
        *chunks.lock().unwrap(),
        [json!({"type":"error", "message":error})]
    );
}

#[test]
fn resume_command_carries_the_global_proxy() {
    let http = "http://127.0.0.1:7890";
    assert_eq!(
        with_resume_proxy("claude --resume abc", Some(http)),
        "HTTPS_PROXY='http://127.0.0.1:7890' https_proxy='http://127.0.0.1:7890' \
         HTTP_PROXY='http://127.0.0.1:7890' http_proxy='http://127.0.0.1:7890' \
         NO_PROXY='localhost,127.0.0.1,::1' no_proxy='localhost,127.0.0.1,::1' claude --resume abc"
    );
    // agy (Go) takes SOCKS5 too and needs no NO_PROXY; Node clients skip a SOCKS-only proxy
    assert_eq!(
        with_resume_proxy("agy --conversation=x", Some("socks5h://127.0.0.1:1080")),
        "HTTPS_PROXY='socks5://127.0.0.1:1080' https_proxy='socks5://127.0.0.1:1080' \
         HTTP_PROXY='socks5://127.0.0.1:1080' http_proxy='socks5://127.0.0.1:1080' \
         agy --conversation=x"
    );
    assert_eq!(
        with_resume_proxy("codex resume x", Some("socks5://127.0.0.1:1080")),
        "codex resume x"
    );
    // No proxy, or one carrying credentials that would show in the terminal: unchanged
    assert_eq!(with_resume_proxy("codex resume x", None), "codex resume x");
    assert_eq!(
        with_resume_proxy("codex resume x", Some("http://user:[email protected]:7890")),
        "codex resume x"
    );
}