story-kit: merge 267_story_mcp_update_story_tool_should_support_front_matter_fields

This commit is contained in:
Dave
2026-03-17 17:33:30 +00:00
parent 1f8ffee38e
commit f72666b39e
3 changed files with 102 additions and 10 deletions

View File

@@ -1,6 +1,6 @@
use crate::agents::AgentStatus;
use crate::http::context::AppContext;
use crate::io::story_metadata::{parse_front_matter, write_coverage_baseline};
use crate::io::story_metadata::{parse_front_matter, set_front_matter_field, write_coverage_baseline};
use crate::workflow::{StoryTestResults, TestCaseResult, TestStatus};
use serde::Serialize;
use std::collections::HashMap;
@@ -706,10 +706,13 @@ pub fn update_story_in_file(
story_id: &str,
user_story: Option<&str>,
description: Option<&str>,
front_matter: Option<&HashMap<String, String>>,
) -> Result<(), String> {
if user_story.is_none() && description.is_none() {
let has_front_matter_updates = front_matter.map(|m| !m.is_empty()).unwrap_or(false);
if user_story.is_none() && description.is_none() && !has_front_matter_updates {
return Err(
"At least one of 'user_story' or 'description' must be provided.".to_string(),
"At least one of 'user_story', 'description', or 'front_matter' must be provided."
.to_string(),
);
}
@@ -717,6 +720,13 @@ pub fn update_story_in_file(
let mut contents = fs::read_to_string(&filepath)
.map_err(|e| format!("Failed to read story file: {e}"))?;
if let Some(fields) = front_matter {
for (key, value) in fields {
let yaml_value = format!("\"{}\"", value.replace('"', "\\\"").replace('\n', " ").replace('\r', ""));
contents = set_front_matter_field(&contents, key, &yaml_value);
}
}
if let Some(us) = user_story {
contents = replace_section_content(&contents, "User Story", us)?;
}
@@ -1597,7 +1607,7 @@ mod tests {
let content = "---\nname: T\n---\n\n## User Story\n\nOld text\n\n## Acceptance Criteria\n\n- [ ] AC\n";
fs::write(&filepath, content).unwrap();
update_story_in_file(tmp.path(), "20_test", Some("New user story text"), None).unwrap();
update_story_in_file(tmp.path(), "20_test", Some("New user story text"), None, None).unwrap();
let result = fs::read_to_string(&filepath).unwrap();
assert!(result.contains("New user story text"), "new text should be present");
@@ -1614,7 +1624,7 @@ mod tests {
let content = "---\nname: T\n---\n\n## Description\n\nOld description\n\n## Acceptance Criteria\n\n- [ ] AC\n";
fs::write(&filepath, content).unwrap();
update_story_in_file(tmp.path(), "21_test", None, Some("New description")).unwrap();
update_story_in_file(tmp.path(), "21_test", None, Some("New description"), None).unwrap();
let result = fs::read_to_string(&filepath).unwrap();
assert!(result.contains("New description"), "new description present");
@@ -1628,7 +1638,7 @@ mod tests {
fs::create_dir_all(&current).unwrap();
fs::write(current.join("22_test.md"), "---\nname: T\n---\n").unwrap();
let result = update_story_in_file(tmp.path(), "22_test", None, None);
let result = update_story_in_file(tmp.path(), "22_test", None, None, None);
assert!(result.is_err());
assert!(result.unwrap_err().contains("At least one"));
}
@@ -1644,11 +1654,65 @@ mod tests {
)
.unwrap();
let result = update_story_in_file(tmp.path(), "23_test", Some("new text"), None);
let result = update_story_in_file(tmp.path(), "23_test", Some("new text"), None, None);
assert!(result.is_err());
assert!(result.unwrap_err().contains("User Story"));
}
#[test]
fn update_story_sets_agent_front_matter_field() {
let tmp = tempfile::tempdir().unwrap();
let current = tmp.path().join(".story_kit/work/2_current");
fs::create_dir_all(&current).unwrap();
let filepath = current.join("24_test.md");
fs::write(&filepath, "---\nname: T\n---\n\n## User Story\n\nSome story\n").unwrap();
let mut fields = HashMap::new();
fields.insert("agent".to_string(), "dev".to_string());
update_story_in_file(tmp.path(), "24_test", None, None, Some(&fields)).unwrap();
let result = fs::read_to_string(&filepath).unwrap();
assert!(result.contains("agent: \"dev\""), "agent field should be set");
assert!(result.contains("name: T"), "name field preserved");
}
#[test]
fn update_story_sets_arbitrary_front_matter_fields() {
let tmp = tempfile::tempdir().unwrap();
let current = tmp.path().join(".story_kit/work/2_current");
fs::create_dir_all(&current).unwrap();
let filepath = current.join("25_test.md");
fs::write(&filepath, "---\nname: T\n---\n\n## User Story\n\nSome story\n").unwrap();
let mut fields = HashMap::new();
fields.insert("manual_qa".to_string(), "true".to_string());
fields.insert("priority".to_string(), "high".to_string());
update_story_in_file(tmp.path(), "25_test", None, None, Some(&fields)).unwrap();
let result = fs::read_to_string(&filepath).unwrap();
assert!(result.contains("manual_qa: \"true\""), "manual_qa field should be set");
assert!(result.contains("priority: \"high\""), "priority field should be set");
assert!(result.contains("name: T"), "name field preserved");
}
#[test]
fn update_story_front_matter_only_no_section_required() {
let tmp = tempfile::tempdir().unwrap();
let current = tmp.path().join(".story_kit/work/2_current");
fs::create_dir_all(&current).unwrap();
// File without a User Story section — front matter update should succeed
let filepath = current.join("26_test.md");
fs::write(&filepath, "---\nname: T\n---\n\nNo sections here.\n").unwrap();
let mut fields = HashMap::new();
fields.insert("agent".to_string(), "dev".to_string());
let result = update_story_in_file(tmp.path(), "26_test", None, None, Some(&fields));
assert!(result.is_ok(), "front-matter-only update should not require body sections");
let contents = fs::read_to_string(&filepath).unwrap();
assert!(contents.contains("agent: \"dev\""));
}
// ── Bug file helper tests ──────────────────────────────────────────────────
#[test]