iron-core 0.1.36

Core AgentIron loop, session state, and tool registry
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
//! Integration tests for the `agent-iron` CLI binary.
//!
//! These tests exercise [`iron_core::cli::execute_run_with_streams`] — the
//! core CLI implementation that accepts generic writers — covering:
//!
//! - Command-level usage failures and env/CLI precedence (task 6.2)
//! - JSON output contract: exactly one stdout object, required fields,
//!   structured failure results (task 6.3)
//! - End-to-end bootstrap and preflight paths: saved default provider/model,
//!   AutoApprove preflight enforcement, plugin exclusion, task resolution
//!   (task 6.4)

use base64::Engine;
use iron_core::automation_task::AutomationTaskInput;
use iron_core::cli;
use iron_core::config::crypto::XChaCha20Poly1305Cipher;
use iron_core::config::{ConfigStore, DefaultModelInput, OpenOptions, ProfileInput, PromptInput};
use iron_core::profile::{
    AgentApproval, AgentProfile, AgentProfileId, AgentProfileProvider, SkillFilter, ToolFilter,
    PROFILE_SCHEMA_VERSION,
};
use iron_core::provider_credential::{
    DurableCredentialStore, ProviderCredentialStore, ProviderSlug, StoredCredential,
};
use iron_core::stored_prompt::{StoredPrompt, STORED_PROMPT_SCHEMA_VERSION};
use std::path::Path;
use std::sync::Arc;

// ============================================================================
// Constants
// ============================================================================

const EXIT_COMPLETED: i32 = 0;
const EXIT_USAGE: i32 = 2;
const EXIT_CONFIG: i32 = 3;
const EXIT_UNSAFE_POLICY: i32 = 4;
const EXIT_PROVIDER_INIT: i32 = 5;

/// Deterministic test encryption key (32 bytes of 0x42).
fn test_key() -> [u8; 32] {
    [0x42u8; 32]
}

/// Base64-encode the test key for the env var.
fn test_key_b64() -> String {
    base64::engine::general_purpose::STANDARD.encode(test_key())
}

// ============================================================================
// Store fixture helpers
// ============================================================================

/// Serializes tests that mutate the process-global encryption-key env var so
/// that parallel `cargo test` execution cannot race on `AGENTIRON_CONFIG_ENCRYPTION_KEY`.
static ENV_LOCK: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());

/// Set the process-level encryption key so `ConfigStore::open_at` can decrypt
/// credentials stored by the same key.
///
/// Returns a guard that must be held for the duration of any code path that
/// reads the env var. This serializes the env-dependent tests under parallel
/// `cargo test` execution to avoid races on the process-global env var.
async fn set_encryption_key() -> tokio::sync::MutexGuard<'static, ()> {
    let guard = ENV_LOCK.lock().await;
    std::env::set_var("AGENTIRON_CONFIG_ENCRYPTION_KEY", test_key_b64());
    guard
}

/// Build a cipher matching the env-var key.
fn test_cipher() -> Arc<dyn iron_core::config::crypto::CredentialCipher> {
    Arc::new(XChaCha20Poly1305Cipher::new(&test_key()))
}

/// Open a file-based ConfigStore at `path` using the test cipher.
async fn open_store(path: impl AsRef<Path>) -> ConfigStore {
    let options = OpenOptions {
        cipher: Some(test_cipher()),
        busy_timeout: None,
    };
    ConfigStore::open_at_with_options(path, options)
        .await
        .expect("failed to open test store")
}

/// Seed a store with credential + default model + AutoApprove profile + prompt + task.
async fn seed_complete_store(store: &ConfigStore) {
    seed_credential_and_model(store, "openai", "gpt-4o", "sk-test-key").await;
    seed_auto_approve_profile(store).await;
    seed_prompt_and_task(store, "automation").await;
}

/// Store a provider API-key credential and set the default model.
async fn seed_credential_and_model(
    store: &ConfigStore,
    provider: &str,
    model: &str,
    api_key: &str,
) {
    let durable = Arc::new(DurableCredentialStore::new(store.clone()));
    ProviderCredentialStore::set(
        &*durable,
        &ProviderSlug::new(provider),
        StoredCredential::ApiKey(api_key.to_string()),
    )
    .await;

    store
        .set_default_model(&DefaultModelInput {
            provider_slug: provider.to_string(),
            model_id: model.to_string(),
        })
        .await
        .unwrap();
}

