chat_message.rs 2.14 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use serde_json::json;
use sglang_router_rs::protocols::spec::{ChatMessage, UserMessageContent};

#[test]
fn test_chat_message_tagged_by_role_system() {
    let json = json!({
        "role": "system",
        "content": "You are a helpful assistant"
    });

    let msg: ChatMessage = serde_json::from_value(json).unwrap();
    match msg {
        ChatMessage::System { content, .. } => {
            assert_eq!(content, "You are a helpful assistant");
        }
        _ => panic!("Expected System variant"),
    }
}

#[test]
fn test_chat_message_tagged_by_role_user() {
    let json = json!({
        "role": "user",
        "content": "Hello"
    });

    let msg: ChatMessage = serde_json::from_value(json).unwrap();
    match msg {
        ChatMessage::User { content, .. } => match content {
            UserMessageContent::Text(text) => assert_eq!(text, "Hello"),
            _ => panic!("Expected text content"),
        },
        _ => panic!("Expected User variant"),
    }
}

#[test]
fn test_chat_message_tagged_by_role_assistant() {
    let json = json!({
        "role": "assistant",
        "content": "Hi there!"
    });

    let msg: ChatMessage = serde_json::from_value(json).unwrap();
    match msg {
        ChatMessage::Assistant { content, .. } => {
            assert_eq!(content, Some("Hi there!".to_string()));
        }
        _ => panic!("Expected Assistant variant"),
    }
}

#[test]
fn test_chat_message_tagged_by_role_tool() {
    let json = json!({
        "role": "tool",
        "content": "Tool result",
        "tool_call_id": "call_123"
    });

    let msg: ChatMessage = serde_json::from_value(json).unwrap();
    match msg {
        ChatMessage::Tool {
            content,
            tool_call_id,
        } => {
            assert_eq!(content, "Tool result");
            assert_eq!(tool_call_id, "call_123");
        }
        _ => panic!("Expected Tool variant"),
    }
}

#[test]
fn test_chat_message_wrong_role_rejected() {
    let json = json!({
        "role": "invalid_role",
        "content": "test"
    });

    let result = serde_json::from_value::<ChatMessage>(json);
    assert!(result.is_err(), "Should reject invalid role");
}