feat(phase9): write_text_file_cmd

Thin UTF-8 writer used by the new save-dialog path. Caller owns path
safety; the source path is always OS-dialog-provided. Two unit tests:
roundtrips a small UTF-8 string with non-ASCII chars and asserts a
nonexistent parent path returns an actionable error.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-24 23:47:41 +01:00
parent 3eb24f2c63
commit 5a15c931d0
3 changed files with 50 additions and 0 deletions

View File

@@ -74,6 +74,11 @@ libloading = "0.8"
sqlx = { version = "0.8", default-features = false, features = ["runtime-tokio", "sqlite"] }
uuid = { version = "1", features = ["v4"] }
[dev-dependencies]
# Phase 9 fs::write_text_file_cmd tests use a temp directory so we don't
# pollute the workspace.
tempfile = "3"
[target.'cfg(target_os = "linux")'.dependencies]
webkit2gtk = "2.0"
# Needed for setting the preview overlay's WindowTypeHint to Utility via

View File

@@ -0,0 +1,44 @@
// Phase 9. Thin filesystem write command for the save-dialog path.
//
// Path safety: the caller is expected to obtain `path` via the OS save
// dialog (tauri-plugin-dialog). We do not validate traversal, extension,
// or parent-dir existence beyond what the OS filesystem reports — the
// dialog already constrains the user's choice to what they can write to.
/// Write UTF-8 text to a user-chosen path. Errors propagate the OS
/// message verbatim wrapped with the path so the toast on the frontend
/// is actionable ("Failed to write …: Permission denied" rather than a
/// bare error code).
#[tauri::command]
pub async fn write_text_file_cmd(path: String, contents: String) -> Result<(), String> {
tokio::fs::write(&path, contents)
.await
.map_err(|e| format!("Failed to write {path}: {e}"))
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn write_text_file_roundtrips_utf8() {
let dir = tempfile::tempdir().expect("tempdir");
let path = dir.path().join("out.md").display().to_string();
write_text_file_cmd(path.clone(), "hello\nwørld\n".into())
.await
.expect("write");
let round = tokio::fs::read_to_string(&path).await.expect("read");
assert_eq!(round, "hello\nwørld\n");
}
#[tokio::test]
async fn write_text_file_errors_on_bad_parent() {
let result = write_text_file_cmd(
"/definitely-not-a-real-path-kon-phase9/out.md".into(),
"x".into(),
)
.await;
assert!(result.is_err(), "expected error for nonexistent parent");
}
}

View File

@@ -2,6 +2,7 @@ pub mod audio;
pub mod clipboard;
pub mod diagnostics;
pub mod feedback;
pub mod fs;
pub mod hardware;
pub mod hotkey;
pub mod intentions;