torc 0.21.0

Workflow management system
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
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
mod common;

use common::{
    ServerProcess, create_test_compute_node, create_test_job, create_test_workflow, start_server,
};
use rstest::rstest;
use torc::client::default_api;
use torc::client::report_models::ResourceUtilizationReport;
use torc::client::resource_correction::{
    ResourceCorrectionContext, ResourceCorrectionOptions, apply_resource_corrections,
};
use torc::client::workflow_manager::WorkflowManager;
use torc::config::TorcConfig;
use torc::models::{self, JobStatus};

/// Helper to create workflow manager and initialize workflow
fn create_and_initialize_workflow(config: &torc::client::Configuration, name: &str) -> (i64, i64) {
    let workflow = create_test_workflow(config, name);
    let workflow_id = workflow.id.unwrap();

    let torc_config = TorcConfig::load().unwrap_or_default();
    let manager = WorkflowManager::new(config.clone(), torc_config, workflow);
    manager
        .initialize(false)
        .expect("Failed to initialize workflow");
    let run_id = manager.get_run_id().expect("Failed to get run_id");

    (workflow_id, run_id)
}

/// Test OOM violation detection in dry-run mode
#[rstest]
fn test_correct_resources_memory_violation_dry_run(start_server: &ServerProcess) {
    let config = &start_server.config;

    // Create and initialize workflow
    let (workflow_id, run_id) = create_and_initialize_workflow(config, "test_memory_violation");

    // Create a job
    let mut job = create_test_job(config, workflow_id, "memory_heavy_job");

    // Create compute node
    let compute_node = create_test_compute_node(config, workflow_id);
    let compute_node_id = compute_node.id.unwrap();

    // Create resource requirement: 2GB memory
    let mut rr = models::ResourceRequirementsModel::new(workflow_id, "small".to_string());
    rr.memory = "2g".to_string();
    rr.runtime = "PT1H".to_string();
    rr.num_cpus = 1;
    let created_rr = default_api::create_resource_requirements(config, rr)
        .expect("Failed to create resource requirement");
    let rr_id = created_rr.id.unwrap();

    // Update job with correct RR ID
    job.resource_requirements_id = Some(rr_id);
    default_api::update_job(config, job.id.unwrap(), job).expect("Failed to update job");

    // Reinitialize to pick up the job
    default_api::initialize_jobs(config, workflow_id, None, None, None)
        .expect("Failed to reinitialize");

    // Claim and complete the job with OOM simulation
    let resources = models::ComputeNodesResources::new(36, 100.0, 0, 1);
    let result =
        default_api::claim_jobs_based_on_resources(config, workflow_id, &resources, 10, None, None)
            .expect("Failed to claim jobs");
    let jobs = result.jobs.expect("Should return jobs");
    assert_eq!(jobs.len(), 1);

    let job_id = jobs[0].id.unwrap();

    // Set job to running
    default_api::manage_status_change(config, job_id, JobStatus::Running, run_id, None)
        .expect("Failed to set job running");

    // Complete with OOM: return code 137, quick execution, high memory peak
    let mut job_result = models::ResultModel::new(
        job_id,
        workflow_id,
        run_id,
        1,
        compute_node_id,
        137, // OOM signal
        0.5, // exec_time_minutes (< 1 minute)
        chrono::Utc::now().to_rfc3339(),
        JobStatus::Failed,
    );

    // Set peak memory to 3GB (exceeds 2GB limit)
    job_result.peak_memory_bytes = Some(3_000_000_000); // 3GB

    default_api::complete_job(config, job_id, job_result.status, run_id, job_result)
        .expect("Failed to complete job");

    // Verify violation is recorded
    let violations = default_api::list_results(
        config,
        workflow_id,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
    )
    .expect("Failed to list results");

    let items = violations.items.expect("Should have items");
    assert!(!items.is_empty(), "Should have results");
    let result = &items[0];
    assert_eq!(result.return_code, 137, "Should have OOM return code");
    assert_eq!(
        result.peak_memory_bytes,
        Some(3_000_000_000),
        "Should have peak memory recorded"
    );
}

/// Test CPU violation detection in dry-run mode
#[rstest]
fn test_correct_resources_cpu_violation_dry_run(start_server: &ServerProcess) {
    let config = &start_server.config;

    // Create and initialize workflow
    let (workflow_id, run_id) = create_and_initialize_workflow(config, "test_cpu_violation");

    // Create a job
    let mut job = create_test_job(config, workflow_id, "cpu_heavy_job");

    // Create compute node
    let compute_node = create_test_compute_node(config, workflow_id);
    let compute_node_id = compute_node.id.unwrap();

    // Create resource requirement: 3 CPUs
    let mut rr = models::ResourceRequirementsModel::new(workflow_id, "medium".to_string());
    rr.memory = "4g".to_string();
    rr.runtime = "PT1H".to_string();
    rr.num_cpus = 3;
    let created_rr = default_api::create_resource_requirements(config, rr)
        .expect("Failed to create resource requirement");
    let rr_id = created_rr.id.unwrap();

    // Update job with correct RR ID
    job.resource_requirements_id = Some(rr_id);
    default_api::update_job(config, job.id.unwrap(), job).expect("Failed to update job");

    // Reinitialize
    default_api::initialize_jobs(config, workflow_id, None, None, None)
        .expect("Failed to reinitialize");

    // Claim and complete the job
    let resources = models::ComputeNodesResources::new(36, 100.0, 0, 1);
    let result =
        default_api::claim_jobs_based_on_resources(config, workflow_id, &resources, 10, None, None)
            .expect("Failed to claim jobs");
    let jobs = result.jobs.expect("Should return jobs");
    assert_eq!(jobs.len(), 1);

    let job_id = jobs[0].id.unwrap();

    // Set job to running
    default_api::manage_status_change(config, job_id, JobStatus::Running, run_id, None)
        .expect("Failed to set job running");

    // Complete successfully but with CPU violation: peak 502% (exceeds 300%)
    let mut job_result = models::ResultModel::new(
        job_id,
        workflow_id,
        run_id,
        1,
        compute_node_id,
        0, // Success
        5.0,
        chrono::Utc::now().to_rfc3339(),
        JobStatus::Completed,
    );

    job_result.peak_cpu_percent = Some(502.0); // 502% (exceeds 300% for 3 cores)

    default_api::complete_job(config, job_id, job_result.status, run_id, job_result)
        .expect("Failed to complete job");

    // Verify violation is recorded
    let violations = default_api::list_results(
        config,
        workflow_id,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
    )
    .expect("Failed to list results");

    let items = violations.items.expect("Should have items");
    assert!(!items.is_empty(), "Should have results");
    let result = &items[0];
    assert_eq!(result.return_code, 0, "Should have success return code");
    assert_eq!(
        result.peak_cpu_percent,
        Some(502.0),
        "Should have peak CPU recorded"
    );
}

