kcode-k1-chat-thread-actions 0.3.0

Synchronous CurrentTime launch for K1 chat threads
Documentation
use super::launch;

#[test]
fn current_time_accepts_empty_objects_and_returns_values() {
    for arguments in ["{}", " \n {\t} "] {
        let result = launch("CurrentTime", arguments);
        assert!(result.is_ok_and(|time| !time.is_empty()));
    }
}

#[test]
fn current_time_rejects_invalid_arguments_exactly() {
    for arguments in ["", "{", "null", "[]", "{\"zone\":\"UTC\"}"] {
        assert_eq!(
            launch("CurrentTime", arguments),
            Err("invalid CurrentTime arguments".to_owned())
        );
    }
}

#[test]
fn unknown_names_return_exact_error() {
    assert_eq!(launch("NoSuchTool", "{}"), Err("unknown Ktool".to_owned()));
}

#[test]
fn repeated_calls_preserve_caller_order_and_result_positions() {
    let calls = [
        ("CurrentTime", "{}"),
        ("NoSuchTool", "{}"),
        ("CurrentTime", "{\"zone\":\"UTC\"}"),
        ("CurrentTime", " { } "),
    ];
    let observed = calls
        .iter()
        .map(|(name, arguments)| (*name, *arguments, launch(name, arguments)))
        .collect::<Vec<_>>();

    assert_eq!(
        observed
            .iter()
            .map(|(name, arguments, _)| (*name, *arguments))
            .collect::<Vec<_>>(),
        calls
    );
    assert!(observed[0].2.as_ref().is_ok_and(|time| !time.is_empty()));
    assert_eq!(observed[1].2, Err("unknown Ktool".to_owned()));
    assert_eq!(
        observed[2].2,
        Err("invalid CurrentTime arguments".to_owned())
    );
    assert!(observed[3].2.as_ref().is_ok_and(|time| !time.is_empty()));
}