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
//! Unit Tests for Messages Module
//!
//! UNIT UNDER TEST: CacheType enum and UnifiedMessage cache builder methods
//!
//! BUSINESS RESPONSIBILITY:
//!   - Provides cache type enum for Anthropic prompt caching (Ephemeral/Extended)
//!   - Provides builder methods to mark messages for specific cache types
//!   - Ensures messages can be marked for 5-minute (ephemeral) or 1-hour (extended) caching
//!
//! TEST COVERAGE:
//!   - CacheType enum variants and default behavior
//!   - Builder methods set correct cache_type and cacheable flags
//!   - Builder methods are chainable and preserve message data
//!   - Edge cases: builder methods on messages with different roles and content types

use crate::messages::{CacheType, MessageRole, UnifiedMessage};

// ============================================================================
// CacheType Enum Tests
// ============================================================================

#[test]
fn test_cache_type_default_is_ephemeral() {
    // Test verifies CacheType::default() returns Ephemeral variant
    //
    // Business rule: Ephemeral (5-minute) cache is the default for safety

    // Act
    let cache_type = CacheType::default();

    // Assert
    assert_eq!(cache_type, CacheType::Ephemeral);
}

#[test]
fn test_cache_type_variants_exist() {
    // Test verifies both cache type variants are available
    //
    // Business rule: Support both Anthropic cache TTL options

    // Act & Assert - verify both variants compile and are distinct
    let ephemeral = CacheType::Ephemeral;
    let extended = CacheType::Extended;

    assert_ne!(ephemeral, extended, "Cache types should be distinct");
}

// ============================================================================
// Builder Method Tests - with_ephemeral_cache()
// ============================================================================

#[test]
fn test_with_ephemeral_cache_sets_cache_type() {
    // Test verifies with_ephemeral_cache() sets cache_type to Ephemeral
    //
    // Business rule: Ephemeral cache marks content for 5-minute caching

    // Arrange
    let message = UnifiedMessage::user("Test content");

    // Act
    let cached_message = message.with_ephemeral_cache();

    // Assert
    assert_eq!(
        cached_message.attributes.cache_type,
        Some(CacheType::Ephemeral),
        "cache_type should be set to Ephemeral"
    );
}

#[test]
fn test_with_ephemeral_cache_sets_cacheable_true() {
    // Test verifies with_ephemeral_cache() enables caching
    //
    // Business rule: Cache type implies message is cacheable

    // Arrange
    let message = UnifiedMessage::user("Test content");
    assert!(!message.attributes.cacheable, "Should start non-cacheable");

    // Act
    let cached_message = message.with_ephemeral_cache();

    // Assert
    assert!(
        cached_message.attributes.cacheable,
        "cacheable flag should be set to true"
    );
}

#[test]
fn test_with_ephemeral_cache_preserves_message_content() {
    // Test verifies builder method doesn't alter message content
    //
    // Business rule: Cache control is metadata, not content modification

    // Arrange
    let original_content = "Important message content";
    let message = UnifiedMessage::user(original_content);

    // Act
    let cached_message = message.with_ephemeral_cache();

    // Assert
    match cached_message.content {
        crate::messages::MessageContent::Text(ref text) => {
            assert_eq!(text, original_content, "Content should be preserved");
        }
        _ => panic!("Expected text content"),
    }
}

#[test]
fn test_with_ephemeral_cache_preserves_role() {
    // Test verifies builder method doesn't alter message role
    //
    // Business rule: Cache control doesn't change message semantics

    // Arrange
    let message = UnifiedMessage::system("System prompt");

    // Act
    let cached_message = message.with_ephemeral_cache();

    // Assert
    assert_eq!(
        cached_message.role,
        MessageRole::System,
        "Role should be preserved"
    );
}

// ============================================================================
// Builder Method Tests - with_extended_cache()
// ============================================================================

#[test]
fn test_with_extended_cache_sets_cache_type() {
    // Test verifies with_extended_cache() sets cache_type to Extended
    //
    // Business rule: Extended cache marks content for 1-hour caching

    // Arrange
    let message = UnifiedMessage::user("Test content");

    // Act
    let cached_message = message.with_extended_cache();

    // Assert
    assert_eq!(
        cached_message.attributes.cache_type,
        Some(CacheType::Extended),
        "cache_type should be set to Extended"
    );
}

