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
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
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
//! Model tuning tests
//!
//! This module contains comprehensive tests for the model tuning functionality,
//! testing all core tuning types, configurations, and utilities.

#![ allow( clippy::float_cmp ) ] // Acceptable in tests for exact value checking
#![ allow( clippy::manual_string_new ) ] // Testing edge cases with empty strings
#![ allow( clippy::uninlined_format_args ) ] // Test readability over micro-optimizations

use api_openai::*;
use std::time::SystemTime;
use core::time::Duration;
use std::collections::HashMap;

#[ tokio::test ]
async fn test_tuning_status_variants()
{
  let validating = TuningStatus::Validating;
  assert!( matches!( validating, TuningStatus::Validating ) );

  let queued = TuningStatus::Queued;
  assert!( matches!( queued, TuningStatus::Queued ) );

  let running = TuningStatus::Running;
  assert!( matches!( running, TuningStatus::Running ) );

  let succeeded = TuningStatus::Succeeded;
  assert!( matches!( succeeded, TuningStatus::Succeeded ) );

  let failed = TuningStatus::Failed( "Training error".to_string() );
  assert!( matches!( failed, TuningStatus::Failed( _ ) ) );

  let cancelled = TuningStatus::Cancelled;
  assert!( matches!( cancelled, TuningStatus::Cancelled ) );
}

#[ tokio::test ]
async fn test_training_objective_variants()
{
  let language_modeling = TrainingObjective::LanguageModeling;
  assert!( matches!( language_modeling, TrainingObjective::LanguageModeling ) );

  let supervised = TrainingObjective::SupervisedFineTuning;
  assert!( matches!( supervised, TrainingObjective::SupervisedFineTuning ) );

  let rlhf = TrainingObjective::RLHF;
  assert!( matches!( rlhf, TrainingObjective::RLHF ) );

  let custom = TrainingObjective::Custom
  {
    name : "custom_objective".to_string(),
    parameters : HashMap::new(),
  };
  assert!( matches!( custom, TrainingObjective::Custom { .. } ) );
}

#[ tokio::test ]
async fn test_fine_tuning_method_variants()
{
  let full = FineTuningMethod::Full;
  assert!( matches!( full, FineTuningMethod::Full ) );

  let lora = FineTuningMethod::LoRA
  {
    rank : 16,
    alpha : 32.0,
    dropout : 0.1,
  };
  assert!( matches!( lora, FineTuningMethod::LoRA { .. } ) );

  let adapter = FineTuningMethod::Adapter
  {
    hidden_dim : 256,
    num_layers : 2,
  };
  assert!( matches!( adapter, FineTuningMethod::Adapter { .. } ) );

  let prefix = FineTuningMethod::PrefixTuning
  {
    prefix_length : 10,
    embedding_dim : 768,
  };
  assert!( matches!( prefix, FineTuningMethod::PrefixTuning { .. } ) );
}

#[ tokio::test ]
async fn test_hyperparameters()
{
  let params = HyperParameters
  {
    learning_rate : 1e-4,
    batch_size : 32,
    epochs : 5,
    warmup_steps : 100,
    weight_decay : 0.01,
    gradient_clip_norm : 1.0,
    lr_schedule : "cosine".to_string(),
    custom_params : HashMap::new(),
  };

  assert_eq!( params.learning_rate, 1e-4 );
  assert_eq!( params.batch_size, 32 );
  assert_eq!( params.epochs, 5 );
  assert_eq!( params.warmup_steps, 100 );
  assert_eq!( params.weight_decay, 0.01 );
  assert_eq!( params.gradient_clip_norm, 1.0 );
  assert_eq!( params.lr_schedule, "cosine" );
}