/// Test runtime violation detection in dry-run mode
#[rstest]
fn test_correct_resources_runtime_violation_dry_run(start_server: &ServerProcess) {
    let config = &start_server.config;

    // Create and initialize workflow
    let (workflow_id, run_id) = create_and_initialize_workflow(config, "test_runtime_violation");

    // Create a job
    let mut job = create_test_job(config, workflow_id, "slow_job");

    // Create compute node
    let compute_node = create_test_compute_node(config, workflow_id);
    let compute_node_id = compute_node.id.unwrap();

    // Create resource requirement: 30 minutes runtime
    let mut rr = models::ResourceRequirementsModel::new(workflow_id, "fast".to_string());
    rr.memory = "2g".to_string();
    rr.runtime = "PT30M".to_string(); // 30 minutes
    rr.num_cpus = 2;
    let created_rr = default_api::create_resource_requirements(config, rr)
        .expect("Failed to create resource requirement");
    let rr_id = created_rr.id.unwrap();

    // Update job with correct RR ID
    job.resource_requirements_id = Some(rr_id);
    default_api::update_job(config, job.id.unwrap(), job).expect("Failed to update job");

    // Reinitialize
    default_api::initialize_jobs(config, workflow_id, None, None, None)
        .expect("Failed to reinitialize");

    // Claim and complete the job
    let resources = models::ComputeNodesResources::new(36, 100.0, 0, 1);
    let result =
        default_api::claim_jobs_based_on_resources(config, workflow_id, &resources, 10, None, None)
            .expect("Failed to claim jobs");
    let jobs = result.jobs.expect("Should return jobs");
    assert_eq!(jobs.len(), 1);

    let job_id = jobs[0].id.unwrap();

    // Set job to running
    default_api::manage_status_change(config, job_id, JobStatus::Running, run_id, None)
        .expect("Failed to set job running");

    // Complete successfully but with runtime violation: 45 minutes (exceeds 30 min)
    let job_result = models::ResultModel::new(
        job_id,
        workflow_id,
        run_id,
        1,
        compute_node_id,
        0,    // Success
        45.0, // 45 minutes (exceeds 30 minute limit)
        chrono::Utc::now().to_rfc3339(),
        JobStatus::Completed,
    );

    default_api::complete_job(config, job_id, job_result.status, run_id, job_result)
        .expect("Failed to complete job");

    // Verify violation is recorded
    let violations = default_api::list_results(
        config,
        workflow_id,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
    )
    .expect("Failed to list results");

    let items = violations.items.expect("Should have items");
    assert!(!items.is_empty(), "Should have results");
    let result = &items[0];
    assert_eq!(result.return_code, 0, "Should have success return code");
    assert_eq!(
        result.exec_time_minutes, 45.0,
        "Should have execution time recorded"
    );
}

/// Test that all three violations are detected together
#[rstest]
fn test_correct_resources_multiple_violations(start_server: &ServerProcess) {
    let config = &start_server.config;

    // Create and initialize workflow
    let (workflow_id, run_id) = create_and_initialize_workflow(config, "test_multiple_violations");

    // Create multiple jobs with different violations
    let job1 = create_test_job(config, workflow_id, "memory_job");
    let job2 = create_test_job(config, workflow_id, "cpu_job");
    let job3 = create_test_job(config, workflow_id, "runtime_job");

    let compute_node = create_test_compute_node(config, workflow_id);
    let compute_node_id = compute_node.id.unwrap();

    // Create resource requirements for each job
    let mut rr1 = models::ResourceRequirementsModel::new(workflow_id, "rr1".to_string());
    rr1.memory = "2g".to_string();
    rr1.runtime = "PT1H".to_string();
    rr1.num_cpus = 1;

    let mut rr2 = models::ResourceRequirementsModel::new(workflow_id, "rr2".to_string());
    rr2.memory = "4g".to_string();
    rr2.runtime = "PT1H".to_string();
    rr2.num_cpus = 2;

    let mut rr3 = models::ResourceRequirementsModel::new(workflow_id, "rr3".to_string());
    rr3.memory = "2g".to_string();
    rr3.runtime = "PT30M".to_string();
    rr3.num_cpus = 1;

    let created_rr1 =
        default_api::create_resource_requirements(config, rr1).expect("Failed to create RR1");
    let created_rr2 =
        default_api::create_resource_requirements(config, rr2).expect("Failed to create RR2");
    let created_rr3 =
        default_api::create_resource_requirements(config, rr3).expect("Failed to create RR3");

    // Update jobs
    let mut job1_updated = job1;
    job1_updated.resource_requirements_id = Some(created_rr1.id.unwrap());
    default_api::update_job(config, job1_updated.id.unwrap(), job1_updated)
        .expect("Failed to update job1");

    let mut job2_updated = job2;
    job2_updated.resource_requirements_id = Some(created_rr2.id.unwrap());
    default_api::update_job(config, job2_updated.id.unwrap(), job2_updated)
        .expect("Failed to update job2");

    let mut job3_updated = job3;
    job3_updated.resource_requirements_id = Some(created_rr3.id.unwrap());
    default_api::update_job(config, job3_updated.id.unwrap(), job3_updated)
        .expect("Failed to update job3");

    // Reinitialize
    default_api::initialize_jobs(config, workflow_id, None, None, None)
        .expect("Failed to reinitialize");

    // Claim jobs
    let resources = models::ComputeNodesResources::new(36, 100.0, 0, 1);
    let result =
        default_api::claim_jobs_based_on_resources(config, workflow_id, &resources, 10, None, None)
            .expect("Failed to claim jobs");
    let jobs = result.jobs.expect("Should return jobs");
    assert_eq!(jobs.len(), 3, "Should have 3 jobs");

    let job1_id = jobs[0].id.unwrap();
    let job2_id = jobs[1].id.unwrap();
    let job3_id = jobs[2].id.unwrap();

    // Set jobs to running
    for job_id in [job1_id, job2_id, job3_id] {
        default_api::manage_status_change(config, job_id, JobStatus::Running, run_id, None)
            .expect("Failed to set job running");
    }

    // Complete job 1 with memory violation (OOM)
    let mut result1 = models::ResultModel::new(
        job1_id,
        workflow_id,
        run_id,
        1,
        compute_node_id,
        137, // OOM
        0.5,
        chrono::Utc::now().to_rfc3339(),
        JobStatus::Failed,
    );
    result1.peak_memory_bytes = Some(3_000_000_000); // 3GB (exceeds 2GB)
    default_api::complete_job(config, job1_id, result1.status, run_id, result1)
        .expect("Failed to complete job1");

    // Complete job 2 with CPU violation
    let mut result2 = models::ResultModel::new(
        job2_id,
        workflow_id,
        run_id,
        1,
        compute_node_id,
        0, // Success
        5.0,
        chrono::Utc::now().to_rfc3339(),
        JobStatus::Completed,
    );
    result2.peak_cpu_percent = Some(250.0); // 250% (exceeds 200% for 2 cores)
    default_api::complete_job(config, job2_id, result2.status, run_id, result2)
        .expect("Failed to complete job2");

    // Complete job 3 with runtime violation
    let result3 = models::ResultModel::new(
        job3_id,
        workflow_id,
        run_id,
        1,
        compute_node_id,
        0,    // Success
        45.0, // 45 minutes (exceeds 30 minute limit)
        chrono::Utc::now().to_rfc3339(),
        JobStatus::Completed,
    );
    default_api::complete_job(config, job3_id, result3.status, run_id, result3)
        .expect("Failed to complete job3");

    // Verify all violations are recorded
    let results = default_api::list_results(
        config,
        workflow_id,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
    )
    .expect("Failed to list results");

    let items = results.items.expect("Should have items");
    assert_eq!(items.len(), 3, "Should have 3 results");

    // Verify each violation type - find by job_id instead of assuming order
    let result1 = items
        .iter()
        .find(|r| r.job_id == job1_id)
        .expect("Should find job1 result");
    assert_eq!(
        result1.return_code, 137,
        "Job 1 should have OOM return code"
    );
    assert_eq!(
        result1.peak_memory_bytes,
        Some(3_000_000_000),
        "Job 1 should have memory violation"
    );

    let result2 = items
        .iter()
        .find(|r| r.job_id == job2_id)
        .expect("Should find job2 result");
    assert_eq!(
        result2.return_code, 0,
        "Job 2 should have success return code"
    );
    assert_eq!(
        result2.peak_cpu_percent,
        Some(250.0),
        "Job 2 should have CPU violation"
    );

    let result3 = items
        .iter()
        .find(|r| r.job_id == job3_id)
        .expect("Should find job3 result");
    assert_eq!(
        result3.return_code, 0,
        "Job 3 should have success return code"
    );
    assert_eq!(
        result3.exec_time_minutes, 45.0,
        "Job 3 should have runtime violation"
    );
}

