//! Shared test utilities for chat handler tests.
//!
//! Import with `use crate::chat::test_helpers::write_story_file;`

use std::path::Path;

/// Write a work-item into the content store and CRDT for testing.
///
/// Also creates the filesystem directory structure and file so that tests
/// which still verify filesystem state (e.g. assign tests that check the
/// physical file) continue to work.
///
/// Story 929: callers pass the typed `name` explicitly — `content` is now
/// just the markdown body and is no longer parsed for metadata.  Other
/// typed fields (depends_on, agent, qa_mode, blocked, …) should be set
/// via the CRDT typed setters after this call.
pub(crate) fn write_story_file(
    root: &Path,
    stage: &str,
    filename: &str,
    content: &str,
    name: Option<&str>,
) {
    let dir = root.join(".huskies/work").join(stage);
    std::fs::create_dir_all(&dir).unwrap();
    std::fs::write(dir.join(filename), content).unwrap();

    let story_id = filename.trim_end_matches(".md");
    crate::db::ensure_content_store();
    let meta = crate::db::ItemMeta {
        name: name.map(str::to_string),
        ..Default::default()
    };
    crate::db::write_item_with_content(story_id, stage, content, meta);
}
