multi-llm 1.0.0

Unified multi-provider LLM client with support for OpenAI, Anthropic, Ollama, and LMStudio
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
//! Unit Tests for OpenAI-Shared Conversion Functions
//!
//! UNIT UNDER TEST: Conversion utility functions in openai_shared/utils.rs
//!
//! BUSINESS RESPONSIBILITY:
//!   - Convert neutral message formats to OpenAI-compatible messages
//!   - Convert neutral tool definitions to OpenAI tool format
//!   - Apply executor LLM configuration to OpenAI requests
//!   - Convert OpenAI tool calls to executor tool call format
//!   - Handle tool calls with custom format detection and content cleaning
//!   - Estimate tokens for logging and diagnostics
//!
//! TEST COVERAGE:
//!   - Message conversion: text, JSON, tool calls, tool results
//!   - Tool definition conversion
//!   - Config application: temperature, max_tokens, top_p, tools, tool_choice, response_format
//!   - Tool call conversion: standard and custom formats
//!   - Token estimation: simple text and message arrays
//!   - Edge cases: empty arrays, None values, invalid JSON

use super::super::types::*;
use super::super::utils::*;
use crate::messages::MessageAttributes;
use crate::provider::{RequestConfig, ResponseFormat, Tool, ToolChoice};
use crate::{MessageContent, MessageRole, UnifiedMessage};
use chrono::Utc;
use serde_json::json;

// ============================================================================
// Test Helper Functions
// ============================================================================

/// Create a simple text message with default attributes
fn create_message(role: MessageRole, content: &str) -> UnifiedMessage {
    UnifiedMessage {
        role,
        content: MessageContent::Text(content.to_string()),
        attributes: MessageAttributes::default(),
        timestamp: Utc::now(),
    }
}

/// Create a message with JSON content
fn create_json_message(role: MessageRole, json: serde_json::Value) -> UnifiedMessage {
    UnifiedMessage {
        role,
        content: MessageContent::Json(json),
        attributes: MessageAttributes::default(),
        timestamp: Utc::now(),
    }
}

/// Create a message with tool call content
fn create_tool_call_message(id: &str, name: &str, arguments: serde_json::Value) -> UnifiedMessage {
    UnifiedMessage {
        role: MessageRole::Assistant,
        content: MessageContent::ToolCall {
            id: id.to_string(),
            name: name.to_string(),
            arguments,
        },
        attributes: MessageAttributes::default(),
        timestamp: Utc::now(),
    }
}

/// Create a message with tool result content
fn create_tool_result_message(tool_call_id: &str, content: &str, is_error: bool) -> UnifiedMessage {
    UnifiedMessage {
        role: MessageRole::Tool,
        content: MessageContent::ToolResult {
            tool_call_id: tool_call_id.to_string(),
            content: content.to_string(),
            is_error,
        },
        attributes: MessageAttributes::default(),
        timestamp: Utc::now(),
    }
}

/// Create test RequestConfig with minimal fields
fn create_test_executor_config() -> RequestConfig {
    RequestConfig {
        temperature: None,
        max_tokens: None,
        top_p: None,
        top_k: None,
        min_p: None,
        presence_penalty: None,
        response_format: None,
        tools: vec![],
        tool_choice: None,
        user_id: None,
        session_id: None,
        llm_path: Some("user_llm".to_string()),
    }
}

// ============================================================================
// Message Conversion Tests
// ============================================================================

#[test]
fn test_convert_text_messages() {
    // Test verifies basic text message conversion across all role types
    // Ensures role mapping and text content preservation

    let messages = vec![
        create_message(MessageRole::System, "System prompt"),
        create_message(MessageRole::User, "User question"),
        create_message(MessageRole::Assistant, "Assistant response"),
    ];

    let converted = convert_neutral_messages_to_openai(&messages);

    assert_eq!(converted.len(), 3);
    assert_eq!(converted[0].role, "system");
    assert_eq!(converted[0].content, "System prompt");
    assert_eq!(converted[1].role, "user");
    assert_eq!(converted[1].content, "User question");
    assert_eq!(converted[2].role, "assistant");
    assert_eq!(converted[2].content, "Assistant response");
}