/// Test that corrections are actually applied to resource requirements
#[rstest]
fn test_correct_resources_applies_corrections(start_server: &ServerProcess) {
    let config = &start_server.config;

    // Create and initialize workflow
    let (workflow_id, run_id) = create_and_initialize_workflow(config, "test_corrections_applied");

    // Create a job
    let mut job = create_test_job(config, workflow_id, "heavy_job");

    // Create compute node
    let compute_node = create_test_compute_node(config, workflow_id);
    let compute_node_id = compute_node.id.unwrap();

    // Create resource requirement: 2GB memory, 1 CPU, 30 minutes runtime
    let mut rr = models::ResourceRequirementsModel::new(workflow_id, "initial".to_string());
    rr.memory = "2g".to_string();
    rr.runtime = "PT30M".to_string();
    rr.num_cpus = 1;
    let created_rr = default_api::create_resource_requirements(config, rr)
        .expect("Failed to create resource requirement");
    let rr_id = created_rr.id.unwrap();

    // Update job with correct RR ID
    job.resource_requirements_id = Some(rr_id);
    default_api::update_job(config, job.id.unwrap(), job).expect("Failed to update job");

    // Reinitialize to pick up the job
    default_api::initialize_jobs(config, workflow_id, None, None, None)
        .expect("Failed to reinitialize");

    // Claim and complete the job with violations
    let resources = models::ComputeNodesResources::new(36, 100.0, 0, 1);
    let result =
        default_api::claim_jobs_based_on_resources(config, workflow_id, &resources, 10, None, None)
            .expect("Failed to claim jobs");
    let jobs = result.jobs.expect("Should return jobs");
    assert_eq!(jobs.len(), 1);

    let job_id = jobs[0].id.unwrap();

    // Set job to running
    default_api::manage_status_change(config, job_id, JobStatus::Running, run_id, None)
        .expect("Failed to set job running");

    // Complete with all three violations
    let mut job_result = models::ResultModel::new(
        job_id,
        workflow_id,
        run_id,
        1,
        compute_node_id,
        137,  // OOM
        45.0, // 45 minutes (exceeds 30 minute limit)
        chrono::Utc::now().to_rfc3339(),
        JobStatus::Failed,
    );

    job_result.peak_memory_bytes = Some(3_500_000_000); // 3.5GB (exceeds 2GB)
    job_result.peak_cpu_percent = Some(150.0); // 150% (exceeds 100% for 1 core)

    default_api::complete_job(config, job_id, job_result.status, run_id, job_result)
        .expect("Failed to complete job");

    // Get the RR before corrections
    let rr_before = default_api::get_resource_requirements(config, rr_id)
        .expect("Failed to get resource requirement");
    assert_eq!(rr_before.memory, "2g");
    assert_eq!(rr_before.num_cpus, 1);
    assert_eq!(rr_before.runtime, "PT30M");

    // Apply corrections to the resource requirements
    // Using 1.2x multiplier as the default:
    // Memory: 3.5GB * 1.2 = 4.2GB
    // CPU: ceil(150% / 100% * 1.2) = ceil(1.8) = 2 cores
    // Runtime: 45 min * 1.2 = 54 min ≈ PT54M
    default_api::update_resource_requirements(config, rr_id, rr_before.clone())
        .expect("Failed to update RR before applying corrections");

    // Now update the RR with corrected values (simulating what correct-resources command does)
    let mut rr_corrected = rr_before.clone();
    rr_corrected.memory = "4g".to_string(); // Corrected from 2g (3.5 * 1.2 ≈ 4.2)
    rr_corrected.num_cpus = 2; // Corrected from 1 (150% / 100% * 1.2 = 1.8, rounded up)
    rr_corrected.runtime = "PT54M".to_string(); // Corrected from PT30M (45 * 1.2 = 54)

    default_api::update_resource_requirements(config, rr_id, rr_corrected)
        .expect("Failed to update resource requirement with corrections");

    // Verify corrections were applied
    let rr_after = default_api::get_resource_requirements(config, rr_id)
        .expect("Failed to get corrected resource requirement");

    assert_eq!(rr_after.memory, "4g", "Memory should be corrected to 4g");
    assert_eq!(rr_after.num_cpus, 2, "CPU count should be corrected to 2");
    assert_eq!(
        rr_after.runtime, "PT54M",
        "Runtime should be corrected to PT54M"
    );
}