#[test]
fn test_with_extended_cache_sets_cacheable_true() {
    // Test verifies with_extended_cache() enables caching
    //
    // Business rule: Cache type implies message is cacheable

    // Arrange
    let message = UnifiedMessage::user("Test content");
    assert!(!message.attributes.cacheable, "Should start non-cacheable");

    // Act
    let cached_message = message.with_extended_cache();

    // Assert
    assert!(
        cached_message.attributes.cacheable,
        "cacheable flag should be set to true"
    );
}

#[test]
fn test_with_extended_cache_preserves_message_content() {
    // Test verifies builder method doesn't alter message content
    //
    // Business rule: Cache control is metadata, not content modification

    // Arrange
    let original_content = "Important message content";
    let message = UnifiedMessage::user(original_content);

    // Act
    let cached_message = message.with_extended_cache();

    // Assert
    match cached_message.content {
        crate::messages::MessageContent::Text(ref text) => {
            assert_eq!(text, original_content, "Content should be preserved");
        }
        _ => panic!("Expected text content"),
    }
}

// ============================================================================
// Builder Method Tests - Chaining and Edge Cases
// ============================================================================

#[test]
fn test_cache_builders_are_distinct() {
    // Test verifies ephemeral and extended builders produce different results
    //
    // Business rule: Two cache types must be distinguishable

    // Arrange
    let message = UnifiedMessage::user("Test content");

    // Act
    let ephemeral_cached = message.clone().with_ephemeral_cache();
    let extended_cached = message.with_extended_cache();

    // Assert
    assert_ne!(
        ephemeral_cached.attributes.cache_type, extended_cached.attributes.cache_type,
        "Different builders should produce different cache types"
    );
}

#[test]
fn test_cache_builder_on_system_message() {
    // Test verifies cache builders work with system messages
    //
    // Business rule: System prompts are prime candidates for caching

    // Arrange
    let message = UnifiedMessage::system("You are a helpful assistant");

    // Act
    let cached_message = message.with_extended_cache();

    // Assert
    assert_eq!(cached_message.role, MessageRole::System);
    assert_eq!(
        cached_message.attributes.cache_type,
        Some(CacheType::Extended)
    );
    assert!(cached_message.attributes.cacheable);
}

#[test]
fn test_cache_builder_on_assistant_message() {
    // Test verifies cache builders work with assistant messages
    //
    // Business rule: All message roles support caching

    // Arrange
    let message = UnifiedMessage::assistant("Previous response");

    // Act
    let cached_message = message.with_ephemeral_cache();

    // Assert
    assert_eq!(cached_message.role, MessageRole::Assistant);
    assert_eq!(
        cached_message.attributes.cache_type,
        Some(CacheType::Ephemeral)
    );
    assert!(cached_message.attributes.cacheable);
}

#[test]
fn test_overwriting_cache_type() {
    // Test verifies calling a second builder overwrites the first
    //
    // Business rule: Last cache type specification wins

    // Arrange
    let message = UnifiedMessage::user("Test content");

    // Act - apply ephemeral then extended
    let cached_message = message.with_ephemeral_cache().with_extended_cache();

    // Assert - should be extended (last applied)
    assert_eq!(
        cached_message.attributes.cache_type,
        Some(CacheType::Extended),
        "Second builder should overwrite first"
    );
}

// ============================================================================
// MessageRole Display Tests
// ============================================================================

#[test]
fn test_message_role_display_system() {
    // Test verifies MessageRole::System displays as "system"
    assert_eq!(format!("{}", MessageRole::System), "system");
}

#[test]
fn test_message_role_display_user() {
    // Test verifies MessageRole::User displays as "user"
    assert_eq!(format!("{}", MessageRole::User), "user");
}

#[test]
fn test_message_role_display_assistant() {
    // Test verifies MessageRole::Assistant displays as "assistant"
    assert_eq!(format!("{}", MessageRole::Assistant), "assistant");
}

