api_openai 0.3.0

OpenAI's API for accessing large language models (LLMs).
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
//! Streaming Control Tests
//!
//! Comprehensive test suite for streaming control functionality including:
//! - Cancellation token operations
//! - Stream control state management
//! - Control command channels
//! - Timeout and combination scenarios
//! - Performance validation

#![ allow( clippy::unreadable_literal ) ]
#![ allow( clippy::uninlined_format_args ) ]
#![ allow( clippy::std_instead_of_core ) ]
#![ allow( clippy::useless_vec ) ]
#![ allow( clippy::unused_async ) ]
#![ allow( clippy::must_use_candidate ) ]
#![ allow( clippy::missing_panics_doc ) ]
#![ allow( clippy::missing_errors_doc ) ]
#![ allow( clippy::doc_markdown ) ]

#[ cfg( test ) ]
mod streaming_control_tests
{
  use api_openai::streaming_control::*;
  use std::time::Duration;
  use tokio::time;

  // ===== CANCELLATION TOKEN TESTS =====

  #[ tokio::test ]
  async fn test_cancellation_token_creation()
  {
    let token = CancellationToken::new();
    assert!( !token.is_cancelled() );

    let token_default = CancellationToken::default();
    assert!( !token_default.is_cancelled() );
  }

  #[ tokio::test ]
  async fn test_cancellation_token_cancel()
  {
    let token = CancellationToken::new();
    assert!( !token.is_cancelled() );

    token.cancel();
    assert!( token.is_cancelled() );
  }

  #[ tokio::test ]
  async fn test_cancellation_token_clone()
  {
    let token = CancellationToken::new();
    let cloned = token.clone();

    assert!( !token.is_cancelled() );
    assert!( !cloned.is_cancelled() );

    token.cancel();
    assert!( token.is_cancelled() );
    assert!( cloned.is_cancelled() ); // Should share state
  }

  #[ tokio::test ]
  async fn test_cancellation_token_wait_immediate()
  {
    let token = CancellationToken::new();
    token.cancel();

    let result = token.wait_for_cancellation( Duration::from_millis( 100 ) ).await;
    assert!( result );
  }

  #[ tokio::test ]
  async fn test_cancellation_token_wait_timeout()
  {
    let token = CancellationToken::new();

    let result = token.wait_for_cancellation( Duration::from_millis( 50 ) ).await;
    assert!( !result ); // Should timeout without cancellation
  }

  #[ tokio::test ]
  async fn test_cancellation_token_wait_during()
  {
    let token = CancellationToken::new();
    let token_clone = token.clone();

    // Cancel after a short delay
    tokio ::spawn( async move
    {
      time ::sleep( Duration::from_millis( 25 ) ).await;
      token_clone.cancel();
    });

    let result = token.wait_for_cancellation( Duration::from_millis( 100 ) ).await;
    assert!( result );
  }

  // ===== STREAM CONTROL TESTS =====

  #[ tokio::test ]
  async fn test_stream_control_creation()
  {
    let control = StreamControl::new();
    assert!( matches!( control.state(), StreamState::Running ) );
    assert!( !control.cancellation_token().is_cancelled() );
    assert!( control.is_active() );

    let control_default = StreamControl::default();
    assert!( matches!( control_default.state(), StreamState::Running ) );
  }

  #[ tokio::test ]
  async fn test_stream_control_state_management()
  {
    let mut control = StreamControl::new();

    // Test state transitions
    control.set_state( StreamState::Paused );
    assert!( matches!( control.state(), StreamState::Paused ) );
    assert!( control.is_active() );

    control.set_state( StreamState::Completed );
    assert!( matches!( control.state(), StreamState::Completed ) );
    assert!( !control.is_active() );

    control.set_state( StreamState::Error( "Test error".to_string() ) );
    assert!( matches!( control.state(), StreamState::Error( _ ) ) );
    assert!( !control.is_active() );
  }

  #[ tokio::test ]
  async fn test_stream_control_cancel()
  {
    let mut control = StreamControl::new();
    assert!( control.is_active() );
    assert!( !control.cancellation_token().is_cancelled() );

    control.cancel();
    assert!( !control.is_active() );
    assert!( control.cancellation_token().is_cancelled() );
    assert!( matches!( control.state(), StreamState::Cancelled ) );
  }