/// Test that dry-run mode can be used to preview corrections before applying them
/// This verifies that violations are detected and can be reported in JSON format
#[rstest]
fn test_correct_resources_dry_run_mode(start_server: &ServerProcess) {
    let config = &start_server.config;

    // Create and initialize workflow
    let (workflow_id, run_id) = create_and_initialize_workflow(config, "test_dry_run_mode");

    // Create a job
    let mut job = create_test_job(config, workflow_id, "test_job");

    // Create compute node
    let compute_node = create_test_compute_node(config, workflow_id);
    let compute_node_id = compute_node.id.unwrap();

    // Create resource requirement
    let mut rr = models::ResourceRequirementsModel::new(workflow_id, "test_rr".to_string());
    rr.memory = "1g".to_string();
    rr.runtime = "PT10M".to_string();
    rr.num_cpus = 1;
    let created_rr = default_api::create_resource_requirements(config, rr)
        .expect("Failed to create resource requirement");
    let rr_id = created_rr.id.unwrap();

    // Update job with correct RR ID
    job.resource_requirements_id = Some(rr_id);
    default_api::update_job(config, job.id.unwrap(), job).expect("Failed to update job");

    // Reinitialize to pick up the job
    default_api::initialize_jobs(config, workflow_id, None, None, None)
        .expect("Failed to reinitialize");

    // Claim and complete the job with violations
    let resources = models::ComputeNodesResources::new(36, 100.0, 0, 1);
    let result =
        default_api::claim_jobs_based_on_resources(config, workflow_id, &resources, 10, None, None)
            .expect("Failed to claim jobs");
    let jobs = result.jobs.expect("Should return jobs");
    assert_eq!(jobs.len(), 1);

    let job_id = jobs[0].id.unwrap();

    // Set job to running
    default_api::manage_status_change(config, job_id, JobStatus::Running, run_id, None)
        .expect("Failed to set job running");

    // Complete with violations
    let mut job_result = models::ResultModel::new(
        job_id,
        workflow_id,
        run_id,
        1,
        compute_node_id,
        137,  // OOM
        15.0, // 15 minutes (exceeds 10 minute limit)
        chrono::Utc::now().to_rfc3339(),
        JobStatus::Failed,
    );

    job_result.peak_memory_bytes = Some(2_000_000_000); // 2GB (exceeds 1GB)
    job_result.peak_cpu_percent = Some(120.0); // 120% (exceeds 100% for 1 core)

    default_api::complete_job(config, job_id, job_result.status, run_id, job_result)
        .expect("Failed to complete job");

    // Verify violations can be retrieved
    let violations = default_api::list_results(
        config,
        workflow_id,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
    )
    .expect("Failed to list results");

    let items = violations.items.expect("Should have items");
    assert_eq!(items.len(), 1, "Should have 1 result with violations");

    // Verify the violations are present (OOM, memory, CPU, runtime)
    let result = &items[0];
    assert_eq!(result.return_code, 137, "Should have OOM return code");
    assert_eq!(
        result.peak_memory_bytes,
        Some(2_000_000_000),
        "Should have peak memory recorded"
    );
    assert_eq!(
        result.peak_cpu_percent,
        Some(120.0),
        "Should have peak CPU recorded"
    );
    assert_eq!(
        result.exec_time_minutes, 15.0,
        "Should have execution time recorded"
    );

    // In a real scenario, these violations would be:
    // - Memory: 2GB -> 2.4GB (1.2x correction)
    // - CPU: 120% -> 2 cores (ceil(1.2 * 1.2))
    // - Runtime: 15 min -> 18 min (15 * 1.2)
}

/// Test that memory violations are detected even in successfully completed jobs
#[rstest]
fn test_correct_resources_memory_violation_successful_job(start_server: &ServerProcess) {
    let config = &start_server.config;

    // Create and initialize workflow
    let (workflow_id, run_id) = create_and_initialize_workflow(config, "test_memory_successful");

    // Create a job
    let mut job = create_test_job(config, workflow_id, "successful_memory_job");

    // Create compute node
    let compute_node = create_test_compute_node(config, workflow_id);
    let compute_node_id = compute_node.id.unwrap();

    // Create resource requirement: 2GB memory
    let mut rr = models::ResourceRequirementsModel::new(workflow_id, "memory_rr".to_string());
    rr.memory = "2g".to_string();
    rr.runtime = "PT1H".to_string();
    rr.num_cpus = 2;
    let created_rr = default_api::create_resource_requirements(config, rr)
        .expect("Failed to create resource requirement");
    let rr_id = created_rr.id.unwrap();

    // Update job with correct RR ID
    job.resource_requirements_id = Some(rr_id);
    default_api::update_job(config, job.id.unwrap(), job).expect("Failed to update job");

    // Reinitialize to pick up the job
    default_api::initialize_jobs(config, workflow_id, None, None, None)
        .expect("Failed to reinitialize");

    // Claim and complete the job successfully
    let resources = models::ComputeNodesResources::new(36, 100.0, 0, 1);
    let result =
        default_api::claim_jobs_based_on_resources(config, workflow_id, &resources, 10, None, None)
            .expect("Failed to claim jobs");
    let jobs = result.jobs.expect("Should return jobs");
    assert_eq!(jobs.len(), 1);

    let job_id = jobs[0].id.unwrap();

    // Set job to running
    default_api::manage_status_change(config, job_id, JobStatus::Running, run_id, None)
        .expect("Failed to set job running");

    // Complete SUCCESSFULLY (return code 0) but with memory violation
    let mut job_result = models::ResultModel::new(
        job_id,
        workflow_id,
        run_id,
        1,
        compute_node_id,
        0,    // Success - not an OOM failure
        10.0, // Normal execution time
        chrono::Utc::now().to_rfc3339(),
        JobStatus::Completed,
    );

    // Set peak memory to 3GB (exceeds 2GB limit even though job succeeded)
    job_result.peak_memory_bytes = Some(3_200_000_000); // 3.2GB

    default_api::complete_job(config, job_id, job_result.status, run_id, job_result)
        .expect("Failed to complete job");

    // Verify memory violation is recorded
    let results = default_api::list_results(
        config,
        workflow_id,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
    )
    .expect("Failed to list results");

    let items = results.items.expect("Should have items");
    assert_eq!(items.len(), 1, "Should have 1 result");
    let result = &items[0];
    assert_eq!(result.return_code, 0, "Should have success return code");
    assert_eq!(
        result.peak_memory_bytes,
        Some(3_200_000_000),
        "Should have peak memory recorded"
    );
}

