openrouter_api 0.7.0

A Rust client library for the OpenRouter API
Documentation
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! Tests for consolidated Message type with ChatRole enum
//!
//! This test suite ensures:
//! - Message uses ChatRole enum instead of raw String
//! - All role variants work correctly
//! - Multimodal content works
//! - Tool and assistant messages work
//! - Serialization/deserialization is correct

use openrouter_api::models::tool::ToolType;
use openrouter_api::types::chat::{
    AudioContent, AudioUrl, ChatRole, ContentPart, ContentType, FileContent, FileUrl, ImageContent,
    ImageUrl, Message, MessageContent, TextContent,
};

use openrouter_api::types::ids::ToolCallId;
use serde_json::{from_str, from_value, to_value};

/// Test that Message uses ChatRole enum, not String
#[test]
fn test_message_uses_chat_role_enum() {
    let msg = Message::text(ChatRole::User, "Hello, world!");

    // This should compile - role is ChatRole, not String. ChatRole is
    // `#[non_exhaustive]`, so integration-test matches need a wildcard arm.
    match msg.role {
        ChatRole::User => {} // ✅ Should match
        ChatRole::Assistant => panic!("Wrong role"),
        ChatRole::System => panic!("Wrong role"),
        ChatRole::Tool => panic!("Wrong role"),
        _ => panic!("Unknown role"),
    }
}

/// Test all ChatRole variants work with Message
#[test]
fn test_all_chat_role_variants() {
    let user_msg = Message::text(ChatRole::User, "User message");
    assert_eq!(user_msg.role, ChatRole::User);

    let assistant_msg = Message::text(ChatRole::Assistant, "Assistant message");
    assert_eq!(assistant_msg.role, ChatRole::Assistant);

    let system_msg = Message::text(ChatRole::System, "System message");
    assert_eq!(system_msg.role, ChatRole::System);

    let tool_msg = Message::text(ChatRole::Tool, "Tool result");
    assert_eq!(tool_msg.role, ChatRole::Tool);
}

/// Test that ChatRole serializes correctly (lowercase)
#[test]
fn test_chat_role_serialization() {
    let msg = Message::text(ChatRole::User, "test");
    let json = to_value(&msg).unwrap();

    assert_eq!(json["role"], "user"); // lowercase, not "User"
}

/// Test that ChatRole deserializes from lowercase strings
#[test]
fn test_chat_role_deserialization() {
    let json = r#"{
        "role": "user",
        "content": "test"
    }"#;

    let msg: Message = from_str(json).unwrap();
    assert_eq!(msg.role, ChatRole::User);
}

/// Test assistant role deserializes correctly
#[test]
fn test_assistant_role_deserialization() {
    let json = r#"{
        "role": "assistant",
        "content": "response"
    }"#;

    let msg: Message = from_str(json).unwrap();
    assert_eq!(msg.role, ChatRole::Assistant);
}

/// Test system role deserializes correctly
#[test]
fn test_system_role_deserialization() {
    let json = r#"{
        "role": "system",
        "content": "instructions"
    }"#;

    let msg: Message = from_str(json).unwrap();
    assert_eq!(msg.role, ChatRole::System);
}

/// Test tool role deserializes correctly
#[test]
fn test_tool_role_deserialization() {
    let json = r#"{
        "role": "tool",
        "content": "tool output",
        "tool_call_id": "call_123"
    }"#;

    let msg: Message = from_str(json).unwrap();
    assert_eq!(msg.role, ChatRole::Tool);
    assert_eq!(msg.tool_call_id, Some(ToolCallId::new("call_123")));
}

/// Test Message with name field
#[test]
fn test_message_with_name() {
    let msg = Message::text_with_name(ChatRole::User, "message", "Alice");
    assert_eq!(msg.role, ChatRole::User);
    assert_eq!(msg.name, Some("Alice".to_string()));

    let json = r#"{
        "role": "user",
        "content": "message",
        "name": "Bob"
    }"#;

    let msg: Message = from_str(json).unwrap();
    assert_eq!(msg.role, ChatRole::User);
    assert_eq!(msg.name, Some("Bob".to_string()));
}