/// Create an AutoApprove profile named "automation".
async fn seed_auto_approve_profile(store: &ConfigStore) {
    let profile = AgentProfile {
        name: "automation".to_string(),
        provider: AgentProfileProvider::RuntimeDefault,
        tools: ToolFilter::Inherit,
        skills: SkillFilter::Inherit,
        approval: AgentApproval::AutoApprove,
        identity_prompt: None,
    };
    store
        .set_profile(&ProfileInput {
            id: "automation".to_string(),
            schema_version: PROFILE_SCHEMA_VERSION,
            payload: serde_json::to_value(&profile).unwrap(),
        })
        .await
        .unwrap();
}

/// Create a PerTool profile named "automation".
async fn seed_pertool_profile(store: &ConfigStore) {
    let profile = AgentProfile {
        name: "automation".to_string(),
        provider: AgentProfileProvider::RuntimeDefault,
        tools: ToolFilter::Inherit,
        skills: SkillFilter::Inherit,
        approval: AgentApproval::PerTool,
        identity_prompt: None,
    };
    store
        .set_profile(&ProfileInput {
            id: "automation".to_string(),
            schema_version: PROFILE_SCHEMA_VERSION,
            payload: serde_json::to_value(&profile).unwrap(),
        })
        .await
        .unwrap();
}

/// Create a stored prompt (referencing the given profile) and an automation task.
async fn seed_prompt_and_task(store: &ConfigStore, profile_id: &str) {
    let prompt = StoredPrompt {
        display_name: "Daily Report".to_string(),
        normalized_name: "daily-report".to_string(),
        instructions: "Generate a daily report".to_string(),
        skills: Vec::new(),
        profile: Some(AgentProfileId::from(profile_id)),
    };
    store
        .set_prompt(&PromptInput {
            id: "report-prompt".to_string(),
            schema_version: STORED_PROMPT_SCHEMA_VERSION,
            payload: serde_json::to_value(&prompt).unwrap(),
            display_name: "Daily Report".to_string(),
            normalized_name: "daily-report".to_string(),
        })
        .await
        .unwrap();

    store
        .set_automation_task(&AutomationTaskInput {
            id: "daily-report".to_string(),
            display_name: "Daily Report".to_string(),
            stored_prompt_id: "report-prompt".to_string(),
            expected_outcome: "A summary of today's activity".to_string(),
            project_root: std::env::temp_dir(),
            timeout_seconds: 300,
        })
        .await
        .unwrap();
}

/// Run the CLI with the given args, env, and return (exit_code, stdout, stderr).
async fn run_cli(args: &[String], env: &mut [(String, String)]) -> (i32, String, String) {
    let mut stdout = Vec::new();
    let mut stderr = Vec::new();
    let code = cli::execute_run_with_streams(args, env, &mut stdout, &mut stderr).await;
    let stdout_str = String::from_utf8_lossy(&stdout).to_string();
    let stderr_str = String::from_utf8_lossy(&stderr).to_string();
    (code, stdout_str, stderr_str)
}

/// Build args for `run <task>` with workspace, config, and timeout.
/// NOTE: args do NOT include the program name (the binary does skip(1)).
fn run_args(task_id: &str, workspace: &str, config: &str, timeout: &str) -> Vec<String> {
    vec![
        "run".to_string(),
        task_id.to_string(),
        "--workspace".to_string(),
        workspace.to_string(),
        "--config".to_string(),
        config.to_string(),
        "--timeout".to_string(),
        timeout.to_string(),
    ]
}

// ============================================================================
// 6.2 Command-level tests
// ============================================================================

#[tokio::test]
async fn usage_no_args() {
    let env = vec![];
    let (code, _out, err) = run_cli(&[], &mut env.clone()).await;
    assert_eq!(code, EXIT_USAGE);
    assert!(!err.is_empty());
}

#[tokio::test]
async fn usage_no_subcommand() {
    let env = vec![];
    let args = vec!["bogus".to_string()];
    let (code, _out, err) = run_cli(&args, &mut env.clone()).await;
    assert_eq!(code, EXIT_USAGE);
    assert!(!err.is_empty());
}

#[tokio::test]
async fn usage_missing_task_id() {
    let env = vec![];
    let args = vec!["run".to_string()];
    let (code, _out, err) = run_cli(&args, &mut env.clone()).await;
    assert_eq!(code, EXIT_USAGE);
    assert!(!err.is_empty());
}