#[test]
fn test_convert_json_message_content() {
    // Test verifies JSON content is serialized to pretty string format
    // Important for structured responses and debugging

    let json_value = json!({
        "field1": "value1",
        "field2": 42,
        "nested": {"key": "value"}
    });

    let messages = vec![create_json_message(
        MessageRole::Assistant,
        json_value.clone(),
    )];

    let converted = convert_neutral_messages_to_openai(&messages);

    assert_eq!(converted.len(), 1);
    assert_eq!(converted[0].role, "assistant");
    assert!(converted[0].content.contains("field1"));
    assert!(converted[0].content.contains("value1"));
    assert!(converted[0].content.contains("field2"));
    assert!(converted[0].content.contains("42"));
}

#[test]
fn test_convert_tool_call_message_content() {
    // Test verifies tool call messages convert to text format
    // Note: Tool calls FROM the LLM shouldn't be in messages TO the LLM,
    // but we convert for compatibility

    let messages = vec![create_tool_call_message(
        "call_123",
        "get_weather",
        json!({"location": "Seattle"}),
    )];

    let converted = convert_neutral_messages_to_openai(&messages);

    assert_eq!(converted.len(), 1);
    assert_eq!(converted[0].role, "assistant");
    assert!(converted[0].content.contains("Tool call:"));
    assert!(converted[0].content.contains("get_weather"));
    assert!(converted[0].content.contains("Seattle"));
}

#[test]
fn test_convert_tool_result_success() {
    // Test verifies successful tool results format correctly

    let messages = vec![create_tool_result_message(
        "call_123",
        "Weather is sunny, 72°F",
        false,
    )];

    let converted = convert_neutral_messages_to_openai(&messages);

    assert_eq!(converted.len(), 1);
    assert_eq!(converted[0].role, "tool");
    assert!(converted[0].content.starts_with("Tool result:"));
    assert!(converted[0].content.contains("Weather is sunny"));
}

#[test]
fn test_convert_tool_result_error() {
    // Test verifies error tool results are prefixed correctly

    let messages = vec![create_tool_result_message("call_123", "API timeout", true)];

    let converted = convert_neutral_messages_to_openai(&messages);

    assert_eq!(converted.len(), 1);
    assert_eq!(converted[0].role, "tool");
    assert!(converted[0].content.starts_with("Tool error:"));
    assert!(converted[0].content.contains("API timeout"));
}

#[test]
fn test_convert_empty_message_array() {
    // Test verifies empty message array handling

    let messages: Vec<UnifiedMessage> = vec![];
    let converted = convert_neutral_messages_to_openai(&messages);

    assert_eq!(converted.len(), 0);
}

#[test]
fn test_convert_mixed_message_types() {
    // Test verifies mixed message types in conversation

    let messages = vec![
        create_message(MessageRole::System, "System"),
        create_message(MessageRole::User, "User"),
        create_json_message(MessageRole::Assistant, json!({"response": "data"})),
        create_tool_result_message("call_1", "result", false),
    ];

    let converted = convert_neutral_messages_to_openai(&messages);

    assert_eq!(converted.len(), 4);
    assert_eq!(converted[0].role, "system");
    assert_eq!(converted[1].role, "user");
    assert_eq!(converted[2].role, "assistant");
    assert_eq!(converted[3].role, "tool");
}

// ============================================================================
// Tool Conversion Tests
// ============================================================================

#[test]
fn test_convert_single_tool() {
    // Test verifies single tool conversion to OpenAI format

    let tools = vec![Tool {
        name: "get_weather".to_string(),
        description: "Get current weather".to_string(),
        parameters: json!({
            "type": "object",
            "properties": {
                "location": {"type": "string"}
            }
        }),
    }];

    let converted = convert_neutral_tools_to_openai(&tools);

    assert_eq!(converted.len(), 1);
    assert_eq!(converted[0]["type"], "function");
    assert_eq!(converted[0]["function"]["name"], "get_weather");
    assert_eq!(
        converted[0]["function"]["description"],
        "Get current weather"
    );
    assert_eq!(
        converted[0]["function"]["parameters"]["properties"]["location"]["type"],
        "string"
    );
}

