alien-commands 2.0.1

Alien Commands protocol implementation
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
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
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
//! Integration tests for alien-commands
//!
//! These tests focus on the 8 core command scenarios:
//! (PUSH, PULL) × (SMALL PARAMS, LARGE PARAMS) × (SMALL RESPONSE, LARGE RESPONSE)
//!
//! 1. (Push, Small Params, Small Response): inline params auto-dispatched, inline response
//! 2. (Push, Small Params, Large Response): inline params auto-dispatched, storage response
//! 3. (Push, Large Params, Small Response): storage params auto-dispatched after upload, inline response
//! 4. (Push, Large Params, Large Response): storage params auto-dispatched after upload, storage response
//! 5. (Pull, Small Params, Small Response): inline params acquired via lease, inline response
//! 6. (Pull, Small Params, Large Response): inline params acquired via lease, storage response
//! 7. (Pull, Large Params, Small Response): storage params acquired via lease after upload, inline response
//! 8. (Pull, Large Params, Large Response): storage params acquired via lease after upload, storage response
//!
//! Additional component tests verify basic API functionality and runtime integration.

#[cfg(feature = "test-utils")]
mod tests {
    use std::time::Duration;

    use alien_commands::{
        runtime::{decode_params, parse_envelope},
        test_utils::{
            dispatcher::MockDispatcherAssertions, server::TestCommandServerAssertions, *,
        },
        types::*,
    };
    use alien_core::{MessagePayload, QueueMessage};
    use chrono::Utc;

    // ===========================================
    // CORE SCENARIOS: (PUSH, PULL) × (SMALL PARAMS, LARGE PARAMS) × (SMALL RESPONSE, LARGE RESPONSE)
    // ===========================================

    /// Core Scenario 1: Push + Small Params + Small Response
    /// Inline params auto-dispatched, inline response
    #[tokio::test]
    async fn test_core_push_small_params_small_response() {
        let server = TestCommandServer::new().await;

        // 1. Client creates small inline command (auto-dispatched immediately)
        let request = test_inline_create_command("push-agent", "generate-report");
        let response = server.create_command(request).await.unwrap();
        assert_eq!(response.state, CommandState::Dispatched); // Auto-dispatched
        assert!(response.storage_upload.is_none());

        // 2. Verify envelope was dispatched to mock dispatcher (simulates push to agent)
        let mock_dispatcher = server
            .mock_dispatcher()
            .expect("Should have mock dispatcher");
        mock_dispatcher.assert_has_dispatched().await;
        let dispatched = mock_dispatcher.get_latest().await.unwrap();
        assert_eq!(dispatched.envelope.command_id, response.command_id);
        assert_eq!(dispatched.envelope.command, "generate-report");
        assert!(matches!(
            dispatched.envelope.params,
            BodySpec::Inline { .. }
        ));

        // 3. Simulate agent receiving envelope and processing
        let params = decode_params(&dispatched.envelope).await.unwrap();
        assert!(params.is_object()); // Should have JSON params

        // 4. Agent submits response
        let agent_response = test_success_response(b"report generated");
        server
            .submit_command_response(&response.command_id, agent_response)
            .await
            .unwrap();

        // 5. Client polls for completion
        let final_status = server
            .wait_for_completion(&response.command_id, Duration::from_secs(5))
            .await
            .unwrap();
        assert_eq!(final_status.state, CommandState::Succeeded);

        let final_response = final_status.response.unwrap();
        assert!(final_response.is_success());
        if let CommandResponse::Success { response: body } = final_response {
            assert_inline_body(&body, b"report generated");
        }
    }

    /// Core Scenario 5: Pull + Small Params + Small Response
    /// Inline params acquired via lease, inline response
    #[tokio::test]
    async fn test_core_pull_small_params_small_response() {
        let server = TestCommandServer::builder().with_pull_mode().build().await;

        // 1. Client creates command (in Pull mode, stays Pending until lease)
        let request = test_inline_create_command("pull-agent", "sync-data");
        let create_response = server.create_command(request).await.unwrap();
        assert_eq!(create_response.state, CommandState::Pending); // Pending until lease

        // 2. Agent polls for lease (moves to Dispatched)
        let lease = server
            .acquire_single_lease("pull-agent")
            .await
            .unwrap()
            .unwrap();
        assert_eq!(lease.command_id, create_response.command_id);
        assert!(matches!(lease.envelope.params, BodySpec::Inline { .. }));

        // Verify state moved to Dispatched after lease
        let status = server
            .get_command_status(&create_response.command_id)
            .await
            .unwrap();
        assert_eq!(status.state, CommandState::Dispatched);

        // 3. Agent processes envelope (simulated)
        let params = decode_params(&lease.envelope).await.unwrap();
        assert!(params.is_object()); // Should have JSON params

        // 4. Agent submits response
        let agent_response = test_json_success_response(&serde_json::json!({
            "status": "synced",
            "command_id": lease.command_id
        }));
        server
            .submit_command_response(&lease.command_id, agent_response)
            .await
            .unwrap();

        // 5. Client checks completion
        let final_status = server
            .wait_for_completion(&create_response.command_id, Duration::from_secs(5))
            .await
            .unwrap();
        assert_eq!(final_status.state, CommandState::Succeeded);

        let response = final_status.response.unwrap();
        assert!(response.is_success());
        if let CommandResponse::Success { response: body } = response {
            let body_data = body.decode_inline().unwrap();
            let json: serde_json::Value = serde_json::from_slice(&body_data).unwrap();
            assert_eq!(json["status"], "synced");
        }
    }

    /// Core Scenario 4: Push + Large Params + Large Response
    /// Storage params auto-dispatched after upload, storage response
    #[tokio::test]
    async fn test_core_push_large_params_large_response() {
        let server = TestCommandServer::new().await; // Use default 150KB limit

        // 1. Client creates large command
        let large_params = vec![b'X'; 160000]; // 160KB > 150KB inline limit
        let request = test_storage_create_command("push-agent", "process-bulk", large_params.len());
        let response = server.create_command(request).await.unwrap();
        assert_eq!(response.state, CommandState::PendingUpload);
        assert!(response.storage_upload.is_some());

        // 2. Client uploads large params using the presigned URL mechanism
        let storage_upload = response.storage_upload.unwrap();
        storage_upload
            .put_request
            .execute(Some(large_params.clone().into()))
            .await
            .unwrap();

        let upload_complete = test_upload_complete_request(160000);
        server
            .upload_complete(&response.command_id, upload_complete)
            .await
            .unwrap();

        // 3. Command should be auto-dispatched after upload
        let mock_dispatcher = server
            .mock_dispatcher()
            .expect("Should have mock dispatcher");
        assert!(mock_dispatcher.has_dispatched().await);
        let dispatched = mock_dispatcher.get_latest().await.unwrap();
        assert_eq!(dispatched.envelope.command_id, response.command_id);
        assert!(matches!(
            dispatched.envelope.params,
            BodySpec::Storage { .. }
        ));

        // 4. Agent submits large response (> 150KB to force storage)
        let large_response_data = vec![b'R'; 160000]; // 160KB > 150KB inline limit

        // Agent uses the presigned upload request from the envelope
        dispatched
            .envelope
            .response_handling
            .storage_upload_request
            .execute(Some(large_response_data.clone().into()))
            .await
            .unwrap();

        let agent_response = CommandResponse::success_storage(large_response_data.len() as u64);
        server
            .submit_command_response(&response.command_id, agent_response)
            .await
            .unwrap();

        // 5. Client gets final result with large storage response
        let final_status = server
            .wait_for_completion(&response.command_id, Duration::from_secs(5))
            .await
            .unwrap();
        assert_eq!(final_status.state, CommandState::Succeeded);

        let final_response = final_status.response.unwrap();
        assert!(final_response.is_success());
        if let CommandResponse::Success { response: body } = final_response {
            assert_storage_body(&body, Some(160000));
            // Verify we can download and the content matches what the agent uploaded
            assert_storage_body_content(&body, &large_response_data).await;
        }

        // Should have storage objects from both large params and response
        assert!(server.storage_object_count().await > 1);
    }