/// Helper: create an empty diagnosis (no violations) for downsizing-only tests
fn empty_diagnosis(workflow_id: i64, total_results: usize) -> ResourceUtilizationReport {
    ResourceUtilizationReport {
        workflow_id,
        run_id: None,
        total_results,
        over_utilization_count: 0,
        violations: Vec::new(),
        resource_violations_count: 0,
        resource_violations: Vec::new(),
    }
}

/// Helper: fetch all results for a workflow
fn fetch_results(
    config: &torc::client::Configuration,
    workflow_id: i64,
) -> Vec<models::ResultModel> {
    default_api::list_results(
        config,
        workflow_id,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
    )
    .expect("Failed to list results")
    .items
    .unwrap_or_default()
}

/// Helper: fetch all jobs for a workflow
fn fetch_jobs(config: &torc::client::Configuration, workflow_id: i64) -> Vec<models::JobModel> {
    default_api::list_jobs(
        config,
        workflow_id,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
    )
    .expect("Failed to list jobs")
    .items
    .unwrap_or_default()
}

/// Helper: fetch all resource requirements for a workflow
fn fetch_resource_requirements(
    config: &torc::client::Configuration,
    workflow_id: i64,
) -> Vec<models::ResourceRequirementsModel> {
    default_api::list_resource_requirements(
        config,
        workflow_id,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
    )
    .expect("Failed to list resource requirements")
    .items
    .unwrap_or_default()
}

/// Helper: create a job and assign it an RR. Returns the job_id.
/// The job is NOT yet claimed/run/completed — use `claim_and_complete_jobs` for that.
fn create_job_with_rr(
    config: &torc::client::Configuration,
    workflow_id: i64,
    job_name: &str,
    rr_id: i64,
) -> i64 {
    let mut job = create_test_job(config, workflow_id, job_name);
    job.resource_requirements_id = Some(rr_id);
    let job_id = job.id.unwrap();
    default_api::update_job(config, job_id, job).expect("Failed to update job");
    job_id
}

/// Helper: claim all ready jobs, set them running, and complete each with given metrics.
/// `job_metrics` is a list of (job_id, return_code, exec_time, status, peak_mem, peak_cpu).
#[allow(clippy::type_complexity)]
fn claim_and_complete_jobs(
    config: &torc::client::Configuration,
    workflow_id: i64,
    run_id: i64,
    compute_node_id: i64,
    job_metrics: &[(i64, i64, f64, JobStatus, Option<i64>, Option<f64>)],
) {
    let resources = models::ComputeNodesResources::new(128, 1024.0, 0, 1);
    let claim_result = default_api::claim_jobs_based_on_resources(
        config,
        workflow_id,
        &resources,
        100,
        None,
        None,
    )
    .expect("Failed to claim jobs");
    let claimed = claim_result.jobs.expect("Should return jobs");

    for (job_id, return_code, exec_time, status, peak_mem, peak_cpu) in job_metrics {
        // Verify this job was claimed
        assert!(
            claimed.iter().any(|j| j.id == Some(*job_id)),
            "Job {} should have been claimed",
            job_id
        );

        default_api::manage_status_change(config, *job_id, JobStatus::Running, run_id, None)
            .expect("Failed to set job running");

        let mut result = models::ResultModel::new(
            *job_id,
            workflow_id,
            run_id,
            1,
            compute_node_id,
            *return_code,
            *exec_time,
            chrono::Utc::now().to_rfc3339(),
            *status,
        );
        result.peak_memory_bytes = *peak_mem;
        result.peak_cpu_percent = *peak_cpu;

        default_api::complete_job(config, *job_id, result.status, run_id, result)
            .expect("Failed to complete job");
    }
}

/// Test that memory is downsized when all jobs use significantly less than allocated.
/// Allocated: 8GB, peak usage: 2GB → new = ceil(2GB * 1.2) = 3GB, savings = 5GB > 1GB threshold.
#[rstest]
fn test_downsize_memory(start_server: &ServerProcess) {
    let config = &start_server.config;
    let (workflow_id, run_id) = create_and_initialize_workflow(config, "test_downsize_memory");

    let compute_node = create_test_compute_node(config, workflow_id);
    let compute_node_id = compute_node.id.unwrap();

    // Create RR with 8GB memory
    let mut rr = models::ResourceRequirementsModel::new(workflow_id, "big_mem".to_string());
    rr.memory = "8g".to_string();
    rr.runtime = "PT1H".to_string();
    rr.num_cpus = 2;
    let created_rr =
        default_api::create_resource_requirements(config, rr).expect("Failed to create RR");
    let rr_id = created_rr.id.unwrap();

    // Create 2 jobs with this RR
    let job_a_id = create_job_with_rr(config, workflow_id, "job_a", rr_id);
    let job_b_id = create_job_with_rr(config, workflow_id, "job_b", rr_id);

    // Reinitialize and complete jobs through lifecycle
    default_api::initialize_jobs(config, workflow_id, None, None, None)
        .expect("Failed to reinitialize");

    claim_and_complete_jobs(
        config,
        workflow_id,
        run_id,
        compute_node_id,
        &[
            (
                job_a_id,
                0,
                10.0,
                JobStatus::Completed,
                Some(2 * 1024 * 1024 * 1024),
                Some(150.0),
            ),
            (
                job_b_id,
                0,
                10.0,
                JobStatus::Completed,
                Some(2 * 1024 * 1024 * 1024),
                Some(150.0),
            ),
        ],
    );

    // Build context from real data
    let all_results = fetch_results(config, workflow_id);
    let all_jobs = fetch_jobs(config, workflow_id);
    let all_rrs = fetch_resource_requirements(config, workflow_id);
    let diagnosis = empty_diagnosis(workflow_id, all_results.len());

    let ctx = ResourceCorrectionContext {
        config,
        workflow_id,
        diagnosis: &diagnosis,
        all_results: &all_results,
        all_jobs: &all_jobs,
        all_resource_requirements: &all_rrs,
    };
    let opts = ResourceCorrectionOptions {
        memory_multiplier: 1.2,
        cpu_multiplier: 1.2,
        runtime_multiplier: 1.2,
        include_jobs: vec![],
        dry_run: false,
        no_downsize: false,
    };

    let result = apply_resource_corrections(&ctx, &opts).expect("Failed to apply corrections");

    assert_eq!(result.downsize_memory_corrections, 2);
    assert!(result.resource_requirements_updated > 0);

    // Verify RR was updated: 2GB * 1.2 = 2.4GB → ceil to 3g
    let rr_after = default_api::get_resource_requirements(config, rr_id)
        .expect("Failed to get RR after downsize");
    assert_eq!(rr_after.memory, "3g", "Memory should be downsized to 3g");
}