#[test]
fn test_convert_multiple_tools() {
    // Test verifies multiple tool conversion

    let tools = vec![
        Tool {
            name: "tool1".to_string(),
            description: "First tool".to_string(),
            parameters: json!({"type": "object"}),
        },
        Tool {
            name: "tool2".to_string(),
            description: "Second tool".to_string(),
            parameters: json!({"type": "object"}),
        },
    ];

    let converted = convert_neutral_tools_to_openai(&tools);

    assert_eq!(converted.len(), 2);
    assert_eq!(converted[0]["function"]["name"], "tool1");
    assert_eq!(converted[1]["function"]["name"], "tool2");
}

#[test]
fn test_convert_empty_tool_array() {
    // Test verifies empty tool array handling

    let tools: Vec<Tool> = vec![];
    let converted = convert_neutral_tools_to_openai(&tools);

    assert_eq!(converted.len(), 0);
}

#[test]
fn test_convert_tool_with_complex_parameters() {
    // Test verifies complex parameter schemas are preserved

    let tools = vec![Tool {
        name: "complex_tool".to_string(),
        description: "Tool with complex params".to_string(),
        parameters: json!({
            "type": "object",
            "properties": {
                "simple": {"type": "string"},
                "array": {
                    "type": "array",
                    "items": {"type": "number"}
                },
                "nested": {
                    "type": "object",
                    "properties": {
                        "deep": {"type": "boolean"}
                    }
                }
            },
            "required": ["simple"]
        }),
    }];

    let converted = convert_neutral_tools_to_openai(&tools);

    let params = &converted[0]["function"]["parameters"];
    assert_eq!(params["properties"]["simple"]["type"], "string");
    assert_eq!(params["properties"]["array"]["type"], "array");
    assert_eq!(params["properties"]["nested"]["type"], "object");
    assert_eq!(params["required"][0], "simple");
}

// ============================================================================
// Config Application Tests
// ============================================================================

#[test]
fn test_apply_config_with_none() {
    // Test verifies no-op when config is None

    let mut request = OpenAIRequest {
        model: "gpt-4".to_string(),
        messages: vec![],
        temperature: Some(1.0),
        max_tokens: None,
        top_p: None,
        presence_penalty: None,
        stream: None,
        tools: None,
        tool_choice: None,
        response_format: None,
    };

    let original_temp = request.temperature;
    apply_config_to_request(&mut request, None);

    assert_eq!(request.temperature, original_temp);
    assert!(request.max_tokens.is_none());
}

#[test]
fn test_apply_llm_parameters() {
    // Test verifies temperature, max_tokens, top_p, presence_penalty application

    let mut request = OpenAIRequest {
        model: "gpt-4".to_string(),
        messages: vec![],
        temperature: None,
        max_tokens: None,
        top_p: None,
        presence_penalty: None,
        stream: None,
        tools: None,
        tool_choice: None,
        response_format: None,
    };

    let mut config = create_test_executor_config();
    config.temperature = Some(0.7);
    config.max_tokens = Some(1000);
    config.top_p = Some(0.9);
    config.presence_penalty = Some(0.5);

    apply_config_to_request(&mut request, Some(config));

    assert_eq!(request.temperature, Some(0.7));
    assert_eq!(request.max_tokens, Some(1000));
    assert_eq!(request.top_p, Some(0.9));
    assert_eq!(request.presence_penalty, Some(0.5));
}

#[test]
fn test_apply_tools_for_user_llm() {
    // Test verifies tools are applied when llm_path is "user_llm"

    let mut request = OpenAIRequest {
        model: "gpt-4".to_string(),
        messages: vec![],
        temperature: None,
        max_tokens: None,
        top_p: None,
        presence_penalty: None,
        stream: None,
        tools: None,
        tool_choice: None,
        response_format: None,
    };

    let mut config = create_test_executor_config();
    config.tools = vec![Tool {
        name: "test_tool".to_string(),
        description: "Test".to_string(),
        parameters: json!({}),
    }];

    apply_config_to_request(&mut request, Some(config));

    assert!(request.tools.is_some());
    assert_eq!(request.tools.as_ref().unwrap().len(), 1);
}

