// tests/protocol_tests.rs
use cryochamber::protocol;

#[test]
fn test_protocol_content_contains_commands() {
    let content = protocol::PROTOCOL_CONTENT;
    assert!(content.contains("cryo-agent hibernate"));
    assert!(content.contains("cryo-agent send"));
    assert!(content.contains("cryo-agent receive"));
    assert!(content.contains("NOTES.md"));
    let removed_note_command = ["cryo-agent", "note"].join(" ");
    assert!(!content.contains(&removed_note_command));
    // Phantom commands removed (code review #3)
    assert!(!content.contains("cryo-agent status"));
    assert!(!content.contains("cryo-agent inbox"));
    // Deprecated dead-man switch command removed.
    assert!(!content.contains("cryo-agent alert"));
}

#[test]
fn test_protocol_content_contains_rules() {
    let content = protocol::PROTOCOL_CONTENT;
    assert!(content.contains("cryo-agent hibernate"));
    assert!(content.contains("plan.md"));
}

#[test]
fn test_protocol_user_channel_is_cryo_agent_only() {
    let content = protocol::PROTOCOL_CONTENT;
    assert!(content.contains("only supported way to communicate with the human"));
    assert!(content.contains("stdout/stderr"));
    assert!(content.contains("cryo-agent send"));
    assert!(!content.contains("cryo-agent reply"));
}

#[test]
fn test_protocol_requires_question_flag_for_feedback_requests() {
    let content = protocol::PROTOCOL_CONTENT;
    assert!(content.contains("MUST use `cryo-agent send --question \"<message>\"`"));
    assert!(content.contains("cryo-agent send --question \"what should I do?\"  #"));
}

#[test]
fn test_protocol_requires_final_todo_hygiene_check() {
    let content = protocol::PROTOCOL_CONTENT;
    assert!(content.contains("Before hibernating, confirm the TODO list is proper"));
    assert!(content.contains("at least one pending TODO with a valid `--at` time"));
    assert!(content.contains("Stale, duplicate, or superseded pending TODOs"));
}

#[test]
fn test_protocol_no_blocked_hibernate_mode() {
    let content = protocol::PROTOCOL_CONTENT;
    assert!(!content.contains("Blocked or failed"));
    assert!(!content.contains("Blocked on X"));
}

#[test]
fn test_write_template_plan_creates_new() {
    let dir = tempfile::tempdir().unwrap();
    let wrote = protocol::write_template_plan(dir.path()).unwrap();
    assert!(wrote);
    let path = dir.path().join("plan.md");
    assert!(path.exists());
    let content = std::fs::read_to_string(&path).unwrap();
    assert!(content.contains("# Interstellar Traveler"));
    assert!(content.contains("cryo-agent dialog"));
}

#[test]
fn test_write_template_plan_skips_existing() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("plan.md");
    std::fs::write(&path, "existing plan").unwrap();
    let wrote = protocol::write_template_plan(dir.path()).unwrap();
    assert!(!wrote);
    let content = std::fs::read_to_string(&path).unwrap();
    assert_eq!(content, "existing plan");
}

#[test]
fn test_protocol_mentions_hibernate() {
    let content = cryochamber::protocol::PROTOCOL_CONTENT;
    assert!(content.contains("cryo-agent hibernate"));
    assert!(content.contains("NOTES.md is your memory"));
    let removed_note_command = ["cryo-agent", "note"].join(" ");
    assert!(!content.contains(&removed_note_command));
    // No stale cryo status/inbox references
    assert!(!content.contains("cryo status"));
    assert!(!content.contains("cryo inbox"));
}

#[test]
fn test_protocol_no_old_markers() {
    let content = cryochamber::protocol::PROTOCOL_CONTENT;
    assert!(!content.contains("[CRYO:EXIT"));
    assert!(!content.contains("[CRYO:WAKE"));
}