    /// Core Scenario 8: Pull + Large Params + Large Response
    /// Storage params acquired via lease after upload, storage response
    #[tokio::test]
    async fn test_core_pull_large_params_large_response() {
        let server = TestCommandServer::builder().with_pull_mode().build().await;

        // 1. Client creates large command
        let large_params = vec![b'Y'; 160000]; // 160KB > 150KB inline limit
        let request = test_storage_create_command("pull-agent", "bulk-process", large_params.len());
        let response = server.create_command(request).await.unwrap();
        assert_eq!(response.state, CommandState::PendingUpload);
        assert!(response.storage_upload.is_some());

        // 2. Client uploads large params using the presigned URL mechanism
        let storage_upload = response.storage_upload.unwrap();
        storage_upload
            .put_request
            .execute(Some(large_params.clone().into()))
            .await
            .unwrap();

        let upload_complete = test_upload_complete_request(160000);
        let upload_response = server
            .upload_complete(&response.command_id, upload_complete)
            .await
            .unwrap();
        // In Pull mode, after upload the state is Pending (waiting for lease)
        assert_eq!(upload_response.state, CommandState::Pending);

        // 3. Agent polls for lease (moves to Dispatched)
        let lease = server
            .acquire_single_lease("pull-agent")
            .await
            .unwrap()
            .unwrap();
        assert_eq!(lease.command_id, response.command_id);
        assert!(matches!(lease.envelope.params, BodySpec::Storage { .. }));

        // Verify state moved to Dispatched after lease
        let status = server
            .get_command_status(&response.command_id)
            .await
            .unwrap();
        assert_eq!(status.state, CommandState::Dispatched);

        // 4. Agent simulates processing large params
        let params_bytes = alien_commands::runtime::decode_params_bytes(&lease.envelope)
            .await
            .unwrap();
        assert_eq!(params_bytes.len(), 160000); // Should have reconstructed the large params
        assert_eq!(params_bytes, vec![b'Y'; 160000]); // Should match original data

        // 5. Agent submits large response (upload to storage since > 150KB)
        let large_response_data = vec![b'Z'; 160000]; // 160KB > 150KB inline limit

        // Agent uses the presigned upload request from the envelope
        lease
            .envelope
            .response_handling
            .storage_upload_request
            .execute(Some(large_response_data.clone().into()))
            .await
            .unwrap();

        let agent_response = CommandResponse::success_storage(large_response_data.len() as u64);
        server
            .submit_command_response(&lease.command_id, agent_response)
            .await
            .unwrap();

        // 6. Verify completion with storage response
        let final_status = server
            .wait_for_completion(&response.command_id, Duration::from_secs(5))
            .await
            .unwrap();
        assert_eq!(final_status.state, CommandState::Succeeded);

        let final_response = final_status.response.unwrap();
        assert!(final_response.is_success());
        if let CommandResponse::Success { response: body } = final_response {
            assert_storage_body(&body, Some(160000));
            // Verify we can download and the content matches what the agent uploaded
            assert_storage_body_content(&body, &large_response_data).await;
        }

        // Should have storage objects from the large payloads
        assert!(server.storage_object_count().await > 0);
    }

    /// Core Scenario 2: Push + Small Params + Large Response
    /// Inline params auto-dispatched, storage response
    #[tokio::test]
    async fn test_core_push_small_params_large_response() {
        let server = TestCommandServer::new().await; // Use default 150KB limit

        // 1. Client creates small inline command (auto-dispatched immediately)
        let request = test_inline_create_command("push-agent", "generate-large-report");
        let response = server.create_command(request).await.unwrap();
        assert_eq!(response.state, CommandState::Dispatched); // Auto-dispatched
        assert!(response.storage_upload.is_none());

        // 2. Verify envelope was dispatched to mock dispatcher (simulates push to agent)
        let mock_dispatcher = server
            .mock_dispatcher()
            .expect("Should have mock dispatcher");
        mock_dispatcher.assert_has_dispatched().await;
        let dispatched = mock_dispatcher.get_latest().await.unwrap();
        assert_eq!(dispatched.envelope.command_id, response.command_id);
        assert!(matches!(
            dispatched.envelope.params,
            BodySpec::Inline { .. }
        ));

        // 3. Agent submits large response (> 150KB to force storage)
        let large_response_data = vec![b'L'; 160000]; // 160KB > 150KB inline limit

        // Agent uses the presigned upload request from the envelope
        dispatched
            .envelope
            .response_handling
            .storage_upload_request
            .execute(Some(large_response_data.clone().into()))
            .await
            .unwrap();

        let agent_response = CommandResponse::success_storage(large_response_data.len() as u64);
        server
            .submit_command_response(&response.command_id, agent_response)
            .await
            .unwrap();

        // 4. Client gets final result with large storage response
        let final_status = server
            .wait_for_completion(&response.command_id, Duration::from_secs(5))
            .await
            .unwrap();
        assert_eq!(final_status.state, CommandState::Succeeded);

        let final_response = final_status.response.unwrap();
        assert!(final_response.is_success());
        if let CommandResponse::Success { response: body } = final_response {
            assert_storage_body(&body, Some(160000));
            // Verify we can download and the content matches what we uploaded
            assert_storage_body_content(&body, &large_response_data).await;
        }

        // Should have storage object from large response
        assert!(server.storage_object_count().await > 0);
    }

