misc: initial code
This commit is contained in:
115
crates/codetaker-agent/tests/sqlite_memory_store.rs
Normal file
115
crates/codetaker-agent/tests/sqlite_memory_store.rs
Normal file
@@ -0,0 +1,115 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use codetaker_agent::memory::{DieselMemoryStore, MemoryStore};
|
||||
use codetaker_agent::types::{Forge, MessageAuthor, ProjectRef};
|
||||
use codetaker_db::create_pool;
|
||||
use serde_json::json;
|
||||
|
||||
fn test_db_path(test_name: &str) -> PathBuf {
|
||||
let mut path = std::env::temp_dir();
|
||||
let nonce = format!(
|
||||
"{}_{}_{}.sqlite",
|
||||
test_name,
|
||||
std::process::id(),
|
||||
chrono::Utc::now().timestamp_nanos_opt().unwrap_or_default()
|
||||
);
|
||||
path.push(nonce);
|
||||
path
|
||||
}
|
||||
|
||||
fn sample_project() -> ProjectRef {
|
||||
ProjectRef {
|
||||
forge: Forge::Gitea,
|
||||
owner: "acme".to_owned(),
|
||||
repo: "rocket".to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn pool_creation_runs_migrations() {
|
||||
let db_path = test_db_path("pool_creation_runs_migrations");
|
||||
let database_url = db_path.display().to_string();
|
||||
|
||||
let pool = create_pool(Some(&database_url))
|
||||
.await
|
||||
.expect("create sqlite pool");
|
||||
let _conn = pool
|
||||
.get()
|
||||
.await
|
||||
.expect("get pooled sqlite connection after migration");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn upsert_memory_entry_overwrites_by_key() {
|
||||
let db_path = test_db_path("upsert_memory_entry_overwrites_by_key");
|
||||
let database_url = db_path.display().to_string();
|
||||
let pool = create_pool(Some(&database_url))
|
||||
.await
|
||||
.expect("create sqlite pool");
|
||||
let store = DieselMemoryStore::new(pool);
|
||||
|
||||
let project = sample_project();
|
||||
|
||||
store
|
||||
.upsert_memory_entry(&project, "style.preferred_errors", &json!("typed"), "seed")
|
||||
.await
|
||||
.expect("insert memory entry");
|
||||
|
||||
store
|
||||
.upsert_memory_entry(
|
||||
&project,
|
||||
"style.preferred_errors",
|
||||
&json!("typed-and-contextual"),
|
||||
"refresh",
|
||||
)
|
||||
.await
|
||||
.expect("update memory entry");
|
||||
|
||||
let snapshot = store
|
||||
.project_context_snapshot(&project)
|
||||
.await
|
||||
.expect("fetch project snapshot");
|
||||
|
||||
assert_eq!(
|
||||
snapshot.entries.get("style.preferred_errors"),
|
||||
Some(&json!("typed-and-contextual"))
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn thread_messages_round_trip() {
|
||||
let db_path = test_db_path("thread_messages_round_trip");
|
||||
let database_url = db_path.display().to_string();
|
||||
let pool = create_pool(Some(&database_url))
|
||||
.await
|
||||
.expect("create sqlite pool");
|
||||
let store = DieselMemoryStore::new(pool);
|
||||
|
||||
let project = sample_project();
|
||||
let thread_id = store
|
||||
.create_review_thread(&project, "src/lib.rs", 42, "Please avoid unwrap here")
|
||||
.await
|
||||
.expect("create review thread");
|
||||
|
||||
store
|
||||
.append_thread_message(thread_id, MessageAuthor::User, "Can you clarify why?")
|
||||
.await
|
||||
.expect("append user message");
|
||||
store
|
||||
.append_thread_message(
|
||||
thread_id,
|
||||
MessageAuthor::Agent,
|
||||
"This path can fail on malformed input.",
|
||||
)
|
||||
.await
|
||||
.expect("append agent message");
|
||||
|
||||
let messages = store
|
||||
.load_thread_messages(thread_id)
|
||||
.await
|
||||
.expect("load thread messages");
|
||||
|
||||
assert_eq!(messages.len(), 2);
|
||||
assert!(matches!(messages[0].author, MessageAuthor::User));
|
||||
assert!(matches!(messages[1].author, MessageAuthor::Agent));
|
||||
}
|
||||
Reference in New Issue
Block a user