xz-provider 0.5.0

LLM 服务提供者抽象层 — 统一的 LLM 服务提供者接口
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
//! Wiremock SSE test utilities and [`SseLineBuffer`] integration tests.
//!
//! # SSE Test Utilities
//!
//! - [`sse_response`] — builds a simple SSE response with all events at once
//! - [`chunked_sse_response`] — builds an SSE response for byte-level fragmentation testing
//! - [`delayed_sse_response`] — builds an SSE response with a delay for cancellation testing
//!
//! # SseLineBuffer Edge-Case Tests
//!
//! Tests verify that [`SseLineBuffer`] correctly reassembles lines under:
//! - 1-byte chunks (worst case fragmentation)
//! - Multi-byte UTF-8 characters split across chunks
//! - `\r\n` and `\n` line endings mixed
//! - `\r\n` split across chunk boundaries
//! - Empty chunks
//! - Events without `data:` prefix
//! - `clear()` and `remaining()` API methods

use std::time::Duration;

use wiremock::{Request, Respond, ResponseTemplate};

use crate::providers::sse::SseLineBuffer;

// ---------------------------------------------------------------------------
// Wiremock SSE test utilities
// ---------------------------------------------------------------------------

/// Build the SSE body string from event payloads.
///
/// Each event payload is wrapped in `data: <payload>\n\n` SSE framing.
fn build_sse_body(events: &[&str]) -> String {
    let mut body = String::new();
    for event in events {
        body.push_str("data: ");
        body.push_str(event);
        body.push_str("\n\n");
    }
    body
}

/// Create a standard SSE response with all events delivered as a single body.
///
/// Each event is automatically wrapped in `data: <payload>\n\n` framing.
///
/// # Example
///
/// ```ignore
/// use xz_provider::test_sse::sse_response;
///
/// let template = sse_response(&[
///     r#"{"delta":"Hello"}"#,
///     r#"{"delta":"World"}"#,
/// ]);
/// ```
pub fn sse_response(events: &[&str]) -> ResponseTemplate {
    ResponseTemplate::new(200).set_body_string(build_sse_body(events))
}

/// Create an SSE response for byte-level fragmentation testing.
///
/// The response body is the full SSE text, but the `chunk_size` parameter
/// indicates the fragmentation granularity. When testing, split the body
/// into chunks of `chunk_size` bytes and feed them sequentially to
/// [`SseLineBuffer::feed()`].
///
/// # Wiremock limitation
///
/// Wiremock does not support streaming responses — the full body is delivered
/// as a single HTTP response. For byte-level fragmentation testing, combine
/// this with manual [`SseLineBuffer::feed()`] calls.
pub fn chunked_sse_response(events: &[&str], chunk_size: usize) -> impl Respond {
    ChunkedSseRespond { body: build_sse_body(events).into_bytes(), _chunk_size: chunk_size }
}

/// Build an SSE response with a delay before delivery.
///
/// The entire response is delayed by `delay_ms` milliseconds. Use this to test
/// cancellation: cancel the stream before the delay expires and verify no
/// events are received.
pub fn delayed_sse_response(events: &[&str], delay_ms: u64) -> ResponseTemplate {
    let body = build_sse_body(events);
    ResponseTemplate::new(200).set_body_string(body).set_delay(Duration::from_millis(delay_ms))
}

// ---------------------------------------------------------------------------
// ChunkedSseRespond — custom Respond impl for chunked SSE
// ---------------------------------------------------------------------------

#[derive(Debug)]
struct ChunkedSseRespond {
    /// Full SSE body bytes (built from events).
    body: Vec<u8>,
    /// The fragmentation granularity (used for documentation/test setup).
    _chunk_size: usize,
}

impl Respond for ChunkedSseRespond {
    fn respond(&self, _request: &Request) -> ResponseTemplate {
        let body_str = String::from_utf8_lossy(&self.body).into_owned();
        ResponseTemplate::new(200).set_body_string(body_str)
    }
}