    /// Core Scenario 3: Push + Large Params + Small Response
    /// Storage params auto-dispatched after upload, inline response
    #[tokio::test]
    async fn test_core_push_large_params_small_response() {
        let server = TestCommandServer::new().await; // Use default 150KB limit

        // 1. Client creates large command
        let large_params = vec![b'X'; 160000]; // 160KB > 150KB inline limit
        let request = test_storage_create_command("push-agent", "process-data", large_params.len());
        let response = server.create_command(request).await.unwrap();
        assert_eq!(response.state, CommandState::PendingUpload);
        assert!(response.storage_upload.is_some());

        // 2. Client uploads large params using the presigned URL mechanism
        let storage_upload = response.storage_upload.unwrap();
        storage_upload
            .put_request
            .execute(Some(large_params.clone().into()))
            .await
            .unwrap();

        let upload_complete = test_upload_complete_request(160000);
        server
            .upload_complete(&response.command_id, upload_complete)
            .await
            .unwrap();

        // 3. Command should be auto-dispatched after upload
        let mock_dispatcher = server
            .mock_dispatcher()
            .expect("Should have mock dispatcher");
        assert!(mock_dispatcher.has_dispatched().await);
        let dispatched = mock_dispatcher.get_latest().await.unwrap();
        assert_eq!(dispatched.envelope.command_id, response.command_id);
        assert!(matches!(
            dispatched.envelope.params,
            BodySpec::Storage { .. }
        ));

        // 4. Agent submits small inline response
        let agent_response = test_success_response(b"ok");
        server
            .submit_command_response(&response.command_id, agent_response)
            .await
            .unwrap();

        // 5. Client gets final result with inline response
        let final_status = server
            .wait_for_completion(&response.command_id, Duration::from_secs(5))
            .await
            .unwrap();
        assert_eq!(final_status.state, CommandState::Succeeded);

        let final_response = final_status.response.unwrap();
        assert!(final_response.is_success());
        if let CommandResponse::Success { response: body } = final_response {
            assert_inline_body(&body, b"ok");
        }

        // Should have storage object from large params but not response
        assert!(server.storage_object_count().await > 0);
    }

    /// Core Scenario 6: Pull + Small Params + Large Response
    /// Inline params acquired via lease, storage response
    #[tokio::test]
    async fn test_core_pull_small_params_large_response() {
        let server = TestCommandServer::builder().with_pull_mode().build().await;

        // 1. Client creates command (in Pull mode, stays Pending until lease)
        let request = test_inline_create_command("pull-agent", "generate-large");
        let create_response = server.create_command(request).await.unwrap();
        assert_eq!(create_response.state, CommandState::Pending); // Pending until lease

        // 2. Agent polls for lease (moves to Dispatched)
        let lease = server
            .acquire_single_lease("pull-agent")
            .await
            .unwrap()
            .unwrap();
        assert_eq!(lease.command_id, create_response.command_id);
        assert!(matches!(lease.envelope.params, BodySpec::Inline { .. }));

        // Verify state moved to Dispatched after lease
        let status = server
            .get_command_status(&create_response.command_id)
            .await
            .unwrap();
        assert_eq!(status.state, CommandState::Dispatched);

        // 3. Agent processes envelope (simulated)
        let params = decode_params(&lease.envelope).await.unwrap();
        assert!(params.is_object()); // Should have JSON params

        // 4. Agent submits large response (> 150KB to force storage)
        let large_response_data = vec![b'M'; 160000]; // 160KB > 150KB inline limit

        // Agent uses the presigned upload request from the envelope
        lease
            .envelope
            .response_handling
            .storage_upload_request
            .execute(Some(large_response_data.clone().into()))
            .await
            .unwrap();

        let agent_response = CommandResponse::success_storage(large_response_data.len() as u64);
        server
            .submit_command_response(&lease.command_id, agent_response)
            .await
            .unwrap();

        // 5. Client checks completion with large storage response
        let final_status = server
            .wait_for_completion(&create_response.command_id, Duration::from_secs(5))
            .await
            .unwrap();
        assert_eq!(final_status.state, CommandState::Succeeded);

        let response = final_status.response.unwrap();
        assert!(response.is_success());
        if let CommandResponse::Success { response: body } = response {
            assert_storage_body(&body, Some(160000));
            // Verify we can download and the content matches what the agent uploaded
            assert_storage_body_content(&body, &large_response_data).await;
        }

        // Should have storage object from large response
        assert!(server.storage_object_count().await > 0);
    }

    /// Core Scenario 7: Pull + Large Params + Small Response
    /// Storage params acquired via lease after upload, inline response
    #[tokio::test]
    async fn test_core_pull_large_params_small_response() {
        let server = TestCommandServer::builder().with_pull_mode().build().await;

        // 1. Client creates large command
        let large_params = vec![b'Y'; 160000]; // 160KB > 150KB inline limit
        let request = test_storage_create_command("pull-agent", "process-bulk", large_params.len());
        let response = server.create_command(request).await.unwrap();
        assert_eq!(response.state, CommandState::PendingUpload);
        assert!(response.storage_upload.is_some());

        // 2. Client uploads large params using the presigned URL mechanism
        let storage_upload = response.storage_upload.unwrap();
        storage_upload
            .put_request
            .execute(Some(large_params.clone().into()))
            .await
            .unwrap();

        let upload_complete = test_upload_complete_request(160000);
        let upload_response = server
            .upload_complete(&response.command_id, upload_complete)
            .await
            .unwrap();
        // In Pull mode, after upload the state is Pending (waiting for lease)
        assert_eq!(upload_response.state, CommandState::Pending);

        // 3. Agent polls for lease (moves to Dispatched)
        let lease = server
            .acquire_single_lease("pull-agent")
            .await
            .unwrap()
            .unwrap();
        assert_eq!(lease.command_id, response.command_id);
        assert!(matches!(lease.envelope.params, BodySpec::Storage { .. }));

        // Verify state moved to Dispatched after lease
        let status = server
            .get_command_status(&response.command_id)
            .await
            .unwrap();
        assert_eq!(status.state, CommandState::Dispatched);

        // 4. Agent simulates processing large params
        let params_bytes = alien_commands::runtime::decode_params_bytes(&lease.envelope)
            .await
            .unwrap();
        assert_eq!(params_bytes.len(), 160000); // Should have reconstructed the large params
        assert_eq!(params_bytes, vec![b'Y'; 160000]); // Should match original data

        // 5. Agent submits small inline response
        let agent_response = test_json_success_response(&serde_json::json!({
            "status": "processed",
            "command_id": lease.command_id
        }));
        server
            .submit_command_response(&lease.command_id, agent_response)
            .await
            .unwrap();

        // 6. Verify completion with inline response
        let final_status = server
            .wait_for_completion(&response.command_id, Duration::from_secs(5))
            .await
            .unwrap();
        assert_eq!(final_status.state, CommandState::Succeeded);

        let final_response = final_status.response.unwrap();
        assert!(final_response.is_success());
        if let CommandResponse::Success { response: body } = final_response {
            let body_data = body.decode_inline().unwrap();
            let json: serde_json::Value = serde_json::from_slice(&body_data).unwrap();
            assert_eq!(json["status"], "processed");
        }

        // Should have storage object from large params but not response
        assert!(server.storage_object_count().await > 0);
    }