#[ tokio::test ]
async fn test_hyperparameters_default()
{
  let default_params = HyperParameters::default();

  assert_eq!( default_params.learning_rate, 1e-4 );
  assert_eq!( default_params.batch_size, 32 );
  assert_eq!( default_params.epochs, 3 );
  assert_eq!( default_params.warmup_steps, 100 );
  assert_eq!( default_params.weight_decay, 0.01 );
  assert_eq!( default_params.gradient_clip_norm, 1.0 );
  assert_eq!( default_params.lr_schedule, "linear" );
  assert!( default_params.custom_params.is_empty() );
}

#[ tokio::test ]
async fn test_training_data_config()
{
  let mut preprocessing = HashMap::new();
  preprocessing.insert( "normalize".to_string(), "true".to_string() );

  let config = TrainingDataConfig
  {
    training_file : "train.jsonl".to_string(),
    validation_file : Some( "val.jsonl".to_string() ),
    data_format : "jsonl".to_string(),
    max_sequence_length : 2048,
    preprocessing,
  };

  assert_eq!( config.training_file, "train.jsonl" );
  assert_eq!( config.validation_file, Some( "val.jsonl".to_string() ) );
  assert_eq!( config.data_format, "jsonl" );
  assert_eq!( config.max_sequence_length, 2048 );
  assert_eq!( config.preprocessing.len(), 1 );
}

#[ tokio::test ]
async fn test_resource_requirements()
{
  let requirements = TuningResourceRequirements
  {
    gpu_count : 4,
    gpu_type : Some( "A100".to_string() ),
    memory_gb : 64,
    cpu_cores : 16,
    storage_gb : 500,
  };

  assert_eq!( requirements.gpu_count, 4 );
  assert_eq!( requirements.gpu_type, Some( "A100".to_string() ) );
  assert_eq!( requirements.memory_gb, 64 );
  assert_eq!( requirements.cpu_cores, 16 );
  assert_eq!( requirements.storage_gb, 500 );
}

#[ tokio::test ]
async fn test_resource_requirements_default()
{
  let default_requirements = TuningResourceRequirements::default();

  assert_eq!( default_requirements.gpu_count, 1 );
  assert_eq!( default_requirements.gpu_type, None );
  assert_eq!( default_requirements.memory_gb, 16 );
  assert_eq!( default_requirements.cpu_cores, 4 );
  assert_eq!( default_requirements.storage_gb, 100 );
}

#[ tokio::test ]
async fn test_checkpoint_config()
{
  let config = CheckpointConfig
  {
    enabled : true,
    save_interval : 500,
    max_checkpoints : 10,
    save_directory : "./models".to_string(),
  };

  assert!( config.enabled );
  assert_eq!( config.save_interval, 500 );
  assert_eq!( config.max_checkpoints, 10 );
  assert_eq!( config.save_directory, "./models" );
}

#[ tokio::test ]
async fn test_checkpoint_config_default()
{
  let default_config = CheckpointConfig::default();

  assert!( default_config.enabled );
  assert_eq!( default_config.save_interval, 1000 );
  assert_eq!( default_config.max_checkpoints, 5 );
  assert_eq!( default_config.save_directory, "./checkpoints" );
}

#[ tokio::test ]
async fn test_model_checkpoint()
{
  let mut validation_metrics = HashMap::new();
  validation_metrics.insert( "accuracy".to_string(), 0.95 );
  validation_metrics.insert( "f1_score".to_string(), 0.92 );

  let checkpoint = ModelCheckpoint
  {
    checkpoint_id : "checkpoint_1000".to_string(),
    step : 1000,
    loss : 0.25,
    validation_metrics,
    created_at : SystemTime::now(),
    file_path : "./checkpoints/model_1000.pt".to_string(),
  };

  assert_eq!( checkpoint.checkpoint_id, "checkpoint_1000" );
  assert_eq!( checkpoint.step, 1000 );
  assert_eq!( checkpoint.loss, 0.25 );
  assert_eq!( checkpoint.validation_metrics.len(), 2 );
  assert_eq!( checkpoint.file_path, "./checkpoints/model_1000.pt" );
}

