//! Invariant: `envseal_import` with `delete_after: true` reports the actual
//! deletion result, not an unconditional echo of the parameter.
//!
//! When the operation fails before reaching the deletion step, `deleted_source`
//! must be `false`, not the value of `delete_after`. The previous bug echoed the
//! parameter value regardless of whether the file was actually removed.

#[tokio::test]
async fn import_delete_after_reports_false_when_source_absent() {
    let result = envseal_mcp::tools::import::execute(envseal_mcp::tools::import::ImportParams {
        env_file: "/tmp/envseal_test_nonexistent_abc123.env".to_string(),
        delete_after: true,
    })
    .await;

    // File doesn't exist → read must fail; we never reach deletion.
    // The execute must return Err (not Ok with deleted_source: true).
    if let Ok(val) = result {
        let deleted = val
            .get("deleted_source")
            .and_then(serde_json::Value::as_bool)
            .expect("response must contain boolean deleted_source");
        assert!(
            !deleted,
            "deleted_source must be false when source file was not actually removed"
        );
    }
    // Err is the expected path here: read fails → correct and safe.
}

#[tokio::test]
async fn import_delete_after_false_does_not_claim_deletion() {
    // With delete_after: false, deleted_source must always be false,
    // even if the operation fails at some later step.
    let result = envseal_mcp::tools::import::execute(envseal_mcp::tools::import::ImportParams {
        env_file: "/tmp/envseal_test_nonexistent_def456.env".to_string(),
        delete_after: false,
    })
    .await;

    if let Ok(val) = result {
        let deleted = val
            .get("deleted_source")
            .and_then(serde_json::Value::as_bool)
            .expect("response must contain boolean deleted_source");
        assert!(
            !deleted,
            "deleted_source must be false when delete_after is false"
        );
    }
}
