mod support;

use support::TestWorkspace;

#[test]
fn verify_reports_requests_and_required_inputs() {
    let workspace = TestWorkspace::new();
    workspace.write_file(
        "fragments/imported.hen",
        r#"Imported request

POST https://example.com/imported/[[ token ]]
"#,
    );
    workspace.write_file(
        "collection.hen",
        r#"Verify Fixture

Verifies imported requests.

? region = [[ region = us-east-1 ]]

---

Root request

GET https://example.com/root

---

<< fragments/imported.hen
"#,
    );

    let output = workspace.run_hen(["verify", "collection.hen"]);

    assert_eq!(output.status_code, 0, "stderr: {}", output.stderr);
    assert!(
        output.stdout.contains("Verification passed"),
        "stdout: {}",
        output.stdout
    );
    assert!(
        output.stdout.contains("[0] GET https://example.com/root"),
        "stdout: {}",
        output.stdout
    );
    assert!(
        output
            .stdout
            .contains("[1] POST https://example.com/imported/[[ token ]]"),
        "stdout: {}",
        output.stdout
    );
    assert!(
        output.stdout.contains("Required inputs"),
        "stdout: {}",
        output.stdout
    );
    assert!(output.stdout.contains("token"), "stdout: {}", output.stdout);
    assert!(
        output.stdout.contains("region (default: us-east-1)"),
        "stdout: {}",
        output.stdout
    );
    assert!(output.stderr.is_empty(), "stderr: {}", output.stderr);
}

#[test]
fn verify_does_not_execute_shell_substitutions() {
    let workspace = TestWorkspace::new();
    workspace.write_file(
        "collection.hen",
        r#"Shell Verify

$ shell_value = $(touch side-effect.txt)

---

Shell request

GET https://example.com/{{ shell_value }}
"#,
    );

    let output = workspace.run_hen(["verify", "collection.hen"]);

    assert_eq!(output.status_code, 0, "stderr: {}", output.stderr);
    assert!(
        !workspace.root().join("side-effect.txt").exists(),
        "verify executed a shell substitution"
    );
}

#[test]
fn verify_reports_parse_failures_without_running_requests() {
    let workspace = TestWorkspace::new();
    workspace.write_file(
        "collection.hen",
        r#"Broken Verify

---

Broken assertion

GET https://example.com

^ & body =~ /foo/
"#,
    );

    let output = workspace.run_hen(["verify", "collection.hen"]);

    assert_eq!(output.status_code, 1, "stderr: {}", output.stderr);
    assert!(output.stdout.is_empty(), "stdout: {}", output.stdout);
    assert!(output
        .stderr
        .contains("No valid operator found in assertion"));
}