// ---------------------------------------------------------------------------
// SseLineBuffer edge-case tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod sse_line_buffer_tests {
    use super::*;
    use futures::StreamExt;

    /// Verify `remaining()` returns partial buffer content.
    #[test]
    fn test_remaining_returns_partial_data() {
        let mut buf = SseLineBuffer::new();
        buf.feed(b"partial-line");
        assert_eq!(buf.remaining(), "partial-line");
    }

    /// Verify `remaining()` returns empty string when buffer has no data.
    #[test]
    fn test_remaining_empty_when_no_data() {
        let buf = SseLineBuffer::new();
        assert_eq!(buf.remaining(), "");
    }

    /// Verify `remaining()` returns empty string after complete lines consumed.
    #[test]
    fn test_remaining_empty_after_complete_line() {
        let mut buf = SseLineBuffer::new();
        buf.feed(b"complete\n");
        assert_eq!(buf.remaining(), "");
    }

    /// Verify `remaining()` returns only the incomplete trailing data.
    #[test]
    fn test_remaining_only_incomplete_trailing() {
        let mut buf = SseLineBuffer::new();
        buf.feed(b"line1\npartial");
        assert_eq!(buf.remaining(), "partial");
    }

    /// Verify `clear()` resets the buffer to empty.
    #[test]
    fn test_clear_removes_all_data() {
        let mut buf = SseLineBuffer::new();
        buf.feed(b"some-data\nmore-data\n");
        buf.feed(b"still-pending");
        buf.clear();
        assert_eq!(buf.remaining(), "");
    }

    /// Verify `clear()` allows buffer reuse with fresh state.
    #[test]
    fn test_clear_then_reuse() {
        let mut buf = SseLineBuffer::new();
        buf.feed(b"old-data\n");
        buf.clear();
        let lines = buf.feed(b"new-data\n");
        assert_eq!(lines, vec!["new-data"]);
    }

    /// Verify `\r\n` split across chunk boundaries: `\r` in one chunk, `\n` in next.
    #[test]
    fn test_crlf_split_across_chunks() {
        let mut buf = SseLineBuffer::new();
        let lines1 = buf.feed(b"hello\r");
        assert!(lines1.is_empty(), "CR without LF should not yield a line");

        let lines2 = buf.feed(b"\nworld\n");
        assert_eq!(lines2, vec!["hello", "world"]);
    }

    /// Verify multiple `\r\n` pairs split across chunks.
    #[test]
    fn test_multiple_crlf_splits_across_chunks() {
        let mut buf = SseLineBuffer::new();
        let lines1 = buf.feed(b"a\r\nb\r");
        assert_eq!(lines1, vec!["a"], "a\r\n should produce 'a'");

        let lines2 = buf.feed(b"\nc\r\nd\n");
        assert_eq!(lines2, vec!["b", "c", "d"]);
    }

    /// Verify carriage return at chunk boundary without trailing newline.
    /// `\r` without following `\n` is treated as regular content, not a line ending.
    #[test]
    fn test_carriage_return_at_end_no_lf() {
        let mut buf = SseLineBuffer::new();
        let lines1 = buf.feed(b"data: hello\r");
        assert!(lines1.is_empty());

        let lines2 = buf.feed(b"world\n");
        // The `\r` is kept as content since it's not followed by `\n`
        assert_eq!(lines2, vec!["data: hello\rworld"]);
    }

    /// Verify that comment lines (starting with `:`) are returned as-is.
    /// SseLineBuffer does not interpret SSE protocol — it returns all lines.
    #[test]
    fn test_comment_lines_are_preserved() {
        let mut buf = SseLineBuffer::new();
        let lines = buf.feed(b": this is a comment\n: another comment\n");
        assert_eq!(lines, vec![": this is a comment", ": another comment"]);
    }

    /// Verify that lines without `data:` prefix are returned as-is.
    /// Higher-level protocol code is responsible for filtering.
    #[test]
    fn test_non_data_lines_are_preserved() {
        let mut buf = SseLineBuffer::new();
        let lines = buf.feed(b"event: ping\ndata: {\"msg\":\"hello\"}\n\n");
        assert_eq!(lines, vec!["event: ping", "data: {\"msg\":\"hello\"}", ""]);
    }

    /// Verify that 1-byte chunks work correctly for SSE-like data.
    #[test]
    fn test_one_byte_chunks_sse_data() {
        let mut buf = SseLineBuffer::new();
        let input = b"data: A\n\ndata: B\n\n";
        let mut all_lines = Vec::new();
        for &byte in input {
            let lines = buf.feed(&[byte]);
            all_lines.extend(lines);
        }
        assert_eq!(all_lines, vec!["data: A", "", "data: B", ""]);
    }

    /// Verify 1-byte chunks with event lines mixed in.
    #[test]
    fn test_one_byte_chunks_with_event_type() {
        let mut buf = SseLineBuffer::new();
        let input = b"event: msg\ndata: hello\n\n";
        let mut all_lines = Vec::new();
        for &byte in input {
            let lines = buf.feed(&[byte]);
            all_lines.extend(lines);
        }
        assert_eq!(all_lines, vec!["event: msg", "data: hello", ""]);
    }

    /// Verify that a large payload crossing many byte chunks is correctly reassembled.
    #[test]
    fn test_large_data_fragmented() {
        let mut buf = SseLineBuffer::new();
        let large_line = "data: ".to_owned() + &"x".repeat(1000) + "\n";
        let input = large_line.repeat(5);
        let bytes = input.as_bytes();

        let mut all_lines = Vec::new();
        for chunk in bytes.chunks(7) {
            let lines = buf.feed(chunk);
            all_lines.extend(lines);
        }

        assert_eq!(all_lines.len(), 5);
        for line in &all_lines {
            assert!(line.starts_with("data: "));
            assert_eq!(line.len(), 1006); // "data: " (6) + 1000 'x's
        }
    }

    /// Verify that a multi-line SSE response via wiremock works end-to-end.
    #[tokio::test]
    async fn test_sse_response_template_utility() {
        let mock_server = wiremock::MockServer::start().await;
        let events = &[r#"{"delta":"Hello"}"#, r#"{"delta":"World"}"#];
        wiremock::Mock::given(wiremock::matchers::method("POST"))
            .and(wiremock::matchers::path("/sse"))
            .respond_with(sse_response(events))
            .mount(&mock_server)
            .await;

        let client = reqwest::Client::new();
        let resp = client.post(format!("{}/sse", mock_server.uri())).send().await.unwrap();

        assert!(resp.status().is_success());

        let stream = crate::http::create_sse_stream(resp, None);
        let lines: Vec<String> =
            futures::StreamExt::filter_map(stream, |r| futures::future::ready(r.ok()))
                .collect()
                .await;

        let expected = vec!["data: {\"delta\":\"Hello\"}", "", "data: {\"delta\":\"World\"}", ""];
        assert_eq!(lines, expected);
    }

    /// Verify that delayed SSE response works with cancellation.
    #[tokio::test]
    async fn test_delayed_sse_response_cancellation() {
        let mock_server = wiremock::MockServer::start().await;
        let events = &[r#"{"delta":"Hello"}"#, r#"{"delta":"World"}"#];
        wiremock::Mock::given(wiremock::matchers::method("POST"))
            .and(wiremock::matchers::path("/delayed"))
            .respond_with(delayed_sse_response(events, 5000))
            .mount(&mock_server)
            .await;

        let client = reqwest::Client::new();
        // Use a short timeout so the request itself times out before the mock's delay
        let resp = client
            .post(format!("{}/delayed", mock_server.uri()))
            .timeout(Duration::from_millis(100))
            .send()
            .await;

        // The request should time out because the mock delays 5000ms but
        // the client timeout is only 100ms
        assert!(resp.is_err(), "expected timeout error");
    }

    /// Verify that the chunked SSE response utility can be used end-to-end.
    #[tokio::test]
    async fn test_chunked_sse_response_utility() {
        let mock_server = wiremock::MockServer::start().await;
        let events = &[r#"{"delta":"Hello"}"#, r#"{"delta":"World"}"#];
        wiremock::Mock::given(wiremock::matchers::method("POST"))
            .and(wiremock::matchers::path("/chunked"))
            .respond_with(chunked_sse_response(events, 5))
            .mount(&mock_server)
            .await;

        let client = reqwest::Client::new();
        let resp = client.post(format!("{}/chunked", mock_server.uri())).send().await.unwrap();

        assert!(resp.status().is_success());

        // The body should be the full SSE text even though the response
        // was created with chunked_sse_response
        let full_body = resp.text().await.unwrap();
        assert!(full_body.contains("Hello"));
        assert!(full_body.contains("World"));
        assert!(full_body.starts_with("data: "));
    }

    /// Verify that SseLineBuffer handles events with multiple data fields.
    #[test]
    fn test_multiple_data_fields() {
        let mut buf = SseLineBuffer::new();
        let lines = buf.feed(b"data: line1\ndata: line2\ndata: line3\n");
        assert_eq!(lines, vec!["data: line1", "data: line2", "data: line3"]);
    }

    /// Verify that an empty string fed does not change buffer state.
    #[test]
    fn test_empty_feed_after_partial() {
        let mut buf = SseLineBuffer::new();
        buf.feed(b"partial");
        let lines = buf.feed(b"");
        assert!(lines.is_empty());
        assert_eq!(buf.remaining(), "partial");
    }

    /// Verify that multiple empty feeds followed by a terminating newline work.
    #[test]
    fn test_multiple_empty_feeds_then_complete() {
        let mut buf = SseLineBuffer::new();
        buf.feed(b"");
        buf.feed(b"");
        buf.feed(b"hello\n");
        assert_eq!(buf.remaining(), "");
        let lines = buf.feed(b"");
        assert!(lines.is_empty());
    }
}

// ---------------------------------------------------------------------------
// create_sse_stream integration tests using wiremock SSE utilities
// ---------------------------------------------------------------------------

#[cfg(test)]
mod sse_integration_tests {
    use super::*;
    use futures::StreamExt;

    /// Full integration: wiremock SSE response → create_sse_stream → line collection.
    #[tokio::test]
    async fn test_create_sse_stream_with_multiple_events() {
        let mock_server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::method("POST"))
            .and(wiremock::matchers::path("/multi"))
            .respond_with(sse_response(&[
                r#"{"type":"event1"}"#,
                r#"{"type":"event2"}"#,
                r#"{"type":"event3"}"#,
            ]))
            .mount(&mock_server)
            .await;

        let client = reqwest::Client::new();
        let resp = client.post(format!("{}/multi", mock_server.uri())).send().await.unwrap();

        let stream = crate::http::create_sse_stream(resp, None);
        let lines: Vec<String> =
            stream.filter_map(|r| futures::future::ready(r.ok())).collect().await;

        assert_eq!(lines.len(), 6); // 3 data lines + 3 empty separators
        assert_eq!(lines[0], "data: {\"type\":\"event1\"}");
        assert_eq!(lines[2], "data: {\"type\":\"event2\"}");
        assert_eq!(lines[4], "data: {\"type\":\"event3\"}");
    }

    /// Verify that a single event SSE response works correctly.
    #[tokio::test]
    async fn test_create_sse_stream_single_event() {
        let mock_server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::method("POST"))
            .and(wiremock::matchers::path("/single"))
            .respond_with(sse_response(&[r#"{"done":true}"#]))
            .mount(&mock_server)
            .await;

        let client = reqwest::Client::new();
        let resp = client.post(format!("{}/single", mock_server.uri())).send().await.unwrap();

        let stream = crate::http::create_sse_stream(resp, None);
        let lines: Vec<String> =
            stream.filter_map(|r| futures::future::ready(r.ok())).collect().await;

        assert_eq!(lines, vec!["data: {\"done\":true}", ""]);
    }

    /// Verify that create_sse_stream with an immediate cancellation token
    /// yields very few or zero lines.
    #[tokio::test]
    async fn test_create_sse_stream_cancellation_immediate() {
        let mock_server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::method("POST"))
            .and(wiremock::matchers::path("/cancel-immediate"))
            .respond_with(sse_response(&[
                r#"{"msg":"one"}"#,
                r#"{"msg":"two"}"#,
                r#"{"msg":"three"}"#,
            ]))
            .mount(&mock_server)
            .await;

        let client = reqwest::Client::new();
        let resp =
            client.post(format!("{}/cancel-immediate", mock_server.uri())).send().await.unwrap();

        let cancel = crate::CancellationToken::new();
        cancel.cancel(); // cancel before reading

        let stream = crate::http::create_sse_stream(resp, Some(cancel));
        let lines: Vec<String> =
            stream.filter_map(|r| futures::future::ready(r.ok())).collect().await;

        // With immediate cancellation, expect 0 or very few lines
        assert!(
            lines.len() < 3,
            "expected fewer than 3 lines with immediate cancellation, got {}",
            lines.len()
        );
    }
}