//! `InMemoryRunLogStore` CRUD smoke tests.

use klieo_core::ids::RunId;
use klieo_runlog::projector::project;
use klieo_runlog::store::{InMemoryRunLogStore, RunLogStore};

fn make_log(agent: &str) -> klieo_runlog::types::RunLog {
    project(RunId::new(), agent, &[])
}

#[tokio::test]
async fn put_then_get_round_trip() {
    let store = InMemoryRunLogStore::new();
    let log = make_log("writer");
    let rid = log.run_id;
    store.put(&log).await.unwrap();
    let back = store.get(rid).await.unwrap().unwrap();
    assert_eq!(back.agent, "writer");
}

#[tokio::test]
async fn list_returns_recent_n() {
    let store = InMemoryRunLogStore::new();
    for _ in 0..5 {
        store.put(&make_log("writer")).await.unwrap();
    }
    let rows = store.list(None, 3).await.unwrap();
    assert_eq!(rows.len(), 3);
}

#[tokio::test]
async fn list_filtered_by_agent() {
    let store = InMemoryRunLogStore::new();
    store.put(&make_log("writer")).await.unwrap();
    store.put(&make_log("editor")).await.unwrap();
    store.put(&make_log("writer")).await.unwrap();
    let rows = store.list(Some("writer"), 10).await.unwrap();
    assert_eq!(rows.len(), 2);
    assert!(rows.iter().all(|r| r.agent == "writer"));
}

#[tokio::test]
async fn delete_removes_log() {
    let store = InMemoryRunLogStore::new();
    let log = make_log("writer");
    let rid = log.run_id;
    store.put(&log).await.unwrap();
    store.delete(rid).await.unwrap();
    assert!(store.get(rid).await.unwrap().is_none());
}
