llmposter 0.4.8

Drop-in mock server for OpenAI, Anthropic & Gemini APIs — library or standalone CLI. SSE streaming, tool calling, OAuth2, failure injection, streaming chaos, stateful scenarios, request capture, hot-reload, response templating. Test LLM apps without burning tokens.
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
//! OpenAI Chat Completions API format module.
//!
//! Spec: https://platform.openai.com/docs/api-reference/chat/object
//! Streaming: https://platform.openai.com/docs/api-reference/chat/streaming
//! Create: https://platform.openai.com/docs/api-reference/chat/create
//! Target: latest API version (2025)

use serde::{Deserialize, Serialize};

use crate::format::{estimate_tokens, IdGenerator};

/// Mock system fingerprint included in all OpenAI responses.
pub(crate) const SYSTEM_FINGERPRINT: &str = "fp_llmposter";

/// Return the current Unix timestamp in seconds, or 0 on clock failure.
pub(crate) fn unix_timestamp() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0)
}

// --- Response types ---

/// Full non-streaming Chat Completions response.
#[derive(Debug, Serialize, Deserialize)]
pub struct ChatCompletionResponse {
    /// Unique response identifier (e.g. `chatcmpl-llmposter-1`).
    pub id: String,
    /// Always `"chat.completion"`.
    pub object: String,
    /// Unix timestamp (seconds) when the response was created.
    pub created: u64,
    /// Model name echoed back from the request.
    pub model: String,
    /// Server fingerprint for reproducibility tracking.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub system_fingerprint: Option<String>,
    /// Service tier used for the request (e.g. `"default"`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub service_tier: Option<String>,
    /// List of completion choices (always one for mock responses).
    pub choices: Vec<Choice>,
    /// Token usage statistics.
    pub usage: Usage,
}

/// A single completion choice within a Chat Completions response.
#[derive(Debug, Serialize, Deserialize)]
pub struct Choice {
    /// Zero-based index of this choice.
    pub index: u32,
    /// The assistant's message content.
    pub message: Message,
    /// Why generation stopped (e.g. `"stop"`, `"tool_calls"`).
    pub finish_reason: String,
    /// Log probabilities (always `None` in mock responses).
    pub logprobs: Option<serde_json::Value>,
}

/// Assistant message within a completion choice.
#[derive(Debug, Serialize, Deserialize)]
pub struct Message {
    /// Always `"assistant"` for responses.
    pub role: String,
    /// Text content, or `None` when the response contains only tool calls.
    pub content: Option<String>,
    /// Tool calls requested by the model, if any.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_calls: Option<Vec<ToolCallOutput>>,
    /// Refusal message if the model declined the request.
    pub refusal: Option<String>,
}

/// A tool call emitted by the model.
#[derive(Debug, Serialize, Deserialize)]
pub struct ToolCallOutput {
    /// Streaming-only index; `None` in non-streaming responses.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub index: Option<u32>,
    /// Unique tool call ID (e.g. `call_llmposter_1`).
    pub id: String,
    /// Always `"function"`.
    #[serde(rename = "type")]
    pub call_type: String,
    /// The function name and serialized arguments.
    pub function: FunctionCall,
}

/// Function call details within a tool call.
#[derive(Debug, Serialize, Deserialize)]
pub struct FunctionCall {
    /// Function name to invoke.
    pub name: String,
    /// Arguments as a JSON-encoded string (not an object).
    pub arguments: String,
}

/// Token usage statistics for a Chat Completions response.
#[derive(Debug, Serialize, Deserialize)]
pub struct Usage {
    /// Estimated tokens in the input prompt.
    pub prompt_tokens: u64,
    /// Estimated tokens in the generated completion.
    pub completion_tokens: u64,
    /// Sum of prompt and completion tokens.
    pub total_tokens: u64,
}

// --- Streaming types ---

/// A single SSE chunk in a streaming Chat Completions response.
#[derive(Debug, Serialize)]
pub struct ChatCompletionChunk {
    /// Same ID across all chunks in one streaming response.
    pub id: String,
    /// Always `"chat.completion.chunk"`.
    pub object: String,
    /// Unix timestamp (seconds) when the response was created.
    pub created: u64,
    /// Model name echoed back from the request.
    pub model: String,
    /// Server fingerprint (present on first chunk).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub system_fingerprint: Option<String>,
    /// Service tier (present on first chunk only).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub service_tier: Option<String>,
    /// Chunk choices containing the delta payload.
    pub choices: Vec<ChunkChoice>,
}