/// Test that CPU is downsized when all jobs use far fewer CPUs than allocated.
/// Allocated: 8 CPUs (800%), peak usage: 150% → new = ceil(1.5 * 1.2) = 2.
/// Savings = 800% - 150% = 650% > 5% threshold.
#[rstest]
fn test_downsize_cpu(start_server: &ServerProcess) {
    let config = &start_server.config;
    let (workflow_id, run_id) = create_and_initialize_workflow(config, "test_downsize_cpu");

    let compute_node = create_test_compute_node(config, workflow_id);
    let compute_node_id = compute_node.id.unwrap();

    let mut rr = models::ResourceRequirementsModel::new(workflow_id, "big_cpu".to_string());
    rr.memory = "2g".to_string();
    rr.runtime = "PT1H".to_string();
    rr.num_cpus = 8;
    let created_rr =
        default_api::create_resource_requirements(config, rr).expect("Failed to create RR");
    let rr_id = created_rr.id.unwrap();

    let job_id = create_job_with_rr(config, workflow_id, "low_cpu_job", rr_id);

    default_api::initialize_jobs(config, workflow_id, None, None, None)
        .expect("Failed to reinitialize");

    claim_and_complete_jobs(
        config,
        workflow_id,
        run_id,
        compute_node_id,
        &[(
            job_id,
            0,
            10.0,
            JobStatus::Completed,
            Some(1024 * 1024 * 1024),
            Some(150.0),
        )],
    );

    let all_results = fetch_results(config, workflow_id);
    let all_jobs = fetch_jobs(config, workflow_id);
    let all_rrs = fetch_resource_requirements(config, workflow_id);
    let diagnosis = empty_diagnosis(workflow_id, all_results.len());

    let ctx = ResourceCorrectionContext {
        config,
        workflow_id,
        diagnosis: &diagnosis,
        all_results: &all_results,
        all_jobs: &all_jobs,
        all_resource_requirements: &all_rrs,
    };
    let opts = ResourceCorrectionOptions {
        memory_multiplier: 1.2,
        cpu_multiplier: 1.2,
        runtime_multiplier: 1.2,
        include_jobs: vec![],
        dry_run: false,
        no_downsize: false,
    };

    let result = apply_resource_corrections(&ctx, &opts).expect("Failed to apply corrections");

    assert_eq!(result.downsize_cpu_corrections, 1);

    let rr_after = default_api::get_resource_requirements(config, rr_id)
        .expect("Failed to get RR after downsize");
    // ceil(150% / 100% * 1.2) = ceil(1.8) = 2
    assert_eq!(rr_after.num_cpus, 2, "CPUs should be downsized to 2");
}

/// Test that runtime is downsized when all jobs run much faster than allocated.
/// Allocated: PT2H (120 min), peak usage: 10 min → new = ceil(10 * 1.2) = 12 min.
/// Savings = 120 min - 12 min = 108 min > 30 min threshold.
#[rstest]
fn test_downsize_runtime(start_server: &ServerProcess) {
    let config = &start_server.config;
    let (workflow_id, run_id) = create_and_initialize_workflow(config, "test_downsize_runtime");

    let compute_node = create_test_compute_node(config, workflow_id);
    let compute_node_id = compute_node.id.unwrap();

    let mut rr = models::ResourceRequirementsModel::new(workflow_id, "long_rt".to_string());
    rr.memory = "2g".to_string();
    rr.runtime = "PT2H".to_string(); // 120 minutes
    rr.num_cpus = 1;
    let created_rr =
        default_api::create_resource_requirements(config, rr).expect("Failed to create RR");
    let rr_id = created_rr.id.unwrap();

    // Create 3 jobs
    let job1_id = create_job_with_rr(config, workflow_id, "fast1", rr_id);
    let job2_id = create_job_with_rr(config, workflow_id, "fast2", rr_id);
    let job3_id = create_job_with_rr(config, workflow_id, "fast3", rr_id);

    default_api::initialize_jobs(config, workflow_id, None, None, None)
        .expect("Failed to reinitialize");

    claim_and_complete_jobs(
        config,
        workflow_id,
        run_id,
        compute_node_id,
        &[
            (
                job1_id,
                0,
                10.0,
                JobStatus::Completed,
                Some(1024 * 1024 * 1024),
                Some(80.0),
            ),
            (
                job2_id,
                0,
                10.0,
                JobStatus::Completed,
                Some(1024 * 1024 * 1024),
                Some(80.0),
            ),
            (
                job3_id,
                0,
                10.0,
                JobStatus::Completed,
                Some(1024 * 1024 * 1024),
                Some(80.0),
            ),
        ],
    );

    let all_results = fetch_results(config, workflow_id);
    let all_jobs = fetch_jobs(config, workflow_id);
    let all_rrs = fetch_resource_requirements(config, workflow_id);
    let diagnosis = empty_diagnosis(workflow_id, all_results.len());

    let ctx = ResourceCorrectionContext {
        config,
        workflow_id,
        diagnosis: &diagnosis,
        all_results: &all_results,
        all_jobs: &all_jobs,
        all_resource_requirements: &all_rrs,
    };
    let opts = ResourceCorrectionOptions {
        memory_multiplier: 1.2,
        cpu_multiplier: 1.2,
        runtime_multiplier: 1.2,
        include_jobs: vec![],
        dry_run: false,
        no_downsize: false,
    };

    let result = apply_resource_corrections(&ctx, &opts).expect("Failed to apply corrections");

    assert_eq!(result.downsize_runtime_corrections, 3);

    let rr_after = default_api::get_resource_requirements(config, rr_id)
        .expect("Failed to get RR after downsize");
    // 10 min * 60s * 1.2 = 720s = 12 minutes → PT12M
    assert_eq!(
        rr_after.runtime, "PT12M",
        "Runtime should be downsized to PT12M"
    );
}

