story-209: accept optional positional path argument on startup

Add CLI path argument support: `story-kit-server /path/to/project` opens
the given project directly (scaffolding .story_kit/ if needed) instead of
relying on auto-detection. Resolves conflict with story-208's port parameter.

Squash merge of feature/story-209

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Dave
2026-02-26 15:17:40 +00:00
parent 6a2fbaf2ed
commit 8e0082f6cd
2 changed files with 187 additions and 14 deletions

View File

@@ -383,6 +383,19 @@ To support both Remote and Local models, the system implements a `ModelProvider`
const STORY_KIT_SCRIPT_TEST: &str = "#!/usr/bin/env bash\nset -euo pipefail\n\n# Add your project's test commands here.\n# Story Kit agents invoke this script as the canonical test runner.\n# Exit 0 on success, non-zero on failure.\necho \"No tests configured\"\n";
/// Resolve a path argument supplied on the CLI against the given working
/// directory. Relative paths (including `.`) are joined with `cwd` and
/// then canonicalized when possible. Absolute paths are returned
/// canonicalized when possible, unchanged otherwise.
pub fn resolve_cli_path(cwd: &Path, path_arg: &str) -> PathBuf {
let p = PathBuf::from(path_arg);
let joined = if p.is_absolute() { p } else { cwd.join(p) };
// Canonicalize resolves `.`, `..` and symlinks. We fall back to the
// joined (non-canonical) path when the target does not yet exist so
// that callers can still create it later.
std::fs::canonicalize(&joined).unwrap_or(joined)
}
/// Walk from `start` up through parent directories, returning the first
/// directory that contains a `.story_kit/` subdirectory, or `None`.
pub fn find_story_kit_root(start: &Path) -> Option<PathBuf> {
@@ -493,6 +506,8 @@ async fn ensure_project_root_with_story_kit(path: PathBuf) -> Result<(), String>
if !path.exists() {
fs::create_dir_all(&path)
.map_err(|e| format!("Failed to create project directory: {}", e))?;
}
if !path.join(".story_kit").is_dir() {
scaffold_story_kit(&path)?;
}
Ok(())
@@ -1058,4 +1073,86 @@ mod tests {
assert_eq!(fs::read_to_string(&readme).unwrap(), "custom content");
}
// --- open_project scaffolding ---
#[tokio::test]
async fn open_project_scaffolds_when_story_kit_missing() {
let dir = tempdir().unwrap();
let project_dir = dir.path().join("myproject");
fs::create_dir_all(&project_dir).unwrap();
let store = make_store(&dir);
let state = SessionState::default();
open_project(
project_dir.to_string_lossy().to_string(),
&state,
&store,
)
.await
.unwrap();
// .story_kit/ should have been created automatically
assert!(project_dir.join(".story_kit").is_dir());
}
#[tokio::test]
async fn open_project_does_not_overwrite_existing_story_kit() {
let dir = tempdir().unwrap();
let project_dir = dir.path().join("myproject");
let sk_dir = project_dir.join(".story_kit");
fs::create_dir_all(&sk_dir).unwrap();
let readme = sk_dir.join("README.md");
fs::write(&readme, "custom content").unwrap();
let store = make_store(&dir);
let state = SessionState::default();
open_project(
project_dir.to_string_lossy().to_string(),
&state,
&store,
)
.await
.unwrap();
// Existing .story_kit/ content should not be overwritten
assert_eq!(fs::read_to_string(&readme).unwrap(), "custom content");
}
// --- resolve_cli_path ---
#[test]
fn resolve_cli_path_absolute_returned_unchanged_when_nonexistent() {
let cwd = PathBuf::from("/some/cwd");
let result = resolve_cli_path(&cwd, "/nonexistent/absolute/path");
assert_eq!(result, PathBuf::from("/nonexistent/absolute/path"));
}
#[test]
fn resolve_cli_path_dot_resolves_to_cwd() {
let tmp = tempdir().unwrap();
let cwd = tmp.path().to_path_buf();
let result = resolve_cli_path(&cwd, ".");
// Canonicalize should resolve "." in an existing dir to the canonical cwd
assert_eq!(result, cwd.canonicalize().unwrap_or(cwd));
}
#[test]
fn resolve_cli_path_relative_resolves_against_cwd() {
let tmp = tempdir().unwrap();
let cwd = tmp.path().to_path_buf();
let subdir = cwd.join("sub");
fs::create_dir_all(&subdir).unwrap();
let result = resolve_cli_path(&cwd, "sub");
assert_eq!(result, subdir.canonicalize().unwrap_or(subdir));
}
#[test]
fn resolve_cli_path_nonexistent_relative_falls_back_to_joined() {
let tmp = tempdir().unwrap();
let cwd = tmp.path().to_path_buf();
let result = resolve_cli_path(&cwd, "newproject");
// Path doesn't exist yet — canonicalize fails, fallback is cwd/newproject
assert_eq!(result, cwd.join("newproject"));
}
}