/// A single choice within a streaming chunk.
#[derive(Debug, Serialize)]
pub struct ChunkChoice {
    /// Zero-based index of this choice.
    pub index: u32,
    /// Incremental content delta for this chunk.
    pub delta: Delta,
    /// Always serialized (as null on non-final chunks) to match real OpenAI streaming.
    pub finish_reason: Option<String>,
    /// Log probabilities (always `None` in mock responses).
    pub logprobs: Option<serde_json::Value>,
}

/// Incremental delta payload within a streaming chunk.
#[derive(Debug, Serialize)]
pub struct Delta {
    /// Present only on the first chunk (`"assistant"`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub role: Option<String>,
    /// Partial text content for this chunk.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content: Option<String>,
    /// Streamed tool call fragments, if any.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_calls: Option<Vec<ToolCallOutput>>,
    /// Refusal message fragment, if any.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub refusal: Option<String>,
}

// --- Builders ---

/// Build a complete (non-streaming) Chat Completions text response.
pub fn build_response(
    id_gen: &IdGenerator,
    model: &str,
    content: &str,
    prompt: &str,
) -> ChatCompletionResponse {
    let prompt_tokens = estimate_tokens(prompt);
    let completion_tokens = estimate_tokens(content);

    ChatCompletionResponse {
        id: id_gen.next_openai(),
        object: "chat.completion".to_string(),
        created: unix_timestamp(),
        model: model.to_string(),
        system_fingerprint: Some(SYSTEM_FINGERPRINT.to_string()),
        service_tier: Some("default".to_string()),
        choices: vec![Choice {
            index: 0,
            message: Message {
                role: "assistant".to_string(),
                content: Some(content.to_string()),
                tool_calls: None,
                refusal: None,
            },
            finish_reason: "stop".to_string(),
            logprobs: None,
        }],
        usage: Usage {
            prompt_tokens,
            completion_tokens,
            total_tokens: prompt_tokens.saturating_add(completion_tokens),
        },
    }
}

/// Build a Chat Completions refusal response.
///
/// Delegates to [`build_response`] and flips the single choice's message
/// so `content` is `null` and `refusal` holds the reason — matching real
/// OpenAI's refusal shape. `finish_reason` stays `"stop"`.
pub fn build_refusal_response(
    id_gen: &IdGenerator,
    model: &str,
    reason: &str,
    prompt: &str,
) -> ChatCompletionResponse {
    let mut resp = build_response(id_gen, model, reason, prompt);
    if let Some(choice) = resp.choices.first_mut() {
        choice.message.content = None;
        choice.message.refusal = Some(reason.to_string());
    }
    resp
}

/// Build a Chat Completions response containing tool/function calls.
pub fn build_tool_call_response(
    id_gen: &IdGenerator,
    model: &str,
    tool_calls: &[(&str, serde_json::Value)],
    prompt: &str,
) -> ChatCompletionResponse {
    let tc_outputs: Vec<ToolCallOutput> = tool_calls
        .iter()
        .map(|(name, args)| ToolCallOutput {
            index: None, // Non-streaming: index is a streaming-only field
            id: format!("call_llmposter_{}", id_gen.next_tool_call_counter()),
            call_type: "function".to_string(),
            function: FunctionCall {
                name: name.to_string(),
                arguments: serde_json::to_string(args).unwrap_or_else(|_| "{}".to_string()),
            },
        })
        .collect();

    let args_str = tool_calls
        .iter()
        .map(|(_, a)| serde_json::to_string(a).unwrap_or_default())
        .collect::<Vec<_>>()
        .join("");

    ChatCompletionResponse {
        id: id_gen.next_openai(),
        object: "chat.completion".to_string(),
        created: unix_timestamp(),
        model: model.to_string(),
        system_fingerprint: Some(SYSTEM_FINGERPRINT.to_string()),
        service_tier: Some("default".to_string()),
        choices: vec![Choice {
            index: 0,
            message: Message {
                role: "assistant".to_string(),
                content: None,
                tool_calls: Some(tc_outputs),
                refusal: None,
            },
            finish_reason: "tool_calls".to_string(),
            logprobs: None,
        }],
        usage: {
            let pt = estimate_tokens(prompt);
            let ct = estimate_tokens(&args_str);
            Usage {
                prompt_tokens: pt,
                completion_tokens: ct,
                total_tokens: pt.saturating_add(ct),
            }
        },
    }
}