/// Test multimodal message with text content
#[test]
fn test_multimodal_text_content() {
    let text_part = ContentPart::Text(TextContent {
        content_type: ContentType::Text,
        text: "Hello".to_string(),
    });

    let msg = Message::multimodal(ChatRole::User, vec![text_part]);

    match msg.content {
        MessageContent::Parts(parts) => {
            assert_eq!(parts.len(), 1);
        }
        MessageContent::Text(_) => panic!("Should be Parts, not Text"),
    }
}

/// Test multimodal message with image content
#[test]
fn test_multimodal_image_content() {
    let image_part = ContentPart::Image(ImageContent {
        content_type: ContentType::ImageUrl,
        image_url: ImageUrl {
            url: "https://example.com/image.jpg".to_string(),
            detail: None,
        },
    });

    let msg = Message::multimodal(ChatRole::User, vec![image_part]);

    match msg.content {
        MessageContent::Parts(parts) => {
            assert_eq!(parts.len(), 1);
        }
        MessageContent::Text(_) => panic!("Should be Parts, not Text"),
    }
}

/// Test multimodal message with audio content
#[test]
fn test_multimodal_audio_content() {
    let audio_part = ContentPart::Audio(AudioContent {
        content_type: ContentType::AudioUrl,
        audio_url: AudioUrl {
            url: "https://example.com/audio.mp3".to_string(),
        },
    });

    let msg = Message::multimodal(ChatRole::User, vec![audio_part]);

    match msg.content {
        MessageContent::Parts(parts) => {
            assert_eq!(parts.len(), 1);
        }
        MessageContent::Text(_) => panic!("Should be Parts, not Text"),
    }
}

/// Test multimodal message with file content
#[test]
fn test_multimodal_file_content() {
    let file_part = ContentPart::File(FileContent {
        content_type: ContentType::FileUrl,
        file_url: FileUrl {
            url: "https://example.com/document.pdf".to_string(),
        },
    });

    let msg = Message::multimodal(ChatRole::User, vec![file_part]);

    match msg.content {
        MessageContent::Parts(parts) => {
            assert_eq!(parts.len(), 1);
        }
        MessageContent::Text(_) => panic!("Should be Parts, not Text"),
    }
}

/// Test multimodal message with mixed content
#[test]
fn test_multimodal_mixed_content() {
    let text_part = ContentPart::Text(TextContent {
        content_type: ContentType::Text,
        text: "Look at this image:".to_string(),
    });

    let image_part = ContentPart::Image(ImageContent {
        content_type: ContentType::ImageUrl,
        image_url: ImageUrl {
            url: "https://example.com/image.jpg".to_string(),
            detail: None,
        },
    });

    let msg = Message::multimodal(ChatRole::User, vec![text_part, image_part]);

    match msg.content {
        MessageContent::Parts(parts) => {
            assert_eq!(parts.len(), 2);
        }
        MessageContent::Text(_) => panic!("Should be Parts, not Text"),
    }
}

/// Test tool message construction
#[test]
fn test_tool_message() {
    let msg = Message::tool("tool output", "call_abc123");

    assert_eq!(msg.role, ChatRole::Tool);
    assert_eq!(msg.tool_call_id, Some(ToolCallId::new("call_abc123")));

    match msg.content {
        MessageContent::Text(s) => assert_eq!(s, "tool output"),
        MessageContent::Parts(_) => panic!("Tool message should have Text content"),
    }
}

/// Test assistant message with tools
#[test]
fn test_assistant_with_tools() {
    use openrouter_api::models::tool::{FunctionCall, ToolCall};

    let tool_calls = vec![ToolCall {
        id: ToolCallId::new("call_123"),
        kind: ToolType::Function,
        function_call: FunctionCall {
            name: "get_weather".to_string(),
            arguments: r#"{"city": "NYC"}"#.to_string(),
        },
    }];

    let msg = Message::assistant_with_tools(Some("I'll check the weather"), tool_calls.clone());

    assert_eq!(msg.role, ChatRole::Assistant);
    assert_eq!(msg.tool_calls, Some(tool_calls));

    match msg.content {
        MessageContent::Text(s) => assert_eq!(s, "I'll check the weather"),
        MessageContent::Parts(_) => panic!("Assistant message should have Text content"),
    }
}