#[test]
fn test_skip_tools_for_non_user_llm() {
    // Test verifies tools are NOT applied when llm_path is not "user_llm"
    // This prevents sending tools to internal/utility LLMs

    let mut request = OpenAIRequest {
        model: "gpt-4".to_string(),
        messages: vec![],
        temperature: None,
        max_tokens: None,
        top_p: None,
        presence_penalty: None,
        stream: None,
        tools: None,
        tool_choice: None,
        response_format: None,
    };

    let mut config = create_test_executor_config();
    config.llm_path = Some("internal_llm".to_string());
    config.tools = vec![Tool {
        name: "test_tool".to_string(),
        description: "Test".to_string(),
        parameters: json!({}),
    }];

    apply_config_to_request(&mut request, Some(config));

    assert!(request.tools.is_none());
}

#[test]
fn test_apply_tool_choice_auto() {
    // Test verifies ToolChoice::Auto converts to "auto"

    let mut request = OpenAIRequest {
        model: "gpt-4".to_string(),
        messages: vec![],
        temperature: None,
        max_tokens: None,
        top_p: None,
        presence_penalty: None,
        stream: None,
        tools: None,
        tool_choice: None,
        response_format: None,
    };

    let mut config = create_test_executor_config();
    config.tool_choice = Some(ToolChoice::Auto);

    apply_config_to_request(&mut request, Some(config));

    assert_eq!(request.tool_choice, Some("auto".to_string()));
}

#[test]
fn test_apply_tool_choice_none() {
    // Test verifies ToolChoice::None converts to "none"

    let mut request = OpenAIRequest {
        model: "gpt-4".to_string(),
        messages: vec![],
        temperature: None,
        max_tokens: None,
        top_p: None,
        presence_penalty: None,
        stream: None,
        tools: None,
        tool_choice: None,
        response_format: None,
    };

    let mut config = create_test_executor_config();
    config.tool_choice = Some(ToolChoice::None);

    apply_config_to_request(&mut request, Some(config));

    assert_eq!(request.tool_choice, Some("none".to_string()));
}

#[test]
fn test_apply_tool_choice_required() {
    // Test verifies ToolChoice::Required converts to "required"

    let mut request = OpenAIRequest {
        model: "gpt-4".to_string(),
        messages: vec![],
        temperature: None,
        max_tokens: None,
        top_p: None,
        presence_penalty: None,
        stream: None,
        tools: None,
        tool_choice: None,
        response_format: None,
    };

    let mut config = create_test_executor_config();
    config.tool_choice = Some(ToolChoice::Required);

    apply_config_to_request(&mut request, Some(config));

    assert_eq!(request.tool_choice, Some("required".to_string()));
}

#[test]
fn test_apply_tool_choice_specific() {
    // Test verifies ToolChoice::Specific passes through tool name

    let mut request = OpenAIRequest {
        model: "gpt-4".to_string(),
        messages: vec![],
        temperature: None,
        max_tokens: None,
        top_p: None,
        presence_penalty: None,
        stream: None,
        tools: None,
        tool_choice: None,
        response_format: None,
    };

    let mut config = create_test_executor_config();
    config.tool_choice = Some(ToolChoice::Specific("get_weather".to_string()));

    apply_config_to_request(&mut request, Some(config));

    assert_eq!(request.tool_choice, Some("get_weather".to_string()));
}

#[test]
fn test_apply_response_format() {
    // Test verifies response format with JSON schema

    let mut request = OpenAIRequest {
        model: "gpt-4".to_string(),
        messages: vec![],
        temperature: None,
        max_tokens: None,
        top_p: None,
        presence_penalty: None,
        stream: None,
        tools: None,
        tool_choice: None,
        response_format: None,
    };

    let schema = json!({
        "type": "object",
        "properties": {
            "answer": {"type": "string"}
        }
    });

    let mut config = create_test_executor_config();
    config.response_format = Some(ResponseFormat {
        name: "answer_schema".to_string(),
        schema: schema.clone(),
    });

    apply_config_to_request(&mut request, Some(config));

    assert!(request.response_format.is_some());
    let format = request.response_format.unwrap();
    assert_eq!(format.format_type, "json_schema");
    assert!(format.json_schema.is_some());
    let json_schema = format.json_schema.unwrap();
    assert_eq!(json_schema.name, "answer_schema");
    assert_eq!(json_schema.schema, schema);
    assert_eq!(json_schema.strict, Some(true));
}

