The great storkit name conversion

This commit is contained in:
Dave
2026-03-20 12:26:02 +00:00
parent 51d878e117
commit c4e45b2841
25 changed files with 1522 additions and 1333 deletions

View File

@@ -3,7 +3,7 @@ use crate::http::context::AppContext;
use crate::log_buffer;
use crate::slog;
use crate::slog_warn;
use serde_json::{json, Value};
use serde_json::{Value, json};
use std::fs;
pub(super) fn tool_get_server_logs(args: &Value) -> Result<String, String> {
@@ -29,7 +29,7 @@ pub(super) fn tool_get_server_logs(args: &Value) -> Result<String, String> {
/// Rebuild the server binary and re-exec.
///
/// 1. Gracefully stops all running agents (kills PTY children).
/// 2. Runs `cargo build [-p story-kit]` from the workspace root, matching
/// 2. Runs `cargo build [-p storkit]` from the workspace root, matching
/// the current build profile (debug or release).
/// 3. If the build fails, returns the build error (server stays up).
/// 4. If the build succeeds, re-execs the process with the new binary via
@@ -92,8 +92,8 @@ pub(super) async fn tool_rebuild_and_restart(ctx: &AppContext) -> Result<String,
// 4. Re-exec with the new binary.
// Collect current argv so we preserve any CLI arguments (e.g. project path).
let current_exe = std::env::current_exe()
.map_err(|e| format!("Cannot determine current executable: {e}"))?;
let current_exe =
std::env::current_exe().map_err(|e| format!("Cannot determine current executable: {e}"))?;
let args: Vec<String> = std::env::args().collect();
// Remove the port file before re-exec so the new process can write its own.
@@ -124,7 +124,7 @@ pub(super) async fn tool_rebuild_and_restart(ctx: &AppContext) -> Result<String,
///
/// - `Edit` / `Write` / `Read` / `Grep` / `Glob` etc. → just the tool name
/// - `Bash` → `Bash(first_word *)` derived from the `command` field in `tool_input`
/// - `mcp__*` → the full tool name (e.g. `mcp__story-kit__create_story`)
/// - `mcp__*` → the full tool name (e.g. `mcp__storkit__create_story`)
fn generate_permission_rule(tool_name: &str, tool_input: &Value) -> String {
if tool_name == "Bash" {
// Extract command from tool_input.command and use first word as prefix
@@ -142,7 +142,10 @@ fn generate_permission_rule(tool_name: &str, tool_input: &Value) -> String {
/// Add a permission rule to `.claude/settings.json` in the project root.
/// Does nothing if the rule already exists. Creates the file if missing.
pub(super) fn add_permission_rule(project_root: &std::path::Path, rule: &str) -> Result<(), String> {
pub(super) fn add_permission_rule(
project_root: &std::path::Path,
rule: &str,
) -> Result<(), String> {
let claude_dir = project_root.join(".claude");
fs::create_dir_all(&claude_dir)
.map_err(|e| format!("Failed to create .claude/ directory: {e}"))?;
@@ -151,8 +154,7 @@ pub(super) fn add_permission_rule(project_root: &std::path::Path, rule: &str) ->
let mut settings: Value = if settings_path.exists() {
let content = fs::read_to_string(&settings_path)
.map_err(|e| format!("Failed to read settings.json: {e}"))?;
serde_json::from_str(&content)
.map_err(|e| format!("Failed to parse settings.json: {e}"))?
serde_json::from_str(&content).map_err(|e| format!("Failed to parse settings.json: {e}"))?
} else {
json!({ "permissions": { "allow": [] } })
};
@@ -184,8 +186,8 @@ pub(super) fn add_permission_rule(project_root: &std::path::Path, rule: &str) ->
return Ok(());
}
// Also check for wildcard coverage: if "mcp__story-kit__*" exists, don't add
// a more specific "mcp__story-kit__create_story".
// Also check for wildcard coverage: if "mcp__storkit__*" exists, don't add
// a more specific "mcp__storkit__create_story".
let dominated = allow.iter().any(|existing| {
if let Some(pat) = existing.as_str()
&& let Some(prefix) = pat.strip_suffix('*')
@@ -202,8 +204,7 @@ pub(super) fn add_permission_rule(project_root: &std::path::Path, rule: &str) ->
let pretty =
serde_json::to_string_pretty(&settings).map_err(|e| format!("Failed to serialize: {e}"))?;
fs::write(&settings_path, pretty)
.map_err(|e| format!("Failed to write settings.json: {e}"))?;
fs::write(&settings_path, pretty).map_err(|e| format!("Failed to write settings.json: {e}"))?;
Ok(())
}
@@ -212,16 +213,16 @@ pub(super) fn add_permission_rule(project_root: &std::path::Path, rule: &str) ->
/// Forwards the permission request through the shared channel to the active
/// WebSocket session, which presents a dialog to the user. Blocks until the
/// user approves or denies (with a 5-minute timeout).
pub(super) async fn tool_prompt_permission(args: &Value, ctx: &AppContext) -> Result<String, String> {
pub(super) async fn tool_prompt_permission(
args: &Value,
ctx: &AppContext,
) -> Result<String, String> {
let tool_name = args
.get("tool_name")
.and_then(|v| v.as_str())
.unwrap_or("unknown")
.to_string();
let tool_input = args
.get("input")
.cloned()
.unwrap_or(json!({}));
let tool_input = args.get("input").cloned().unwrap_or(json!({}));
let request_id = uuid::Uuid::new_v4().to_string();
let (response_tx, response_rx) = tokio::sync::oneshot::channel();
@@ -237,17 +238,14 @@ pub(super) async fn tool_prompt_permission(args: &Value, ctx: &AppContext) -> Re
use crate::http::context::PermissionDecision;
let decision = tokio::time::timeout(
std::time::Duration::from_secs(300),
response_rx,
)
.await
.map_err(|_| {
let msg = format!("Permission request for '{tool_name}' timed out after 5 minutes");
slog_warn!("[permission] {msg}");
msg
})?
.map_err(|_| "Permission response channel closed unexpectedly".to_string())?;
let decision = tokio::time::timeout(std::time::Duration::from_secs(300), response_rx)
.await
.map_err(|_| {
let msg = format!("Permission request for '{tool_name}' timed out after 5 minutes");
slog_warn!("[permission] {msg}");
msg
})?
.map_err(|_| "Permission response channel closed unexpectedly".to_string())?;
if decision == PermissionDecision::AlwaysAllow {
// Persist the rule so Claude Code won't prompt again for this tool.
@@ -362,9 +360,11 @@ mod tests {
#[test]
fn tool_get_server_logs_with_filter_returns_matching_lines() {
let result =
tool_get_server_logs(&json!({"filter": "xyz_unlikely_match_999"})).unwrap();
assert_eq!(result, "", "filter with no matches should return empty string");
let result = tool_get_server_logs(&json!({"filter": "xyz_unlikely_match_999"})).unwrap();
assert_eq!(
result, "",
"filter with no matches should return empty string"
);
}
#[test]
@@ -431,13 +431,13 @@ mod tests {
cache_read_input_tokens: 0,
total_cost_usd: 0.5,
};
let r1 = crate::agents::token_usage::build_record("10_story_a", "coder-1", None, usage.clone());
let r1 =
crate::agents::token_usage::build_record("10_story_a", "coder-1", None, usage.clone());
let r2 = crate::agents::token_usage::build_record("20_story_b", "coder-2", None, usage);
crate::agents::token_usage::append_record(root, &r1).unwrap();
crate::agents::token_usage::append_record(root, &r2).unwrap();
let result =
tool_get_token_usage(&json!({"story_id": "10_story_a"}), &ctx).unwrap();
let result = tool_get_token_usage(&json!({"story_id": "10_story_a"}), &ctx).unwrap();
let parsed: Value = serde_json::from_str(&result).unwrap();
assert_eq!(parsed["records"].as_array().unwrap().len(), 1);
assert_eq!(parsed["records"][0]["story_id"], "10_story_a");
@@ -454,7 +454,9 @@ mod tests {
tokio::spawn(async move {
let mut rx = perm_rx.lock().await;
if let Some(forward) = rx.recv().await {
let _ = forward.response_tx.send(crate::http::context::PermissionDecision::Approve);
let _ = forward
.response_tx
.send(crate::http::context::PermissionDecision::Approve);
}
});
@@ -486,19 +488,21 @@ mod tests {
tokio::spawn(async move {
let mut rx = perm_rx.lock().await;
if let Some(forward) = rx.recv().await {
let _ = forward.response_tx.send(crate::http::context::PermissionDecision::Deny);
let _ = forward
.response_tx
.send(crate::http::context::PermissionDecision::Deny);
}
});
let result = tool_prompt_permission(
&json!({"tool_name": "Write", "input": {}}),
&ctx,
)
.await
.expect("denial must return Ok, not Err");
let result = tool_prompt_permission(&json!({"tool_name": "Write", "input": {}}), &ctx)
.await
.expect("denial must return Ok, not Err");
let parsed: Value = serde_json::from_str(&result).expect("result should be valid JSON");
assert_eq!(parsed["behavior"], "deny", "denied must return behavior:deny");
assert_eq!(
parsed["behavior"], "deny",
"denied must return behavior:deny"
);
assert!(parsed["message"].is_string(), "deny must include a message");
}
@@ -518,15 +522,13 @@ mod tests {
#[test]
fn generate_rule_for_bash_git() {
let rule =
generate_permission_rule("Bash", &json!({"command": "git status"}));
let rule = generate_permission_rule("Bash", &json!({"command": "git status"}));
assert_eq!(rule, "Bash(git *)");
}
#[test]
fn generate_rule_for_bash_cargo() {
let rule =
generate_permission_rule("Bash", &json!({"command": "cargo test --all"}));
let rule = generate_permission_rule("Bash", &json!({"command": "cargo test --all"}));
assert_eq!(rule, "Bash(cargo *)");
}
@@ -538,11 +540,8 @@ mod tests {
#[test]
fn generate_rule_for_mcp_tool() {
let rule = generate_permission_rule(
"mcp__story-kit__create_story",
&json!({"name": "foo"}),
);
assert_eq!(rule, "mcp__story-kit__create_story");
let rule = generate_permission_rule("mcp__storkit__create_story", &json!({"name": "foo"}));
assert_eq!(rule, "mcp__storkit__create_story");
}
// ── Settings.json writing tests ──────────────────────────────
@@ -578,17 +577,17 @@ mod tests {
fs::create_dir_all(&claude_dir).unwrap();
fs::write(
claude_dir.join("settings.json"),
r#"{"permissions":{"allow":["mcp__story-kit__*"]}}"#,
r#"{"permissions":{"allow":["mcp__storkit__*"]}}"#,
)
.unwrap();
add_permission_rule(tmp.path(), "mcp__story-kit__create_story").unwrap();
add_permission_rule(tmp.path(), "mcp__storkit__create_story").unwrap();
let content = fs::read_to_string(claude_dir.join("settings.json")).unwrap();
let settings: Value = serde_json::from_str(&content).unwrap();
let allow = settings["permissions"]["allow"].as_array().unwrap();
assert_eq!(allow.len(), 1);
assert_eq!(allow[0], "mcp__story-kit__*");
assert_eq!(allow[0], "mcp__storkit__*");
}
#[test]
@@ -634,7 +633,7 @@ mod tests {
#[test]
fn rebuild_and_restart_in_tools_list() {
use super::super::{handle_tools_list};
use super::super::handle_tools_list;
let resp = handle_tools_list(Some(json!(1)));
let tools = resp.result.unwrap()["tools"].as_array().unwrap().clone();
let tool = tools.iter().find(|t| t["name"] == "rebuild_and_restart");
@@ -687,7 +686,7 @@ mod tests {
#[test]
fn move_story_in_tools_list() {
use super::super::{handle_tools_list};
use super::super::handle_tools_list;
let resp = handle_tools_list(Some(json!(1)));
let tools = resp.result.unwrap()["tools"].as_array().unwrap().clone();
let tool = tools.iter().find(|t| t["name"] == "move_story");
@@ -814,6 +813,10 @@ mod tests {
&ctx,
);
assert!(result.is_err());
assert!(result.unwrap_err().contains("not found in any pipeline stage"));
assert!(
result
.unwrap_err()
.contains("not found in any pipeline stage")
);
}
}

View File

@@ -2,7 +2,7 @@ use crate::agents::{move_story_to_merge, move_story_to_qa, reject_story_from_qa}
use crate::http::context::AppContext;
use crate::slog;
use crate::slog_warn;
use serde_json::{json, Value};
use serde_json::{Value, json};
pub(super) async fn tool_request_qa(args: &Value, ctx: &AppContext) -> Result<String, String> {
let story_id = args
@@ -160,7 +160,7 @@ pub(super) async fn tool_launch_qa_app(args: &Value, ctx: &AppContext) -> Result
// Launch the server from the worktree
let child = std::process::Command::new("cargo")
.args(["run"])
.env("STORYKIT_PORT", port.to_string())
.env("STORKIT_PORT", port.to_string())
.current_dir(&wt_path)
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
@@ -202,7 +202,7 @@ mod tests {
#[test]
fn request_qa_in_tools_list() {
use super::super::{handle_tools_list};
use super::super::handle_tools_list;
let resp = handle_tools_list(Some(json!(1)));
let tools = resp.result.unwrap()["tools"].as_array().unwrap().clone();
let tool = tools.iter().find(|t| t["name"] == "request_qa");
@@ -217,7 +217,7 @@ mod tests {
#[test]
fn approve_qa_in_tools_list() {
use super::super::{handle_tools_list};
use super::super::handle_tools_list;
let resp = handle_tools_list(Some(json!(1)));
let tools = resp.result.unwrap()["tools"].as_array().unwrap().clone();
let tool = tools.iter().find(|t| t["name"] == "approve_qa");
@@ -230,7 +230,7 @@ mod tests {
#[test]
fn reject_qa_in_tools_list() {
use super::super::{handle_tools_list};
use super::super::handle_tools_list;
let resp = handle_tools_list(Some(json!(1)));
let tools = resp.result.unwrap()["tools"].as_array().unwrap().clone();
let tool = tools.iter().find(|t| t["name"] == "reject_qa");
@@ -244,7 +244,7 @@ mod tests {
#[test]
fn launch_qa_app_in_tools_list() {
use super::super::{handle_tools_list};
use super::super::handle_tools_list;
let resp = handle_tools_list(Some(json!(1)));
let tools = resp.result.unwrap()["tools"].as_array().unwrap().clone();
let tool = tools.iter().find(|t| t["name"] == "launch_qa_app");

View File

@@ -1,14 +1,16 @@
use crate::agents::{
close_bug_to_archive, feature_branch_has_unmerged_changes, move_story_to_archived,
};
use crate::http::context::AppContext;
use crate::http::workflow::{
add_criterion_to_file, check_criterion_in_file, create_bug_file, create_refactor_file,
create_spike_file, create_story_file, list_bug_files, list_refactor_files,
load_pipeline_state, load_upcoming_stories, update_story_in_file, validate_story_dirs,
create_spike_file, create_story_file, list_bug_files, list_refactor_files, load_pipeline_state,
load_upcoming_stories, update_story_in_file, validate_story_dirs,
};
use crate::agents::{close_bug_to_archive, feature_branch_has_unmerged_changes, move_story_to_archived};
use crate::slog_warn;
use crate::io::story_metadata::{parse_front_matter, parse_unchecked_todos};
use crate::workflow::{evaluate_acceptance_with_coverage, TestCaseResult, TestStatus};
use serde_json::{json, Value};
use crate::slog_warn;
use crate::workflow::{TestCaseResult, TestStatus, evaluate_acceptance_with_coverage};
use serde_json::{Value, json};
use std::collections::HashMap;
use std::fs;
@@ -40,27 +42,31 @@ pub(super) fn tool_create_story(args: &Value, ctx: &AppContext) -> Result<String
pub(super) fn tool_validate_stories(ctx: &AppContext) -> Result<String, String> {
let root = ctx.state.get_project_root()?;
let results = validate_story_dirs(&root)?;
serde_json::to_string_pretty(&json!(results
.iter()
.map(|r| json!({
"story_id": r.story_id,
"valid": r.valid,
"error": r.error,
}))
.collect::<Vec<_>>()))
serde_json::to_string_pretty(&json!(
results
.iter()
.map(|r| json!({
"story_id": r.story_id,
"valid": r.valid,
"error": r.error,
}))
.collect::<Vec<_>>()
))
.map_err(|e| format!("Serialization error: {e}"))
}
pub(super) fn tool_list_upcoming(ctx: &AppContext) -> Result<String, String> {
let stories = load_upcoming_stories(ctx)?;
serde_json::to_string_pretty(&json!(stories
.iter()
.map(|s| json!({
"story_id": s.story_id,
"name": s.name,
"error": s.error,
}))
.collect::<Vec<_>>()))
serde_json::to_string_pretty(&json!(
stories
.iter()
.map(|s| json!({
"story_id": s.story_id,
"name": s.name,
"error": s.error,
}))
.collect::<Vec<_>>()
))
.map_err(|e| format!("Serialization error: {e}"))
}
@@ -131,12 +137,10 @@ pub(super) fn tool_get_story_todos(args: &Value, ctx: &AppContext) -> Result<Str
return Err(format!("Story file not found: {story_id}.md"));
}
let contents = fs::read_to_string(&filepath)
.map_err(|e| format!("Failed to read story file: {e}"))?;
let contents =
fs::read_to_string(&filepath).map_err(|e| format!("Failed to read story file: {e}"))?;
let story_name = parse_front_matter(&contents)
.ok()
.and_then(|m| m.name);
let story_name = parse_front_matter(&contents).ok().and_then(|m| m.name);
let todos = parse_unchecked_todos(&contents);
serde_json::to_string_pretty(&json!({
@@ -166,8 +170,11 @@ pub(super) fn tool_record_tests(args: &Value, ctx: &AppContext) -> Result<String
// Persist to story file (best-effort — file write errors are warnings, not failures).
if let Ok(project_root) = ctx.state.get_project_root()
&& let Some(results) = workflow.results.get(story_id)
&& let Err(e) =
crate::http::workflow::write_test_results_to_story_file(&project_root, story_id, results)
&& let Err(e) = crate::http::workflow::write_test_results_to_story_file(
&project_root,
story_id,
results,
)
{
slog_warn!("[record_tests] Could not persist results to story file: {e}");
}
@@ -305,7 +312,11 @@ pub(super) fn tool_update_story(args: &Value, ctx: &AppContext) -> Result<String
front_matter.insert(k.clone(), val);
}
}
let front_matter_opt = if front_matter.is_empty() { None } else { Some(&front_matter) };
let front_matter_opt = if front_matter.is_empty() {
None
} else {
Some(&front_matter)
};
let root = ctx.state.get_project_root()?;
update_story_in_file(&root, story_id, user_story, description, front_matter_opt)?;
@@ -368,10 +379,11 @@ pub(super) fn tool_create_bug(args: &Value, ctx: &AppContext) -> Result<String,
pub(super) fn tool_list_bugs(ctx: &AppContext) -> Result<String, String> {
let root = ctx.state.get_project_root()?;
let bugs = list_bug_files(&root)?;
serde_json::to_string_pretty(&json!(bugs
.iter()
.map(|(id, name)| json!({ "bug_id": id, "name": name }))
.collect::<Vec<_>>()))
serde_json::to_string_pretty(&json!(
bugs.iter()
.map(|(id, name)| json!({ "bug_id": id, "name": name }))
.collect::<Vec<_>>()
))
.map_err(|e| format!("Serialization error: {e}"))
}
@@ -401,7 +413,10 @@ pub(super) async fn tool_delete_story(args: &Value, ctx: &AppContext) -> Result<
// 1. Stop any running agents for this story (best-effort)
if let Ok(agents) = ctx.agents.list_agents() {
for agent in agents.iter().filter(|a| a.story_id == story_id) {
let _ = ctx.agents.stop_agent(&project_root, story_id, &agent.agent_name).await;
let _ = ctx
.agents
.stop_agent(&project_root, story_id, &agent.agent_name)
.await;
}
}
@@ -410,18 +425,25 @@ pub(super) async fn tool_delete_story(args: &Value, ctx: &AppContext) -> Result<
// 3. Remove worktree (best-effort)
if let Ok(config) = crate::config::ProjectConfig::load(&project_root) {
let _ = crate::worktree::remove_worktree_by_story_id(&project_root, story_id, &config).await;
let _ =
crate::worktree::remove_worktree_by_story_id(&project_root, story_id, &config).await;
}
// 4. Find and delete the story file from any pipeline stage
let sk = project_root.join(".storkit").join("work");
let stage_dirs = ["1_backlog", "2_current", "3_qa", "4_merge", "5_done", "6_archived"];
let stage_dirs = [
"1_backlog",
"2_current",
"3_qa",
"4_merge",
"5_done",
"6_archived",
];
let mut deleted = false;
for stage in &stage_dirs {
let path = sk.join(stage).join(format!("{story_id}.md"));
if path.exists() {
fs::remove_file(&path)
.map_err(|e| format!("Failed to delete story file: {e}"))?;
fs::remove_file(&path).map_err(|e| format!("Failed to delete story file: {e}"))?;
slog_warn!("[delete_story] Deleted '{story_id}' from work/{stage}/");
deleted = true;
break;
@@ -448,12 +470,8 @@ pub(super) fn tool_create_refactor(args: &Value, ctx: &AppContext) -> Result<Str
.and_then(|v| serde_json::from_value(v.clone()).ok());
let root = ctx.state.get_project_root()?;
let refactor_id = create_refactor_file(
&root,
name,
description,
acceptance_criteria.as_deref(),
)?;
let refactor_id =
create_refactor_file(&root, name, description, acceptance_criteria.as_deref())?;
Ok(format!("Created refactor: {refactor_id}"))
}
@@ -461,10 +479,12 @@ pub(super) fn tool_create_refactor(args: &Value, ctx: &AppContext) -> Result<Str
pub(super) fn tool_list_refactors(ctx: &AppContext) -> Result<String, String> {
let root = ctx.state.get_project_root()?;
let refactors = list_refactor_files(&root)?;
serde_json::to_string_pretty(&json!(refactors
.iter()
.map(|(id, name)| json!({ "refactor_id": id, "name": name }))
.collect::<Vec<_>>()))
serde_json::to_string_pretty(&json!(
refactors
.iter()
.map(|(id, name)| json!({ "refactor_id": id, "name": name }))
.collect::<Vec<_>>()
))
.map_err(|e| format!("Serialization error: {e}"))
}
@@ -489,9 +509,16 @@ pub(super) fn parse_test_cases(value: Option<&Value>) -> Result<Vec<TestCaseResu
let status = match status_str {
"pass" => TestStatus::Pass,
"fail" => TestStatus::Fail,
other => return Err(format!("Invalid test status '{other}'. Use 'pass' or 'fail'.")),
other => {
return Err(format!(
"Invalid test status '{other}'. Use 'pass' or 'fail'."
));
}
};
let details = item.get("details").and_then(|v| v.as_str()).map(String::from);
let details = item
.get("details")
.and_then(|v| v.as_str())
.map(String::from);
Ok(TestCaseResult {
name,
status,
@@ -643,7 +670,10 @@ mod tests {
let active = parsed["active"].as_array().unwrap();
assert_eq!(active.len(), 4);
let stages: Vec<&str> = active.iter().map(|i| i["stage"].as_str().unwrap()).collect();
let stages: Vec<&str> = active
.iter()
.map(|i| i["stage"].as_str().unwrap())
.collect();
assert!(stages.contains(&"current"));
assert!(stages.contains(&"qa"));
assert!(stages.contains(&"merge"));
@@ -783,7 +813,7 @@ mod tests {
#[test]
fn create_bug_in_tools_list() {
use super::super::{handle_tools_list};
use super::super::handle_tools_list;
let resp = handle_tools_list(Some(json!(1)));
let tools = resp.result.unwrap()["tools"].as_array().unwrap().clone();
let tool = tools.iter().find(|t| t["name"] == "create_bug");
@@ -809,7 +839,7 @@ mod tests {
#[test]
fn list_bugs_in_tools_list() {
use super::super::{handle_tools_list};
use super::super::handle_tools_list;
let resp = handle_tools_list(Some(json!(1)));
let tools = resp.result.unwrap()["tools"].as_array().unwrap().clone();
let tool = tools.iter().find(|t| t["name"] == "list_bugs");
@@ -828,7 +858,7 @@ mod tests {
#[test]
fn close_bug_in_tools_list() {
use super::super::{handle_tools_list};
use super::super::handle_tools_list;
let resp = handle_tools_list(Some(json!(1)));
let tools = resp.result.unwrap()["tools"].as_array().unwrap().clone();
let tool = tools.iter().find(|t| t["name"] == "close_bug");
@@ -921,11 +951,7 @@ mod tests {
let tmp = tempfile::tempdir().unwrap();
let backlog_dir = tmp.path().join(".storkit/work/1_backlog");
std::fs::create_dir_all(&backlog_dir).unwrap();
std::fs::write(
backlog_dir.join("1_bug_crash.md"),
"# Bug 1: App Crash\n",
)
.unwrap();
std::fs::write(backlog_dir.join("1_bug_crash.md"), "# Bug 1: App Crash\n").unwrap();
std::fs::write(
backlog_dir.join("2_bug_typo.md"),
"# Bug 2: Typo in Header\n",
@@ -975,12 +1001,16 @@ mod tests {
let result = tool_close_bug(&json!({"bug_id": "1_bug_crash"}), &ctx).unwrap();
assert!(result.contains("1_bug_crash"));
assert!(!bug_file.exists());
assert!(tmp.path().join(".storkit/work/5_done/1_bug_crash.md").exists());
assert!(
tmp.path()
.join(".storkit/work/5_done/1_bug_crash.md")
.exists()
);
}
#[test]
fn create_spike_in_tools_list() {
use super::super::{handle_tools_list};
use super::super::handle_tools_list;
let resp = handle_tools_list(Some(json!(1)));
let tools = resp.result.unwrap()["tools"].as_array().unwrap().clone();
let tool = tools.iter().find(|t| t["name"] == "create_spike");
@@ -1041,7 +1071,9 @@ mod tests {
let result = tool_create_spike(&json!({"name": "My Spike"}), &ctx).unwrap();
assert!(result.contains("1_spike_my_spike"));
let spike_file = tmp.path().join(".storkit/work/1_backlog/1_spike_my_spike.md");
let spike_file = tmp
.path()
.join(".storkit/work/1_backlog/1_spike_my_spike.md");
assert!(spike_file.exists());
let contents = std::fs::read_to_string(&spike_file).unwrap();
assert!(contents.starts_with("---\nname: \"My Spike\"\n---"));
@@ -1052,10 +1084,7 @@ mod tests {
fn tool_record_tests_missing_story_id() {
let tmp = tempfile::tempdir().unwrap();
let ctx = test_ctx(tmp.path());
let result = tool_record_tests(
&json!({"unit": [], "integration": []}),
&ctx,
);
let result = tool_record_tests(&json!({"unit": [], "integration": []}), &ctx);
assert!(result.is_err());
assert!(result.unwrap_err().contains("story_id"));
}
@@ -1106,11 +1135,7 @@ mod tests {
let tmp = tempfile::tempdir().unwrap();
let current_dir = tmp.path().join(".storkit").join("work").join("2_current");
fs::create_dir_all(&current_dir).unwrap();
fs::write(
current_dir.join("1_test.md"),
"## No front matter at all\n",
)
.unwrap();
fs::write(current_dir.join("1_test.md"), "## No front matter at all\n").unwrap();
let ctx = test_ctx(tmp.path());
let result = tool_validate_stories(&ctx).unwrap();
let parsed: Vec<Value> = serde_json::from_str(&result).unwrap();
@@ -1123,7 +1148,11 @@ mod tests {
let tmp = tempfile::tempdir().unwrap();
let current = tmp.path().join(".storkit/work/2_current");
fs::create_dir_all(&current).unwrap();
fs::write(current.join("1_story_persist.md"), "---\nname: Persist\n---\n# Story\n").unwrap();
fs::write(
current.join("1_story_persist.md"),
"---\nname: Persist\n---\n# Story\n",
)
.unwrap();
let ctx = test_ctx(tmp.path());
tool_record_tests(
@@ -1137,8 +1166,14 @@ mod tests {
.unwrap();
let contents = fs::read_to_string(current.join("1_story_persist.md")).unwrap();
assert!(contents.contains("## Test Results"), "file should have Test Results section");
assert!(contents.contains("story-kit-test-results:"), "file should have JSON marker");
assert!(
contents.contains("## Test Results"),
"file should have Test Results section"
);
assert!(
contents.contains("storkit-test-results:"),
"file should have JSON marker"
);
assert!(contents.contains("u1"), "file should contain test name");
}
@@ -1149,7 +1184,7 @@ mod tests {
fs::create_dir_all(&current).unwrap();
// Write a story file with a pre-populated Test Results section (simulating a restart)
let story_content = "---\nname: Persist\n---\n# Story\n\n## Test Results\n\n<!-- story-kit-test-results: {\"unit\":[{\"name\":\"u1\",\"status\":\"pass\",\"details\":null}],\"integration\":[{\"name\":\"i1\",\"status\":\"pass\",\"details\":null}]} -->\n";
let story_content = "---\nname: Persist\n---\n# Story\n\n## Test Results\n\n<!-- storkit-test-results: {\"unit\":[{\"name\":\"u1\",\"status\":\"pass\",\"details\":null}],\"integration\":[{\"name\":\"i1\",\"status\":\"pass\",\"details\":null}]} -->\n";
fs::write(current.join("2_story_file_only.md"), story_content).unwrap();
// Use a fresh context (empty in-memory state, simulating a restart)
@@ -1157,7 +1192,11 @@ mod tests {
// ensure_acceptance should read from file and succeed
let result = tool_ensure_acceptance(&json!({"story_id": "2_story_file_only"}), &ctx);
assert!(result.is_ok(), "should accept based on file data, got: {:?}", result);
assert!(
result.is_ok(),
"should accept based on file data, got: {:?}",
result
);
assert!(result.unwrap().contains("All gates pass"));
}
@@ -1167,7 +1206,7 @@ mod tests {
let current = tmp.path().join(".storkit/work/2_current");
fs::create_dir_all(&current).unwrap();
let story_content = "---\nname: Fail\n---\n# Story\n\n## Test Results\n\n<!-- story-kit-test-results: {\"unit\":[{\"name\":\"u1\",\"status\":\"fail\",\"details\":\"error\"}],\"integration\":[]} -->\n";
let story_content = "---\nname: Fail\n---\n# Story\n\n## Test Results\n\n<!-- storkit-test-results: {\"unit\":[{\"name\":\"u1\",\"status\":\"fail\",\"details\":\"error\"}],\"integration\":[]} -->\n";
fs::write(current.join("3_story_fail.md"), story_content).unwrap();
let ctx = test_ctx(tmp.path());
@@ -1191,7 +1230,11 @@ mod tests {
let ctx = test_ctx(tmp.path());
let result = tool_delete_story(&json!({"story_id": "99_nonexistent"}), &ctx).await;
assert!(result.is_err());
assert!(result.unwrap_err().contains("not found in any pipeline stage"));
assert!(
result
.unwrap_err()
.contains("not found in any pipeline stage")
);
}
#[tokio::test]
@@ -1280,9 +1323,11 @@ mod tests {
.unwrap();
let ctx = test_ctx(tmp.path());
let result =
tool_accept_story(&json!({"story_id": "50_story_test"}), &ctx);
assert!(result.is_err(), "should refuse when feature branch has unmerged code");
let result = tool_accept_story(&json!({"story_id": "50_story_test"}), &ctx);
assert!(
result.is_err(),
"should refuse when feature branch has unmerged code"
);
let err = result.unwrap_err();
assert!(
err.contains("unmerged"),
@@ -1306,9 +1351,11 @@ mod tests {
.unwrap();
let ctx = test_ctx(tmp.path());
let result =
tool_accept_story(&json!({"story_id": "51_story_no_branch"}), &ctx);
assert!(result.is_ok(), "should succeed when no feature branch: {result:?}");
let result = tool_accept_story(&json!({"story_id": "51_story_no_branch"}), &ctx);
assert!(
result.is_ok(),
"should succeed when no feature branch: {result:?}"
);
}
#[test]
@@ -1352,10 +1399,8 @@ mod tests {
.unwrap();
let ctx = test_ctx(tmp.path());
let result = tool_check_criterion(
&json!({"story_id": "1_test", "criterion_index": 0}),
&ctx,
);
let result =
tool_check_criterion(&json!({"story_id": "1_test", "criterion_index": 0}), &ctx);
assert!(result.is_ok(), "Expected ok: {result:?}");
assert!(result.unwrap().contains("Criterion 0 checked"));
}