/// Test assistant message with tools but no content
#[test]
fn test_assistant_with_tools_no_content() {
    use openrouter_api::models::tool::{FunctionCall, ToolCall};

    let tool_calls = vec![ToolCall {
        id: ToolCallId::new("call_456"),
        kind: ToolType::Function,
        function_call: FunctionCall {
            name: "get_weather".to_string(),
            arguments: r#"{"city": "LA"}"#.to_string(),
        },
    }];

    let msg = Message::assistant_with_tools(None::<String>, tool_calls);

    assert_eq!(msg.role, ChatRole::Assistant);
    assert!(msg.tool_calls.is_some());
}

/// Test Message default implementation
#[test]
fn test_message_default() {
    let msg = Message::default();
    assert_eq!(msg.role, ChatRole::User); // Should default to user
    assert_eq!(msg.name, None);
    assert_eq!(msg.tool_call_id, None);
    assert_eq!(msg.tool_calls, None);
}

/// Test full round-trip serialization/deserialization
#[test]
fn test_message_roundtrip_serialization() {
    let original = Message::text_with_name(ChatRole::System, "You are helpful", "SystemPrompt");

    let json = to_value(&original).unwrap();
    let deserialized: Message = from_value(json).unwrap();

    assert_eq!(original.role, deserialized.role);
    assert_eq!(original.name, deserialized.name);
    assert_eq!(original.tool_call_id, deserialized.tool_call_id);
}

/// Test that invalid role strings are rejected during deserialization
#[test]
fn test_invalid_role_deserialization_fails() {
    let json = r#"{
        "role": "invalid_role",
        "content": "test"
    }"#;

    let result: Result<Message, _> = from_str(json);
    assert!(result.is_err(), "Should reject invalid role strings");
}

/// Test case-insensitive role deserialization
#[test]
fn test_case_insensitive_role_deserialization() {
    let json = r#"{
        "role": "USER",
        "content": "test"
    }"#;

    // This should fail - we want strict lowercase only
    let result: Result<Message, _> = from_str(json);
    assert!(result.is_err(), "Should reject uppercase role");
}

/// Test that MessageContent::Text serializes correctly
#[test]
fn test_message_content_text_serialization() {
    let msg = Message::text(ChatRole::User, "Hello");
    let json = to_value(&msg).unwrap();

    assert_eq!(json["content"], "Hello");
}

/// Test that MessageContent::Parts serializes correctly
#[test]
fn test_message_content_parts_serialization() {
    let parts = vec![
        ContentPart::Text(TextContent {
            content_type: ContentType::Text,
            text: "Check this:".to_string(),
        }),
        ContentPart::Image(ImageContent {
            content_type: ContentType::ImageUrl,
            image_url: ImageUrl {
                url: "https://example.com/img.jpg".to_string(),
                detail: None,
            },
        }),
    ];

    let msg = Message::multimodal(ChatRole::User, parts);
    let json = to_value(&msg).unwrap();

    assert!(json["content"].is_array());
    assert_eq!(json["content"].as_array().unwrap().len(), 2);
}

/// Test that ChatRole implements all required traits
#[test]
fn test_chat_role_traits() {
    let role = ChatRole::User;

    // Test Clone
    let role_clone = role.clone();
    assert_eq!(role, role_clone);

    // Test PartialEq
    assert_eq!(role, ChatRole::User);
    assert_ne!(role, ChatRole::Assistant);

    // Test Debug
    let debug_str = format!("{:?}", role);
    assert!(debug_str.contains("User"));
}

/// Test that Message implements all required traits
#[test]
fn test_message_traits() {
    let msg = Message::text(ChatRole::User, "test");

    // Test Clone
    let msg_clone = msg.clone();
    assert_eq!(msg.role, msg_clone.role);

    // Test Debug
    let debug_str = format!("{:?}", msg);
    assert!(debug_str.contains("Message"));

    // Test PartialEq
    assert_eq!(msg, msg_clone);
}