#[test]
fn test_message_role_display_tool() {
    // Test verifies MessageRole::Tool displays as "tool"
    assert_eq!(format!("{}", MessageRole::Tool), "tool");
}

// ============================================================================
// MessageContent Display Tests
// ============================================================================

use crate::messages::MessageContent;

#[test]
fn test_message_content_display_text() {
    // Test verifies MessageContent::Text displays the text content
    let content = MessageContent::Text("Hello, world!".to_string());
    assert_eq!(format!("{}", content), "Hello, world!");
}

#[test]
fn test_message_content_display_json() {
    // Test verifies MessageContent::Json displays pretty-printed JSON
    let content = MessageContent::Json(serde_json::json!({"key": "value"}));
    let display = format!("{}", content);
    assert!(display.contains("key"));
    assert!(display.contains("value"));
}

#[test]
fn test_message_content_display_tool_call() {
    // Test verifies MessageContent::ToolCall displays name and args
    let content = MessageContent::ToolCall {
        id: "call_123".to_string(),
        name: "get_weather".to_string(),
        arguments: serde_json::json!({"city": "London"}),
    };
    let display = format!("{}", content);
    assert!(display.contains("get_weather"));
    assert!(display.contains("London"));
}

#[test]
fn test_message_content_display_tool_result_success() {
    // Test verifies MessageContent::ToolResult displays content for success
    let content = MessageContent::ToolResult {
        tool_call_id: "call_123".to_string(),
        content: "Sunny, 22°C".to_string(),
        is_error: false,
    };
    assert_eq!(format!("{}", content), "Sunny, 22°C");
}

#[test]
fn test_message_content_display_tool_result_error() {
    // Test verifies MessageContent::ToolResult displays "Error:" prefix for errors
    let content = MessageContent::ToolResult {
        tool_call_id: "call_123".to_string(),
        content: "API timeout".to_string(),
        is_error: true,
    };
    let display = format!("{}", content);
    assert!(display.starts_with("Error:"));
    assert!(display.contains("API timeout"));
}

// ============================================================================
// Semantic Constructor Tests
// ============================================================================

use crate::messages::{MessageAttributes, MessageCategory};

#[test]
fn test_unified_message_with_attributes() {
    // Test verifies with_attributes creates message with custom attributes
    let attrs = MessageAttributes {
        priority: 10,
        cacheable: true,
        cache_type: Some(CacheType::Extended),
        cache_key: Some("custom-key".to_string()),
        category: MessageCategory::Context,
        metadata: std::collections::HashMap::new(),
    };

    let message = UnifiedMessage::with_attributes(
        MessageRole::System,
        MessageContent::Text("Test content".to_string()),
        attrs,
    );

    assert_eq!(message.role, MessageRole::System);
    assert_eq!(message.attributes.priority, 10);
    assert!(message.attributes.cacheable);
    assert_eq!(message.attributes.cache_type, Some(CacheType::Extended));
    assert_eq!(message.attributes.cache_key, Some("custom-key".to_string()));
    assert_eq!(message.attributes.category, MessageCategory::Context);
}

#[test]
fn test_system_instruction_constructor() {
    // Test verifies system_instruction creates cacheable high-priority message
    //
    // Business rule: System instructions are priority 0 and cacheable
    let message =
        UnifiedMessage::system_instruction("You are helpful.".to_string(), Some("v1".to_string()));

    assert_eq!(message.role, MessageRole::System);
    assert_eq!(message.attributes.priority, 0);
    assert!(message.attributes.cacheable);
    assert_eq!(message.attributes.cache_key, Some("v1".to_string()));
    assert_eq!(
        message.attributes.category,
        MessageCategory::SystemInstruction
    );
}

#[test]
fn test_system_instruction_without_cache_key() {
    // Test verifies system_instruction works without cache key
    let message = UnifiedMessage::system_instruction("You are helpful.".to_string(), None);

    assert_eq!(message.attributes.cache_key, None);
    assert!(message.attributes.cacheable);
}

