use assert_cmd::Command; use assert_fs::fixture::{PathChild, PathCreateDir}; use assert_fs::TempDir; use std::fs; use std::path::PathBuf; const BIN_NAME: &str = env!("CARGO_BIN_EXE_worklane"); fn setup_worklane(temp: &TempDir) -> PathBuf { let worklane = temp.child(".worklane"); worklane.create_dir_all().unwrap(); for dir in ["backlog", "committed", "reviewing", "done"] { worklane.child(dir).create_dir_all().unwrap(); } worklane.path().to_path_buf() } fn create_ticket( worklane_dir: &PathBuf, status: &str, title: &str, ) -> String { let uuid = uuid::Uuid::new_v4().to_string(); let slug = title .to_lowercase() .chars() .filter(|c| c.is_alphanumeric() || *c == ' ' || *c == '-' || *c == '_') .map(|c| if c == ' ' { '-' } else { c }) .collect::(); let filename = format!("{}_{}.md", uuid, slug); let content = format!( "# {}\n\ \n\ ## Metadata\n\ \n\ - **ID**: {}\n\ - **Status**: {}\n\ ", title, uuid, status ); fs::create_dir_all(worklane_dir.join(status)).unwrap(); fs::write(worklane_dir.join(status).join(&filename), content).unwrap(); uuid } #[test] fn comment_creates_file_with_valid_ticket_uuid() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = create_ticket(&worklane, "backlog", "Test Ticket"); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(&temp.path()) .arg("comment") .args([&uuid, "This is a test comment"]) .assert() .success(); let comments_dir = worklane.join("comments").join(&uuid); assert!(comments_dir.exists()); let files: Vec<_> = fs::read_dir(&comments_dir) .unwrap() .filter_map(|e| e.ok()) .collect(); assert_eq!(files.len(), 1); let content = fs::read_to_string(files[0].path()).unwrap(); assert!(content.contains("This is a test comment")); } #[test] fn comment_creates_file_with_valid_ticket_title() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane, "committed", "My Ticket"); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(&temp.path()) .arg("comment") .args(["My Ticket", "Testing title lookup"]) .assert() .success(); } #[test] fn comment_error_when_ticket_not_found() { let _temp = TempDir::new().unwrap(); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.arg("comment") .arg("Nonexistent") .arg("Some comment") .assert() .code(1); } #[test] fn comment_includes_git_user_and_body() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = create_ticket(&worklane, "backlog", "Comment Test"); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(&temp.path()) .arg("comment") .args([&uuid, "My comment body"]) .assert() .success(); let comments_dir = worklane.join("comments").join(&uuid); let files: Vec<_> = fs::read_dir(&comments_dir) .unwrap() .filter_map(|e| e.ok()) .collect(); let content = fs::read_to_string(files[0].path()).unwrap(); assert!(content.contains("My comment body")); assert!(content.contains("**Author**")); } #[test] fn comment_stored_in_timestamped_file() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = create_ticket(&worklane, "done", "Timestamp Test"); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(&temp.path()) .arg("comment") .args([&uuid, "Timestamp test"]) .assert() .success(); let comments_dir = worklane.join("comments").join(&uuid); let files: Vec<_> = fs::read_dir(&comments_dir) .unwrap() .filter_map(|e| e.ok()) .collect(); assert_eq!(files.len(), 1); let filename = files[0].file_name().into_string().unwrap(); assert!(filename.ends_with(".md")); } #[test] fn comment_creates_comments_directory() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = create_ticket(&worklane, "reviewing", "Dir Test"); let comments_dir = worklane.join("comments").join(&uuid); assert!(!comments_dir.exists()); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(&temp.path()) .arg("comment") .args([&uuid, "Create dir test"]) .assert() .success(); assert!(comments_dir.exists()); } #[test] fn comment_uses_owner_flag_when_provided() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = create_ticket(&worklane, "backlog", "Owner Test"); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(&temp.path()) .arg("comment") .args([&uuid, "Test comment with owner"]) .arg("-o") .arg("TestUser") .assert() .success(); let comments_dir = worklane.join("comments").join(&uuid); let files: Vec<_> = fs::read_dir(&comments_dir) .unwrap() .filter_map(|e| e.ok()) .collect(); let content = fs::read_to_string(files[0].path()).unwrap(); assert!(content.contains("**Author**: TestUser")); } #[test] fn comment_falls_back_to_git_user_when_owner_omitted() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = create_ticket(&worklane, "done", "Fallback Test"); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(&temp.path()) .arg("comment") .args([&uuid, "Test comment fallback"]) .assert() .success(); let comments_dir = worklane.join("comments").join(&uuid); let files: Vec<_> = fs::read_dir(&comments_dir) .unwrap() .filter_map(|e| e.ok()) .collect(); let content = fs::read_to_string(files[0].path()).unwrap(); assert!(content.contains("**Author**")); } /// Second-resolution filenames meant the second of two quick comments /// truncated the first. #[test] fn comments_added_back_to_back_are_both_kept() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = create_ticket(&worklane, "backlog", "Rapid Fire"); for body in ["first comment", "second comment", "third comment"] { Command::cargo_bin(BIN_NAME) .unwrap() .current_dir(&temp.path()) .arg("comment") .args([&uuid, body]) .assert() .success(); } let comments_dir = worklane.join("comments").join(&uuid); let files: Vec<_> = fs::read_dir(&comments_dir) .unwrap() .filter_map(|e| e.ok()) .collect(); assert_eq!(files.len(), 3, "a comment was overwritten"); // And show has to render them in the order they were added let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(&temp.path()).arg("show").arg(&uuid); let assert = cmd.assert().success(); let stdout = String::from_utf8(assert.get_output().stdout.clone()).unwrap(); let first = stdout.find("first comment").expect("first comment missing"); let second = stdout.find("second comment").expect("second comment missing"); let third = stdout.find("third comment").expect("third comment missing"); assert!(first < second && second < third, "comments out of order"); }