#[ tokio::test ]
async fn test_training_metrics()
{
  let mut custom_metrics = HashMap::new();
  custom_metrics.insert( "perplexity".to_string(), 15.5 );

  let metrics = TrainingMetrics
  {
    step : 2500,
    epoch : 2,
    training_loss : 0.18,
    validation_loss : Some( 0.22 ),
    learning_rate : 5e-5,
    throughput : 1200.0,
    eta_seconds : Some( 3600 ),
    custom_metrics,
  };

  assert_eq!( metrics.step, 2500 );
  assert_eq!( metrics.epoch, 2 );
  assert_eq!( metrics.training_loss, 0.18 );
  assert_eq!( metrics.validation_loss, Some( 0.22 ) );
  assert_eq!( metrics.learning_rate, 5e-5 );
  assert_eq!( metrics.throughput, 1200.0 );
  assert_eq!( metrics.eta_seconds, Some( 3600 ) );
  assert_eq!( metrics.custom_metrics.len(), 1 );
}

#[ tokio::test ]
async fn test_tuning_job_config()
{
  let config = TuningJobConfig
  {
    job_name : "bert_finetuning".to_string(),
    base_model : "bert-base-uncased".to_string(),
    training_data : TrainingDataConfig
    {
      training_file : "train.jsonl".to_string(),
      validation_file : Some( "val.jsonl".to_string() ),
      data_format : "jsonl".to_string(),
      max_sequence_length : 512,
      preprocessing : HashMap::new(),
    },
    hyperparameters : HyperParameters::default(),
    method : FineTuningMethod::LoRA
    {
      rank : 16,
      alpha : 32.0,
      dropout : 0.1,
    },
    objective : TrainingObjective::SupervisedFineTuning,
    resource_requirements : TuningResourceRequirements::default(),
    checkpointing : CheckpointConfig::default(),
    env_vars : HashMap::new(),
  };

  assert_eq!( config.job_name, "bert_finetuning" );
  assert_eq!( config.base_model, "bert-base-uncased" );
  assert!( matches!( config.method, FineTuningMethod::LoRA { .. } ) );
  assert!( matches!( config.objective, TrainingObjective::SupervisedFineTuning ) );
}

#[ tokio::test ]
async fn test_tuning_job_creation()
{
  let config = TuningJobConfig
  {
    job_name : "test_job".to_string(),
    base_model : "gpt-5-nano".to_string(),
    training_data : TrainingDataConfig
    {
      training_file : "train.jsonl".to_string(),
      validation_file : None,
      data_format : "jsonl".to_string(),
      max_sequence_length : 1024,
      preprocessing : HashMap::new(),
    },
    hyperparameters : HyperParameters::default(),
    method : FineTuningMethod::Full,
    objective : TrainingObjective::LanguageModeling,
    resource_requirements : TuningResourceRequirements::default(),
    checkpointing : CheckpointConfig::default(),
    env_vars : HashMap::new(),
  };

  let job = TuningJob::new( config );

  assert_eq!( job.config.job_name, "test_job" );
  assert!( matches!( job.status, TuningStatus::Validating ) );
  assert!( job.current_metrics.is_none() );
  assert!( job.checkpoints.is_empty() );
  assert!( job.execution_log.is_empty() );
}

#[ tokio::test ]
async fn test_tuning_job_status_update()
{
  let config = TuningJobConfig
  {
    job_name : "test_job".to_string(),
    base_model : "gpt-5-nano".to_string(),
    training_data : TrainingDataConfig
    {
      training_file : "train.jsonl".to_string(),
      validation_file : None,
      data_format : "jsonl".to_string(),
      max_sequence_length : 1024,
      preprocessing : HashMap::new(),
    },
    hyperparameters : HyperParameters::default(),
    method : FineTuningMethod::Full,
    objective : TrainingObjective::LanguageModeling,
    resource_requirements : TuningResourceRequirements::default(),
    checkpointing : CheckpointConfig::default(),
    env_vars : HashMap::new(),
  };

  let mut job = TuningJob::new( config );
  assert!( matches!( job.status, TuningStatus::Validating ) );

  job.update_status( TuningStatus::Running );
  assert!( matches!( job.status, TuningStatus::Running ) );
  assert_eq!( job.execution_log.len(), 1 );
}

