huskies: merge 942
This commit is contained in:
@@ -1,80 +1,71 @@
|
||||
//! create_story_file: write new story to CRDT/content store.
|
||||
|
||||
#[allow(unused_imports)]
|
||||
use super::super::{
|
||||
create_section_content, next_item_number, read_story_content, replace_section_content,
|
||||
slugify_name, story_stage, write_story_content,
|
||||
};
|
||||
use super::super::create_item_in_backlog;
|
||||
|
||||
/// Write a new story file to the CRDT content store and return the generated story ID.
|
||||
///
|
||||
/// Routes through `create_item_in_backlog`, the single internal creation path.
|
||||
/// Validates non-empty title and ≥ 1 acceptance criterion before writing anything.
|
||||
pub fn create_story_file(
|
||||
root: &std::path::Path,
|
||||
name: &str,
|
||||
user_story: Option<&str>,
|
||||
description: Option<&str>,
|
||||
acceptance_criteria: Option<&[String]>,
|
||||
acceptance_criteria: &[String],
|
||||
depends_on: Option<&[u32]>,
|
||||
_commit: bool,
|
||||
) -> Result<String, String> {
|
||||
let story_number = next_item_number(root)?;
|
||||
let slug = slugify_name(name);
|
||||
let name_owned = name.to_string();
|
||||
let user_story_owned = user_story.map(str::to_string);
|
||||
let description_owned = description.map(str::to_string);
|
||||
let depends_on_owned: Option<Vec<u32>> = depends_on.map(<[u32]>::to_vec);
|
||||
let acs_owned: Vec<String> = acceptance_criteria.to_vec();
|
||||
|
||||
if slug.is_empty() {
|
||||
return Err("Name must contain at least one alphanumeric character.".to_string());
|
||||
}
|
||||
create_item_in_backlog(
|
||||
root,
|
||||
"story",
|
||||
name,
|
||||
acceptance_criteria,
|
||||
depends_on,
|
||||
move |story_number| {
|
||||
let mut content = String::new();
|
||||
content.push_str("---\n");
|
||||
content.push_str("type: story\n");
|
||||
content.push_str(&format!("name: \"{}\"\n", name_owned.replace('"', "\\\"")));
|
||||
if let Some(ref deps) = depends_on_owned.filter(|d| !d.is_empty()) {
|
||||
let nums: Vec<String> = deps.iter().map(|n| n.to_string()).collect();
|
||||
content.push_str(&format!("depends_on: [{}]\n", nums.join(", ")));
|
||||
}
|
||||
content.push_str("---\n\n");
|
||||
content.push_str(&format!("# Story {story_number}: {name_owned}\n\n"));
|
||||
|
||||
let story_id = format!("{story_number}");
|
||||
content.push_str("## User Story\n\n");
|
||||
if let Some(ref us) = user_story_owned {
|
||||
content.push_str(us);
|
||||
content.push('\n');
|
||||
} else {
|
||||
content.push_str("As a ..., I want ..., so that ...\n");
|
||||
}
|
||||
content.push('\n');
|
||||
|
||||
let mut content = String::new();
|
||||
content.push_str("---\n");
|
||||
content.push_str("type: story\n");
|
||||
content.push_str(&format!("name: \"{}\"\n", name.replace('"', "\\\"")));
|
||||
if let Some(deps) = depends_on.filter(|d| !d.is_empty()) {
|
||||
let nums: Vec<String> = deps.iter().map(|n| n.to_string()).collect();
|
||||
content.push_str(&format!("depends_on: [{}]\n", nums.join(", ")));
|
||||
}
|
||||
content.push_str("---\n\n");
|
||||
content.push_str(&format!("# Story {story_number}: {name}\n\n"));
|
||||
if let Some(ref desc) = description_owned {
|
||||
content.push_str("## Description\n\n");
|
||||
content.push_str(desc);
|
||||
content.push('\n');
|
||||
content.push('\n');
|
||||
}
|
||||
|
||||
content.push_str("## User Story\n\n");
|
||||
if let Some(us) = user_story {
|
||||
content.push_str(us);
|
||||
content.push('\n');
|
||||
} else {
|
||||
content.push_str("As a ..., I want ..., so that ...\n");
|
||||
}
|
||||
content.push('\n');
|
||||
content.push_str("## Acceptance Criteria\n\n");
|
||||
for criterion in &acs_owned {
|
||||
content.push_str(&format!("- [ ] {criterion}\n"));
|
||||
}
|
||||
content.push('\n');
|
||||
|
||||
if let Some(desc) = description {
|
||||
content.push_str("## Description\n\n");
|
||||
content.push_str(desc);
|
||||
content.push('\n');
|
||||
content.push('\n');
|
||||
}
|
||||
|
||||
content.push_str("## Acceptance Criteria\n\n");
|
||||
if let Some(criteria) = acceptance_criteria {
|
||||
for criterion in criteria {
|
||||
content.push_str(&format!("- [ ] {criterion}\n"));
|
||||
}
|
||||
} else {
|
||||
content.push_str("- [ ] TODO\n");
|
||||
}
|
||||
content.push('\n');
|
||||
|
||||
content.push_str("## Out of Scope\n\n");
|
||||
content.push_str("- TBD\n");
|
||||
|
||||
// Write to database content store and CRDT.
|
||||
write_story_content(root, &story_id, "1_backlog", &content, Some(name));
|
||||
|
||||
// Sync depends_on to the typed CRDT register.
|
||||
crate::crdt_state::set_depends_on(&story_id, depends_on.unwrap_or(&[]));
|
||||
|
||||
// Story 933: typed CRDT register for item_type.
|
||||
crate::crdt_state::set_item_type(&story_id, Some("story"));
|
||||
|
||||
Ok(story_id)
|
||||
content.push_str("## Out of Scope\n\n");
|
||||
content.push_str("- TBD\n");
|
||||
content
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
/// Check off the Nth unchecked acceptance criterion in a story.
|
||||
@@ -187,7 +178,8 @@ mod tests {
|
||||
crate::crdt_state::init_for_test();
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let name = "Server-owned agent completion: remove report_completion dependency";
|
||||
let result = create_story_file(tmp.path(), name, None, None, None, None, false);
|
||||
let acs = vec!["Completion handled server-side".to_string()];
|
||||
let result = create_story_file(tmp.path(), name, None, None, &acs, None, false);
|
||||
assert!(result.is_ok(), "create_story_file failed: {result:?}");
|
||||
|
||||
let story_id = result.unwrap();
|
||||
@@ -202,12 +194,13 @@ mod tests {
|
||||
fn create_story_with_depends_on_persists_to_crdt() {
|
||||
crate::crdt_state::init_for_test();
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let acs = vec!["Dependent criterion".to_string()];
|
||||
let story_id = create_story_file(
|
||||
tmp.path(),
|
||||
"Dependent Story",
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
&acs,
|
||||
Some(&[489]),
|
||||
false,
|
||||
)
|
||||
@@ -224,7 +217,8 @@ mod tests {
|
||||
fn create_story_file_returns_numeric_only_id() {
|
||||
crate::db::ensure_content_store();
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let result = create_story_file(tmp.path(), "My Feature", None, None, None, None, false);
|
||||
let acs = vec!["Feature works".to_string()];
|
||||
let result = create_story_file(tmp.path(), "My Feature", None, None, &acs, None, false);
|
||||
assert!(
|
||||
result.is_ok(),
|
||||
"create_story_file should succeed: {result:?}"
|
||||
@@ -241,8 +235,9 @@ mod tests {
|
||||
crate::crdt_state::init_for_test();
|
||||
crate::db::ensure_content_store();
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let acs = vec!["Type validated".to_string()];
|
||||
let story_id =
|
||||
create_story_file(tmp.path(), "Type Test Story", None, None, None, None, false)
|
||||
create_story_file(tmp.path(), "Type Test Story", None, None, &acs, None, false)
|
||||
.unwrap();
|
||||
let view = crate::crdt_state::read_item(&story_id).expect("CRDT entry must exist");
|
||||
assert_eq!(
|
||||
@@ -252,5 +247,38 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_story_file_rejects_empty_title() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let acs = vec!["Some criterion".to_string()];
|
||||
let err = create_story_file(tmp.path(), "", None, None, &acs, None, false).unwrap_err();
|
||||
assert!(
|
||||
err.contains("empty") || err.contains("whitespace"),
|
||||
"error should mention empty/whitespace, got: {err}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_story_file_rejects_whitespace_only_title() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let acs = vec!["Some criterion".to_string()];
|
||||
let err = create_story_file(tmp.path(), " ", None, None, &acs, None, false).unwrap_err();
|
||||
assert!(
|
||||
err.contains("empty") || err.contains("whitespace"),
|
||||
"error should mention empty/whitespace, got: {err}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_story_file_rejects_empty_acceptance_criteria() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let result = create_story_file(tmp.path(), "Valid Title", None, None, &[], None, false);
|
||||
assert!(result.is_err(), "empty ACs should be rejected");
|
||||
assert!(
|
||||
result.unwrap_err().contains("acceptance criterion"),
|
||||
"error should mention acceptance criterion"
|
||||
);
|
||||
}
|
||||
|
||||
// ── Story 504: native JSON types in front_matter ───────────────────────────
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user