#[tokio::test]
async fn task_timeout_used_as_fallback() {
    let dir = tempfile::tempdir().unwrap();
    let db_path = dir.path().join("config.db");
    let store = open_store(&db_path).await;
    seed_complete_store(&store).await;

    let args = vec![
        "run".to_string(),
        "daily-report".to_string(),
        "--workspace".to_string(),
        dir.path().to_str().unwrap().to_string(),
        "--config".to_string(),
        db_path.to_str().unwrap().to_string(),
    ];
    let env = vec![];
    let (code, _out, err) = run_cli(&args, &mut env.clone()).await;
    assert_eq!(code, EXIT_COMPLETED);
    // Verify the execution-boundary timeout resolved from the task-level
    // fallback (300s), not just the seeded store value.
    assert!(
        err.contains("timeout: 300s"),
        "expected execution timeout of 300s in stderr, got: {err}"
    );
}

#[tokio::test]
async fn usage_invalid_timeout() {
    let dir = tempfile::tempdir().unwrap();
    let args = vec![
        "run".to_string(),
        "task".to_string(),
        "--workspace".to_string(),
        dir.path().to_str().unwrap().to_string(),
        "--timeout".to_string(),
        "notaduration".to_string(),
    ];
    let env = vec![];
    let (code, _out, _err) = run_cli(&args, &mut env.clone()).await;
    assert_eq!(code, EXIT_USAGE);
}

#[tokio::test]
async fn config_invalid_workspace() {
    let args = vec![
        "run".to_string(),
        "task".to_string(),
        "--workspace".to_string(),
        "/nonexistent/path/that/should/not/exist".to_string(),
        "--timeout".to_string(),
        "30s".to_string(),
    ];
    let env = vec![];
    let (code, _out, _err) = run_cli(&args, &mut env.clone()).await;
    assert_eq!(code, EXIT_CONFIG);
}

#[tokio::test]
async fn precedence_timeout_cli_over_env() {
    let dir = tempfile::tempdir().unwrap();
    let db_path = dir.path().join("config.db");
    let _store = open_store(&db_path).await;

    let args = vec![
        "run".to_string(),
        "task".to_string(),
        "--workspace".to_string(),
        dir.path().to_str().unwrap().to_string(),
        "--config".to_string(),
        db_path.to_str().unwrap().to_string(),
        "--timeout".to_string(),
        "1m".to_string(),
    ];
    let mut env = vec![("AGENTIRON_TIMEOUT".to_string(), "30s".to_string())];
    let (code, _out, _err) = run_cli(&args, &mut env).await;
    assert_eq!(code, EXIT_PROVIDER_INIT);
}

#[tokio::test]
async fn precedence_workspace_cli_over_env() {
    let dir = tempfile::tempdir().unwrap();
    let env_workspace = tempfile::tempdir().unwrap();
    let db_path = dir.path().join("config.db");
    let _store = open_store(&db_path).await;

    let args = vec![
        "run".to_string(),
        "task".to_string(),
        "--workspace".to_string(),
        dir.path().to_str().unwrap().to_string(),
        "--config".to_string(),
        db_path.to_str().unwrap().to_string(),
        "--timeout".to_string(),
        "30s".to_string(),
    ];
    let mut env = vec![(
        "AGENTIRON_WORKSPACE".to_string(),
        env_workspace.path().to_str().unwrap().to_string(),
    )];
    let (code, _out, _stderr) = run_cli(&args, &mut env).await;
    assert_eq!(code, EXIT_PROVIDER_INIT);
}

#[tokio::test]
async fn precedence_format_cli_over_env() {
    let dir = tempfile::tempdir().unwrap();
    let db_path = dir.path().join("config.db");
    let _store = open_store(&db_path).await;

    let args = vec![
        "run".to_string(),
        "task".to_string(),
        "--workspace".to_string(),
        dir.path().to_str().unwrap().to_string(),
        "--config".to_string(),
        db_path.to_str().unwrap().to_string(),
        "--timeout".to_string(),
        "30s".to_string(),
        "--format".to_string(),
        "json".to_string(),
    ];
    let mut env = vec![("AGENTIRON_FORMAT".to_string(), "text".to_string())];
    let (code, stdout, _stderr) = run_cli(&args, &mut env).await;
    assert_eq!(code, EXIT_PROVIDER_INIT);
    let v: serde_json::Value = serde_json::from_str(stdout.trim()).expect("valid JSON");
    assert_eq!(v["status"], "failed");
}

