#![cfg(test)]
use crate::cli::io_writer::CliOutput;
use crate::{db, models};
use chrono::Utc;
use std::path::{Path, PathBuf};
pub struct TestEnv {
pub db_path: PathBuf,
pub stdout: Vec<u8>,
pub stderr: Vec<u8>,
_tmp: tempfile::TempDir,
}
impl TestEnv {
pub fn fresh() -> Self {
let _tmp = tempfile::tempdir().expect("tempdir");
let db_path = _tmp.path().join("ai-memory.db");
Self {
db_path,
stdout: Vec::new(),
stderr: Vec::new(),
_tmp,
}
}
pub fn output(&mut self) -> CliOutput<'_> {
CliOutput::from_std(&mut self.stdout, &mut self.stderr)
}
pub fn stdout_str(&self) -> &str {
std::str::from_utf8(&self.stdout).expect("stdout utf-8")
}
pub fn stderr_str(&self) -> &str {
std::str::from_utf8(&self.stderr).expect("stderr utf-8")
}
}
pub fn seed_memory(db_path: &Path, namespace: &str, title: &str, content: &str) -> String {
let conn = db::open(db_path).expect("db::open");
let now = Utc::now().to_rfc3339();
let mut metadata = models::default_metadata();
if let Some(obj) = metadata.as_object_mut() {
obj.insert(
"agent_id".to_string(),
serde_json::Value::String("test-agent".to_string()),
);
}
let mem = models::Memory {
id: uuid::Uuid::new_v4().to_string(),
tier: models::Tier::Mid,
namespace: namespace.to_string(),
title: title.to_string(),
content: content.to_string(),
tags: vec![],
priority: 5,
confidence: 1.0,
source: "import".to_string(),
access_count: 0,
created_at: now.clone(),
updated_at: now,
last_accessed_at: None,
expires_at: None,
metadata,
};
db::insert(&conn, &mem).expect("db::insert")
}