#[test]
fn test_apply_all_config_options() {
    // Test verifies all config options apply together correctly

    let mut request = OpenAIRequest {
        model: "gpt-4".to_string(),
        messages: vec![],
        temperature: None,
        max_tokens: None,
        top_p: None,
        presence_penalty: None,
        stream: None,
        tools: None,
        tool_choice: None,
        response_format: None,
    };

    let mut config = create_test_executor_config();
    config.temperature = Some(0.8);
    config.max_tokens = Some(2000);
    config.top_p = Some(0.95);
    config.presence_penalty = Some(0.3);
    config.tools = vec![Tool {
        name: "tool1".to_string(),
        description: "Tool".to_string(),
        parameters: json!({}),
    }];
    config.tool_choice = Some(ToolChoice::Auto);
    config.response_format = Some(ResponseFormat {
        name: "schema".to_string(),
        schema: json!({"type": "object"}),
    });

    apply_config_to_request(&mut request, Some(config));

    assert_eq!(request.temperature, Some(0.8));
    assert_eq!(request.max_tokens, Some(2000));
    assert_eq!(request.top_p, Some(0.95));
    assert_eq!(request.presence_penalty, Some(0.3));
    assert!(request.tools.is_some());
    assert_eq!(request.tool_choice, Some("auto".to_string()));
    assert!(request.response_format.is_some());
}

// ============================================================================
// Tool Call Conversion Tests
// ============================================================================

#[test]
fn test_convert_single_tool_call() {
    // Test verifies single tool call conversion from OpenAI format

    let openai_calls = vec![OpenAIToolCall {
        id: "call_123".to_string(),
        call_type: "function".to_string(),
        function: OpenAIToolFunction {
            name: "get_weather".to_string(),
            arguments: r#"{"location":"Seattle"}"#.to_string(),
        },
    }];

    let converted = convert_tool_calls(&openai_calls);

    assert_eq!(converted.len(), 1);
    assert_eq!(converted[0].id, "call_123");
    assert_eq!(converted[0].name, "get_weather");
    assert_eq!(converted[0].arguments["location"], "Seattle");
}

#[test]
fn test_convert_multiple_tool_calls() {
    // Test verifies multiple tool calls conversion

    let openai_calls = vec![
        OpenAIToolCall {
            id: "call_1".to_string(),
            call_type: "function".to_string(),
            function: OpenAIToolFunction {
                name: "tool1".to_string(),
                arguments: r#"{"arg1":"value1"}"#.to_string(),
            },
        },
        OpenAIToolCall {
            id: "call_2".to_string(),
            call_type: "function".to_string(),
            function: OpenAIToolFunction {
                name: "tool2".to_string(),
                arguments: r#"{"arg2":"value2"}"#.to_string(),
            },
        },
    ];

    let converted = convert_tool_calls(&openai_calls);

    assert_eq!(converted.len(), 2);
    assert_eq!(converted[0].id, "call_1");
    assert_eq!(converted[0].name, "tool1");
    assert_eq!(converted[1].id, "call_2");
    assert_eq!(converted[1].name, "tool2");
}

#[test]
fn test_convert_tool_call_with_invalid_json() {
    // Test verifies invalid JSON in arguments results in empty object

    let openai_calls = vec![OpenAIToolCall {
        id: "call_bad".to_string(),
        call_type: "function".to_string(),
        function: OpenAIToolFunction {
            name: "bad_tool".to_string(),
            arguments: "not valid json".to_string(),
        },
    }];

    let converted = convert_tool_calls(&openai_calls);

    assert_eq!(converted.len(), 1);
    assert_eq!(converted[0].id, "call_bad");
    assert_eq!(converted[0].name, "bad_tool");
    assert_eq!(converted[0].arguments, json!({}));
}