#[tokio::test]
async fn quiet_suppresses_progress() {
    let dir = tempfile::tempdir().unwrap();
    let db_path = dir.path().join("config.db");
    let _store = open_store(&db_path).await;

    let args = vec![
        "run".to_string(),
        "task".to_string(),
        "--workspace".to_string(),
        dir.path().to_str().unwrap().to_string(),
        "--config".to_string(),
        db_path.to_str().unwrap().to_string(),
        "--timeout".to_string(),
        "30s".to_string(),
        "--quiet".to_string(),
    ];
    let env = vec![];
    let (_code, _out, stderr) = run_cli(&args, &mut env.clone()).await;
    assert!(!stderr.contains("workspace:"));
    assert!(!stderr.contains("timeout:"));
}

// ============================================================================
// 6.3 JSON contract tests
// ============================================================================

#[tokio::test]
async fn json_failure_has_one_stdout_object() {
    let dir = tempfile::tempdir().unwrap();
    let db_path = dir.path().join("config.db");
    let _store = open_store(&db_path).await;

    let args = vec![
        "run".to_string(),
        "task".to_string(),
        "--workspace".to_string(),
        dir.path().to_str().unwrap().to_string(),
        "--config".to_string(),
        db_path.to_str().unwrap().to_string(),
        "--timeout".to_string(),
        "30s".to_string(),
        "--format".to_string(),
        "json".to_string(),
    ];
    let env = vec![];
    let (code, stdout, _stderr) = run_cli(&args, &mut env.clone()).await;
    assert_eq!(code, EXIT_PROVIDER_INIT);

    let trimmed = stdout.trim();
    assert!(
        trimmed.starts_with('{') && trimmed.ends_with('}'),
        "stdout should be a single JSON object"
    );
    let _v: serde_json::Value = serde_json::from_str(trimmed).expect("stdout should be valid JSON");
}

#[tokio::test]
async fn json_failure_has_required_fields() {
    let dir = tempfile::tempdir().unwrap();
    let db_path = dir.path().join("config.db");
    let _store = open_store(&db_path).await;

    let args = vec![
        "run".to_string(),
        "task".to_string(),
        "--workspace".to_string(),
        dir.path().to_str().unwrap().to_string(),
        "--config".to_string(),
        db_path.to_str().unwrap().to_string(),
        "--timeout".to_string(),
        "30s".to_string(),
        "--format".to_string(),
        "json".to_string(),
    ];
    let env = vec![];
    let (code, stdout, _stderr) = run_cli(&args, &mut env.clone()).await;
    assert_eq!(code, EXIT_PROVIDER_INIT);

    let v: serde_json::Value = serde_json::from_str(stdout.trim()).unwrap();

    assert!(v.get("schema_version").is_some(), "missing schema_version");
    assert!(v.get("run_id").is_some(), "missing run_id");
    assert!(v.get("task_id").is_some(), "missing task_id");
    assert!(v.get("task_name").is_some(), "missing task_name");
    assert!(v.get("status").is_some(), "missing status");
    assert!(v.get("output").is_some(), "missing output");
    assert!(
        v.get("expected_outcome").is_some(),
        "missing expected_outcome"
    );
    assert!(v.get("profile_id").is_some(), "missing profile_id");
    assert!(v.get("workspace").is_some(), "missing workspace");
    assert!(v.get("started_at").is_some(), "missing started_at");
    assert!(v.get("ended_at").is_some(), "missing ended_at");
    assert!(v.get("duration_ms").is_some(), "missing duration_ms");

    assert!(v.get("error").is_some(), "missing error");
    assert!(
        v["error"].get("category").is_some(),
        "missing error.category"
    );
    assert!(v["error"].get("message").is_some(), "missing error.message");

    assert_eq!(v["status"], "failed");
    assert_eq!(v["error"]["category"], "provider_init");
}

#[tokio::test]
async fn json_failure_for_usage_error() {
    let dir = tempfile::tempdir().unwrap();

    let args = vec![
        "run".to_string(),
        "task".to_string(),
        "--workspace".to_string(),
        dir.path().to_str().unwrap().to_string(),
        "--format".to_string(),
        "json".to_string(),
    ];
    let env = vec![];
    let (code, stdout, _stderr) = run_cli(&args, &mut env.clone()).await;
    assert_eq!(code, EXIT_USAGE);

    let v: serde_json::Value = serde_json::from_str(stdout.trim()).unwrap();
    assert_eq!(v["status"], "failed");
    assert_eq!(v["error"]["category"], "config");
}

