sdforge 0.3.0

Multi-protocol SDK framework with unified macro configuration
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
// Copyright (c) 2026 Kirky.X
// SPDX-License-Identifier: MIT
//! Tests for `stream_to_sse` SSE conversion and `StreamResponse`'s `IntoResponse`
//! HTTP SSE response impl.

use crate::streaming::{stream_to_sse, StreamEvent, StreamResponse};
use futures_util::StreamExt;
use tokio::sync::mpsc;
use tokio_stream::wrappers::ReceiverStream;

#[tokio::test]
async fn test_stream_to_sse_basic() {
    use futures_util::stream;

    let input_stream = stream::iter(vec![1i32, 2, 3]);
    let sse_stream = stream_to_sse(input_stream, |n| StreamEvent::data(serde_json::json!(n)));

    let results: Vec<String> = sse_stream.map(|r| r.unwrap()).collect().await;

    assert!(results.len() >= 3);
    assert!(results[0].starts_with("data: "));
    assert!(results[0].contains(r#""type":"data""#));
    assert!(results[results.len() - 1].contains("complete"));
}

#[tokio::test]
async fn test_stream_to_sse_with_ping() {
    use futures_util::stream;

    let input_stream = stream::iter(vec![()]);
    let sse_stream = stream_to_sse(input_stream, |_| StreamEvent::ping());

    let results: Vec<String> = sse_stream.map(|r| r.unwrap()).collect().await;

    assert!(!results.is_empty());
    assert!(results[0].contains(r#""type":"ping""#));
    assert!(results[0].contains("timestamp"));
}

#[tokio::test]
async fn test_stream_to_sse_with_error() {
    use futures_util::stream;

    let input_stream = stream::iter(vec![()]);
    let sse_stream = stream_to_sse(input_stream, |_| {
        StreamEvent::error("test error".to_string())
    });

    let results: Vec<String> = sse_stream.map(|r| r.unwrap()).collect().await;

    assert!(!results.is_empty());
    assert!(results[0].contains(r#""type":"error""#));
    assert!(results[0].contains("test error"));
}

#[tokio::test]
async fn test_stream_to_sse_with_complete() {
    use futures_util::stream;

    let input_stream = stream::iter(vec![()]);
    let sse_stream = stream_to_sse(input_stream, |_| StreamEvent::complete());

    let results: Vec<String> = sse_stream.map(|r| r.unwrap()).collect().await;

    assert!(!results.is_empty());
    let has_complete = results.iter().any(|r| r.contains("complete"));
    assert!(has_complete);
}

#[tokio::test]
async fn test_stream_to_sse_empty_stream() {
    use futures_util::stream;

    let input_stream = stream::iter(Vec::<i32>::new());
    let sse_stream = stream_to_sse(input_stream, |n| StreamEvent::data(serde_json::json!(n)));

    let results: Vec<String> = sse_stream.map(|r| r.unwrap()).collect().await;

    assert_eq!(results.len(), 1);
    assert!(results[0].contains("complete"));
}

#[tokio::test]
async fn test_stream_to_sse_format() {
    use futures_util::stream;

    let input_stream = stream::iter(vec!["hello"]);
    let sse_stream = stream_to_sse(input_stream, |s| StreamEvent::data(serde_json::json!(s)));

    let results: Vec<String> = sse_stream.map(|r| r.unwrap()).collect().await;

    assert!(results[0].starts_with("data: "));
    assert!(results[0].ends_with("\n\n"));
}

#[tokio::test]
async fn test_stream_to_sse_multiple_items() {
    use futures_util::stream;

    let input_stream = stream::iter(vec![1, 2, 3, 4, 5]);
    let sse_stream = stream_to_sse(input_stream, |n| StreamEvent::data(serde_json::json!(n)));

    let results: Vec<String> = sse_stream.map(|r| r.unwrap()).collect().await;

    let data_events: Vec<_> = results
        .iter()
        .filter(|r| r.contains(r#""type":"data""#))
        .collect();
    assert_eq!(data_events.len(), 5);
}

#[tokio::test]
async fn test_stream_to_sse_json_value_mapper() {
    use futures_util::stream;

    let input_stream = stream::iter(vec![serde_json::json!({"key": "value"})]);
    let sse_stream = stream_to_sse(input_stream, StreamEvent::data);

    let results: Vec<String> = sse_stream.map(|r| r.unwrap()).collect().await;

    assert!(results[0].contains(r#""key":"value""#));
}

// ============================================================================
// stream_to_sse Edge Case Tests
// ============================================================================

#[tokio::test]
async fn test_stream_to_sse_single_item() {
    use futures_util::stream;

    let input_stream = stream::iter(vec![42i64]);
    let sse_stream = stream_to_sse(input_stream, |n| {
        StreamEvent::data(serde_json::json!({"value": n}))
    });

    let results: Vec<String> = sse_stream.map(|r| r.unwrap()).collect().await;

    // Should have at least the data event and completion event
    assert!(!results.is_empty());
    let has_data = results.iter().any(|r| r.contains(r#""type":"data""#));
    assert!(has_data);
}

#[tokio::test]
async fn test_stream_to_sse_error_in_mapper() {
    use futures_util::stream;

    let input_stream = stream::iter(vec![()]);
    // Mapper returns StreamEvent (default serde_json::Value type)
    let sse_stream = stream_to_sse(input_stream, |_| {
        StreamEvent::error("mapper error".to_string())
    });

    let results: Vec<String> = sse_stream.map(|r| r.unwrap()).collect().await;

    assert!(!results.is_empty());
    assert!(results[0].contains(r#""type":"error""#));
}

// ============================================================================
// stream_to_sse normal data passthrough tests
//
// stream_to_sse serializes each StreamEvent<serde_json::Value> produced by
// the mapper and emits it as an SSE "data:" line, followed by a completion
// event when the source stream ends.
// ============================================================================

#[tokio::test]
async fn test_stream_to_sse_normal_data_passthrough() {
    use futures_util::StreamExt;

    // Verify that serializable items pass through the Ok branch.
    let stream = futures_util::stream::iter(vec![1i32, 2i32, 3i32]);
    let sse_stream = stream_to_sse(stream, |item| {
        StreamEvent::data(serde_json::Value::from(item))
    });

    let mut sse_stream = Box::pin(sse_stream);
    let mut count = 0;
    while let Some(item) = sse_stream.next().await {
        let data = item.unwrap();
        if data.contains("complete") {
            break;
        }
        assert!(data.starts_with("data: "), "SSE should start with 'data: '");
        count += 1;
    }
    assert_eq!(count, 3, "Should have received 3 data events");
}

// ============================================================================
// stream_to_sse send error (receiver dropped) tests
//
// When the ReceiverStream is dropped before the spawned task finishes,
// tx.send returns an error and the task breaks out of the loop (line 168).
// ============================================================================

#[tokio::test]
async fn test_stream_to_sse_send_error_when_receiver_dropped() {
    // Create a stream that yields many items slowly. By dropping the
    // ReceiverStream immediately, the spawned task's tx.send will fail
    // and the loop will break.
    let (tx_stream, rx_stream) = mpsc::channel::<i32>(100);
    let stream = ReceiverStream::new(rx_stream);

    let sse_stream = stream_to_sse(stream, |item| {
        StreamEvent::data(serde_json::Value::from(item))
    });

    // Drop the receiver side immediately to cause send errors.
    drop(sse_stream);

    // Feed items into the source stream; the spawned task will try to
    // forward them via tx.send, which fails because the receiver was
    // dropped. The task should break out of its loop gracefully.
    for i in 0..10 {
        // try_send may also fail once the receiver is gone; ignore errors
        let _ = tx_stream.send(i).await;
    }
    drop(tx_stream);

    // Give the spawned task time to observe the send error and break.
    tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;
    // If we reach here without hanging, the send-error break path
    // executed successfully.
}

// ============================================================================
// IntoResponse impl tests
//
// StreamResponse<T> implements axum's IntoResponse trait to produce an
// SSE HTTP response. These tests cover the into_response() method.
// ============================================================================

#[cfg(feature = "http")]
#[tokio::test]
async fn test_stream_response_into_response_headers() {
    use axum::response::IntoResponse;

    let response = StreamResponse::single("test_item");
    let http_response = response.into_response();

    // Verify SSE headers are set correctly
    assert_eq!(http_response.status(), 200);
    let headers = http_response.headers();
    assert_eq!(headers.get("content-type").unwrap(), "text/event-stream");
    assert_eq!(headers.get("cache-control").unwrap(), "no-cache");
    assert_eq!(headers.get("connection").unwrap(), "keep-alive");
    assert_eq!(headers.get("x-accel-buffering").unwrap(), "no");
}

#[cfg(feature = "http")]
#[tokio::test]
async fn test_stream_response_into_response_with_error_item() {
    use axum::response::IntoResponse;

    // Build a stream that yields an error item to cover the
    // StreamEvent::error branch in the IntoResponse mapper.
    let (tx, rx) = mpsc::channel::<Result<String, String>>(10);
    let stream = StreamResponse::new(ReceiverStream::new(rx));

    tokio::spawn(async move {
        let _ = tx.send(Err("test error".to_string())).await;
    });

    let http_response = stream.into_response();
    assert_eq!(http_response.status(), 200);
    // The response body is a stream; we just verify the response builds.
}

#[cfg(feature = "http")]
#[tokio::test]
async fn test_stream_response_into_response_collects_body() {
    use axum::response::IntoResponse;

    // Build a single-item stream and convert to response, then collect
    // the body to verify the SSE data flows through.
    let response = StreamResponse::single(serde_json::json!({"hello": "world"}));
    let http_response = response.into_response();

    assert_eq!(http_response.status(), 200);

    // Collect the body stream to verify data flows through.
    let body = http_response.into_body();
    let bytes = axum::body::to_bytes(body, 1024 * 1024).await.unwrap();
    let body_str = String::from_utf8_lossy(&bytes);
    assert!(
        body_str.contains("data: "),
        "Body should contain SSE data prefix, got: {}",
        body_str
    );
    assert!(
        body_str.contains("hello"),
        "Body should contain the JSON data, got: {}",
        body_str
    );
}

// ============================================================================
// stream_to_sse completion event tests
//
// The `Err` branch of `serde_json::to_string(&event)` in stream_to_sse
// (lines 156–163) is defensive code that handles the theoretical case where
// serialization fails. In practice, `stream_to_sse`'s mapper returns
// `StreamEvent<serde_json::Value>` (the default type parameter), and
// `serde_json::Value`'s `Serialize` implementation never returns `Err`.
// Therefore this branch is unreachable through the public API and cannot
// be covered by integration tests without modifying production code.
//
// The tests below verify the reachable behavior: the stream always
// terminates with a completion event after all data events.
// ============================================================================

/// Verify that stream_to_sse always emits a completion event as the last
/// SSE message, regardless of how many data items were in the input stream.
#[tokio::test]
async fn test_stream_to_sse_always_emits_completion_event() {
    use futures_util::stream;
    use futures_util::StreamExt;

    let values = vec![
        serde_json::json!(1),
        serde_json::json!(2),
        serde_json::json!(3),
    ];
    let input_stream = stream::iter(values);
    let sse_stream = stream_to_sse(input_stream, StreamEvent::data);

    let results: Vec<String> = sse_stream.map(|r| r.unwrap()).collect().await;

    // Should have 3 data events + 1 completion event = 4 total.
    assert_eq!(
        results.len(),
        4,
        "Expected 4 events (3 data + 1 complete), got: {:?}",
        results
    );

    // The last event should be the completion event.
    let last = results.last().expect("should have at least one result");
    assert!(
        last.contains("complete"),
        "Last event should be the completion event, got: {}",
        last
    );

    // All prior events should be data events.
    for data_event in &results[..results.len() - 1] {
        assert!(
            data_event.contains(r#""type":"data""#),
            "Non-completion event should be a data event, got: {}",
            data_event
        );
    }
}

// ============================================================================
// IntoResponse Err branch coverage test
//
// The `IntoResponse` impl for `StreamResponse<T>` maps `Result<T, String>`
// items from the underlying stream: `Ok(data)` -> `StreamEvent::data(...)`,
// `Err(err)` -> `StreamEvent::error(err)` (line 216 of src/streaming/mod.rs).
//
// The existing `test_stream_response_into_response_with_error_item` test
// sends an Err item but never collects the response body — the lazy SSE
// stream is never polled, so the mapper closure (and thus the Err branch)
// never executes. This test forces the body to be consumed via
// `axum::body::to_bytes`, exercising the Err branch end-to-end.
// ============================================================================

#[cfg(feature = "http")]
#[tokio::test]
async fn test_stream_response_into_response_err_branch_collected() {
    use axum::response::IntoResponse;

    // Build a stream that yields an error item to cover the
    // `Err(err) => StreamEvent::error(err)` branch in the IntoResponse mapper.
    let (tx, rx) = mpsc::channel::<Result<String, String>>(10);
    let stream = StreamResponse::new(ReceiverStream::new(rx));

    tokio::spawn(async move {
        let _ = tx.send(Err("test error".to_string())).await;
    });

    let http_response = stream.into_response();
    assert_eq!(http_response.status(), 200);

    // Collect the body stream to force the mapper closure to execute.
    let body = http_response.into_body();
    let bytes = axum::body::to_bytes(body, 1024 * 1024).await.unwrap();
    let body_str = String::from_utf8_lossy(&bytes);

    // The error event should appear in the SSE output.
    assert!(
        body_str.contains(r#""type":"error""#),
        "Body should contain an error event, got: {}",
        body_str
    );
    assert!(
        body_str.contains("test error"),
        "Body should contain the error message, got: {}",
        body_str
    );
}