use crate::db::Database;
use crate::db::models::Session;
use crate::db::repository::session::*;
use tokio;
#[tokio::test]
async fn test_session_crud() {
let db = Database::connect_in_memory()
.await
.expect("Failed to create database");
db.run_migrations().await.expect("Failed to run migrations");
let repo = SessionRepository::new(db.pool().clone());
let session = Session::new(
Some("Test Session".to_string()),
Some("claude-sonnet-4-5".to_string()),
Some("anthropic".to_string()),
);
repo.create(&session)
.await
.expect("Failed to create session");
let found = repo
.find_by_id(session.id)
.await
.expect("Failed to find session");
assert!(found.is_some());
assert_eq!(
found.as_ref().unwrap().title,
Some("Test Session".to_string())
);
let mut updated_session = session.clone();
updated_session.title = Some("Updated Title".to_string());
repo.update(&updated_session)
.await
.expect("Failed to update session");
let found = repo
.find_by_id(session.id)
.await
.expect("Failed to find session");
assert_eq!(found.unwrap().title, Some("Updated Title".to_string()));
repo.delete(session.id)
.await
.expect("Failed to delete session");
let found = repo
.find_by_id(session.id)
.await
.expect("Failed to find session");
let found = found.expect("Soft-deleted session should still be findable by ID");
assert!(
found.archived_at.is_some(),
"Soft-deleted session should have archived_at set"
);
}
#[tokio::test]
async fn test_session_archive() {
let db = Database::connect_in_memory()
.await
.expect("Failed to create database");
db.run_migrations().await.expect("Failed to run migrations");
let repo = SessionRepository::new(db.pool().clone());
let session = Session::new(Some("Test".to_string()), Some("model".to_string()), None);
repo.create(&session)
.await
.expect("Failed to create session");
repo.archive(session.id).await.expect("Failed to archive");
let found = repo
.find_by_id(session.id)
.await
.expect("Failed to find")
.unwrap();
assert!(found.is_archived());
repo.unarchive(session.id)
.await
.expect("Failed to unarchive");
let found = repo
.find_by_id(session.id)
.await
.expect("Failed to find")
.unwrap();
assert!(!found.is_archived());
}