#[ tokio::test ]
async fn test_tuning_job_metrics_update()
{
  let config = TuningJobConfig
  {
    job_name : "test_job".to_string(),
    base_model : "gpt-5-nano".to_string(),
    training_data : TrainingDataConfig
    {
      training_file : "train.jsonl".to_string(),
      validation_file : None,
      data_format : "jsonl".to_string(),
      max_sequence_length : 1024,
      preprocessing : HashMap::new(),
    },
    hyperparameters : HyperParameters::default(),
    method : FineTuningMethod::Full,
    objective : TrainingObjective::LanguageModeling,
    resource_requirements : TuningResourceRequirements::default(),
    checkpointing : CheckpointConfig::default(),
    env_vars : HashMap::new(),
  };

  let mut job = TuningJob::new( config );
  assert!( job.current_metrics.is_none() );

  let metrics = TrainingMetrics
  {
    step : 100,
    epoch : 1,
    training_loss : 0.5,
    validation_loss : None,
    learning_rate : 1e-4,
    throughput : 800.0,
    eta_seconds : Some( 7200 ),
    custom_metrics : HashMap::new(),
  };

  job.update_metrics( metrics );
  assert!( job.current_metrics.is_some() );
  assert_eq!( job.execution_log.len(), 1 );
}

#[ tokio::test ]
async fn test_tuning_job_checkpoint()
{
  let config = TuningJobConfig
  {
    job_name : "test_job".to_string(),
    base_model : "gpt-5-nano".to_string(),
    training_data : TrainingDataConfig
    {
      training_file : "train.jsonl".to_string(),
      validation_file : None,
      data_format : "jsonl".to_string(),
      max_sequence_length : 1024,
      preprocessing : HashMap::new(),
    },
    hyperparameters : HyperParameters::default(),
    method : FineTuningMethod::Full,
    objective : TrainingObjective::LanguageModeling,
    resource_requirements : TuningResourceRequirements::default(),
    checkpointing : CheckpointConfig::default(),
    env_vars : HashMap::new(),
  };

  let mut job = TuningJob::new( config );
  assert!( job.checkpoints.is_empty() );

  let checkpoint = ModelCheckpoint
  {
    checkpoint_id : "checkpoint_500".to_string(),
    step : 500,
    loss : 0.3,
    validation_metrics : HashMap::new(),
    created_at : SystemTime::now(),
    file_path : "./checkpoints/model_500.pt".to_string(),
  };

  job.add_checkpoint( checkpoint );
  assert_eq!( job.checkpoints.len(), 1 );
  assert_eq!( job.execution_log.len(), 1 );
}

#[ tokio::test ]
async fn test_tuning_job_duration()
{
  let config = TuningJobConfig
  {
    job_name : "test_job".to_string(),
    base_model : "gpt-5-nano".to_string(),
    training_data : TrainingDataConfig
    {
      training_file : "train.jsonl".to_string(),
      validation_file : None,
      data_format : "jsonl".to_string(),
      max_sequence_length : 1024,
      preprocessing : HashMap::new(),
    },
    hyperparameters : HyperParameters::default(),
    method : FineTuningMethod::Full,
    objective : TrainingObjective::LanguageModeling,
    resource_requirements : TuningResourceRequirements::default(),
    checkpointing : CheckpointConfig::default(),
    env_vars : HashMap::new(),
  };

  let job = TuningJob::new( config );
  let duration = job.duration();

  // Duration should be very small (just created)
  assert!( duration.as_secs() < 5 );
}

