remem-ai 0.4.4

Persistent memory for Claude Code — single binary, zero subprocesses
Documentation
//! Render schema database status lines for `remem status` and diagnostics.
//! Pure formatting, no DB connection required — only checks for the presence
//! of the schema database file.

use std::path::Path;

use crate::db::schema::default_path;

/// Render the schema database status block at the default path.
pub fn format_summary() -> Vec<String> {
    format_summary_at(&default_path())
}

/// Render the schema database status block for an arbitrary path.
pub fn format_summary_at(path: &Path) -> Vec<String> {
    let mut lines = vec!["schema database:".to_string()];
    if path.exists() {
        let size = std::fs::metadata(path).map(|m| m.len()).unwrap_or(0);
        lines.push(format!("  Location: {}", path.display()));
        lines.push("  Status:   initialized".to_string());
        lines.push(format!("  Size:     {:.1} MB", size as f64 / 1_048_576.0));
    } else {
        lines.push(format!(
            "  Location: {} (would be created here)",
            path.display()
        ));
        lines.push("  Status:   not initialized".to_string());
        lines.push(
            "  Hint:     run `remem admin reset-schema --confirm-destructive` to create"
                .to_string(),
        );
    }
    lines
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;
    use std::time::{SystemTime, UNIX_EPOCH};

    fn unique_marker_path(label: &str) -> PathBuf {
        let nonce = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_nanos())
            .unwrap_or(0);
        std::env::temp_dir().join(format!(
            "remem-schema-status-{label}-{}-{}.tmp",
            std::process::id(),
            nonce
        ))
    }

    #[test]
    fn summary_indicates_initialized_when_path_exists() {
        let path = unique_marker_path("exists");
        std::fs::write(&path, b"placeholder").unwrap();
        let lines = format_summary_at(&path);
        let body = lines.join("\n");
        assert!(body.contains("schema database:"), "got: {body}");
        assert!(body.contains("Status:   initialized"), "got: {body}");
        assert!(body.contains("Size:"), "got: {body}");
        assert!(!body.contains("not initialized"), "got: {body}");
        let _ = std::fs::remove_file(&path);
    }

    #[test]
    fn summary_indicates_not_initialized_when_path_missing() {
        let path = unique_marker_path("missing");
        // Ensure absent (best-effort cleanup; nonce already makes it unique).
        let _ = std::fs::remove_file(&path);
        let lines = format_summary_at(&path);
        let body = lines.join("\n");
        assert!(body.contains("not initialized"), "got: {body}");
        assert!(body.contains("remem admin reset-schema"), "got: {body}");
    }

    #[test]
    fn summary_first_line_is_section_header() {
        let path = unique_marker_path("hdr");
        let lines = format_summary_at(&path);
        assert_eq!(lines[0], "schema database:");
    }
}