storkit: merge 444_refactor_extract_shared_test_helpers_test_ctx_write_story_file_make_api

This commit is contained in:
dave
2026-03-28 19:47:59 +00:00
parent d216f3c267
commit ddc4a57cd2
27 changed files with 188 additions and 187 deletions
+25 -29
View File
@@ -104,27 +104,23 @@ pub fn get_editor_command_from_store(ctx: &AppContext) -> Option<String> {
.and_then(|v| v.as_str().map(|s| s.to_string()))
}
#[cfg(test)]
impl From<std::sync::Arc<AppContext>> for SettingsApi {
fn from(ctx: std::sync::Arc<AppContext>) -> Self {
Self { ctx }
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::http::context::AppContext;
use std::sync::Arc;
use crate::http::test_helpers::{make_api, test_ctx};
use tempfile::TempDir;
fn test_ctx(dir: &TempDir) -> AppContext {
AppContext::new_test(dir.path().to_path_buf())
}
fn make_api(dir: &TempDir) -> SettingsApi {
SettingsApi {
ctx: Arc::new(AppContext::new_test(dir.path().to_path_buf())),
}
}
#[tokio::test]
async fn get_editor_returns_none_when_unset() {
let dir = TempDir::new().unwrap();
let api = make_api(&dir);
let api = make_api::<SettingsApi>(&dir);
let result = api.get_editor().await.unwrap();
assert!(result.0.editor_command.is_none());
}
@@ -132,7 +128,7 @@ mod tests {
#[tokio::test]
async fn set_editor_stores_command() {
let dir = TempDir::new().unwrap();
let api = make_api(&dir);
let api = make_api::<SettingsApi>(&dir);
let payload = Json(EditorCommandPayload {
editor_command: Some("zed".to_string()),
});
@@ -143,7 +139,7 @@ mod tests {
#[tokio::test]
async fn set_editor_clears_command_on_null() {
let dir = TempDir::new().unwrap();
let api = make_api(&dir);
let api = make_api::<SettingsApi>(&dir);
api.set_editor(Json(EditorCommandPayload {
editor_command: Some("zed".to_string()),
}))
@@ -161,7 +157,7 @@ mod tests {
#[tokio::test]
async fn set_editor_clears_command_on_empty_string() {
let dir = TempDir::new().unwrap();
let api = make_api(&dir);
let api = make_api::<SettingsApi>(&dir);
let result = api
.set_editor(Json(EditorCommandPayload {
editor_command: Some(String::new()),
@@ -174,7 +170,7 @@ mod tests {
#[tokio::test]
async fn set_editor_trims_whitespace_only() {
let dir = TempDir::new().unwrap();
let api = make_api(&dir);
let api = make_api::<SettingsApi>(&dir);
let result = api
.set_editor(Json(EditorCommandPayload {
editor_command: Some(" ".to_string()),
@@ -187,7 +183,7 @@ mod tests {
#[tokio::test]
async fn get_editor_returns_value_after_set() {
let dir = TempDir::new().unwrap();
let api = make_api(&dir);
let api = make_api::<SettingsApi>(&dir);
api.set_editor(Json(EditorCommandPayload {
editor_command: Some("cursor".to_string()),
}))
@@ -200,7 +196,7 @@ mod tests {
#[test]
fn editor_command_defaults_to_null() {
let dir = TempDir::new().unwrap();
let ctx = test_ctx(&dir);
let ctx = test_ctx(dir.path());
let result = get_editor_command_from_store(&ctx);
assert!(result.is_none());
}
@@ -208,7 +204,7 @@ mod tests {
#[test]
fn set_editor_command_persists_in_store() {
let dir = TempDir::new().unwrap();
let ctx = test_ctx(&dir);
let ctx = test_ctx(dir.path());
ctx.store.set(EDITOR_COMMAND_KEY, json!("zed"));
ctx.store.save().unwrap();
@@ -220,7 +216,7 @@ mod tests {
#[test]
fn get_editor_command_from_store_returns_value() {
let dir = TempDir::new().unwrap();
let ctx = test_ctx(&dir);
let ctx = test_ctx(dir.path());
ctx.store.set(EDITOR_COMMAND_KEY, json!("code"));
let result = get_editor_command_from_store(&ctx);
@@ -230,7 +226,7 @@ mod tests {
#[test]
fn delete_editor_command_returns_none() {
let dir = TempDir::new().unwrap();
let ctx = test_ctx(&dir);
let ctx = test_ctx(dir.path());
ctx.store.set(EDITOR_COMMAND_KEY, json!("cursor"));
ctx.store.delete(EDITOR_COMMAND_KEY);
@@ -258,7 +254,7 @@ mod tests {
#[tokio::test]
async fn get_editor_http_handler_returns_null_when_not_set() {
let dir = TempDir::new().unwrap();
let ctx = test_ctx(&dir);
let ctx = test_ctx(dir.path());
let api = SettingsApi {
ctx: Arc::new(ctx),
};
@@ -269,7 +265,7 @@ mod tests {
#[tokio::test]
async fn set_editor_http_handler_stores_value() {
let dir = TempDir::new().unwrap();
let ctx = test_ctx(&dir);
let ctx = test_ctx(dir.path());
let api = SettingsApi {
ctx: Arc::new(ctx),
};
@@ -286,7 +282,7 @@ mod tests {
#[tokio::test]
async fn set_editor_http_handler_clears_value_when_null() {
let dir = TempDir::new().unwrap();
let ctx = test_ctx(&dir);
let ctx = test_ctx(dir.path());
let api = SettingsApi {
ctx: Arc::new(ctx),
};
@@ -310,7 +306,7 @@ mod tests {
#[tokio::test]
async fn open_file_returns_error_when_no_editor_configured() {
let dir = TempDir::new().unwrap();
let api = make_api(&dir);
let api = make_api::<SettingsApi>(&dir);
let result = api
.open_file(Query("src/main.rs".to_string()), Query(Some(42)))
.await;
@@ -322,7 +318,7 @@ mod tests {
#[tokio::test]
async fn open_file_spawns_editor_with_path_and_line() {
let dir = TempDir::new().unwrap();
let api = make_api(&dir);
let api = make_api::<SettingsApi>(&dir);
// Configure the editor to "echo" which is a safe no-op command
api.set_editor(Json(EditorCommandPayload {
editor_command: Some("echo".to_string()),
@@ -339,7 +335,7 @@ mod tests {
#[tokio::test]
async fn open_file_spawns_editor_with_path_only_when_no_line() {
let dir = TempDir::new().unwrap();
let api = make_api(&dir);
let api = make_api::<SettingsApi>(&dir);
api.set_editor(Json(EditorCommandPayload {
editor_command: Some("echo".to_string()),
}))
@@ -355,7 +351,7 @@ mod tests {
#[tokio::test]
async fn open_file_returns_error_for_nonexistent_editor() {
let dir = TempDir::new().unwrap();
let api = make_api(&dir);
let api = make_api::<SettingsApi>(&dir);
api.set_editor(Json(EditorCommandPayload {
editor_command: Some("this_editor_does_not_exist_xyz_abc".to_string()),
}))