#[ tokio::test ]
async fn test_tuning_manager_creation()
{
  let manager = TuningManager::new();
  let stats = manager.tuning_stats();

  assert_eq!( stats.total, 0 );
  assert_eq!( stats.validating, 0 );
  assert_eq!( stats.queued, 0 );
  assert_eq!( stats.running, 0 );
  assert_eq!( stats.succeeded, 0 );
  assert_eq!( stats.failed, 0 );
  assert_eq!( stats.cancelled, 0 );
}

#[ tokio::test ]
async fn test_tuning_manager_create_job()
{
  let mut manager = TuningManager::new();

  let config = TuningJobConfig
  {
    job_name : "test_job".to_string(),
    base_model : "gpt-5-nano".to_string(),
    training_data : TrainingDataConfig
    {
      training_file : "train.jsonl".to_string(),
      validation_file : None,
      data_format : "jsonl".to_string(),
      max_sequence_length : 1024,
      preprocessing : HashMap::new(),
    },
    hyperparameters : HyperParameters::default(),
    method : FineTuningMethod::Full,
    objective : TrainingObjective::LanguageModeling,
    resource_requirements : TuningResourceRequirements::default(),
    checkpointing : CheckpointConfig::default(),
    env_vars : HashMap::new(),
  };

  let result = manager.create_job( config ).await;
  assert!( result.is_ok() );
  assert_eq!( result.unwrap(), "test_job" );

  let stats = manager.tuning_stats();
  assert_eq!( stats.total, 1 );
  assert_eq!( stats.validating, 1 );
}

#[ tokio::test ]
async fn test_tuning_manager_get_job()
{
  let mut manager = TuningManager::new();

  let config = TuningJobConfig
  {
    job_name : "test_job".to_string(),
    base_model : "gpt-5-nano".to_string(),
    training_data : TrainingDataConfig
    {
      training_file : "train.jsonl".to_string(),
      validation_file : None,
      data_format : "jsonl".to_string(),
      max_sequence_length : 1024,
      preprocessing : HashMap::new(),
    },
    hyperparameters : HyperParameters::default(),
    method : FineTuningMethod::Full,
    objective : TrainingObjective::LanguageModeling,
    resource_requirements : TuningResourceRequirements::default(),
    checkpointing : CheckpointConfig::default(),
    env_vars : HashMap::new(),
  };

  manager.create_job( config ).await.unwrap();
  let job = manager.get_job( "test_job" ).await;

  assert!( job.is_some() );
  assert_eq!( job.unwrap().config.job_name, "test_job" );
}

#[ tokio::test ]
async fn test_tuning_manager_list_jobs()
{
  let mut manager = TuningManager::new();

  let config1 = TuningJobConfig
  {
    job_name : "job1".to_string(),
    base_model : "gpt-5-nano".to_string(),
    training_data : TrainingDataConfig
    {
      training_file : "train1.jsonl".to_string(),
      validation_file : None,
      data_format : "jsonl".to_string(),
      max_sequence_length : 1024,
      preprocessing : HashMap::new(),
    },
    hyperparameters : HyperParameters::default(),
    method : FineTuningMethod::Full,
    objective : TrainingObjective::LanguageModeling,
    resource_requirements : TuningResourceRequirements::default(),
    checkpointing : CheckpointConfig::default(),
    env_vars : HashMap::new(),
  };

  let config2 = TuningJobConfig
  {
    job_name : "job2".to_string(),
    base_model : "gpt-4".to_string(),
    training_data : TrainingDataConfig
    {
      training_file : "train2.jsonl".to_string(),
      validation_file : None,
      data_format : "jsonl".to_string(),
      max_sequence_length : 2048,
      preprocessing : HashMap::new(),
    },
    hyperparameters : HyperParameters::default(),
    method : FineTuningMethod::LoRA
    {
      rank : 16,
      alpha : 32.0,
      dropout : 0.1,
    },
    objective : TrainingObjective::SupervisedFineTuning,
    resource_requirements : TuningResourceRequirements::default(),
    checkpointing : CheckpointConfig::default(),
    env_vars : HashMap::new(),
  };

  manager.create_job( config1 ).await.unwrap();
  manager.create_job( config2 ).await.unwrap();

  let jobs = manager.list_jobs().await;
  assert_eq!( jobs.len(), 2 );

  let stats = manager.tuning_stats();
  assert_eq!( stats.total, 2 );
  assert_eq!( stats.validating, 2 );
}