    // ===============================================
    // TARGET ROUTING
    // ===============================================

    /// Status responses and lease envelopes carry the resolved target
    /// (single-target shorthand: no targetResourceId in the request).
    #[tokio::test]
    async fn test_status_and_envelope_carry_resolved_target() {
        let server = TestCommandServer::builder().with_pull_mode().build().await;

        let request = test_inline_create_command("target-agent", "targeted-command");
        let response = server.create_command(request).await.unwrap();

        let status = server
            .get_command_status(&response.command_id)
            .await
            .unwrap();
        assert_eq!(status.target, server.default_target);

        let lease = server
            .acquire_single_lease("target-agent")
            .await
            .unwrap()
            .unwrap();
        assert_eq!(lease.envelope.target, server.default_target);
    }

    /// An explicitly requested target that doesn't exist is rejected with the
    /// stable COMMAND_TARGET_NOT_FOUND code.
    #[tokio::test]
    async fn test_create_with_unknown_target_rejected() {
        let server = TestCommandServer::builder().with_pull_mode().build().await;

        let mut request = test_inline_create_command("target-agent", "targeted-command");
        request.target_resource_id = Some("no-such-resource".to_string());

        let err = server.create_command(request).await.unwrap_err();
        assert_eq!(err.code, "COMMAND_TARGET_NOT_FOUND");
    }

    /// With two registered targets, shorthand creation (no targetResourceId)
    /// is rejected with the stable COMMAND_TARGET_AMBIGUOUS code.
    #[tokio::test]
    async fn test_create_shorthand_with_two_targets_ambiguous() {
        let server = TestCommandServer::builder().with_pull_mode().build().await;
        server
            .registry
            .register_target("second-daemon", CommandTargetType::Daemon)
            .await
            .unwrap();

        let request = test_inline_create_command("target-agent", "targeted-command");
        let err = server.create_command(request).await.unwrap_err();
        assert_eq!(err.code, "COMMAND_TARGET_AMBIGUOUS");
    }

    /// Each target leases only its own commands: two targets, two commands,
    /// each lease scan returns only the requester's command.
    #[tokio::test]
    async fn test_lease_scans_only_requesting_targets_prefix() {
        let server = TestCommandServer::builder().with_pull_mode().build().await;
        server
            .registry
            .register_target("second-daemon", CommandTargetType::Daemon)
            .await
            .unwrap();
        let second_target = CommandTarget::new("second-daemon", CommandTargetType::Daemon);

        // Command for the default target.
        let mut request_a = test_inline_create_command("target-agent", "for-default");
        request_a.target_resource_id = Some(server.default_target.resource_id.clone());
        let cmd_a = server.create_command(request_a).await.unwrap();

        // Command for the second target.
        let mut request_b = test_inline_create_command("target-agent", "for-second");
        request_b.target_resource_id = Some("second-daemon".to_string());
        let cmd_b = server.create_command(request_b).await.unwrap();

        // Default target leases only its own command, even asking for many.
        let default_leases = server
            .acquire_lease(
                "target-agent",
                LeaseRequest {
                    deployment_id: "target-agent".to_string(),
                    target: server.default_target.clone(),
                    max_leases: 10,
                    lease_seconds: 60,
                },
            )
            .await
            .unwrap();
        assert_eq!(default_leases.leases.len(), 1);
        assert_eq!(default_leases.leases[0].command_id, cmd_a.command_id);
        assert_eq!(
            default_leases.leases[0].envelope.target,
            server.default_target
        );

        // Second target leases only its own command.
        let second_leases = server
            .acquire_lease(
                "target-agent",
                LeaseRequest {
                    deployment_id: "target-agent".to_string(),
                    target: second_target.clone(),
                    max_leases: 10,
                    lease_seconds: 60,
                },
            )
            .await
            .unwrap();
        assert_eq!(second_leases.leases.len(), 1);
        assert_eq!(second_leases.leases[0].command_id, cmd_b.command_id);
        assert_eq!(second_leases.leases[0].envelope.target, second_target);
    }

    /// Expiry-driven redelivery: a leased command whose lease TTL runs out
    /// WITHOUT a response (crashed receiver, network partition — no explicit
    /// release) must become leasable again, and the redelivered envelope must
    /// carry an incremented attempt so handlers can detect redelivery
    /// (`ctx.attempt > 1` is the documented at-least-once signal on both
    /// receiver twins).
    #[tokio::test]
    async fn test_expired_lease_redelivers_with_incremented_attempt() {
        let server = TestCommandServer::builder().with_pull_mode().build().await;

        let mut request = test_inline_create_command("expiry-agent", "expiry-redelivery");
        request.target_resource_id = Some(server.default_target.resource_id.clone());
        let created = server.create_command(request).await.unwrap();

        let lease_request = LeaseRequest {
            deployment_id: "expiry-agent".to_string(),
            target: server.default_target.clone(),
            max_leases: 1,
            lease_seconds: 1,
        };

        // First lease: attempt 1. No response is ever submitted.
        let first = server
            .acquire_lease("expiry-agent", lease_request.clone())
            .await
            .unwrap();
        assert_eq!(first.leases.len(), 1);
        assert_eq!(first.leases[0].command_id, created.command_id);
        assert_eq!(first.leases[0].attempt, 1);

        // While the lease is live, the command must NOT be re-leasable.
        let while_held = server
            .acquire_lease("expiry-agent", lease_request.clone())
            .await
            .unwrap();
        assert!(
            while_held.leases.is_empty(),
            "a live lease must not be double-leased"
        );

        // Let the 1s lease TTL expire without any response.
        tokio::time::sleep(std::time::Duration::from_millis(2500)).await;

        // Redelivery: same command, incremented attempt.
        let second = server
            .acquire_lease("expiry-agent", lease_request)
            .await
            .unwrap();
        assert_eq!(
            second.leases.len(),
            1,
            "an expired lease must make the command leasable again"
        );
        assert_eq!(second.leases[0].command_id, created.command_id);
        assert_eq!(
            second.leases[0].attempt, 2,
            "expiry-driven redelivery must increment the attempt"
        );
        assert_eq!(
            second.leases[0].envelope.attempt, 2,
            "the redelivered envelope must carry the incremented attempt"
        );
    }