#[test]
fn test_tool_definition_constructor() {
    // Test verifies tool_definition creates cacheable priority-1 message
    //
    // Business rule: Tool definitions come after system instructions
    let message =
        UnifiedMessage::tool_definition("Tool schema".to_string(), Some("tools-v1".to_string()));

    assert_eq!(message.role, MessageRole::System);
    assert_eq!(message.attributes.priority, 1);
    assert!(message.attributes.cacheable);
    assert_eq!(message.attributes.category, MessageCategory::ToolDefinition);
}

#[test]
fn test_context_constructor() {
    // Test verifies context creates cacheable medium-priority message
    //
    // Business rule: Context is priority 5
    let message = UnifiedMessage::context("User preferences".to_string(), None);

    assert_eq!(message.role, MessageRole::System);
    assert_eq!(message.attributes.priority, 5);
    assert!(message.attributes.cacheable);
    assert_eq!(message.attributes.category, MessageCategory::Context);
}

#[test]
fn test_history_constructor() {
    // Test verifies history creates cacheable lower-priority message
    //
    // Business rule: History is priority 20
    let message = UnifiedMessage::history(MessageRole::User, "Previous question".to_string());

    assert_eq!(message.role, MessageRole::User);
    assert_eq!(message.attributes.priority, 20);
    assert!(message.attributes.cacheable);
    assert_eq!(message.attributes.category, MessageCategory::History);
}

#[test]
fn test_current_user_constructor() {
    // Test verifies current_user creates non-cacheable lowest-priority message
    //
    // Business rule: Current user input is priority 30 and not cached
    let message = UnifiedMessage::current_user("What's the weather?".to_string());

    assert_eq!(message.role, MessageRole::User);
    assert_eq!(message.attributes.priority, 30);
    assert!(!message.attributes.cacheable);
    assert_eq!(message.attributes.category, MessageCategory::Current);
}

#[test]
fn test_tool_call_constructor() {
    // Test verifies tool_call creates assistant message with ToolCall content
    let message = UnifiedMessage::tool_call(
        "call_abc".to_string(),
        "get_weather".to_string(),
        serde_json::json!({"city": "London"}),
    );

    assert_eq!(message.role, MessageRole::Assistant);
    assert_eq!(message.attributes.priority, 25);
    assert!(!message.attributes.cacheable);
    match message.content {
        MessageContent::ToolCall {
            id,
            name,
            arguments,
        } => {
            assert_eq!(id, "call_abc");
            assert_eq!(name, "get_weather");
            assert_eq!(arguments["city"], "London");
        }
        _ => panic!("Expected ToolCall content"),
    }
}

#[test]
fn test_tool_result_constructor() {
    // Test verifies tool_result creates tool message with ToolResult content
    let message =
        UnifiedMessage::tool_result("call_abc".to_string(), "Sunny, 22°C".to_string(), false);

    assert_eq!(message.role, MessageRole::Tool);
    assert_eq!(message.attributes.priority, 26);
    assert!(!message.attributes.cacheable);
    match message.content {
        MessageContent::ToolResult {
            tool_call_id,
            content,
            is_error,
        } => {
            assert_eq!(tool_call_id, "call_abc");
            assert_eq!(content, "Sunny, 22°C");
            assert!(!is_error);
        }
        _ => panic!("Expected ToolResult content"),
    }
}

#[test]
fn test_tool_result_constructor_with_error() {
    // Test verifies tool_result handles error flag correctly
    let message = UnifiedMessage::tool_result(
        "call_abc".to_string(),
        "Connection failed".to_string(),
        true,
    );

    match message.content {
        MessageContent::ToolResult { is_error, .. } => {
            assert!(is_error);
        }
        _ => panic!("Expected ToolResult content"),
    }
}

// ============================================================================
// UnifiedLLMRequest Tests
// ============================================================================

use crate::messages::UnifiedLLMRequest;
use crate::provider::RequestConfig;

#[test]
fn test_unified_llm_request_new() {
    // Test verifies new() creates request with messages only
    let messages = vec![
        UnifiedMessage::system("System prompt"),
        UnifiedMessage::user("User input"),
    ];

    let request = UnifiedLLMRequest::new(messages);

    assert_eq!(request.messages.len(), 2);
    assert!(request.response_schema.is_none());
    assert!(request.config.is_none());
}