#[ tokio::test ]
async fn test_tuning_manager_update_status()
{
  let mut manager = TuningManager::new();

  let config = TuningJobConfig
  {
    job_name : "test_job".to_string(),
    base_model : "gpt-5-nano".to_string(),
    training_data : TrainingDataConfig
    {
      training_file : "train.jsonl".to_string(),
      validation_file : None,
      data_format : "jsonl".to_string(),
      max_sequence_length : 1024,
      preprocessing : HashMap::new(),
    },
    hyperparameters : HyperParameters::default(),
    method : FineTuningMethod::Full,
    objective : TrainingObjective::LanguageModeling,
    resource_requirements : TuningResourceRequirements::default(),
    checkpointing : CheckpointConfig::default(),
    env_vars : HashMap::new(),
  };

  manager.create_job( config ).await.unwrap();
  let result = manager.update_job_status( "test_job", TuningStatus::Running ).await;

  assert!( result.is_ok() );

  let job = manager.get_job( "test_job" ).await.unwrap();
  assert!( matches!( job.status, TuningStatus::Running ) );

  let stats = manager.tuning_stats();
  assert_eq!( stats.running, 1 );
  assert_eq!( stats.validating, 0 );
}

#[ tokio::test ]
async fn test_tuning_manager_cancel_job()
{
  let mut manager = TuningManager::new();

  let config = TuningJobConfig
  {
    job_name : "test_job".to_string(),
    base_model : "gpt-5-nano".to_string(),
    training_data : TrainingDataConfig
    {
      training_file : "train.jsonl".to_string(),
      validation_file : None,
      data_format : "jsonl".to_string(),
      max_sequence_length : 1024,
      preprocessing : HashMap::new(),
    },
    hyperparameters : HyperParameters::default(),
    method : FineTuningMethod::Full,
    objective : TrainingObjective::LanguageModeling,
    resource_requirements : TuningResourceRequirements::default(),
    checkpointing : CheckpointConfig::default(),
    env_vars : HashMap::new(),
  };

  manager.create_job( config ).await.unwrap();
  let result = manager.cancel_job( "test_job" ).await;

  assert!( result.is_ok() );

  let job = manager.get_job( "test_job" ).await.unwrap();
  assert!( matches!( job.status, TuningStatus::Cancelled ) );

  let stats = manager.tuning_stats();
  assert_eq!( stats.cancelled, 1 );
}

#[ tokio::test ]
async fn test_tuning_manager_delete_job()
{
  let mut manager = TuningManager::new();

  let config = TuningJobConfig
  {
    job_name : "test_job".to_string(),
    base_model : "gpt-5-nano".to_string(),
    training_data : TrainingDataConfig
    {
      training_file : "train.jsonl".to_string(),
      validation_file : None,
      data_format : "jsonl".to_string(),
      max_sequence_length : 1024,
      preprocessing : HashMap::new(),
    },
    hyperparameters : HyperParameters::default(),
    method : FineTuningMethod::Full,
    objective : TrainingObjective::LanguageModeling,
    resource_requirements : TuningResourceRequirements::default(),
    checkpointing : CheckpointConfig::default(),
    env_vars : HashMap::new(),
  };

  manager.create_job( config ).await.unwrap();
  let result = manager.delete_job( "test_job" ).await;

  assert!( result.is_ok() );

  let job = manager.get_job( "test_job" ).await;
  assert!( job.is_none() );

  let stats = manager.tuning_stats();
  assert_eq!( stats.total, 0 );
}

#[ tokio::test ]
async fn test_tuning_event_notifier()
{
  let ( _sender, _receiver ) = ModelTuningUtils::create_event_notifier();
  // Basic test to ensure the notifier can be created without errors
}