    /// Lease-served envelopes carry manager URLs relative to the exact lease
    /// endpoint. The pull consumer resolves them against the endpoint it
    /// reached, preserving both a network-corrected origin and any reverse-
    /// proxy prefix. Signed query parameters must survive untouched.
    #[tokio::test]
    async fn test_leased_envelope_manager_urls_are_path_relative() {
        let server = TestCommandServer::builder().with_pull_mode().build().await;

        let mut request = test_inline_create_command("relative-agent", "relative-urls");
        request.target_resource_id = Some(server.default_target.resource_id.clone());
        let created = server.create_command(request).await.unwrap();

        let leases = server
            .acquire_lease(
                "relative-agent",
                LeaseRequest {
                    deployment_id: "relative-agent".to_string(),
                    target: server.default_target.clone(),
                    max_leases: 1,
                    lease_seconds: 60,
                },
            )
            .await
            .unwrap();
        assert_eq!(leases.leases.len(), 1);
        let envelope = &leases.leases[0].envelope;

        let submit = &envelope.response_handling.submit_response_url;
        assert!(
            submit.starts_with(&format!("{}/response?", created.command_id)),
            "submit URL must be relative to the lease endpoint, got '{submit}'"
        );
        assert!(
            submit.contains("response_token="),
            "relativization must preserve the signed query, got '{submit}'"
        );
    }

    /// Defense-in-depth: a pending-index entry under target A's prefix whose
    /// stored command metadata says target B is corruption — the lease call
    /// must fail loudly instead of misdelivering the command.
    #[tokio::test]
    async fn test_lease_target_mismatch_in_pending_index_is_loud_error() {
        let server = TestCommandServer::builder().with_pull_mode().build().await;
        server
            .registry
            .register_target("second-daemon", CommandTargetType::Daemon)
            .await
            .unwrap();

        // Create a command registered to second-daemon.
        let mut request = test_inline_create_command("target-agent", "for-second");
        request.target_resource_id = Some("second-daemon".to_string());
        let cmd = server.create_command(request).await.unwrap();

        // Corrupt the index: plant the command under the DEFAULT target's prefix.
        let corrupt_key = format!(
            "target:target-agent:{}:pending:{}:{}",
            server.default_target.resource_id,
            chrono::Utc::now().timestamp_nanos_opt().unwrap_or(0),
            cmd.command_id
        );
        alien_bindings::traits::Kv::put(server.kv.as_ref(), &corrupt_key, vec![], None)
            .await
            .unwrap();

        // Leasing as the default target must fail loudly, not deliver the command.
        let result = server
            .acquire_lease(
                "target-agent",
                LeaseRequest {
                    deployment_id: "target-agent".to_string(),
                    target: server.default_target.clone(),
                    max_leases: 1,
                    lease_seconds: 60,
                },
            )
            .await;
        let err = result.unwrap_err();
        assert!(
            err.message.contains(&cmd.command_id) || err.message.contains("target"),
            "expected loud target-mismatch error, got: {}",
            err.message
        );
    }

    /// Idempotency keys are scoped per target: same key on two different
    /// targets creates two distinct commands; same key + same target replays
    /// the same command.
    #[tokio::test]
    async fn test_idempotency_scoped_per_target() {
        let server = TestCommandServer::builder().with_pull_mode().build().await;
        server
            .registry
            .register_target("second-daemon", CommandTargetType::Daemon)
            .await
            .unwrap();

        let make_request = |target: &str| {
            let mut request = test_inline_create_command("target-agent", "idem-command");
            request.target_resource_id = Some(target.to_string());
            request.idempotency_key = Some("same-key".to_string());
            request
        };

        let default_id = server.default_target.resource_id.clone();
        let first = server
            .create_command(make_request(&default_id))
            .await
            .unwrap();
        let second = server
            .create_command(make_request("second-daemon"))
            .await
            .unwrap();
        // Same key, different target: distinct commands.
        assert_ne!(first.command_id, second.command_id);

        // Same key, same target: replays the same command.
        let replay = server
            .create_command(make_request(&default_id))
            .await
            .unwrap();
        assert_eq!(replay.command_id, first.command_id);
    }

    /// One deployment, two command-capable targets of different types
    /// (Worker + Daemon) sharing the exact same command name: the Worker's
    /// command routes Push (mock dispatcher receives it), the Daemon's lands
    /// only in the Daemon's own pending index (leasable there, absent from
    /// the Worker's). The two never cross.
    #[tokio::test]
    async fn test_worker_and_daemon_share_command_name_route_independently() {
        // Push-capable dispatcher: the default auto-registered target is a
        // Worker in Push mode (see TestCommandServerBuilder::build).
        let server = TestCommandServer::new().await;
        server
            .registry
            .register_target("shared-daemon", CommandTargetType::Daemon)
            .await
            .unwrap();
        let daemon_target = CommandTarget::new("shared-daemon", CommandTargetType::Daemon);

        // Command addressed to the Worker.
        let mut worker_request = test_inline_create_command("target-agent", "shared-command");
        worker_request.target_resource_id = Some(server.default_target.resource_id.clone());
        let worker_response = server.create_command(worker_request).await.unwrap();
        assert_eq!(worker_response.state, CommandState::Dispatched);

        // Command addressed to the Daemon, same command name.
        let mut daemon_request = test_inline_create_command("target-agent", "shared-command");
        daemon_request.target_resource_id = Some("shared-daemon".to_string());
        let daemon_response = server.create_command(daemon_request).await.unwrap();
        assert_eq!(daemon_response.state, CommandState::Pending);

        // The Worker's command reached the mock dispatcher (push); the
        // Daemon's did not — exactly one dispatch, and it's the Worker's.
        let mock_dispatcher = server
            .mock_dispatcher()
            .expect("Should have mock dispatcher");
        mock_dispatcher.assert_dispatch_count(1).await;
        let dispatched = mock_dispatcher.get_latest().await.unwrap();
        assert_eq!(dispatched.envelope.command_id, worker_response.command_id);
        assert_eq!(dispatched.envelope.target, server.default_target);

        // The Daemon's command sits only in ITS OWN pending index: leasing as
        // the Daemon target returns exactly the Daemon's command.
        let daemon_leases = server
            .acquire_lease(
                "target-agent",
                LeaseRequest {
                    deployment_id: "target-agent".to_string(),
                    target: daemon_target.clone(),
                    max_leases: 10,
                    lease_seconds: 60,
                },
            )
            .await
            .unwrap();
        assert_eq!(daemon_leases.leases.len(), 1);
        assert_eq!(
            daemon_leases.leases[0].command_id,
            daemon_response.command_id
        );
        assert_eq!(daemon_leases.leases[0].envelope.target, daemon_target);

        // Leasing as the Worker target never surfaces the Daemon's command
        // (the Worker's pending index is untouched — its command was pushed,
        // not enqueued, and the Daemon's command was never indexed there).
        let worker_leases = server
            .acquire_lease(
                "target-agent",
                LeaseRequest {
                    deployment_id: "target-agent".to_string(),
                    target: server.default_target.clone(),
                    max_leases: 10,
                    lease_seconds: 60,
                },
            )
            .await
            .unwrap();
        assert_eq!(worker_leases.leases.len(), 0);
    }

    // ===============================================
    // ESSENTIAL COMPONENT TESTS
    // ===============================================