  #[ tokio::test ]
  async fn test_stream_control_elapsed_time()
  {
    let control = StreamControl::new();
    time ::sleep( Duration::from_millis( 10 ) ).await;

    let elapsed = control.elapsed();
    assert!( elapsed >= Duration::from_millis( 10 ) );
    assert!( elapsed < Duration::from_millis( 100 ) ); // Reasonable upper bound
  }

  // ===== STREAM STATE TESTS =====

  #[ tokio::test ]
  async fn test_stream_state_serialization()
  {
    let states = vec![
      StreamState::Running,
      StreamState::Paused,
      StreamState::Cancelled,
      StreamState::Completed,
      StreamState::Error( "Test error".to_string() ),
    ];

    for state in states
    {
      // Test serialization
      let serialized = serde_json::to_string( &state ).expect( "Failed to serialize state" );
      assert!( !serialized.is_empty() );

      // Test deserialization
      let deserialized : StreamState = serde_json::from_str( &serialized )
        .expect( "Failed to deserialize state" );

      assert_eq!( state, deserialized );
    }
  }

  #[ tokio::test ]
  async fn test_stream_state_equality()
  {
    assert_eq!( StreamState::Running, StreamState::Running );
    assert_eq!( StreamState::Paused, StreamState::Paused );
    assert_ne!( StreamState::Running, StreamState::Paused );

    let error1 = StreamState::Error( "Error 1".to_string() );
    let error2 = StreamState::Error( "Error 1".to_string() );
    let error3 = StreamState::Error( "Error 2".to_string() );

    assert_eq!( error1, error2 );
    assert_ne!( error1, error3 );
  }

  // ===== CONFIG TESTS =====

  #[ tokio::test ]
  async fn test_stream_control_config_defaults()
  {
    let config = StreamControlConfig::default();

    assert_eq!( config.max_pause_duration, Duration::from_secs( 300 ) );
    assert_eq!( config.pause_buffer_size, 1024 * 1024 );
    assert_eq!( config.control_timeout, Duration::from_secs( 5 ) );
  }

  #[ tokio::test ]
  async fn test_stream_control_config_serialization()
  {
    let config = StreamControlConfig::default();

    // Test serialization
    let serialized = serde_json::to_string( &config ).expect( "Failed to serialize config" );
    assert!( !serialized.is_empty() );

    // Test deserialization
    let deserialized : StreamControlConfig = serde_json::from_str( &serialized )
      .expect( "Failed to deserialize config" );

    assert_eq!( config.max_pause_duration, deserialized.max_pause_duration );
    assert_eq!( config.pause_buffer_size, deserialized.pause_buffer_size );
    assert_eq!( config.control_timeout, deserialized.control_timeout );
  }

  // ===== MANAGER UTILITY TESTS =====

  #[ tokio::test ]
  async fn test_with_cancellation_success()
  {
    let token = CancellationToken::new();

    let result = StreamControlManager::with_cancellation( token, || async { 42 } ).await;

    assert!( result.is_ok() );
    assert_eq!( result.unwrap(), 42 );
  }

  #[ tokio::test ]
  async fn test_with_cancellation_cancelled()
  {
    let token = CancellationToken::new();
    let token_clone = token.clone();

    // Cancel immediately
    token.cancel();

    let result = StreamControlManager::with_cancellation( token_clone, || async
    {
      time ::sleep( Duration::from_millis( 100 ) ).await;
      42
    }).await;

    assert!( result.is_err() );
    assert_eq!( result.unwrap_err(), "Operation was cancelled" );
  }

  #[ tokio::test ]
  async fn test_with_cancellation_during_operation()
  {
    let token = CancellationToken::new();
    let token_clone = token.clone();

    // Cancel after a short delay
    tokio ::spawn( async move
    {
      time ::sleep( Duration::from_millis( 25 ) ).await;
      token.cancel();
    });

    let result = StreamControlManager::with_cancellation( token_clone, || async
    {
      time ::sleep( Duration::from_millis( 100 ) ).await;
      42
    }).await;

    assert!( result.is_err() );
    assert_eq!( result.unwrap_err(), "Operation was cancelled" );
  }

  #[ tokio::test ]
  async fn test_timeout_token()
  {
    let token = StreamControlManager::create_timeout_token( Duration::from_millis( 50 ) );

    assert!( !token.is_cancelled() );

    // Wait for the timeout to trigger with some buffer for scheduling delays
    time ::sleep( Duration::from_millis( 100 ) ).await;

    // Retry mechanism to handle timing variations
    let mut attempts = 0;
    while !token.is_cancelled() && attempts < 5
    {
      time ::sleep( Duration::from_millis( 10 ) ).await;
      attempts += 1;
    }

    assert!( token.is_cancelled(), "Token should be cancelled after timeout" );
  }