#[ tokio::test ]
async fn test_config_validation_success()
{
  let config = TuningJobConfig
  {
    job_name : "valid_job".to_string(),
    base_model : "gpt-5-nano".to_string(),
    training_data : TrainingDataConfig
    {
      training_file : "train.jsonl".to_string(),
      validation_file : None,
      data_format : "jsonl".to_string(),
      max_sequence_length : 1024,
      preprocessing : HashMap::new(),
    },
    hyperparameters : HyperParameters::default(),
    method : FineTuningMethod::Full,
    objective : TrainingObjective::LanguageModeling,
    resource_requirements : TuningResourceRequirements::default(),
    checkpointing : CheckpointConfig::default(),
    env_vars : HashMap::new(),
  };

  let result = ModelTuningUtils::validate_config( &config );
  assert!( result.is_ok() );
}

#[ tokio::test ]
async fn test_config_validation_failure()
{
  let config = TuningJobConfig
  {
    job_name : "".to_string(), // Invalid empty name
    base_model : "".to_string(), // Invalid empty model
    training_data : TrainingDataConfig
    {
      training_file : "".to_string(), // Invalid empty file
      validation_file : None,
      data_format : "jsonl".to_string(),
      max_sequence_length : 1024,
      preprocessing : HashMap::new(),
    },
    hyperparameters : HyperParameters
    {
      learning_rate : -0.1, // Invalid negative learning rate
      batch_size : 0, // Invalid zero batch size
      epochs : 0, // Invalid zero epochs
      warmup_steps : 100,
      weight_decay : 0.01,
      gradient_clip_norm : 1.0,
      lr_schedule : "linear".to_string(),
      custom_params : HashMap::new(),
    },
    method : FineTuningMethod::Full,
    objective : TrainingObjective::LanguageModeling,
    resource_requirements : TuningResourceRequirements::default(),
    checkpointing : CheckpointConfig::default(),
    env_vars : HashMap::new(),
  };

  let result = ModelTuningUtils::validate_config( &config );
  assert!( result.is_err() );

  let errors = result.unwrap_err();
  assert!( errors.len() >= 5 ); // Should have multiple validation errors
}

#[ tokio::test ]
async fn test_training_time_estimation()
{
  let config = TuningJobConfig
  {
    job_name : "estimation_test".to_string(),
    base_model : "gpt-5-nano".to_string(),
    training_data : TrainingDataConfig
    {
      training_file : "train.jsonl".to_string(),
      validation_file : None,
      data_format : "jsonl".to_string(),
      max_sequence_length : 1024,
      preprocessing : HashMap::new(),
    },
    hyperparameters : HyperParameters
    {
      epochs : 5,
      ..HyperParameters::default()
    },
    method : FineTuningMethod::Full,
    objective : TrainingObjective::LanguageModeling,
    resource_requirements : TuningResourceRequirements::default(),
    checkpointing : CheckpointConfig::default(),
    env_vars : HashMap::new(),
  };

  let dataset_size = 100_000;
  let estimated_time = ModelTuningUtils::estimate_training_time( &config, dataset_size );

  assert!( estimated_time.as_secs() > 0 );
  assert!( estimated_time.as_secs() >= 500 ); // Should be at least 500 seconds for this dataset
}