/// Split content into streaming chunks.
///
/// Produces:
/// 1. First chunk: delta with role "assistant" only (no content)
/// 2. Content chunks: delta with content only
/// 3. Final chunk: empty delta with finish_reason "stop"
pub fn build_stream_chunks(
    id: &str,
    model: &str,
    content: &str,
    chunk_size: usize,
) -> Vec<ChatCompletionChunk> {
    let mut chunks = Vec::new();
    let content_pieces = crate::stream::chunk_content(content, chunk_size);

    let created = unix_timestamp();
    let fingerprint = Some(SYSTEM_FINGERPRINT.to_string());

    // Always emit a role-only initial chunk
    chunks.push(ChatCompletionChunk {
        id: id.to_string(),
        object: "chat.completion.chunk".to_string(),
        created,
        model: model.to_string(),
        system_fingerprint: fingerprint.clone(),
        service_tier: Some("default".to_string()),
        choices: vec![ChunkChoice {
            index: 0,
            delta: Delta {
                role: Some("assistant".to_string()),
                content: None,
                tool_calls: None,
                refusal: None,
            },
            finish_reason: None,
            logprobs: None,
        }],
    });

    for piece in &content_pieces {
        chunks.push(ChatCompletionChunk {
            id: id.to_string(),
            object: "chat.completion.chunk".to_string(),
            created,
            model: model.to_string(),
            system_fingerprint: fingerprint.clone(),
            service_tier: None,
            choices: vec![ChunkChoice {
                index: 0,
                delta: Delta {
                    role: None,
                    content: Some(piece.to_string()),
                    tool_calls: None,
                    refusal: None,
                },
                finish_reason: None,
                logprobs: None,
            }],
        });
    }

    // Final chunk with finish_reason "stop" and empty delta
    chunks.push(ChatCompletionChunk {
        id: id.to_string(),
        object: "chat.completion.chunk".to_string(),
        created,
        model: model.to_string(),
        system_fingerprint: fingerprint,
        service_tier: None,
        choices: vec![ChunkChoice {
            index: 0,
            delta: Delta {
                role: None,
                content: None,
                tool_calls: None,
                refusal: None,
            },
            finish_reason: Some("stop".to_string()),
            logprobs: None,
        }],
    });

    chunks
}

// --- Request extraction ---

/// Extract model and last user message from an OpenAI chat completions request body.
///
/// Message content can be a plain string or an array of content parts
/// like `[{"type": "text", "text": "..."}]`.
pub fn extract_request_info(body: &serde_json::Value) -> Result<(String, String), String> {
    let model = body
        .get("model")
        .and_then(|v| v.as_str())
        .map(|s| s.trim())
        .filter(|s| !s.is_empty())
        .ok_or("Missing or empty 'model' field in request")?
        .to_string();

    let messages = body
        .get("messages")
        .and_then(|v| v.as_array())
        .ok_or("Missing 'messages' array in request")?;

    let user_msg = messages
        .iter()
        .rev()
        .find(|m| m.get("role").and_then(|v| v.as_str()) == Some("user"))
        .ok_or("No user message found in request")?;

    let content = extract_content(user_msg)?;
    Ok((model, content))
}