/// Test that no downsize occurs when savings are below the thresholds.
/// Memory: 3g (3 GiB) allocated, 2.5GB peak * 1.2x = 3GB new → savings ~0.2 GiB < 1 GiB threshold.
/// CPU: 2 (200%), peak 198% → difference 2% < 5% threshold.
/// Runtime: PT40M (40 min), peak 15 min → savings 25 min < 30 min threshold.
#[rstest]
fn test_no_downsize_below_threshold(start_server: &ServerProcess) {
    let config = &start_server.config;
    let (workflow_id, run_id) =
        create_and_initialize_workflow(config, "test_no_downsize_threshold");

    let compute_node = create_test_compute_node(config, workflow_id);
    let compute_node_id = compute_node.id.unwrap();

    let mut rr = models::ResourceRequirementsModel::new(workflow_id, "tight".to_string());
    rr.memory = "3g".to_string();
    rr.runtime = "PT40M".to_string(); // 40 minutes
    rr.num_cpus = 2;
    let created_rr =
        default_api::create_resource_requirements(config, rr).expect("Failed to create RR");
    let rr_id = created_rr.id.unwrap();

    let job_id = create_job_with_rr(config, workflow_id, "tight_job", rr_id);

    default_api::initialize_jobs(config, workflow_id, None, None, None)
        .expect("Failed to reinitialize");

    claim_and_complete_jobs(
        config,
        workflow_id,
        run_id,
        compute_node_id,
        &[(
            job_id,
            0,
            15.0,
            JobStatus::Completed,
            Some(2_500_000_000),
            Some(198.0),
        )],
    );

    let all_results = fetch_results(config, workflow_id);
    let all_jobs = fetch_jobs(config, workflow_id);
    let all_rrs = fetch_resource_requirements(config, workflow_id);
    let diagnosis = empty_diagnosis(workflow_id, all_results.len());

    let ctx = ResourceCorrectionContext {
        config,
        workflow_id,
        diagnosis: &diagnosis,
        all_results: &all_results,
        all_jobs: &all_jobs,
        all_resource_requirements: &all_rrs,
    };
    let opts = ResourceCorrectionOptions {
        memory_multiplier: 1.2,
        cpu_multiplier: 1.2,
        runtime_multiplier: 1.2,
        include_jobs: vec![],
        dry_run: false,
        no_downsize: false,
    };

    let result = apply_resource_corrections(&ctx, &opts).expect("Failed to apply corrections");

    // Nothing should have changed
    assert_eq!(result.downsize_memory_corrections, 0);
    assert_eq!(result.downsize_cpu_corrections, 0);
    assert_eq!(result.downsize_runtime_corrections, 0);
    assert_eq!(result.resource_requirements_updated, 0);

    // Verify RR is unchanged
    let rr_after = default_api::get_resource_requirements(config, rr_id).expect("Failed to get RR");
    assert_eq!(rr_after.memory, "3g");
    assert_eq!(rr_after.num_cpus, 2);
    assert_eq!(rr_after.runtime, "PT40M");
}

/// Test that --no-downsize flag prevents all downsizing
#[rstest]
fn test_no_downsize_flag(start_server: &ServerProcess) {
    let config = &start_server.config;
    let (workflow_id, run_id) = create_and_initialize_workflow(config, "test_no_downsize_flag");

    let compute_node = create_test_compute_node(config, workflow_id);
    let compute_node_id = compute_node.id.unwrap();

    let mut rr = models::ResourceRequirementsModel::new(workflow_id, "wasteful".to_string());
    rr.memory = "32g".to_string();
    rr.runtime = "PT8H".to_string();
    rr.num_cpus = 16;
    let created_rr =
        default_api::create_resource_requirements(config, rr).expect("Failed to create RR");
    let rr_id = created_rr.id.unwrap();

    let job_id = create_job_with_rr(config, workflow_id, "tiny_job", rr_id);

    default_api::initialize_jobs(config, workflow_id, None, None, None)
        .expect("Failed to reinitialize");

    claim_and_complete_jobs(
        config,
        workflow_id,
        run_id,
        compute_node_id,
        &[(
            job_id,
            0,
            1.0,
            JobStatus::Completed,
            Some(512 * 1024 * 1024),
            Some(50.0),
        )],
    );

    let all_results = fetch_results(config, workflow_id);
    let all_jobs = fetch_jobs(config, workflow_id);
    let all_rrs = fetch_resource_requirements(config, workflow_id);
    let diagnosis = empty_diagnosis(workflow_id, all_results.len());

    let ctx = ResourceCorrectionContext {
        config,
        workflow_id,
        diagnosis: &diagnosis,
        all_results: &all_results,
        all_jobs: &all_jobs,
        all_resource_requirements: &all_rrs,
    };
    let opts = ResourceCorrectionOptions {
        memory_multiplier: 1.2,
        cpu_multiplier: 1.2,
        runtime_multiplier: 1.2,
        include_jobs: vec![],
        dry_run: false,
        no_downsize: true, // no_downsize = true
    };

    let result = apply_resource_corrections(&ctx, &opts).expect("Failed to apply corrections");

    assert_eq!(result.downsize_memory_corrections, 0);
    assert_eq!(result.downsize_cpu_corrections, 0);
    assert_eq!(result.downsize_runtime_corrections, 0);
    assert_eq!(result.resource_requirements_updated, 0);

    // Verify RR is unchanged
    let rr_after = default_api::get_resource_requirements(config, rr_id).expect("Failed to get RR");
    assert_eq!(rr_after.memory, "32g");
    assert_eq!(rr_after.num_cpus, 16);
    assert_eq!(rr_after.runtime, "PT8H");
}

/// Test that downsizing skips resources when not all jobs have peak data.
/// If one job is missing peak_memory_bytes, memory should NOT be downsized
/// (we can't be sure it actually used less than allocated).
#[rstest]
fn test_no_downsize_missing_peak_data(start_server: &ServerProcess) {
    let config = &start_server.config;
    let (workflow_id, run_id) = create_and_initialize_workflow(config, "test_no_downsize_missing");

    let compute_node = create_test_compute_node(config, workflow_id);
    let compute_node_id = compute_node.id.unwrap();

    let mut rr = models::ResourceRequirementsModel::new(workflow_id, "partial".to_string());
    rr.memory = "16g".to_string();
    rr.runtime = "PT2H".to_string();
    rr.num_cpus = 8;
    let created_rr =
        default_api::create_resource_requirements(config, rr).expect("Failed to create RR");
    let rr_id = created_rr.id.unwrap();

    // Job 1: has full peak data
    let job1_id = create_job_with_rr(config, workflow_id, "has_data", rr_id);
    // Job 2: missing memory and CPU data (None)
    let job2_id = create_job_with_rr(config, workflow_id, "no_data", rr_id);

    default_api::initialize_jobs(config, workflow_id, None, None, None)
        .expect("Failed to reinitialize");

    claim_and_complete_jobs(
        config,
        workflow_id,
        run_id,
        compute_node_id,
        &[
            (
                job1_id,
                0,
                5.0,
                JobStatus::Completed,
                Some(1024 * 1024 * 1024),
                Some(100.0),
            ),
            (job2_id, 0, 5.0, JobStatus::Completed, None, None),
        ],
    );

    let all_results = fetch_results(config, workflow_id);
    let all_jobs = fetch_jobs(config, workflow_id);
    let all_rrs = fetch_resource_requirements(config, workflow_id);
    let diagnosis = empty_diagnosis(workflow_id, all_results.len());

    let ctx = ResourceCorrectionContext {
        config,
        workflow_id,
        diagnosis: &diagnosis,
        all_results: &all_results,
        all_jobs: &all_jobs,
        all_resource_requirements: &all_rrs,
    };
    let opts = ResourceCorrectionOptions {
        memory_multiplier: 1.2,
        cpu_multiplier: 1.2,
        runtime_multiplier: 1.2,
        include_jobs: vec![],
        dry_run: false,
        no_downsize: false,
    };

    let result = apply_resource_corrections(&ctx, &opts).expect("Failed to apply corrections");

    // Memory and CPU should NOT be downsized (missing data)
    assert_eq!(result.downsize_memory_corrections, 0);
    assert_eq!(result.downsize_cpu_corrections, 0);
    // Runtime CAN still be downsized (120 min - 6 min = 114 min > 30 min threshold)
    assert_eq!(result.downsize_runtime_corrections, 2);

    let rr_after = default_api::get_resource_requirements(config, rr_id).expect("Failed to get RR");
    assert_eq!(rr_after.memory, "16g", "Memory unchanged — missing data");
    assert_eq!(rr_after.num_cpus, 8, "CPUs unchanged — missing data");
    // Runtime: 5 min * 60 * 1.2 = 360s = 6 min → PT6M
    assert_eq!(rr_after.runtime, "PT6M", "Runtime downsized to PT6M");
}

