//! e2e: `sigil show events` snapshot mode picks up real file_change events
//! generated by a running `TestAgent`. Unix-only + feature-gated (follow-mode
//! is covered by unit tests; this verifies the read-side wiring with a real
//! agent on the write side).
#![cfg(all(unix, feature = "operator-cli"))]

mod common;
use common::{policy_for_paths, TestAgentBuilder};

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn snapshot_mode_reads_real_file_change_events() {
    let dir = tempfile::tempdir().unwrap();
    let watched = dir.path().join("a.json");
    let policy = policy_for_paths(&[watched.to_str().unwrap()], "standard");
    let agent = TestAgentBuilder::new().policy(&policy).start().await;

    // Generate file_change events.
    std::fs::write(&watched, b"x1").unwrap();
    let ev = agent
        .wait_for_event(
            |v| v["evidence"]["kind"] == "file_change",
            common::fs_event_timeout(),
        )
        .await
        .expect("agent emitted no file_change in time");
    assert_eq!(ev["schema_version"], 1);

    // Snapshot-mode read from the same events_dir. Use the test-only entry
    // point so we don't need to spawn the binary.
    let mut buf = Vec::new();
    let code = sigil_agent::show::show_events_for_test(&agent.events_dir, 10, false, &mut buf)
        .expect("show_events_for_test should not error");
    assert_eq!(code, 0);
    let out = String::from_utf8(buf).unwrap();
    assert!(
        out.lines().any(|l| l.contains("\"file_change\"")),
        "expected file_change line in snapshot output:\n{out}"
    );

    // And `--pretty` too.
    let mut pbuf = Vec::new();
    let pcode = sigil_agent::show::show_events_for_test(&agent.events_dir, 10, true, &mut pbuf)
        .expect("pretty mode should not error");
    assert_eq!(pcode, 0);
    let pout = String::from_utf8(pbuf).unwrap();
    assert!(
        pout.lines().any(|l| l.contains("\tfile_change\t")),
        "expected pretty file_change row in:\n{pout}"
    );

    agent.join.abort();
}