    /// Test basic API operations
    #[tokio::test]
    async fn test_basic_api_operations() {
        let server = TestCommandServer::new().await;

        // Test create command with inline payload
        let request = test_inline_create_command("api-agent", "test-command");
        let response = server.create_command(request).await.unwrap();
        assert_eq!(response.state, CommandState::Dispatched);
        assert!(response.command_id.starts_with("cmd_"));
        assert!(response.storage_upload.is_none());

        // Test status check
        let status = server
            .get_command_status(&response.command_id)
            .await
            .unwrap();
        assert_eq!(status.command_id, response.command_id);
        assert_eq!(status.state, CommandState::Dispatched);
        assert_eq!(status.attempt, 1);

        // Test create large command requiring storage
        let large_request = test_storage_create_command("storage-agent", "upload-command", 200_000);
        let response = server.create_command(large_request).await.unwrap();
        assert_eq!(response.state, CommandState::PendingUpload);
        assert!(response.storage_upload.is_some());

        // Test upload completion
        let upload_complete = test_upload_complete_request(200_000);
        let complete_response = server
            .upload_complete(&response.command_id, upload_complete)
            .await
            .unwrap();
        assert_eq!(complete_response.state, CommandState::Dispatched);
    }

    /// A transport error is an ambiguous acknowledgement: the target may have
    /// accepted the envelope before the response was lost. The command must
    /// stay Dispatched so a late response remains valid. Creation still returns
    /// the durable command ID, avoiding an unknown orphan and an immediate
    /// duplicate when default clients retry without an idempotency key.
    #[tokio::test]
    async fn ambiguous_push_failure_keeps_dispatched_and_accepts_late_response() {
        let server = TestCommandServer::new().await;
        let dispatcher = server
            .mock_dispatcher()
            .expect("push test server uses the mock dispatcher");
        dispatcher.set_should_fail(true).await;

        let created = server
            .create_command(test_inline_create_command(
                "push-agent",
                "ambiguous-dispatch",
            ))
            .await
            .expect("ambiguous acknowledgement must still return the durable command ID");
        assert_eq!(created.state, CommandState::Dispatched);
        dispatcher.assert_dispatch_count(0).await;

        let status = server
            .get_command_status(&created.command_id)
            .await
            .expect("command status");
        assert_eq!(status.command_id, created.command_id);
        assert_eq!(status.state, CommandState::Dispatched);
        assert_eq!(status.attempt, 1);

        server
            .submit_command_response(
                &created.command_id,
                test_success_response(b"completed after ambiguous acknowledgement"),
            )
            .await
            .expect("late response remains valid");
        server.assert_command_succeeded(&created.command_id).await;
    }

    #[tokio::test]
    async fn definite_push_rejection_becomes_terminal_delivery_failure() {
        let server = TestCommandServer::new().await;
        let dispatcher = server
            .mock_dispatcher()
            .expect("push test server uses the mock dispatcher");
        dispatcher.set_should_reject(true).await;

        let created = server
            .create_command(test_inline_create_command(
                "push-agent",
                "definite-rejection",
            ))
            .await
            .expect("definite rejection must return the durable terminal command ID");
        assert_eq!(created.state, CommandState::Failed);

        let status = server
            .get_command_status(&created.command_id)
            .await
            .expect("command status");
        assert_eq!(status.state, CommandState::Failed);
        let Some(CommandResponse::Error { code, message, .. }) = status.response else {
            panic!("definite delivery rejection must persist an error response");
        };
        assert_eq!(code, "DELIVERY_FAILED");
        assert_eq!(message, "Worker runtime did not accept command delivery");

        let late = server
            .submit_command_response(
                &created.command_id,
                test_success_response(b"must not replace delivery failure"),
            )
            .await;
        assert!(
            late.is_ok(),
            "terminal duplicate submissions are idempotent"
        );
        server.assert_command_failed(&created.command_id).await;
    }

    /// Test lease operations
    #[tokio::test]
    async fn test_lease_operations() {
        // Lease operations require Pull mode (Push mode dispatches immediately)
        let server = TestCommandServer::builder().with_pull_mode().build().await;

        // Create command (stays Pending in Pull mode)
        let request = test_inline_create_command("lease-agent", "lease-command");
        let response = server.create_command(request).await.unwrap();
        assert_eq!(response.state, CommandState::Pending);

        // Acquire lease
        let lease = server
            .acquire_single_lease("lease-agent")
            .await
            .unwrap()
            .unwrap();
        assert_eq!(lease.command_id, response.command_id);
        assert_eq!(lease.attempt, 1);
        assert!(lease.lease_expires_at > Utc::now());

        // Verify envelope details
        assert_envelope_command_id(&lease.envelope, &response.command_id);
        assert_envelope_command(&lease.envelope, "lease-command");

        // Test no available leases
        let empty_lease_request = LeaseRequest {
            deployment_id: "nonexistent-agent".to_string(),
            target: server.default_target.clone(),
            max_leases: 1,
            lease_seconds: 60,
        };
        let empty_response = server
            .acquire_lease("nonexistent-agent", empty_lease_request)
            .await
            .unwrap();
        assert_eq!(empty_response.leases.len(), 0);

        // Release lease
        server
            .release_lease(&lease.command_id, &lease.lease_id)
            .await
            .unwrap();
        server
            .assert_command_state(&response.command_id, CommandState::Pending)
            .await;
    }

    #[tokio::test]
    async fn max_timeout_lease_receives_response_credentials_beyond_lease_headroom() {
        let server = TestCommandServer::builder().with_pull_mode().build().await;
        let created = server
            .create_command(test_inline_create_command("max-timeout", "run"))
            .await
            .unwrap();
        let lease = server
            .acquire_lease(
                "max-timeout",
                LeaseRequest {
                    deployment_id: "max-timeout".to_string(),
                    target: server.default_target.clone(),
                    max_leases: 1,
                    lease_seconds: 3660,
                },
            )
            .await
            .unwrap()
            .leases
            .into_iter()
            .next()
            .expect("max-timeout lease");
        assert_eq!(lease.command_id, created.command_id);

        let submit = url::Url::parse("http://manager.invalid")
            .unwrap()
            .join(&lease.envelope.response_handling.submit_response_url)
            .unwrap();
        let expires = submit
            .query_pairs()
            .find_map(|(key, value)| (key == "expires").then(|| value.parse::<i64>().unwrap()))
            .expect("response token expiry");
        let required = Utc::now() + chrono::Duration::seconds(3660);
        assert!(
            expires > required.timestamp(),
            "response token must outlive max execution plus lease headroom"
        );
        assert!(
            lease
                .envelope
                .response_handling
                .storage_upload_request
                .expiration
                > required,
            "response upload credential must outlive max execution plus lease headroom"
        );
    }