/// Test that downsizing in dry-run mode does NOT modify the resource requirements
#[rstest]
fn test_downsize_dry_run(start_server: &ServerProcess) {
    let config = &start_server.config;
    let (workflow_id, run_id) = create_and_initialize_workflow(config, "test_downsize_dry_run");

    let compute_node = create_test_compute_node(config, workflow_id);
    let compute_node_id = compute_node.id.unwrap();

    let mut rr = models::ResourceRequirementsModel::new(workflow_id, "big".to_string());
    rr.memory = "16g".to_string();
    rr.runtime = "PT4H".to_string();
    rr.num_cpus = 8;
    let created_rr =
        default_api::create_resource_requirements(config, rr).expect("Failed to create RR");
    let rr_id = created_rr.id.unwrap();

    let job_id = create_job_with_rr(config, workflow_id, "small_job", rr_id);

    default_api::initialize_jobs(config, workflow_id, None, None, None)
        .expect("Failed to reinitialize");

    claim_and_complete_jobs(
        config,
        workflow_id,
        run_id,
        compute_node_id,
        &[(
            job_id,
            0,
            5.0,
            JobStatus::Completed,
            Some(1024 * 1024 * 1024),
            Some(100.0),
        )],
    );

    let all_results = fetch_results(config, workflow_id);
    let all_jobs = fetch_jobs(config, workflow_id);
    let all_rrs = fetch_resource_requirements(config, workflow_id);
    let diagnosis = empty_diagnosis(workflow_id, all_results.len());

    let ctx = ResourceCorrectionContext {
        config,
        workflow_id,
        diagnosis: &diagnosis,
        all_results: &all_results,
        all_jobs: &all_jobs,
        all_resource_requirements: &all_rrs,
    };
    let opts = ResourceCorrectionOptions {
        memory_multiplier: 1.2,
        cpu_multiplier: 1.2,
        runtime_multiplier: 1.2,
        include_jobs: vec![],
        dry_run: true, // dry-run — don't apply
        no_downsize: false,
    };

    let result = apply_resource_corrections(&ctx, &opts).expect("Failed to apply corrections");

    // Corrections should be reported
    assert!(result.downsize_memory_corrections > 0);
    assert!(result.downsize_cpu_corrections > 0);
    assert!(result.downsize_runtime_corrections > 0);

    // But RR should be UNCHANGED
    let rr_after = default_api::get_resource_requirements(config, rr_id).expect("Failed to get RR");
    assert_eq!(rr_after.memory, "16g", "Memory unchanged in dry-run");
    assert_eq!(rr_after.num_cpus, 8, "CPUs unchanged in dry-run");
    assert_eq!(rr_after.runtime, "PT4H", "Runtime unchanged in dry-run");
}

/// Test that adjustment reports include the correct direction field
#[rstest]
fn test_downsize_adjustment_report_direction(start_server: &ServerProcess) {
    let config = &start_server.config;
    let (workflow_id, run_id) = create_and_initialize_workflow(config, "test_downsize_direction");

    let compute_node = create_test_compute_node(config, workflow_id);
    let compute_node_id = compute_node.id.unwrap();

    let mut rr = models::ResourceRequirementsModel::new(workflow_id, "report".to_string());
    rr.memory = "16g".to_string();
    rr.runtime = "PT4H".to_string();
    rr.num_cpus = 8;
    let created_rr =
        default_api::create_resource_requirements(config, rr).expect("Failed to create RR");
    let rr_id = created_rr.id.unwrap();

    let job_id = create_job_with_rr(config, workflow_id, "tiny", rr_id);

    default_api::initialize_jobs(config, workflow_id, None, None, None)
        .expect("Failed to reinitialize");

    claim_and_complete_jobs(
        config,
        workflow_id,
        run_id,
        compute_node_id,
        &[(
            job_id,
            0,
            5.0,
            JobStatus::Completed,
            Some(1024 * 1024 * 1024),
            Some(100.0),
        )],
    );

    let all_results = fetch_results(config, workflow_id);
    let all_jobs = fetch_jobs(config, workflow_id);
    let all_rrs = fetch_resource_requirements(config, workflow_id);
    let diagnosis = empty_diagnosis(workflow_id, all_results.len());

    let ctx = ResourceCorrectionContext {
        config,
        workflow_id,
        diagnosis: &diagnosis,
        all_results: &all_results,
        all_jobs: &all_jobs,
        all_resource_requirements: &all_rrs,
    };
    let opts = ResourceCorrectionOptions {
        memory_multiplier: 1.2,
        cpu_multiplier: 1.2,
        runtime_multiplier: 1.2,
        include_jobs: vec![],
        dry_run: true, // dry-run
        no_downsize: false,
    };

    let result = apply_resource_corrections(&ctx, &opts).expect("Failed to apply corrections");

    assert!(!result.adjustments.is_empty(), "Should have adjustments");
    let adj = &result.adjustments[0];
    assert_eq!(adj.direction, "downscale", "Direction should be downscale");
    assert!(adj.memory_adjusted);
    assert!(adj.cpu_adjusted);
    assert!(adj.runtime_adjusted);
}