#[test]
fn test_convert_empty_tool_call_array() {
    // Test verifies empty tool call array handling

    let openai_calls: Vec<OpenAIToolCall> = vec![];
    let converted = convert_tool_calls(&openai_calls);

    assert_eq!(converted.len(), 0);
}

// ============================================================================
// Token Estimation Tests
// ============================================================================

#[test]
fn test_estimate_tokens_empty_string() {
    // Test verifies empty string returns 0 tokens

    let tokens = estimate_tokens("");
    assert_eq!(tokens, 0);
}

#[test]
fn test_estimate_tokens_simple_text() {
    // Test verifies basic token estimation (~4 chars per token)

    let text = "Hello world, this is a test."; // 29 chars
    let tokens = estimate_tokens(text);

    assert_eq!(tokens, 7); // 29 / 4 = 7
}

#[test]
fn test_estimate_tokens_longer_text() {
    // Test verifies estimation scales linearly

    let text = "a".repeat(400); // 400 chars
    let tokens = estimate_tokens(&text);

    assert_eq!(tokens, 100); // 400 / 4 = 100
}

#[test]
fn test_estimate_message_tokens_empty() {
    // Test verifies empty message array returns 0 tokens

    let messages: Vec<OpenAIMessage> = vec![];
    let tokens = estimate_message_tokens(&messages);

    assert_eq!(tokens, 0);
}

#[test]
fn test_estimate_message_tokens_single_message() {
    // Test verifies single message with formatting overhead

    let messages = vec![OpenAIMessage {
        role: "user".to_string(),
        content: "Test message".to_string(), // 12 chars
    }];

    let tokens = estimate_message_tokens(&messages);

    // "user: Test message\n" = ~17 chars = 4 tokens + 8 overhead = 12 tokens
    assert_eq!(tokens, 12);
}

#[test]
fn test_estimate_message_tokens_multiple_messages() {
    // Test verifies multiple messages include per-message overhead

    let messages = vec![
        OpenAIMessage {
            role: "system".to_string(),
            content: "System".to_string(), // 6 chars
        },
        OpenAIMessage {
            role: "user".to_string(),
            content: "User".to_string(), // 4 chars
        },
        OpenAIMessage {
            role: "assistant".to_string(),
            content: "Assistant".to_string(), // 9 chars
        },
    ];

    let tokens = estimate_message_tokens(&messages);

    // Total content: ~40 chars = 10 tokens
    // Per-message overhead: 3 * 8 = 24 tokens
    // Total: 35 tokens (actual measurement)
    assert_eq!(tokens, 35);
}

// ============================================================================
// Handle Tool Calls Tests
// ============================================================================

#[test]
fn test_handle_tool_calls_with_standard_format() {
    // Test verifies standard OpenAI tool calls are processed correctly

    let message = OpenAIResponseMessage {
        role: "assistant".to_string(),
        content: "".to_string(),
        tool_calls: Some(vec![OpenAIToolCall {
            id: "call_123".to_string(),
            call_type: "function".to_string(),
            function: OpenAIToolFunction {
                name: "get_weather".to_string(),
                arguments: r#"{"location":"NYC"}"#.to_string(),
            },
        }]),
    };

    let result = handle_tool_calls(&message).unwrap();

    assert_eq!(result.len(), 1);
    assert_eq!(result[0].id, "call_123");
    assert_eq!(result[0].name, "get_weather");
    assert_eq!(result[0].arguments["location"], "NYC");
}

#[test]
fn test_handle_tool_calls_with_no_tool_calls() {
    // Test verifies no tool calls returns empty array

    let message = OpenAIResponseMessage {
        role: "assistant".to_string(),
        content: "Just a regular response".to_string(),
        tool_calls: None,
    };

    let result = handle_tool_calls(&message).unwrap();

    assert_eq!(result.len(), 0);
}