#[ tokio::test ]
async fn test_training_cost_estimation()
{
  let config = TuningJobConfig
  {
    job_name : "cost_test".to_string(),
    base_model : "gpt-5-nano".to_string(),
    training_data : TrainingDataConfig
    {
      training_file : "train.jsonl".to_string(),
      validation_file : None,
      data_format : "jsonl".to_string(),
      max_sequence_length : 1024,
      preprocessing : HashMap::new(),
    },
    hyperparameters : HyperParameters::default(),
    method : FineTuningMethod::Full,
    objective : TrainingObjective::LanguageModeling,
    resource_requirements : TuningResourceRequirements
    {
      gpu_count : 2,
      gpu_type : Some( "A100".to_string() ),
      ..TuningResourceRequirements::default()
    },
    checkpointing : CheckpointConfig::default(),
    env_vars : HashMap::new(),
  };

  let duration = Duration::from_secs( 3600 ); // 1 hour
  let estimated_cost = ModelTuningUtils::estimate_training_cost( &config, duration );

  assert!( estimated_cost > 0.0 );
  assert!( estimated_cost == 8.0 ); // 2 A100 GPUs * 1 hour * $4/hour = $8
}

#[ tokio::test ]
async fn test_hyperparameter_suggestions()
{
  let full_method = FineTuningMethod::Full;
  let dataset_size = 50000;
  let suggested = ModelTuningUtils::suggest_hyperparameters( &full_method, dataset_size );

  assert_eq!( suggested.learning_rate, 5e-5 );
  assert_eq!( suggested.batch_size, 32 );

  let lora_method = FineTuningMethod::LoRA { rank : 16, alpha : 32.0, dropout : 0.1 };
  let lora_suggested = ModelTuningUtils::suggest_hyperparameters( &lora_method, dataset_size );

  assert_eq!( lora_suggested.learning_rate, 1e-4 );
  assert_eq!( lora_suggested.batch_size, 64 );
  assert_eq!( lora_suggested.epochs, 5 );
}

#[ tokio::test ]
async fn test_serialization_roundtrip()
{
  let status = TuningStatus::Running;
  let serialized = serde_json::to_string( &status ).unwrap();
  let deserialized : TuningStatus = serde_json::from_str( &serialized ).unwrap();
  assert!( matches!( deserialized, TuningStatus::Running ) );

  let method = FineTuningMethod::LoRA { rank : 16, alpha : 32.0, dropout : 0.1 };
  let serialized = serde_json::to_string( &method ).unwrap();
  let deserialized : FineTuningMethod = serde_json::from_str( &serialized ).unwrap();
  assert!( matches!( deserialized, FineTuningMethod::LoRA { .. } ) );
}

#[ tokio::test ]
async fn test_tuning_stats()
{
  let mut manager = TuningManager::new();

  // Create jobs with different statuses
  for i in 0..3
  {
    let config = TuningJobConfig
    {
      job_name : format!( "job_{}", i ),
      base_model : "gpt-5-nano".to_string(),
      training_data : TrainingDataConfig
      {
        training_file : "train.jsonl".to_string(),
        validation_file : None,
        data_format : "jsonl".to_string(),
        max_sequence_length : 1024,
        preprocessing : HashMap::new(),
      },
      hyperparameters : HyperParameters::default(),
      method : FineTuningMethod::Full,
      objective : TrainingObjective::LanguageModeling,
      resource_requirements : TuningResourceRequirements::default(),
      checkpointing : CheckpointConfig::default(),
      env_vars : HashMap::new(),
    };

    manager.create_job( config ).await.unwrap();
  }

  // Update statuses
  manager.update_job_status( "job_1", TuningStatus::Running ).await.unwrap();
  manager.update_job_status( "job_2", TuningStatus::Succeeded ).await.unwrap();

  let stats = manager.tuning_stats();
  assert_eq!( stats.total, 3 );
  assert_eq!( stats.validating, 1 );
  assert_eq!( stats.running, 1 );
  assert_eq!( stats.succeeded, 1 );
}

#[ tokio::test ]
async fn test_module_exports_availability()
{
  // This test ensures all the main types are exported and accessible
  let _ = TuningStatus::Validating;
  let _ = TrainingObjective::LanguageModeling;
  let _ = FineTuningMethod::Full;
  let _ = HyperParameters::default();
  let _ = TuningResourceRequirements::default();
  let _ = CheckpointConfig::default();
  let _ = TuningManager::new();
  let ( _, _ ) = ModelTuningUtils::create_event_notifier();
}