#[tokio::test]
async fn json_failure_for_pre_parse_usage_error() {
    // Missing task-id with --format json: parse_args fails before format
    // resolution, but the JSON output contract must still be honored.
    let args = vec![
        "run".to_string(),
        "--format".to_string(),
        "json".to_string(),
    ];
    let env = vec![];
    let (code, stdout, stderr) = run_cli(&args, &mut env.clone()).await;
    assert_eq!(code, EXIT_USAGE);

    let v: serde_json::Value = serde_json::from_str(stdout.trim())
        .expect("stdout should be a JSON object even for pre-parse usage errors");
    assert_eq!(v["status"], "failed");
    assert_eq!(v["error"]["category"], "config");
    assert!(
        v["error"]["message"]
            .as_str()
            .unwrap()
            .contains("missing required <task-id>"),
        "message should describe the usage error: {:?}",
        v["error"]["message"]
    );
    assert!(stderr.is_empty(), "stderr should be empty in JSON mode");
}

#[tokio::test]
async fn json_failure_for_pre_parse_usage_error_via_env() {
    // AGENTIRON_FORMAT=json in env should also trigger JSON for usage errors.
    let args = vec!["run".to_string()];
    let env = vec![("AGENTIRON_FORMAT".to_string(), "json".to_string())];
    let (code, stdout, stderr) = run_cli(&args, &mut env.clone()).await;
    assert_eq!(code, EXIT_USAGE);

    let v: serde_json::Value =
        serde_json::from_str(stdout.trim()).expect("stdout should be JSON via env override");
    assert_eq!(v["status"], "failed");
    assert!(stderr.is_empty(), "stderr should be empty in JSON mode");
}

#[tokio::test]
async fn json_failure_for_config_error() {
    let args = vec![
        "run".to_string(),
        "task".to_string(),
        "--workspace".to_string(),
        "/nonexistent/path".to_string(),
        "--timeout".to_string(),
        "30s".to_string(),
        "--format".to_string(),
        "json".to_string(),
    ];
    let env = vec![];
    let (code, stdout, _stderr) = run_cli(&args, &mut env.clone()).await;
    assert_eq!(code, EXIT_CONFIG);

    let v: serde_json::Value = serde_json::from_str(stdout.trim()).unwrap();
    assert_eq!(v["status"], "failed");
    assert_eq!(v["error"]["category"], "config");
}

#[tokio::test]
async fn json_failure_for_unsafe_policy() {
    let _env_guard = set_encryption_key().await;
    let dir = tempfile::tempdir().unwrap();
    let db_path = dir.path().join("config.db");
    let store = open_store(&db_path).await;
    seed_credential_and_model(&store, "openai", "gpt-4o", "sk-test").await;
    seed_pertool_profile(&store).await;
    seed_prompt_and_task(&store, "automation").await;

    let args = vec![
        "run".to_string(),
        "daily-report".to_string(),
        "--workspace".to_string(),
        dir.path().to_str().unwrap().to_string(),
        "--config".to_string(),
        db_path.to_str().unwrap().to_string(),
        "--timeout".to_string(),
        "30s".to_string(),
        "--format".to_string(),
        "json".to_string(),
    ];
    let env = vec![];
    let (code, stdout, _stderr) = run_cli(&args, &mut env.clone()).await;
    assert_eq!(code, EXIT_UNSAFE_POLICY);

    let v: serde_json::Value = serde_json::from_str(stdout.trim()).unwrap();
    assert_eq!(v["status"], "failed");
    assert_eq!(v["error"]["category"], "unsafe_policy");
}

#[tokio::test]
async fn text_failure_for_unsafe_policy() {
    let _env_guard = set_encryption_key().await;
    let dir = tempfile::tempdir().unwrap();
    let db_path = dir.path().join("config.db");
    let store = open_store(&db_path).await;
    seed_credential_and_model(&store, "openai", "gpt-4o", "sk-test").await;
    seed_pertool_profile(&store).await;
    seed_prompt_and_task(&store, "automation").await;

    let args = run_args(
        "daily-report",
        dir.path().to_str().unwrap(),
        db_path.to_str().unwrap(),
        "30s",
    );
    let env = vec![];
    let (code, _stdout, stderr) = run_cli(&args, &mut env.clone()).await;
    assert_eq!(code, EXIT_UNSAFE_POLICY);
    assert!(!stderr.is_empty());
}

