use crate::config::profile::with_profile_home_async;
use crate::memory::store::get_store;
#[tokio::test]
async fn each_profile_gets_its_own_store() {
let a = format!("store-a-{}", uuid::Uuid::new_v4());
let b = format!("store-b-{}", uuid::Uuid::new_v4());
let store_a = with_profile_home_async(Some(&a), async { get_store().map(|s| s as *const _) })
.await
.expect("store A opens");
let store_b = with_profile_home_async(Some(&b), async { get_store().map(|s| s as *const _) })
.await
.expect("store B opens");
assert_ne!(
store_a, store_b,
"two profiles resolved to the same store handle, so one is writing into the other"
);
}
#[tokio::test]
async fn the_same_profile_reuses_one_store() {
let p = format!("store-same-{}", uuid::Uuid::new_v4());
let first = with_profile_home_async(Some(&p), async { get_store().map(|s| s as *const _) })
.await
.expect("first open");
let second = with_profile_home_async(Some(&p), async { get_store().map(|s| s as *const _) })
.await
.expect("second open");
assert_eq!(
first, second,
"the same profile must reuse its store rather than opening another"
);
}
#[tokio::test]
async fn content_written_under_one_profile_is_invisible_from_another() {
let a = format!("store-data-a-{}", uuid::Uuid::new_v4());
let b = format!("store-data-b-{}", uuid::Uuid::new_v4());
let marker = format!("zmarker{}", uuid::Uuid::new_v4().simple());
with_profile_home_async(Some(&a), async {
let store = get_store().expect("store A");
let guard = store.lock().expect("lock A");
let body = format!("# Note\n\nThis body contains {marker} exactly once.\n");
let hash = qmd::Store::hash_content(&body);
let now = crate::utils::string::utc_timestamp();
guard.insert_content(&hash, &body, &now).expect("content");
guard
.insert_document("memory", "note.md", "Note", &hash, &now, &now)
.expect("document");
})
.await;
let found_in_a = with_profile_home_async(Some(&a), async {
let store = get_store().expect("store A");
let guard = store.lock().expect("lock A");
guard
.search_fts(&marker, 10, None)
.map(|r| r.len())
.unwrap_or(0)
})
.await;
assert_eq!(found_in_a, 1, "profile A must see its own document");
let found_in_b = with_profile_home_async(Some(&b), async {
let store = get_store().expect("store B");
let guard = store.lock().expect("lock B");
guard
.search_fts(&marker, 10, None)
.map(|r| r.len())
.unwrap_or(0)
})
.await;
assert_eq!(
found_in_b, 0,
"profile B saw profile A's content, the isolation boundary is broken"
);
}