  #[ tokio::test ]
  async fn test_combine_tokens()
  {
    let token1 = CancellationToken::new();
    let token2 = CancellationToken::new();
    let token3 = CancellationToken::new();

    let combined = StreamControlManager::combine_tokens( vec![ token1.clone(), token2.clone(), token3.clone() ] );

    assert!( !combined.is_cancelled() );

    // Cancel one token
    token2.cancel();

    // Wait for combination to propagate
    time ::sleep( Duration::from_millis( 20 ) ).await;

    assert!( combined.is_cancelled() );
  }

  // ===== CONTROL CHANNEL TESTS =====

  #[ tokio::test ]
  async fn test_control_channel_creation()
  {
    let ( sender, mut receiver ) = StreamControlManager::create_control_channel();

    // Test that we can send commands
    assert!( sender.pause().is_ok() );
    assert!( sender.resume().is_ok() );
    assert!( sender.cancel().is_ok() );

    // Test that we can receive commands
    assert!( matches!( receiver.try_recv(), Some( StreamControlCommand::Pause ) ) );
    assert!( matches!( receiver.try_recv(), Some( StreamControlCommand::Resume ) ) );
    assert!( matches!( receiver.try_recv(), Some( StreamControlCommand::Cancel ) ) );
    assert!( receiver.try_recv().is_none() ); // Should be empty now
  }

  #[ tokio::test ]
  async fn test_control_channel_async_recv()
  {
    let ( sender, mut receiver ) = StreamControlManager::create_control_channel();

    // Send command after a delay
    tokio ::spawn( async move
    {
      time ::sleep( Duration::from_millis( 25 ) ).await;
      let _ = sender.pause();
    });

    // Receive asynchronously
    let command = receiver.recv().await;
    assert!( matches!( command, Some( StreamControlCommand::Pause ) ) );
  }

  #[ tokio::test ]
  async fn test_control_command_equality()
  {
    assert_eq!( StreamControlCommand::Pause, StreamControlCommand::Pause );
    assert_eq!( StreamControlCommand::Resume, StreamControlCommand::Resume );
    assert_eq!( StreamControlCommand::Cancel, StreamControlCommand::Cancel );

    assert_ne!( StreamControlCommand::Pause, StreamControlCommand::Resume );
    assert_ne!( StreamControlCommand::Resume, StreamControlCommand::Cancel );
    assert_ne!( StreamControlCommand::Cancel, StreamControlCommand::Pause );
  }

  // ===== INTEGRATION TESTS =====

  #[ tokio::test ]
  async fn test_streaming_control_integration()
  {
    let mut control = StreamControl::new();
    let ( sender, mut receiver ) = StreamControlManager::create_control_channel();

    // Simulate receiving pause command
    sender.pause().expect( "Failed to send pause command" );
    let command = receiver.try_recv();
    assert!( matches!( command, Some( StreamControlCommand::Pause ) ) );

    // Update control state based on command
    control.set_state( StreamState::Paused );
    assert!( matches!( control.state(), StreamState::Paused ) );
    assert!( control.is_active() );

    // Cancel the control
    control.cancel();
    assert!( !control.is_active() );
    assert!( control.cancellation_token().is_cancelled() );
  }

  #[ tokio::test ]
  async fn test_performance_many_tokens()
  {
    let start = std::time::Instant::now();

    // Create many tokens quickly
    let mut tokens = Vec::new();
    for _ in 0..1000
    {
      tokens.push( CancellationToken::new() );
    }

    // Cancel all tokens
    for token in &tokens
    {
      token.cancel();
    }

    // Verify all are cancelled
    for token in &tokens
    {
      assert!( token.is_cancelled() );
    }

    let duration = start.elapsed();
    assert!( duration < Duration::from_millis( 100 ) ); // Should be very fast
  }

  #[ tokio::test ]
  async fn test_memory_cleanup()
  {
    // Create and drop many controls to test memory cleanup
    for _ in 0..100
    {
      let control = StreamControl::new();
      let token = control.cancellation_token().clone();
      drop( control );

      // Token should still work
      assert!( !token.is_cancelled() );
      token.cancel();
      assert!( token.is_cancelled() );
    }
  }
}