// ============================================================================
// 6.4 End-to-end bootstrap and preflight tests
// ============================================================================

#[tokio::test]
async fn e2e_missing_default_provider_exit5() {
    let dir = tempfile::tempdir().unwrap();
    let db_path = dir.path().join("config.db");
    let _store = open_store(&db_path).await;

    let args = run_args(
        "daily-report",
        dir.path().to_str().unwrap(),
        db_path.to_str().unwrap(),
        "30s",
    );
    let env = vec![];
    let (code, _out, _err) = run_cli(&args, &mut env.clone()).await;
    assert_eq!(code, EXIT_PROVIDER_INIT);
}

#[tokio::test]
async fn e2e_missing_credential_exit5() {
    let dir = tempfile::tempdir().unwrap();
    let db_path = dir.path().join("config.db");
    let store = open_store(&db_path).await;
    store
        .set_default_model(&DefaultModelInput {
            provider_slug: "openai".to_string(),
            model_id: "gpt-4o".to_string(),
        })
        .await
        .unwrap();
    seed_auto_approve_profile(&store).await;
    seed_prompt_and_task(&store, "automation").await;

    let args = run_args(
        "daily-report",
        dir.path().to_str().unwrap(),
        db_path.to_str().unwrap(),
        "30s",
    );
    let env = vec![];
    let (code, _out, _err) = run_cli(&args, &mut env.clone()).await;
    assert_eq!(code, EXIT_PROVIDER_INIT);
}

#[tokio::test]
async fn e2e_task_not_found_exit3() {
    let _env_guard = set_encryption_key().await;
    let dir = tempfile::tempdir().unwrap();
    let db_path = dir.path().join("config.db");
    let store = open_store(&db_path).await;
    seed_complete_store(&store).await;

    let args = run_args(
        "nonexistent-task",
        dir.path().to_str().unwrap(),
        db_path.to_str().unwrap(),
        "30s",
    );
    let env = vec![];
    let (code, _out, _err) = run_cli(&args, &mut env.clone()).await;
    assert_eq!(code, EXIT_CONFIG);
}

#[tokio::test]
async fn e2e_auto_approve_preflight_blocks_pertool() {
    let _env_guard = set_encryption_key().await;
    let dir = tempfile::tempdir().unwrap();
    let db_path = dir.path().join("config.db");
    let store = open_store(&db_path).await;
    seed_credential_and_model(&store, "openai", "gpt-4o", "sk-test").await;
    seed_pertool_profile(&store).await;
    seed_prompt_and_task(&store, "automation").await;

    let args = run_args(
        "daily-report",
        dir.path().to_str().unwrap(),
        db_path.to_str().unwrap(),
        "30s",
    );
    let env = vec![];
    let (code, _out, _err) = run_cli(&args, &mut env.clone()).await;
    assert_eq!(code, EXIT_UNSAFE_POLICY);
}

#[tokio::test]
async fn e2e_plugin_tool_excluded_from_allow_list() {
    let _env_guard = set_encryption_key().await;
    let dir = tempfile::tempdir().unwrap();
    let db_path = dir.path().join("config.db");
    let store = open_store(&db_path).await;
    seed_credential_and_model(&store, "openai", "gpt-4o", "sk-test").await;

    let profile = AgentProfile {
        name: "automation".to_string(),
        provider: AgentProfileProvider::RuntimeDefault,
        tools: ToolFilter::Allow(vec!["plugin_my_plugin_some_tool".to_string()]),
        skills: SkillFilter::Inherit,
        approval: AgentApproval::AutoApprove,
        identity_prompt: None,
    };
    store
        .set_profile(&ProfileInput {
            id: "automation".to_string(),
            schema_version: PROFILE_SCHEMA_VERSION,
            payload: serde_json::to_value(&profile).unwrap(),
        })
        .await
        .unwrap();
    seed_prompt_and_task(&store, "automation").await;

    let args = run_args(
        "daily-report",
        dir.path().to_str().unwrap(),
        db_path.to_str().unwrap(),
        "30s",
    );
    let env = vec![];
    let (code, _out, _err) = run_cli(&args, &mut env.clone()).await;
    assert_eq!(code, EXIT_UNSAFE_POLICY);
}