    #[tokio::test]
    async fn delayed_storage_command_lease_refreshes_expired_params_get() {
        let server = TestCommandServer::builder().with_pull_mode().build().await;
        let created = server
            .create_command(test_storage_create_command(
                "delayed-storage",
                "run",
                200_000,
            ))
            .await
            .unwrap();
        server
            .upload_complete(&created.command_id, test_upload_complete_request(200_000))
            .await
            .unwrap();

        let mut stored = server
            .command_server
            .get_params(&created.command_id)
            .await
            .unwrap()
            .expect("stored params");
        let BodySpec::Storage {
            storage_get_request: Some(request),
            ..
        } = &mut stored
        else {
            panic!("storage params with GET request");
        };
        request.expiration = Utc::now() - chrono::Duration::hours(1);
        server
            .command_server
            .store_params(&created.command_id, &stored)
            .await
            .unwrap();

        let lease = server
            .acquire_single_lease("delayed-storage")
            .await
            .unwrap()
            .expect("delayed command lease");
        let BodySpec::Storage {
            storage_get_request: Some(fresh),
            ..
        } = lease.envelope.params
        else {
            panic!("leased storage params with fresh GET request");
        };
        assert!(
            fresh.expiration > Utc::now() + chrono::Duration::minutes(50),
            "leasing must replace the stale upload-time params URL"
        );
    }

    /// Test response submission and idempotency
    #[tokio::test]
    async fn test_response_operations() {
        // Lease operations require Pull mode (Push mode dispatches immediately)
        let server = TestCommandServer::builder().with_pull_mode().build().await;

        // Create command and acquire lease
        let request = test_inline_create_command("response-agent", "response-command");
        let response = server.create_command(request).await.unwrap();
        let lease = server
            .acquire_single_lease("response-agent")
            .await
            .unwrap()
            .unwrap();

        // Submit response
        let agent_response = test_json_success_response(&serde_json::json!({
            "result": "success",
            "data": [1, 2, 3]
        }));
        server
            .submit_command_response(&lease.command_id, agent_response)
            .await
            .unwrap();
        server.assert_command_succeeded(&response.command_id).await;

        // Verify response data
        let status = server
            .get_command_status(&response.command_id)
            .await
            .unwrap();
        let final_response = status.response.unwrap();
        assert!(final_response.is_success());
        if let CommandResponse::Success { response: body } = final_response {
            let decoded = body.decode_inline().unwrap();
            let json: serde_json::Value = serde_json::from_slice(&decoded).unwrap();
            assert_eq!(json["result"], "success");
        }

        // Test duplicate response submission (should be idempotent)
        let duplicate_response = test_success_response(b"second response");
        let result = server
            .submit_command_response(&lease.command_id, duplicate_response)
            .await;
        assert!(result.is_ok()); // Should not error, just ignore

        // Original response should still be there
        let status = server
            .get_command_status(&response.command_id)
            .await
            .unwrap();
        let final_response = status.response.unwrap();
        if let CommandResponse::Success { response: body } = final_response {
            let decoded = body.decode_inline().unwrap();
            let json: serde_json::Value = serde_json::from_slice(&decoded).unwrap();
            assert_eq!(json["result"], "success"); // Not changed
        }
    }

    /// Regression: a failure partway through response cleanup must not strand the
    /// command as non-terminal with an invisible response, and must not fail the
    /// `submit_command_response` call either.
    ///
    /// `submit_command_response` stores the response blob, commits the terminal
    /// registry state (the source of truth), then cleans up the lease and pending
    /// index. Here the KV is armed to fail the pending-index scan performed by
    /// that cleanup. Cleanup is best-effort — a leftover pending-index/lease entry
    /// is reaped by `acquire_lease`'s terminal-state check on its next scan — so
    /// the submit call must still return `Ok`, only logging a warning, and the
    /// command must still be observable as `Succeeded` with its stored response.
    /// Under the old ordering (cleanup first, state last) the same fault left the
    /// command stuck as `Dispatched` with a stored-but-invisible response; under a
    /// prior version of the new ordering, the cleanup error was still propagated
    /// with `?`, failing the call despite the terminal state already being safe.
    #[tokio::test]
    #[tracing_test::traced_test]
    async fn test_response_cleanup_failure_keeps_command_terminal() {
        let server = TestCommandServer::builder()
            .with_pull_mode()
            .with_fault_injection()
            .build()
            .await;
        let fault_kv = server
            .fault_kv
            .clone()
            .expect("fault injection was requested");

        // Create + lease the command. Lease acquisition scans the same pending
        // prefix, so it must run before the fault is armed.
        let request = test_inline_create_command("cleanup-agent", "cleanup-command");
        let response = server.create_command(request).await.unwrap();
        let lease = server
            .acquire_single_lease("cleanup-agent")
            .await
            .unwrap()
            .unwrap();

        // Arm the fault so the pending-index cleanup scan fails during submit.
        fault_kv.arm_pending_scan_failure();

        let agent_response = test_json_success_response(&serde_json::json!({ "result": "ok" }));
        let submit_result = server
            .submit_command_response(&lease.command_id, agent_response)
            .await;
        assert!(
            submit_result.is_ok(),
            "cleanup failures are best-effort and must not fail submit_command_response \
             once the terminal state is committed"
        );
        assert!(
            logs_contain("Failed to clean up pending index"),
            "a cleanup failure must still be logged as a warning"
        );

        // Despite the cleanup failure, the terminal state was committed first, so
        // the command is visible as Succeeded with its stored response rather than
        // stranded as Dispatched with an invisible one.
        let status = server
            .get_command_status(&response.command_id)
            .await
            .unwrap();
        assert_eq!(
            status.state,
            CommandState::Succeeded,
            "command must be terminal even though response cleanup failed"
        );
        let final_response = status
            .response
            .expect("stored response must be visible on the terminal command");
        assert!(final_response.is_success());
        if let CommandResponse::Success { response: body } = final_response {
            let decoded = body.decode_inline().unwrap();
            let json: serde_json::Value = serde_json::from_slice(&decoded).unwrap();
            assert_eq!(json["result"], "ok");
        }
    }

    /// Test runtime envelope parsing
    #[tokio::test]
    async fn test_runtime_integration() {
        // Test envelope parsing from queue message
        let envelope = test_simple_envelope("cmd_runtime_test", "runtime-command");
        let envelope_json = serde_json::to_value(&envelope).unwrap();
        let queue_message = QueueMessage {
            id: "msg_123".to_string(),
            payload: MessagePayload::Json(envelope_json),
            receipt_handle: "handle_123".to_string(),
            timestamp: Utc::now(),
            source: "test-queue".to_string(),
            attributes: std::collections::HashMap::new(),
            attempt_count: Some(1),
        };

        let parsed = parse_envelope(&queue_message).unwrap();
        assert!(parsed.is_some());
        let parsed_envelope = parsed.unwrap();
        assert_eq!(parsed_envelope.command_id, "cmd_runtime_test");
        assert_eq!(parsed_envelope.command, "runtime-command");

        // Test non-command message
        let non_arc_message = QueueMessage {
            id: "msg_456".to_string(),
            payload: MessagePayload::Json(serde_json::json!({"regular": "message"})),
            receipt_handle: "handle_456".to_string(),
            timestamp: Utc::now(),
            source: "test-queue".to_string(),
            attributes: std::collections::HashMap::new(),
            attempt_count: Some(1),
        };
        let parsed = parse_envelope(&non_arc_message).unwrap();
        assert!(parsed.is_none());

        // Test params decoding
        let params_json = serde_json::json!({"key": "value", "number": 42});
        let params_bytes = serde_json::to_vec(&params_json).unwrap();
        let test_envelope = test_envelope(
            "cmd_params",
            "params-command",
            BodySpec::inline(&params_bytes),
        );
        let decoded_params = decode_params(&test_envelope).await.unwrap();
        assert_eq!(decoded_params["key"], "value");
        assert_eq!(decoded_params["number"], 42);
    }