/// Extract text content from a message, handling both string and array formats.
///
/// Blank/whitespace-only content — whether a bare empty string or an array
/// whose text parts all trim to empty — is rejected so we never silently
/// match a fixture on `""`. This mirrors Anthropic's behavior since v0.4.3.
fn extract_content(message: &serde_json::Value) -> Result<String, String> {
    let Some(content) = message.get("content") else {
        return Err("Message has no 'content' field".to_string());
    };

    // String content: {"content": "hello"}
    if let Some(s) = content.as_str() {
        let trimmed = s.trim();
        if trimmed.is_empty() {
            return Err("User message has blank text content".to_string());
        }
        return Ok(trimmed.to_string());
    }

    // Array content: {"content": [{"type": "text", "text": "hello"}]}
    if let Some(parts) = content.as_array() {
        let texts: Vec<&str> = parts
            .iter()
            .filter(|p| p.get("type").and_then(|v| v.as_str()) == Some("text"))
            .filter_map(|p| p.get("text").and_then(|v| v.as_str()))
            .collect();

        let joined = texts.join("\n");
        let trimmed = joined.trim();
        if trimmed.is_empty() {
            return Err("User message has no text content (image-only or unsupported)".to_string());
        }
        return Ok(trimmed.to_string());
    }

    Err("Message content is neither string nor array".to_string())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::format::{estimate_tokens, IdGenerator};

    #[test]
    fn should_build_chat_completion_response() {
        let gen = IdGenerator::new();
        let resp = build_response(&gen, "gpt-4", "Hello!", "What is Rust?");
        assert_eq!(resp.id, "chatcmpl-llmposter-1");
        assert_eq!(resp.object, "chat.completion");
        assert_eq!(resp.model, "gpt-4");
        assert_eq!(resp.choices[0].message.content.as_deref(), Some("Hello!"));
        assert_eq!(resp.choices[0].message.role, "assistant");
        assert_eq!(resp.choices[0].finish_reason, "stop");
        assert_eq!(resp.choices[0].index, 0);
        assert!(resp.usage.prompt_tokens > 0);
        assert!(resp.usage.completion_tokens > 0);
    }

    #[test]
    fn should_serialize_to_valid_json() {
        let gen = IdGenerator::new();
        let resp = build_response(&gen, "gpt-4", "test", "prompt");
        let json = serde_json::to_string(&resp).unwrap();
        assert!(json.contains("chat.completion"));
        assert!(json.contains("chatcmpl-llmposter-1"));
        // Should be deserializable back
        let _: ChatCompletionResponse = serde_json::from_str(&json).unwrap();
    }

    #[test]
    fn should_round_trip_serialization() {
        let gen = IdGenerator::new();
        let resp = build_response(&gen, "gpt-4", "round trip test", "prompt text");
        let json = serde_json::to_string(&resp).unwrap();
        let parsed: ChatCompletionResponse = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.id, resp.id);
        assert_eq!(parsed.model, resp.model);
        assert_eq!(
            parsed.choices[0].message.content.as_deref(),
            Some("round trip test")
        );
        assert_eq!(parsed.usage.prompt_tokens, resp.usage.prompt_tokens);
        assert_eq!(parsed.usage.completion_tokens, resp.usage.completion_tokens);
        assert_eq!(parsed.usage.total_tokens, resp.usage.total_tokens);
    }

    #[test]
    fn should_build_tool_call_response() {
        let gen = IdGenerator::new();
        let args = serde_json::json!({"location": "SF"});
        let tool_calls = vec![("get_weather", args)];
        let resp = build_tool_call_response(&gen, "gpt-4", &tool_calls, "prompt");
        assert_eq!(
            resp.choices[0].message.tool_calls.as_ref().unwrap().len(),
            1
        );
        let tc = &resp.choices[0].message.tool_calls.as_ref().unwrap()[0];
        assert_eq!(tc.function.name, "get_weather");
        // OpenAI sends arguments as a JSON string
        assert!(tc.function.arguments.contains("SF"));
        assert_eq!(resp.choices[0].finish_reason, "tool_calls");
    }

    #[test]
    fn should_serialize_tool_call_arguments_as_json_string() {
        let gen = IdGenerator::new();
        let args = serde_json::json!({"city": "London", "units": "celsius"});
        let tool_calls = vec![("get_weather", args)];
        let resp = build_tool_call_response(&gen, "gpt-4", &tool_calls, "prompt");
        let tc = &resp.choices[0].message.tool_calls.as_ref().unwrap()[0];
        // arguments should be a JSON string, not an object
        let parsed: serde_json::Value = serde_json::from_str(&tc.function.arguments).unwrap();
        assert_eq!(parsed["city"], "London");
        assert_eq!(parsed["units"], "celsius");
    }

    #[test]
    fn should_assign_sequential_tool_call_ids() {
        let gen = IdGenerator::new();
        let tool_calls = vec![
            ("func_a", serde_json::json!({})),
            ("func_b", serde_json::json!({"x": 1})),
        ];
        let resp = build_tool_call_response(&gen, "gpt-4", &tool_calls, "prompt");
        let tcs = resp.choices[0].message.tool_calls.as_ref().unwrap();
        // Assert prefix format and uniqueness, not exact counter values
        // (counter is shared across all IdGenerator methods)
        assert!(tcs[0].id.starts_with("call_llmposter_"));
        assert!(tcs[1].id.starts_with("call_llmposter_"));
        assert_ne!(tcs[0].id, tcs[1].id);
        assert_eq!(tcs[0].call_type, "function");
        assert_eq!(tcs[1].call_type, "function");
    }

    #[test]
    fn should_build_correct_number_of_stream_chunks() {
        // "Hello, world!" is 13 chars, chunk_size 5 → 1 role + 3 content + 1 stop = 5
        let chunks = build_stream_chunks("id-1", "gpt-4", "Hello, world!", 5);
        assert_eq!(chunks.len(), 5);
    }

    #[test]
    fn should_set_role_on_first_stream_chunk_only() {
        let chunks = build_stream_chunks("id-1", "gpt-4", "Hello!", 3);
        // First chunk has role
        assert_eq!(
            chunks[0].choices[0].delta.role.as_deref(),
            Some("assistant")
        );
        // Second chunk has no role
        assert!(chunks[1].choices[0].delta.role.is_none());
    }

    #[test]
    fn should_set_finish_reason_on_last_stream_chunk_only() {
        let chunks = build_stream_chunks("id-1", "gpt-4", "Hi", 2);
        // Content chunk: no finish_reason
        assert!(chunks[0].choices[0].finish_reason.is_none());
        // Final chunk: finish_reason "stop"
        let last = chunks.last().unwrap();
        assert_eq!(last.choices[0].finish_reason.as_deref(), Some("stop"));
        // Final chunk delta should be empty
        assert!(last.choices[0].delta.role.is_none());
        assert!(last.choices[0].delta.content.is_none());
    }

    #[test]
    fn should_produce_final_chunk_for_empty_content() {
        let chunks = build_stream_chunks("id-1", "gpt-4", "", 5);
        // Role chunk + final stop chunk
        assert_eq!(chunks.len(), 2);
        // First: role chunk
        assert_eq!(
            chunks[0].choices[0].delta.role.as_deref(),
            Some("assistant")
        );
        assert!(chunks[0].choices[0].finish_reason.is_none());
        // Second: stop chunk
        assert_eq!(chunks[1].choices[0].finish_reason.as_deref(), Some("stop"));
    }

    #[test]
    fn should_set_chunk_object_to_chat_completion_chunk() {
        let chunks = build_stream_chunks("id-1", "gpt-4", "test", 10);
        for chunk in &chunks {
            assert_eq!(chunk.object, "chat.completion.chunk");
        }
    }

    #[test]
    fn should_extract_user_message_from_request() {
        let json = serde_json::json!({
            "model": "gpt-4",
            "messages": [
                {"role": "system", "content": "You are helpful"},
                {"role": "user", "content": "Hello!"},
            ]
        });
        let (model, user_msg) = extract_request_info(&json).unwrap();
        assert_eq!(model, "gpt-4");
        assert_eq!(user_msg, "Hello!");
    }

    #[test]
    fn should_extract_last_user_message() {
        let json = serde_json::json!({
            "model": "gpt-4",
            "messages": [
                {"role": "user", "content": "first"},
                {"role": "assistant", "content": "response"},
                {"role": "user", "content": "second"},
            ]
        });
        let (_, user_msg) = extract_request_info(&json).unwrap();
        assert_eq!(user_msg, "second");
    }

    #[test]
    fn should_extract_array_content_from_user_message() {
        let json = serde_json::json!({
            "model": "gpt-4",
            "messages": [
                {
                    "role": "user",
                    "content": [
                        {"type": "text", "text": "What is this?"},
                        {"type": "image_url", "image_url": {"url": "http://example.com/img.png"}}
                    ]
                }
            ]
        });
        let (model, user_msg) = extract_request_info(&json).unwrap();
        assert_eq!(model, "gpt-4");
        assert_eq!(user_msg, "What is this?");
    }

    #[test]
    fn should_return_error_for_missing_messages() {
        let json = serde_json::json!({"model": "gpt-4"});
        let result = extract_request_info(&json);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("messages"));
    }

    #[test]
    fn should_reject_whitespace_only_model_field() {
        let json = serde_json::json!({
            "model": "   ",
            "messages": [{"role": "user", "content": "hi"}]
        });
        let result = extract_request_info(&json);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("Missing or empty 'model'"));
    }

    #[test]
    fn should_trim_padded_model_field() {
        let json = serde_json::json!({
            "model": "  gpt-4  ",
            "messages": [{"role": "user", "content": "hi"}]
        });
        let (model, _content) = extract_request_info(&json).unwrap();
        assert_eq!(model, "gpt-4");
    }

    #[test]
    fn should_return_error_for_no_user_message() {
        let json = serde_json::json!({
            "model": "gpt-4",
            "messages": [
                {"role": "system", "content": "system prompt"},
            ]
        });
        let result = extract_request_info(&json);
        assert!(result.is_err());
    }

    #[test]
    fn should_return_error_for_non_string_non_array_content() {
        let json = serde_json::json!({
            "model": "gpt-4",
            "messages": [
                {"role": "user", "content": 42}
            ]
        });
        let result = extract_request_info(&json);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("neither string nor array"));
    }

    #[test]
    fn should_return_error_for_array_content_with_no_text_parts() {
        let json = serde_json::json!({
            "model": "gpt-4",
            "messages": [
                {
                    "role": "user",
                    "content": [
                        {"type": "image_url", "image_url": {"url": "http://example.com/img.png"}}
                    ]
                }
            ]
        });
        let result = extract_request_info(&json);
        let err = result.unwrap_err();
        assert!(
            err.contains("no text content") || err.contains("blank text content"),
            "unexpected error: {}",
            err
        );
    }

    #[test]
    fn should_extract_multiple_text_parts_joined_with_newline() {
        let json = serde_json::json!({
            "model": "gpt-4",
            "messages": [
                {
                    "role": "user",
                    "content": [
                        {"type": "text", "text": "First part"},
                        {"type": "image_url", "image_url": {"url": "http://example.com"}},
                        {"type": "text", "text": "Second part"}
                    ]
                }
            ]
        });
        let (_, content) = extract_request_info(&json).unwrap();
        assert_eq!(content, "First part\nSecond part");
    }

    #[test]
    fn should_reject_blank_string_content() {
        // Regression: OpenAI previously returned Ok("") for blank content,
        // which would silently match a fixture with an empty substring.
        // Now matches Anthropic's since-v0.4.3 behavior: reject at extract.
        let json = serde_json::json!({
            "model": "gpt-4",
            "messages": [{"role": "user", "content": "   "}]
        });
        let err = extract_request_info(&json).unwrap_err();
        assert!(err.contains("blank"), "unexpected: {}", err);
    }

    #[test]
    fn should_reject_array_content_with_all_blank_text_parts() {
        let json = serde_json::json!({
            "model": "gpt-4",
            "messages": [{
                "role": "user",
                "content": [
                    {"type": "text", "text": ""},
                    {"type": "text", "text": "   "}
                ]
            }]
        });
        let err = extract_request_info(&json).unwrap_err();
        assert!(err.contains("no text content"), "unexpected: {}", err);
    }

    #[test]
    fn should_return_error_for_empty_model() {
        let json = serde_json::json!({
            "model": "",
            "messages": [
                {"role": "user", "content": "hello"}
            ]
        });
        let result = extract_request_info(&json);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("model"));
    }

    #[test]
    fn should_reject_missing_model() {
        let json = serde_json::json!({
            "messages": [
                {"role": "user", "content": "hi"},
            ]
        });
        let result = extract_request_info(&json);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("model"));
    }

    #[test]
    fn should_compute_usage_tokens() {
        let gen = IdGenerator::new();
        let resp = build_response(&gen, "gpt-4", "hello", "prompt");
        assert_eq!(resp.usage.prompt_tokens, estimate_tokens("prompt"));
        assert_eq!(resp.usage.completion_tokens, estimate_tokens("hello"));
        assert_eq!(
            resp.usage.total_tokens,
            resp.usage.prompt_tokens + resp.usage.completion_tokens
        );
    }

    #[test]
    fn should_return_error_when_user_message_has_no_content_field() {
        // User message object with no "content" key at all — triggers the
        // `let Some(content) = message.get("content") else { return Err(...) }`
        // path in extract_content (line 397-398).
        let json = serde_json::json!({
            "model": "gpt-4",
            "messages": [
                {"role": "user"}
            ]
        });
        let result = extract_request_info(&json);
        let err = result.unwrap_err();
        assert!(
            err.contains("no 'content' field"),
            "unexpected error: {}",
            err
        );
    }
}