#[tokio::test]
async fn e2e_uses_saved_default_provider_and_model() {
    let _env_guard = set_encryption_key().await;
    let dir = tempfile::tempdir().unwrap();
    let db_path = dir.path().join("config.db");
    let store = open_store(&db_path).await;

    seed_credential_and_model(&store, "openai", "gpt-4o-mini", "sk-test-key").await;
    seed_auto_approve_profile(&store).await;
    seed_prompt_and_task(&store, "automation").await;

    let args = vec![
        "run".to_string(),
        "daily-report".to_string(),
        "--workspace".to_string(),
        dir.path().to_str().unwrap().to_string(),
        "--config".to_string(),
        db_path.to_str().unwrap().to_string(),
        "--timeout".to_string(),
        "5s".to_string(),
        "--format".to_string(),
        "json".to_string(),
    ];
    let env = vec![];
    let (code, stdout, _stderr) = run_cli(&args, &mut env.clone()).await;

    let v: serde_json::Value = serde_json::from_str(stdout.trim()).unwrap();
    assert_eq!(v["status"], "completed");
    assert_eq!(v["provider"], "openai");
    assert_eq!(v["model"], "gpt-4o-mini");
    assert_eq!(code, EXIT_COMPLETED);
}

#[tokio::test]
async fn e2e_json_success_contract() {
    let _env_guard = set_encryption_key().await;
    let dir = tempfile::tempdir().unwrap();
    let db_path = dir.path().join("config.db");
    let store = open_store(&db_path).await;
    seed_credential_and_model(&store, "openai", "gpt-4o", "sk-test").await;
    seed_auto_approve_profile(&store).await;
    seed_prompt_and_task(&store, "automation").await;

    let args = vec![
        "run".to_string(),
        "daily-report".to_string(),
        "--workspace".to_string(),
        dir.path().to_str().unwrap().to_string(),
        "--config".to_string(),
        db_path.to_str().unwrap().to_string(),
        "--timeout".to_string(),
        "5s".to_string(),
        "--format".to_string(),
        "json".to_string(),
    ];
    let env = vec![];
    let (code, stdout, stderr) = run_cli(&args, &mut env.clone()).await;
    assert_eq!(code, EXIT_COMPLETED);

    let v: serde_json::Value =
        serde_json::from_str(stdout.trim()).expect("stdout should be valid JSON");

    assert_eq!(v["schema_version"], 1);
    assert!(v["run_id"].is_string());
    assert_eq!(v["task_id"], "daily-report");
    assert_eq!(v["task_name"], "Daily Report");
    assert_eq!(v["status"], "completed");
    assert!(v["output"].is_string());
    assert_eq!(v["expected_outcome"], "A summary of today's activity");
    assert!(v["profile_id"].is_string());
    assert_eq!(v["provider"], "openai");
    assert_eq!(v["model"], "gpt-4o");
    assert!(v["workspace"].is_string());
    assert!(v["effective_tools"].is_array());
    assert!(v["started_at"].is_string());
    assert!(v["ended_at"].is_string());
    assert!(v["duration_ms"].is_number());
    assert!(v["error"].is_null());

    let _single: serde_json::Value =
        serde_json::from_str(stdout.trim()).expect("exactly one JSON object");
    let _ = stderr;
}

#[tokio::test]
async fn e2e_text_mode_output_is_final_text_only() {
    let _env_guard = set_encryption_key().await;
    let dir = tempfile::tempdir().unwrap();
    let db_path = dir.path().join("config.db");
    let store = open_store(&db_path).await;
    seed_credential_and_model(&store, "openai", "gpt-4o", "sk-test").await;
    seed_auto_approve_profile(&store).await;
    seed_prompt_and_task(&store, "automation").await;

    let args = vec![
        "run".to_string(),
        "daily-report".to_string(),
        "--workspace".to_string(),
        dir.path().to_str().unwrap().to_string(),
        "--config".to_string(),
        db_path.to_str().unwrap().to_string(),
        "--timeout".to_string(),
        "5s".to_string(),
        "--quiet".to_string(),
    ];
    let env = vec![];
    let (code, stdout, stderr) = run_cli(&args, &mut env.clone()).await;
    assert_eq!(code, EXIT_COMPLETED);

    assert!(stderr.is_empty(), "stderr should be empty in quiet mode");
    assert!(
        !stdout.trim().starts_with('{'),
        "text mode should not output JSON"
    );
}