#[test]
fn test_unified_llm_request_with_schema() {
    // Test verifies with_schema() creates request with response schema
    let messages = vec![UnifiedMessage::user("Extract data")];
    let schema = serde_json::json!({
        "type": "object",
        "properties": {
            "name": {"type": "string"}
        }
    });

    let request = UnifiedLLMRequest::with_schema(messages, schema.clone());

    assert_eq!(request.messages.len(), 1);
    assert_eq!(request.response_schema, Some(schema));
    assert!(request.config.is_none());
}

#[test]
fn test_unified_llm_request_with_config() {
    // Test verifies with_config() creates request with config override
    let messages = vec![UnifiedMessage::user("Hello")];
    let config = RequestConfig {
        temperature: Some(0.7),
        max_tokens: Some(1000),
        ..Default::default()
    };

    let request = UnifiedLLMRequest::with_config(messages, config);

    assert_eq!(request.messages.len(), 1);
    assert!(request.response_schema.is_none());
    assert!(request.config.is_some());
    assert_eq!(request.config.as_ref().unwrap().temperature, Some(0.7));
}

#[test]
fn test_sort_messages_by_priority() {
    // Test verifies sort_messages() orders by priority (lower = first)
    let messages = vec![
        UnifiedMessage::current_user("User input".to_string()), // priority 30
        UnifiedMessage::system_instruction("System".to_string(), None), // priority 0
        UnifiedMessage::context("Context".to_string(), None),   // priority 5
    ];

    let mut request = UnifiedLLMRequest::new(messages);
    request.sort_messages();

    assert_eq!(request.messages[0].attributes.priority, 0);
    assert_eq!(request.messages[1].attributes.priority, 5);
    assert_eq!(request.messages[2].attributes.priority, 30);
}

#[test]
fn test_get_sorted_messages_does_not_modify_original() {
    // Test verifies get_sorted_messages() returns sorted view without modifying
    let messages = vec![
        UnifiedMessage::current_user("User input".to_string()), // priority 30
        UnifiedMessage::system_instruction("System".to_string(), None), // priority 0
    ];

    let request = UnifiedLLMRequest::new(messages);
    let sorted = request.get_sorted_messages();

    // Original unchanged
    assert_eq!(request.messages[0].attributes.priority, 30);
    // Sorted view is ordered
    assert_eq!(sorted[0].attributes.priority, 0);
    assert_eq!(sorted[1].attributes.priority, 30);
}

#[test]
fn test_get_system_messages() {
    // Test verifies get_system_messages() filters to system role only
    let messages = vec![
        UnifiedMessage::system("System 1"),
        UnifiedMessage::user("User"),
        UnifiedMessage::system("System 2"),
        UnifiedMessage::assistant("Assistant"),
    ];

    let request = UnifiedLLMRequest::new(messages);
    let system_msgs = request.get_system_messages();

    assert_eq!(system_msgs.len(), 2);
    assert!(system_msgs.iter().all(|m| m.role == MessageRole::System));
}

#[test]
fn test_get_conversation_messages() {
    // Test verifies get_conversation_messages() excludes system messages
    let messages = vec![
        UnifiedMessage::system("System"),
        UnifiedMessage::user("User"),
        UnifiedMessage::assistant("Assistant"),
        UnifiedMessage::tool_result("id".to_string(), "result".to_string(), false),
    ];

    let request = UnifiedLLMRequest::new(messages);
    let conv_msgs = request.get_conversation_messages();

    assert_eq!(conv_msgs.len(), 3);
    assert!(conv_msgs.iter().all(|m| m.role != MessageRole::System));
}

#[test]
fn test_get_cacheable_messages() {
    // Test verifies get_cacheable_messages() filters to cacheable only
    let messages = vec![
        UnifiedMessage::system_instruction("Cacheable system".to_string(), None), // cacheable
        UnifiedMessage::current_user("Not cached".to_string()),                   // not cacheable
        UnifiedMessage::context("Cacheable context".to_string(), None),           // cacheable
    ];

    let request = UnifiedLLMRequest::new(messages);
    let cacheable = request.get_cacheable_messages();

    assert_eq!(cacheable.len(), 2);
    assert!(cacheable.iter().all(|m| m.attributes.cacheable));
}