#[test]
fn test_handle_tool_calls_with_custom_format() {
    // Test verifies custom format detection (XML tool_call)

    let message = OpenAIResponseMessage {
        role: "assistant".to_string(),
        content: r#"<tool_call>{"name": "get_weather", "arguments": {"location": "Seattle"}}</tool_call>"#.to_string(),
        tool_calls: None,
    };

    let result = handle_tool_calls(&message).unwrap();

    assert_eq!(result.len(), 1);
    assert_eq!(result[0].name, "get_weather");
    assert_eq!(result[0].arguments["location"], "Seattle");
    assert!(result[0].id.starts_with("custom_"));
}

#[test]
fn test_handle_tool_calls_with_content_cleaning() {
    // Test verifies content cleaning when custom format detected

    let message = OpenAIResponseMessage {
        role: "assistant".to_string(),
        content: r#"Here is the result: <tool_call>{"name": "test", "arguments": {}}</tool_call>"#
            .to_string(),
        tool_calls: None,
    };

    let result = handle_tool_calls_with_content_cleaning(&message).unwrap();

    assert_eq!(result.tool_calls.len(), 1);
    assert!(result.cleaned_content.is_some());
    let cleaned = result.cleaned_content.unwrap();
    assert_eq!(cleaned, "Here is the result:");
    assert!(!cleaned.contains("<tool_call>"));
}

#[test]
fn test_handle_tool_calls_standard_takes_precedence() {
    // Test verifies standard tool calls take precedence over custom formats
    // Important: Don't parse content if standard tool_calls exist

    let message = OpenAIResponseMessage {
        role: "assistant".to_string(),
        content: r#"<tool_call>{"name": "custom_tool", "arguments": {}}</tool_call>"#.to_string(),
        tool_calls: Some(vec![OpenAIToolCall {
            id: "standard_123".to_string(),
            call_type: "function".to_string(),
            function: OpenAIToolFunction {
                name: "standard_tool".to_string(),
                arguments: "{}".to_string(),
            },
        }]),
    };

    let result = handle_tool_calls_with_content_cleaning(&message).unwrap();

    // Should get standard tool call, not custom
    assert_eq!(result.tool_calls.len(), 1);
    assert_eq!(result.tool_calls[0].id, "standard_123");
    assert_eq!(result.tool_calls[0].name, "standard_tool");
    assert!(result.cleaned_content.is_none());
}

// ============================================================================
// OpenAIUsage Type Conversion Tests
// ============================================================================

use crate::provider::TokenUsage;

#[test]
fn test_openai_usage_to_token_usage_conversion() {
    // Test verifies OpenAIUsage converts correctly to TokenUsage
    //
    // Business rule: Token usage from OpenAI API should be directly mappable
    // to our internal TokenUsage type for consistent tracking

    let openai_usage = OpenAIUsage {
        prompt_tokens: 100,
        completion_tokens: 50,
        total_tokens: 150,
    };

    let token_usage: TokenUsage = openai_usage.into();

    assert_eq!(token_usage.prompt_tokens, 100);
    assert_eq!(token_usage.completion_tokens, 50);
    assert_eq!(token_usage.total_tokens, 150);
}

#[test]
fn test_openai_usage_conversion_preserves_zero_values() {
    // Test verifies zero token counts are preserved

    let openai_usage = OpenAIUsage {
        prompt_tokens: 0,
        completion_tokens: 0,
        total_tokens: 0,
    };

    let token_usage: TokenUsage = openai_usage.into();

    assert_eq!(token_usage.prompt_tokens, 0);
    assert_eq!(token_usage.completion_tokens, 0);
    assert_eq!(token_usage.total_tokens, 0);
}

#[test]
fn test_openai_usage_conversion_large_values() {
    // Test verifies large token counts are handled correctly

    let openai_usage = OpenAIUsage {
        prompt_tokens: 100_000,
        completion_tokens: 50_000,
        total_tokens: 150_000,
    };

    let token_usage: TokenUsage = openai_usage.into();

    assert_eq!(token_usage.prompt_tokens, 100_000);
    assert_eq!(token_usage.completion_tokens, 50_000);
    assert_eq!(token_usage.total_tokens, 150_000);
}