use super::{MOUNT, Workspace};
use std::path::Path;

#[test]
fn links_under_the_mount_resolve_to_the_state_folder() {
    let ws = Workspace::open(Path::new("/p/my-app"));
    assert_eq!(
        ws.resolve(&format!("{MOUNT}/assets/a.png")),
        ws.state.join("assets/a.png")
    );
    assert_eq!(ws.resolve("docs/b.png"), Path::new("/p/my-app/docs/b.png"));
    assert_eq!(ws.resolve("/castle/c.png"), Path::new("/castle/c.png"));
}

#[test]
fn prompts_live_outside_the_project() {
    let dir = std::env::temp_dir().join(format!("cast-ws-test-{}", std::process::id()));
    std::fs::create_dir_all(&dir).unwrap();
    let mut ws = Workspace::open(&dir);
    let stem = ws.new_prompt();
    assert!(ws.prompt_path(&stem).starts_with(&ws.state));
    assert!(!ws.state.starts_with(&dir));
    assert_eq!(std::fs::read_dir(&dir).unwrap().count(), 0);
    assert_eq!(ws.prompts, vec![stem]);
    std::fs::remove_dir_all(&dir).unwrap();
}

#[test]
fn a_title_is_the_first_line_without_heading_marks() {
    use super::title::title;
    assert_eq!(title("\n## Fix login\nbody").as_deref(), Some("Fix login"));
    assert_eq!(
        title("# Turning this initial design into a fun game").as_deref(),
        Some("Turning this initial des…")
    );
    assert_eq!(title(" \n#\n"), None);
}