    /// Test error handling and edge cases
    #[tokio::test]
    async fn test_error_handling() {
        let server = TestCommandServer::new().await;

        // Test invalid command (empty command name)
        let invalid_request = CreateCommandRequest {
            deployment_id: "error-agent".to_string(),
            command: "".to_string(), // Invalid: empty command name
            params: BodySpec::inline(b"{}"),
            deadline: None,
            idempotency_key: None,
            target_resource_id: None,
        };
        let result = server.create_command(invalid_request).await;
        assert!(result.is_err());

        // Test invalid command (empty deployment_id)
        let invalid_request = CreateCommandRequest {
            deployment_id: "".to_string(), // Invalid: empty deployment_id
            command: "test".to_string(),
            params: BodySpec::inline(b"{}"),
            deadline: None,
            idempotency_key: None,
            target_resource_id: None,
        };
        let result = server.create_command(invalid_request).await;
        assert!(result.is_err());

        // Test operations on non-existent commands
        let upload_complete = test_upload_complete_request(1000);
        assert!(server
            .upload_complete("nonexistent", upload_complete)
            .await
            .is_err());
        assert!(server.get_command_status("nonexistent").await.is_err());
        assert!(server
            .submit_command_response("nonexistent", test_success_response(b"test"))
            .await
            .is_err());

        // Test command expiration (past deadline)
        let past_deadline = Utc::now() - chrono::Duration::minutes(1);
        let expired_request = test_create_command_with_deadline(
            "expired-agent",
            "expired-command",
            BodySpec::inline(b"{}"),
            past_deadline,
        );
        assert!(server.create_command(expired_request).await.is_err());
    }

    /// Test error response handling
    #[tokio::test]
    async fn test_error_response_handling() {
        // Lease operations require Pull mode (Push mode dispatches immediately)
        let server = TestCommandServer::builder().with_pull_mode().build().await;

        // Create command
        let request = test_inline_create_command("error-agent", "error-command");
        let response = server.create_command(request).await.unwrap();

        // Acquire lease
        let lease = server
            .acquire_single_lease("error-agent")
            .await
            .unwrap()
            .unwrap();

        // Submit error response
        let agent_response = test_error_response("PROCESSING_FAILED", "Something went wrong");
        server
            .submit_command_response(&lease.command_id, agent_response)
            .await
            .unwrap();

        // Verify command failed
        let status = server
            .get_command_status(&response.command_id)
            .await
            .unwrap();
        assert_eq!(status.state, CommandState::Failed);

        let final_response = status.response.unwrap();
        assert!(final_response.is_error());
        if let CommandResponse::Error { code, message, .. } = final_response {
            assert_eq!(code, "PROCESSING_FAILED");
            assert_eq!(message, "Something went wrong");
        }
    }
}

/// Two racing submitters with OPPOSITE outcomes (a redelivered execution
/// racing the original whose lease expired): exactly one wins the terminal
/// transition, the loser is swallowed as a duplicate, and the recorded state
/// matches the served response — a terminal record is never overwritten.
#[cfg(feature = "test-utils")]
#[tokio::test]
async fn concurrent_opposite_submits_keep_state_and_response_consistent() {
    use alien_commands::test_utils::*;
    use alien_commands::types::{CommandResponse, CommandState};

    let server = TestCommandServer::builder().with_pull_mode().build().await;

    let request = test_inline_create_command("pull-agent", "flaky-op");
    let created = server.create_command(request).await.unwrap();
    let lease = server
        .acquire_single_lease("pull-agent")
        .await
        .unwrap()
        .unwrap();

    let success = test_json_success_response(&serde_json::json!({ "ok": true }));
    let failure = CommandResponse::Error {
        code: "HANDLER_ERROR".to_string(),
        message: "boom".to_string(),
        details: None,
    };

    // Race the two submissions.
    let (a, b) = tokio::join!(
        server.submit_command_response(&lease.command_id, success),
        server.submit_command_response(&lease.command_id, failure),
    );
    // Both calls succeed: the loser is silently treated as a duplicate.
    a.unwrap();
    b.unwrap();

    let status = server
        .get_command_status(&created.command_id)
        .await
        .unwrap();
    assert!(status.state.is_terminal());
    let response = status.response.expect("terminal command serves a response");
    match (&status.state, &response) {
        (CommandState::Succeeded, CommandResponse::Success { .. }) => {}
        (CommandState::Failed, CommandResponse::Error { .. }) => {}
        (state, response) => {
            panic!("torn terminal record: state {state:?} does not match response {response:?}")
        }
    }
}

/// The deadline reaper terminates commands nothing else would ever touch:
/// a Pending pull command past its deadline (with its pending-index entry
/// cleaned) — the same path also covers PendingUpload commands whose params
/// upload never completed, since the reaper transitions ANY non-terminal
/// state.
#[cfg(feature = "test-utils")]
#[tokio::test]
async fn deadline_reaper_expires_overdue_commands() {
    use alien_commands::test_utils::*;
    use alien_commands::types::CommandState;

    let server = TestCommandServer::builder().with_pull_mode().build().await;

    // A multi-second deadline matters here: the index entry gets a KV TTL,
    // and the regression this guards is the TTL expiring AT the deadline —
    // hiding the entry from the reaper's scan exactly when it became due.
    let mut request = test_inline_create_command("pull-agent", "slow-op");
    request.deadline = Some(chrono::Utc::now() + chrono::Duration::seconds(2));
    let created = server.create_command(request).await.unwrap();
    assert_eq!(created.state, CommandState::Pending);

    tokio::time::sleep(std::time::Duration::from_millis(2600)).await;
    let expired = server.command_server.reap_expired_commands().await.unwrap();
    assert_eq!(expired, 1, "the overdue command must be reaped");

    let status = server
        .get_command_status(&created.command_id)
        .await
        .unwrap();
    assert_eq!(status.state, CommandState::Expired);

    // The pending index entry is gone: a poller can no longer lease it.
    let lease = server.acquire_single_lease("pull-agent").await.unwrap();
    assert!(lease.is_none(), "expired command must not be leasable");

    // A second reap pass is a no-op (index entry deleted).
    assert_eq!(
        server.command_server.reap_expired_commands().await.unwrap(),
        0
    );
}